forked from IntelligentSystemsLab/CHARGED
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknowledge_transfer.py
More file actions
158 lines (138 loc) · 5.36 KB
/
Copy pathknowledge_transfer.py
File metadata and controls
158 lines (138 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# -*- coding: utf-8 -*-
# @Author : GZH
# @Created Time : 2025/4/13 19:19
# @Email : [email protected]
# @Last Modified By : GZH
# @Last Modified Time : 2025/4/13 19:19
"""
Main entry point for federated EV charging demand prediction across multiple cities.
This script parses federated learning arguments, initializes datasets, clients, and server,
then runs global training and localization procedures.
"""
import warnings
warnings.filterwarnings("ignore", category=FutureWarning)
import os
import sys
import torch
# Ensure parent directory is in path for package imports
parent_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
if parent_dir not in sys.path:
sys.path.append(parent_dir)
from api.parsing.federated import federated_parse_args
from api.model.config import PredictionModel
from api.trainer.common import PredictionTrainer
from api.utils import random_seed, get_n_feature, Logger, get_data_paths
from api.dataset.distributed import DistributedEVDataset
from api.federated.client import CommonClient
from api.federated.server import CommonServer
from api.trainer.federated import ClientTrainer
def main():
"""
Execute federated learning workflow.
- Parse CLI arguments.
- Configure output directory and logging.
- Seed randomness and determine compute device.
- Load distributed dataset across cities.
- Instantiate CommonClient for each training and evaluation client.
- Initialize CommonServer and run federation (train + localize).
"""
args = federated_parse_args()
# Construct base output path
if args.pred_type == 'site':
base = f"{args.output_path}{args.pred_type}/{args.city}/" + \
f"{args.model}-{args.feature}-{args.auxiliary}-{args.seq_l}-{args.pre_len}-{args.max_sites}-{args.eval_percentage}"
else:
base = f"{args.output_path}{args.pred_type}/{args.city}/" + \
f"{args.model}-{args.feature}-{args.auxiliary}-{args.seq_l}-{args.pre_len}-{args.max_sites}-{args.eval_city}"
# Ensure unique directory
out_dir = base
count = 0
while os.path.exists(out_dir):
count += 1
out_dir = f"{base}#{count}/"
os.makedirs(out_dir)
# Setup logging
sys.stdout = Logger(os.path.join(out_dir, 'logging.txt'))
# Device and randomness
device = torch.device(f"cuda:{args.device}" if torch.cuda.is_available() else "cpu")
random_seed(args.seed)
# Prepare data paths per city
data_paths = get_data_paths(ori_path=args.data_path, cities=args.city, suffix='_remove_zero')
# Load distributed dataset
ev_dataset = DistributedEVDataset(
feature=args.feature,
auxiliary=args.auxiliary,
data_paths=data_paths,
pred_type=args.pred_type,
eval_percentage=args.eval_percentage,
eval_city=args.eval_city,
max_sites=args.max_sites,
)
print(f"Running federated evaluation on {args.city} with model={args.model}, feature={args.feature}, "
f"auxiliary={args.auxiliary}, pred_type={args.pred_type}")
# Instantiate training clients
train_clients = []
train_clients_id = []
eval_clients = []
eval_clients_id = []
for client_id, data_dict in ev_dataset.training_clients_data.items():
client_path = f'{new_path}{client_id}/'
# os.makedirs(client_path)
train_clients.append(
CommonClient(
client_id=client_id,
data_dict=data_dict,
scaler=ev_dataset.city_scalers[client_id[:3]],
model_module=PredictionModel,
trainer_module=ClientTrainer,
seq_l=args.seq_l,
pre_len=args.pre_len,
model_name=args.model,
n_fea=ev_dataset.n_fea,
batch_size=args.batch_size,
device=args.device,
save_path=client_path,
support_rate=1,
)
)
train_clients_id.append(client_id)
print('Training on:')
print(train_clients_id)
for client_id, data_dict in ev_dataset.eval_clients_data.items():
client_path = f'{new_path}{client_id}/'
os.makedirs(client_path)
eval_clients.append(
CommonClient(
client_id=client_id,
data_dict=data_dict,
scaler=ev_dataset.city_scalers[client_id[:3]],
model_module=PredictionModel,
trainer_module=ClientTrainer,
seq_l=args.seq_l,
pre_len=args.pre_len,
model_name=args.model,
n_fea=ev_dataset.n_fea,
batch_size=args.batch_size,
device=args.device,
save_path=client_path,
support_rate=0.5,
)
)
eval_clients_id.append(client_id)
print('Evaluation on:')
print(eval_clients_id)
ev_server = CommonServer(
train_clients=train_clients,
eval_clients=eval_clients,
model=PredictionModel(
num_node=1,
n_fea=ev_dataset.n_fea,
model_name=args.model,
seq_l=args.seq_l,
pre_len=args.pre_len,
),
)
ev_server.train(global_epochs=args.global_epoch, local_epochs=args.local_epoch)
ev_server.localize(now_epoch=args.global_epoch, deploy_epochs=args.deploy_epoch)
if __name__ == '__main__':
main()