-
Notifications
You must be signed in to change notification settings - Fork 9
/
ce_cs.py
202 lines (153 loc) · 8.5 KB
/
ce_cs.py
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import copy
import time
import numpy as np
from tqdm import tqdm
import torch
from itertools import chain
import matplotlib.pyplot as plt
from scipy import stats
from options import args_parser
from dataset_processing import sampling, average_weights,asy_average_weights, sampling_mobility
from user_cluster_recommend import recommend, Oracle_recommend
from local_update import LocalUpdate, cache_hit_ratio,Asy_LocalUpdate
from model import AutoEncoder
from utils import exp_details, ModelManager, count_top_items
from Thompson_Sampling import thompson_sampling
from data_set import convert
from select_vehicle import select_vehicle_mobility, vehicle_p_v_mobility
if __name__ == '__main__':
idx=0
# 开始时间
start_time = time.time()
# args & 输出实验参数
args = args_parser()
exp_details(args)
# gpu or cpu
if args.gpu: torch.cuda.set_device(args.gpu)
device = 'cuda' if args.gpu else 'cpu'
# load sample users_group_train users_group_test
sample, users_group_train, users_group_test, request_content, vehicle_request_num = sampling_mobility(args, args.clients_num)
print('different epoch vehicle request num',vehicle_request_num)
data_set = np.array(sample)
# test_dataset & test_dataset_idx
test_dataset_idxs = []
for i in range(args.clients_num):
test_dataset_idxs.append(users_group_test[i])
test_dataset_idxs = list(chain.from_iterable(test_dataset_idxs))
test_dataset = data_set[test_dataset_idxs]
request_dataset = []
for i in range(args.epochs):
request_dataset_idxs=[]
request_dataset_idxs.append(request_content[i])
request_dataset_idxs = list(chain.from_iterable(request_dataset_idxs))
request_dataset.append(data_set[request_dataset_idxs])
all_pos_weight, veh_speed, veh_dis = select_vehicle_mobility(args.clients_num)
time_slow = 0.1
# build model
global_model = AutoEncoder(int(max(data_set[:, 1])), 100)
# Set the model to train and send it to device.
global_model.to(device)
global_model.train()
vehicle_model_dict = [[], [], [], [], [], [], [], [], [], []]
for i in range(args.clients_num):
vehicle_model_dict[i].append(copy.deepcopy(global_model))
# copy weights
global_weights = global_model.state_dict()
# all epoch weights
w_all_epochs = dict([(k, []) for k in range(args.epochs)])
# Training loss
train_loss = []
# each epoch train time
each_epoch_time=[]
each_epoch_time.append(0)
while idx < args.epochs:
# 开始
print(f'\n | Global Training Round : {idx + 1} |\n')
global_model.train()
#each vehicle local learning rate
local_lr = args.lr * max(1,np.log(max(1,idx)))
local_net = copy.deepcopy(vehicle_model_dict[idx % args.clients_num][-1])
local_net.to(device)
print('vehicle position',veh_dis)
print('vehicle speed', veh_speed)
print("vehicle ", idx % args.clients_num + 1, " start training for ", args.local_ep,
" epochs with learning rate ",local_lr)
if (1000 - veh_dis[idx % args.clients_num]) / veh_speed[idx % args.clients_num] > 4:
epoch_start_time = time.time()
local_model = Asy_LocalUpdate(args=args, dataset=data_set,
idxs=users_group_train[idx % args.clients_num])
w, loss, local_net = local_model.update_weights(
model=local_net, client_idx=idx % args.clients_num + 1, global_round=idx + 1,
local_learning_rate=local_lr)
vehicle_model_dict[idx % args.clients_num].append(local_net)
v_w=vehicle_model_dict[idx % args.clients_num][-1].state_dict()
#local weight * (position weight + v2i rate weight)
for key in v_w.keys():
v_w[key] = v_w[key] * all_pos_weight[idx % args.clients_num]
vehicle_model_dict[idx % args.clients_num][-1].load_state_dict(v_w)
#aggeration
for name, param in vehicle_model_dict[idx % args.clients_num][-1].named_parameters():
for name2, param2 in vehicle_model_dict[idx % args.clients_num][-2].named_parameters():
if name == name2:
param.data.copy_(args.update_decay * param2.data + param.data)
global_w = asy_average_weights(l=vehicle_model_dict[idx % args.clients_num][-1], g=global_model
, l_old=vehicle_model_dict[idx % args.clients_num][-2],vehicle_all_num=args.clients_num)
epoch_time = time.time() - epoch_start_time
each_epoch_time.append(epoch_time)
global_model.load_state_dict(global_w)
w_all_epochs[idx] = global_w['linear1.weight'].tolist()
if idx == args.epochs-1:
cache_size=[50,100,150,200,250,300,350,400,450,500]
Oracle_recommend_movies = dict([(k, []) for k in cache_size])
TS_recommend_movies = dict([(k, []) for k in cache_size])
cache_efficiency = np.zeros(len(cache_size))
random_cache_efficiency = np.zeros(len(cache_size))
Oracle_cache_efficiency = np.zeros(len(cache_size))
Greedy_cache_efficiency = np.zeros(len(cache_size))
TS_cache_efficiency = np.zeros(len(cache_size))
cache_efficiency_list=[]
# algorithm parameters
# m-ε-greedy ε represents the probability to select files randomly from all the files.
e = 0.1
for i in range(len(cache_size)):
c_s=cache_size[i]
recommend_movies_c500 = []
for j in range(args.clients_num):
vehicle_seq = j
test_dataset_i = data_set[users_group_test[vehicle_seq]]
user_movie_i = convert(test_dataset_i, max(sample['movie_id']))
recommend_list = recommend(user_movie_i, test_dataset_i, w_all_epochs[idx])
recommend_list500 = count_top_items(c_s, recommend_list)
recommend_movies_c500.append(list(recommend_list500))
Oracle_recommend_movies[c_s].append(list(Oracle_recommend(data_set, c_s)))
# AFPCC
recommend_movies_c500 = count_top_items(c_s, recommend_movies_c500)
all_vehicle_request_num = 0
for v_num in range(10):
all_vehicle_request_num += vehicle_request_num[idx][v_num]
cache_efficiency = cache_hit_ratio(request_dataset[idx], recommend_movies_c500,
all_vehicle_request_num)
cache_efficiency_list.append(cache_efficiency)
# random caching
random_caching_movies = list(np.random.choice(range(1, max(sample['movie_id']) + 1), c_s, replace=False))
random_cache_efficiency[i] = cache_hit_ratio(request_dataset[idx], random_caching_movies, all_vehicle_request_num)
# Oracle
Oracle_recommend_movies[c_s] = count_top_items(c_s, Oracle_recommend_movies[c_s])
Oracle_cache_efficiency[i] = cache_hit_ratio(request_dataset[idx], Oracle_recommend_movies[c_s], all_vehicle_request_num)
# Thompson Sampling
TS_recommend_movies[c_s] = thompson_sampling(args, data_set, test_dataset, c_s)
TS_cache_efficiency[i] = cache_hit_ratio(request_dataset[idx], TS_recommend_movies[c_s], all_vehicle_request_num)
# m-e-greedy
Greedy_cache_efficiency = Oracle_cache_efficiency * (1 - e) + random_cache_efficiency * e
# MCAF
print('MCAF',cache_efficiency_list)
# m-ε-greedy
print('Greedy_cache_efficiency',Greedy_cache_efficiency)
# Thompson Sampling
print('TS_cache_efficiency',TS_cache_efficiency)
# Random Caching
print('random_cache_efficiency',random_cache_efficiency)
idx += 1
veh_dis, veh_speed, all_pos_weight = vehicle_p_v_mobility(veh_dis, epoch_time, args.clients_num, idx, args.clients_num)
if idx > args.epochs:
break