From 15e03ccb9108004f544a7fcebfb282d5d8bdb024 Mon Sep 17 00:00:00 2001 From: sjinglong Date: Mon, 19 Apr 2021 11:42:57 +0800 Subject: [PATCH 1/2] user position development finished --- Client.py | 50 ++++++++++++++++++++++++++++++++++++++++--- Server.py | 60 +++++++++++++++++++++++++++++++++++----------------- TFTrainer.py | 5 ++++- main.py | 5 ++++- params.py | 8 +++---- 5 files changed, 100 insertions(+), 28 deletions(-) diff --git a/Client.py b/Client.py index b0a3b4b..61d80ae 100644 --- a/Client.py +++ b/Client.py @@ -8,14 +8,36 @@ def compute_norm(data): return sum([np.sum(item**2) for item in data]) +""" + @brief: 极坐标转欧氏坐标 + @param [polar_coordinate]: 要转换的极坐标 | 都是用普通列表表示的坐标 + @return: 转换结果(欧氏坐标) +""" +def polar2euclid(polar_coordinate): + return [polar_coordinate[0] * np.math.cos(polar_coordinate[1]), polar_coordinate[0] * np.math.sin(polar_coordinate[1])] + +""" + @brief: 欧氏坐标转极坐标 + @param [polar_coordinate]: 要转换的欧氏坐标 | 都是用普通列表表示的坐标 + @return: 转换结果(极坐标) +""" +def euclid2polar(euclid_coordinate): + return [np.math.sqrt(euclid_coordinate[0]**2 + euclid_coordinate[1]**2), np.math.atan2(euclid_coordinate[1], euclid_coordinate[0])] + class Client(): def __init__(self) -> None: pass - def run(self, data, label): + def run(self, data, label, p_d): self.__comm = CommClient('127.0.0.1', 12345) self.__trainer = TR() - self.__hi = np.random.uniform() + + self.__polar_position = p_d[0] + self.__polar_direction = p_d[1] + self.__euclid_position = polar2euclid(self.__polar_position) + self.__euclid_direction = polar2euclid(self.__polar_direction) + + self.__hi = self.__polar_position[0]**(-params.PATHLOSS_FACTOR) self.__transmit_power = params.CLIENT_TRANSMIT_POWER for _ in range(params.ITERATION_NUM): @@ -26,7 +48,7 @@ def run(self, data, label): # 计算梯度的二范数 grad_norm = compute_norm(grad) # 向服务器发送结果 - self.__comm.send({'grad_norm': grad_norm, 'received_power': self.__hi * self.__transmit_power}) + self.__comm.send({'grad_norm': grad_norm, 'received_power': self.__hi * self.__transmit_power, 'position': self.__euclid_position}) # 接收服务器的调度结果:1为调度,0为未调度 sche_sig = self.__comm.recv() if sche_sig == 1: @@ -34,6 +56,28 @@ def run(self, data, label): self.__trainer.train_with_grad(grad) # 向服务器发送 local model self.__comm.send(self.__trainer.get_weights()) + self.__update_user() + + def __update_user(self): + self.__move(1) + self.__hi = self.__polar_position[0]**(-params.PATHLOSS_FACTOR) + + def __move(self, time_elapsed): + distance = self.__polar_direction[0] * time_elapsed + pose_d = polar2euclid([distance, self.__polar_direction[1]]) + self.__euclid_position[0] += pose_d[0] + self.__euclid_position[1] += pose_d[1] + + self.__polar_position = euclid2polar(self.__euclid_position) + + if self.__polar_position[0] > 100: + normal_dir = polar2euclid([1, self.__polar_position[1]]) + dot_product = self.__euclid_direction[0] * normal_dir[0] + self.__euclid_direction[1] * normal_dir[1] + polar_rho_vec = [dot_product, self.__polar_position[1]] + euclid_rho_vec = polar2euclid(polar_rho_vec) + euclid_side_vec = [self.__euclid_direction[0] - euclid_rho_vec[0], self.__euclid_direction[1] - euclid_rho_vec[1]] + self.__euclid_direction[0], self.__euclid_direction[1] = euclid_side_vec[0] - euclid_rho_vec[0], euclid_side_vec[1] - euclid_rho_vec[1] + self.__polar_direction = euclid2polar(self.__euclid_direction) if __name__ == '__main__': client = Client() diff --git a/Server.py b/Server.py index 9f26a34..b464b2a 100644 --- a/Server.py +++ b/Server.py @@ -29,7 +29,7 @@ def random_select(): return sche_sig, a def get_uplink_delay(received_power): - I = np.random.random((params.RB_NUM,1)) + I = np.random.uniform(1e-4, 0.01, size=(params.RB_NUM,1)) SINR = received_power / (I + params.UPLINK_BANDWIDTH * params.NOISE_POWER_SPECTRAL_DENSITY) rate = params.UPLINK_BANDWIDTH * np.log2(1 + SINR) delay = params.MODEL_SIZE / rate @@ -116,6 +116,12 @@ def random_RB_schedual(msg, user_sche): user_delay.append(0) return RB_sche, user_delay, max(user_delay) +def draw_circle(ax): + thetas = np.linspace(0, np.math.pi*2, 200) + x = 100 * np.cos(thetas) + y = 100 * np.sin(thetas) + ax.plot(x, y) + class Server(): def __init__(self) -> None: pass @@ -133,7 +139,12 @@ def run(self, data, label): uplink_delay_recorder = [] # plt.figure(figsize=(8, 6), dpi=80) - f, (ax1, ax2, ax3) = plt.subplots(3, 1) + f, axes = plt.subplots(3, 2) + for ax in axes[:, 0]: + ax.remove() + axes = axes[:, 1] + gs = axes[0].get_gridspec() + axbig = f.add_subplot(gs[:, 0]) plt.ion() for iter in range(params.ITERATION_NUM): @@ -167,23 +178,34 @@ def run(self, data, label): acc_recorder.append(eval_result[1]) iter_recorder.append(iter) uplink_delay_recorder.append(iter_delay) - - ax1.clear() - ax1.set_xlim(0, params.ITERATION_NUM) - ax1.set_ylim(0, 1) - ax1.plot(iter_recorder, acc_recorder, label='acc') - ax1.legend() - ax2.clear() - ax2.set_xlim(0, params.ITERATION_NUM) - ax2.set_ylim(0, 3) - ax2.plot(iter_recorder, loss_recorder, label='loss') - ax2.legend() - ax3.clear() - ax3.set_xlim(0, params.ITERATION_NUM) - ax3.plot(iter_recorder, uplink_delay_recorder, label='uplink delay') - ax3.plot([0, params.ITERATION_NUM], [np.mean(uplink_delay_recorder)]*2, label='mean value') - ax3.text(int(params.ITERATION_NUM / 2), np.mean(uplink_delay_recorder) + 2, f'{np.mean(uplink_delay_recorder):.2f}') - ax3.legend() + user_position_recorder = [] + for i in range(params.CLIENT_NUM): + user_position_recorder.append(msg[i]['position']) + + axes[0].clear() + axes[0].set_xlim(0, params.ITERATION_NUM) + axes[0].set_ylim(0, 1) + axes[0].plot(iter_recorder, acc_recorder, label='acc') + axes[0].legend() + axes[1].clear() + axes[1].set_xlim(0, params.ITERATION_NUM) + axes[1].set_ylim(0, 3) + axes[1].plot(iter_recorder, loss_recorder, label='loss') + axes[1].legend() + axes[2].clear() + axes[2].set_xlim(0, params.ITERATION_NUM) + axes[2].plot(iter_recorder, uplink_delay_recorder, label='uplink delay') + axes[2].plot([0, params.ITERATION_NUM], [np.mean(uplink_delay_recorder)]*2, label='mean value') + axes[2].text(int(params.ITERATION_NUM / 2), np.mean(uplink_delay_recorder) + 2, f'{np.mean(uplink_delay_recorder):.2f}') + axes[2].legend() + axbig.clear() + axbig.axis('equal') + draw_circle(axbig) + for i, p in enumerate(user_position_recorder): + axbig.scatter(p[0], p[1]) + if i in a: + axbig.plot([0, p[0]], [0, p[1]]) + plt.pause(0.0001) plt.ioff() diff --git a/TFTrainer.py b/TFTrainer.py index 4553830..f65414a 100644 --- a/TFTrainer.py +++ b/TFTrainer.py @@ -41,4 +41,7 @@ def evaluate(self, data, label): return self.__model.evaluate(data, label) def get_weights(self): - return self.__model.get_weights() \ No newline at end of file + return self.__model.get_weights() + +if __name__ == '__main__': + r = TFTrainer() \ No newline at end of file diff --git a/main.py b/main.py index 0bd2abc..57b6b24 100644 --- a/main.py +++ b/main.py @@ -34,13 +34,16 @@ edr.close() elr.close() + np.random.seed(4) server_p = mp.Process(target=server.run, args=(edata, elabel)) clients_p = [] local_data_size = int(np.floor(params.DATASET_SIZE_USED_TO_TRAIN / params.CLIENT_NUM)) for i, client in enumerate(clients): clients_p.append(mp.Process(target=client.run, args=( data[i*local_data_size:(i+1)*local_data_size, :, :, :], - label[i*local_data_size:(i+1)*local_data_size] + label[i*local_data_size:(i+1)*local_data_size], + [[np.random.uniform(0, 100), np.random.uniform(0, 2*np.math.pi)], + [np.random.uniform(3, 5), np.random.uniform(0, 2*np.math.pi)]] ))) server_p.start() diff --git a/params.py b/params.py index 967d651..8645765 100644 --- a/params.py +++ b/params.py @@ -2,8 +2,8 @@ SCHEDUAL_SIZE = 10 # 每一轮调度的用户数量 CLIENT_NUM = 20 # 用户总数 - ITERATION_NUM = 100 # 迭代次数 +RB_NUM = 10 # RB数量 DATASET_SIZE_USED_TO_TRAIN = 60000 # 用于训练的数据集大小 最多 60000 DATASET_SIZE_USED_TO_EVAL = 10000 # 用于eval的数据集大小 最多 10000 @@ -13,8 +13,8 @@ MNIST_EVAL_IMAGES_PATH = './MNIST/t10k-images.idx3-ubyte' # mnist eval数据集路径 MNIST_EVAL_LABELS_PATH = './MNIST/t10k-labels.idx1-ubyte' # mnist eval标签路径 -UPLINK_BANDWIDTH = 18 * 1000 # 上行链路带宽 +UPLINK_BANDWIDTH = 1 * 1e3 * 1e3 # 上行链路带宽 CLIENT_TRANSMIT_POWER = 1 # 用户发送功率 -NOISE_POWER_SPECTRAL_DENSITY = 1e-10 # 噪声功率谱密度 +NOISE_POWER_SPECTRAL_DENSITY = 10**(-14.4) # 噪声功率谱密度 MODEL_SIZE = 1024 * 1024 / 2 # 模型大小 -RB_NUM = 30 # RB数量 \ No newline at end of file +PATHLOSS_FACTOR = 3.7 # 路损因子 \ No newline at end of file From b21a284fa74eb045021e9ff94e234507e1a6df24 Mon Sep 17 00:00:00 2001 From: sjinglong Date: Mon, 10 May 2021 08:51:25 +0800 Subject: [PATCH 2/2] finished development --- .gitignore | 3 +- Server.py | 117 +++++++++++++++++++-- main.py | 30 +++++- params.py | 8 +- plot_board.py | 285 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 430 insertions(+), 13 deletions(-) create mode 100644 plot_board.py diff --git a/.gitignore b/.gitignore index 3040d64..84e5e6e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ __pycache__/ -.vscode/ \ No newline at end of file +.vscode/ +output/ \ No newline at end of file diff --git a/Server.py b/Server.py index b464b2a..53f5b5b 100644 --- a/Server.py +++ b/Server.py @@ -1,4 +1,5 @@ +from Client import Client import numpy as np import matplotlib.pyplot as plt @@ -29,6 +30,7 @@ def random_select(): return sche_sig, a def get_uplink_delay(received_power): + # I = np.zeros((params.RB_NUM,1)) I = np.random.uniform(1e-4, 0.01, size=(params.RB_NUM,1)) SINR = received_power / (I + params.UPLINK_BANDWIDTH * params.NOISE_POWER_SPECTRAL_DENSITY) rate = params.UPLINK_BANDWIDTH * np.log2(1 + SINR) @@ -90,7 +92,7 @@ def get_RB_schedual(eng, msg, user_sche): # print(np.max(r_delay_ma, axis=0)) # print(result[-1]) # print(np.argmax(r_delay_ma, axis=0)) - return np.argmax(r_delay_ma, axis=0), np.max(r_delay_ma, axis=0), result[-1] + return np.argmax(r_delay_ma, axis=0), np.max(r_delay_ma, axis=0), result[-1][0] def random_RB_schedual(msg, user_sche): received_power = [] @@ -116,6 +118,85 @@ def random_RB_schedual(msg, user_sche): user_delay.append(0) return RB_sche, user_delay, max(user_delay) +def joint_user_RB_sche(eng, msg): + import matlab + + kappa = 10 # 梯度在优化问题里所占的权重 + + received_power = [] + for id in range(params.CLIENT_NUM): + received_power.append(msg[id]['received_power']) + uplink_delay = get_uplink_delay(np.array([received_power])) + + # 构造不等式约束 + part_1 = [] + for i in range(params.CLIENT_NUM): + part_1.append(np.identity(params.RB_NUM)) + part_1.append(np.zeros((params.RB_NUM, 1 + params.CLIENT_NUM))) + part_1 = np.concatenate(part_1, axis=1) + part_2 = [] + for i in range(params.CLIENT_NUM): + ma = np.zeros((params.CLIENT_NUM, params.RB_NUM)) + ma[i, :] = uplink_delay[:, i].transpose() + part_2.append(ma) + part_2.append(np.zeros((params.CLIENT_NUM, params.CLIENT_NUM))) + part_2.append(-np.ones((params.CLIENT_NUM, 1))) + part_2 = np.concatenate(part_2, axis=1) + A = matlab.double(np.concatenate([part_1, part_2], axis=0).tolist()) + b = matlab.double(np.concatenate([np.ones((params.RB_NUM, 1)), np.zeros((params.CLIENT_NUM, 1))], axis=0).tolist()) + + # 构造等式约束 + part_1 = [] + for i in range(params.CLIENT_NUM): + ma = np.zeros((params.CLIENT_NUM, params.RB_NUM)) + ma[i, :] = np.ones((1, params.RB_NUM)) + part_1.append(ma) + part_1.append(-np.identity(params.CLIENT_NUM)) + part_1.append(np.zeros((params.CLIENT_NUM, 1))) + part_1 = np.concatenate(part_1, axis=1) + part_2 = np.zeros((1, params.RB_NUM * params.CLIENT_NUM + params.CLIENT_NUM + 1)) + part_2[0, params.RB_NUM * params.CLIENT_NUM:params.RB_NUM * params.CLIENT_NUM + params.CLIENT_NUM] = np.ones((1, params.CLIENT_NUM)) + Aeq = matlab.double(np.concatenate([part_1, part_2], axis=0).tolist()) + a = np.zeros((params.CLIENT_NUM + 1, 1)) + a[-1,0] = params.SCHEDUAL_SIZE + beq = matlab.double(a.tolist()) + + # 限定上下界 + lb = matlab.double(np.zeros((params.CLIENT_NUM * params.RB_NUM + params.CLIENT_NUM + 1,)).tolist()) + ub = np.ones((params.CLIENT_NUM * params.RB_NUM + params.CLIENT_NUM + 1,)) + ub[-1] = np.Inf + ub = matlab.double(ub.tolist()) + + # 目标函数 + grad_norms = [] + for id in range(params.CLIENT_NUM): + grad_norms.append(msg[id]['grad_norm']) + f = np.zeros((params.RB_NUM * params.CLIENT_NUM + params.CLIENT_NUM + 1, 1)) + f[params.RB_NUM * params.CLIENT_NUM:params.RB_NUM * params.CLIENT_NUM + params.CLIENT_NUM] = -kappa * np.array([grad_norms]).transpose() + f[-1, 0] = 1 + f = matlab.double(f.tolist()) + intcon = matlab.double(list(range(1, params.CLIENT_NUM * params.RB_NUM + params.CLIENT_NUM + 1))) + + # 求解 + result = eng.intlinprog(f, intcon, A, b, Aeq, beq, lb, ub) + result = np.array(result) + allo_ma = result[:params.CLIENT_NUM * params.RB_NUM].reshape((params.CLIENT_NUM, params.RB_NUM)).transpose() + r_delay_ma = allo_ma * uplink_delay + RB_sche = np.argmax(r_delay_ma, axis=0), + user_delay = np.max(r_delay_ma, axis=0), + iter_delay = result[-1][0] + + sche_ma = result[params.CLIENT_NUM * params.RB_NUM:params.CLIENT_NUM * params.RB_NUM + params.CLIENT_NUM] + sche_sig = {} + a = [] + for id in range(params.CLIENT_NUM): + if sche_ma[id][0] > 0.5: + sche_sig[id] = 1 + a.append(id) + else: + sche_sig[id] = 0 + return sche_sig, a, RB_sche, user_delay, iter_delay + def draw_circle(ax): thetas = np.linspace(0, np.math.pi*2, 200) x = 100 * np.cos(thetas) @@ -137,6 +218,8 @@ def run(self, data, label): acc_recorder = [] iter_recorder = [] uplink_delay_recorder = [] + schedualed_user_info = {} + uplink_delay_cost = [] # plt.figure(figsize=(8, 6), dpi=80) f, axes = plt.subplots(3, 2) @@ -158,11 +241,13 @@ def run(self, data, label): msg = self.__comm.recv_all() print('gradient received ...') # 基于梯度范数给出调度结果 - sche_sig, a = select_based_on_weight(msg) + # sche_sig, a = select_based_on_weight(msg) # sche_sig, a = random_select() # 随机调度 # 给出资源分配结果 - RB_sche, user_delay, iter_delay = get_RB_schedual(eng, msg, sche_sig) + # RB_sche, user_delay, iter_delay = get_RB_schedual(eng, msg, sche_sig) # RB_sche, user_delay, iter_delay = random_RB_schedual(msg, sche_sig) # 随机分配 + sche_sig, a, RB_sche, user_delay, iter_delay = joint_user_RB_sche(eng, msg) + # print("schedualed user num : ", len(a)) # 下发调度结果 print('sending schedual command ...') self.__comm.send(sche_sig) @@ -181,22 +266,31 @@ def run(self, data, label): user_position_recorder = [] for i in range(params.CLIENT_NUM): user_position_recorder.append(msg[i]['position']) + if len(uplink_delay_cost) == 0: + uplink_delay_cost.append(iter_delay) + else: + uplink_delay_cost.append(iter_delay + uplink_delay_cost[-1]) + schedualed_user_info[iter] = {sche_id: {'position': msg[sche_id]['position'], 'grad_norm': msg[sche_id]['grad_norm']} for sche_id in a} axes[0].clear() axes[0].set_xlim(0, params.ITERATION_NUM) axes[0].set_ylim(0, 1) axes[0].plot(iter_recorder, acc_recorder, label='acc') axes[0].legend() + # axes[1].clear() + # axes[1].set_xlim(0, params.ITERATION_NUM) + # axes[1].set_ylim(0, 3) + # axes[1].plot(iter_recorder, loss_recorder, label='loss') + # axes[1].legend() axes[1].clear() axes[1].set_xlim(0, params.ITERATION_NUM) - axes[1].set_ylim(0, 3) - axes[1].plot(iter_recorder, loss_recorder, label='loss') + axes[1].plot(iter_recorder, uplink_delay_cost, label='uplink delay cost') axes[1].legend() axes[2].clear() axes[2].set_xlim(0, params.ITERATION_NUM) axes[2].plot(iter_recorder, uplink_delay_recorder, label='uplink delay') axes[2].plot([0, params.ITERATION_NUM], [np.mean(uplink_delay_recorder)]*2, label='mean value') - axes[2].text(int(params.ITERATION_NUM / 2), np.mean(uplink_delay_recorder) + 2, f'{np.mean(uplink_delay_recorder):.2f}') + axes[2].text(int(params.ITERATION_NUM / 2), np.mean(uplink_delay_recorder) * 0.8, f'{np.mean(uplink_delay_recorder):.2f}') axes[2].legend() axbig.clear() axbig.axis('equal') @@ -211,6 +305,17 @@ def run(self, data, label): plt.ioff() plt.show() + # 保存数据 + # np.savez( + # f'./output/random_with_sche_info_2.npz', + # iter_recorder = iter_recorder, + # acc_recorder = acc_recorder, + # loss_recorder = loss_recorder, + # uplink_delay_recorder = uplink_delay_recorder, + # uplink_delay_cost = uplink_delay_cost, + # schedualed_user_info = schedualed_user_info + # ) + eng.exit() if __name__ == '__main__': diff --git a/main.py b/main.py index 57b6b24..a05c5e1 100644 --- a/main.py +++ b/main.py @@ -8,6 +8,20 @@ from MNIST.MNISTReader import MNISTImageReader as ds_r from MNIST.MNISTReader import MNISTLabelReader as lb_r +def classify_dataset(data, label): + d = [[], [], [], [], [], [], [], [], [], []] + print(d) + print(data.shape) + print(label.shape) + + for i in range(60000): + d[label[i]].append(data[i, :, :, :]) + + for i in range(10): + d[i] = np.array(d[i]) + + return d + if __name__ == '__main__': server = Server() clients = [] @@ -34,7 +48,7 @@ edr.close() elr.close() - np.random.seed(4) + np.random.seed(2) server_p = mp.Process(target=server.run, args=(edata, elabel)) clients_p = [] local_data_size = int(np.floor(params.DATASET_SIZE_USED_TO_TRAIN / params.CLIENT_NUM)) @@ -45,7 +59,19 @@ [[np.random.uniform(0, 100), np.random.uniform(0, 2*np.math.pi)], [np.random.uniform(3, 5), np.random.uniform(0, 2*np.math.pi)]] ))) - + + # result = classify_dataset(data, label) + # np.random.seed(4) + # server_p = mp.Process(target=server.run, args=(edata, elabel)) + # clients_p = [] + # for i, client in enumerate(clients): + # clients_p.append(mp.Process(target=client.run, args=( + # result[i], + # i * np.ones((len(result[i]),), dtype=np.uint8), + # [[np.random.uniform(0, 100), np.random.uniform(0, 2*np.math.pi)], + # [np.random.uniform(3, 5), np.random.uniform(0, 2*np.math.pi)]] + # ))) + server_p.start() for client_p in clients_p: client_p.start() diff --git a/params.py b/params.py index 8645765..b293922 100644 --- a/params.py +++ b/params.py @@ -13,8 +13,8 @@ MNIST_EVAL_IMAGES_PATH = './MNIST/t10k-images.idx3-ubyte' # mnist eval数据集路径 MNIST_EVAL_LABELS_PATH = './MNIST/t10k-labels.idx1-ubyte' # mnist eval标签路径 -UPLINK_BANDWIDTH = 1 * 1e3 * 1e3 # 上行链路带宽 +UPLINK_BANDWIDTH = 50 * 1e3 * 1e3 # 上行链路带宽 CLIENT_TRANSMIT_POWER = 1 # 用户发送功率 -NOISE_POWER_SPECTRAL_DENSITY = 10**(-14.4) # 噪声功率谱密度 -MODEL_SIZE = 1024 * 1024 / 2 # 模型大小 -PATHLOSS_FACTOR = 3.7 # 路损因子 \ No newline at end of file +NOISE_POWER_SPECTRAL_DENSITY = 10**(-10) # 噪声功率谱密度 +MODEL_SIZE = 1024 * 1024 * 3 # 模型大小 +PATHLOSS_FACTOR = 2 # 路损因子 \ No newline at end of file diff --git a/plot_board.py b/plot_board.py new file mode 100644 index 0000000..ae79739 --- /dev/null +++ b/plot_board.py @@ -0,0 +1,285 @@ +import numpy as np +import matplotlib.pyplot as plt + +color1 = '#4b778d' +color2 = '#28b5b5' +color3 = '#8fd9a8' +color4 = '#d2e69c' + +if __name__ == '__main__': + random = np.load('./output/random.npz') + algorithm1 = np.load('./output/algorithm1.npz') + algorithm2 = np.load('./output/algorithm2.npz') + + font = { + 'size': 10 + } + + # x = np.mean(random['uplink_delay_recorder']) + # y = np.mean(algorithm1['uplink_delay_recorder']) + # z = np.mean(algorithm2['uplink_delay_recorder']) + # print(x, y, z) + # print((x - y) / x) + # print((x - z) / x) + # print((y - z) / y) + + # f, axes = plt.subplots(2, 2) + # axes[0, 0].plot(random['iter_recorder'], random['uplink_delay_cost'], label='random') + # axes[0, 0].plot(algorithm1['iter_recorder'], algorithm1['uplink_delay_cost'], label='algorithm1') + # axes[0, 0].plot(algorithm2['iter_recorder'], algorithm2['uplink_delay_cost'], label='algorithm2') + # axes[0, 0].legend() + + + # axes[0, 1].plot(random['iter_recorder'], random['acc_recorder'], label='random') + # axes[0, 1].plot(algorithm1['iter_recorder'], algorithm1['acc_recorder'], label='algorithm1') + # axes[0, 1].plot(algorithm2['iter_recorder'], algorithm2['acc_recorder'], label='algorithm2') + # axes[0, 1].legend() + + # axes[1, 0].plot(random['iter_recorder'], random['loss_recorder'], label='random') + # axes[1, 0].plot(algorithm1['iter_recorder'], algorithm1['loss_recorder'], label='algorithm1') + # axes[1, 0].plot(algorithm2['iter_recorder'], algorithm2['loss_recorder'], label='algorithm2') + # axes[1, 0].legend() + + # axes[1, 1].plot(random['iter_recorder'], random['uplink_delay_recorder'], label='random') + # axes[1, 1].plot(algorithm1['iter_recorder'], algorithm1['uplink_delay_recorder'], label='algorithm1') + # axes[1, 1].plot(algorithm2['iter_recorder'], algorithm2['uplink_delay_recorder'], label='algorithm2') + # axes[1, 1].legend() + + D = 3000 # 每个用户的数据集大小 + K = 1000000 # 处理一个样本所需的CPU周期 + L = 1 # 本地迭代次数 + F = 1 * 1e3 * 1e3 * 1e3 # 每个用户的CPU频率 + computing_delay = D * K * L / F # 计算延时 + + random_training_time_cost = [] + for i, item in enumerate(random['uplink_delay_cost']): + random_training_time_cost.append(item + computing_delay * (i + 1)) + + alg1_training_time_cost = [] + for i, item in enumerate(algorithm1['uplink_delay_cost']): + alg1_training_time_cost.append(item + computing_delay * (i + 1)) + + alg2_training_time_cost = [] + for i, item in enumerate(algorithm2['uplink_delay_cost']): + alg2_training_time_cost.append(item + computing_delay * (i + 1)) + + f, axes = plt.subplots(1, 2) + axes[1].plot(random_training_time_cost, random['loss_recorder'], '-.', label='Random', color=color3, markersize=4) + axes[1].plot(alg1_training_time_cost, algorithm1['loss_recorder'], '--', label='Separated Optimization', color=color2, markersize=4) + axes[1].plot(alg2_training_time_cost, algorithm2['loss_recorder'], '-', label='Joint Optimization', color=color1, markersize=4) + axes[1].legend() + axes[1].grid() + axes[1].set_xlabel('Training Time (s)', fontdict=font) + axes[1].set_ylabel('Loss', fontdict=font) + axes[1].set_title('Comparison of the Convergence Speed of the three methods [Loss]', fontdict=font) + + + axes[0].plot(random_training_time_cost, random['acc_recorder'], '-.', label='Random', color=color3, markersize=4) + axes[0].plot(alg1_training_time_cost, algorithm1['acc_recorder'], '--', label='Separated Optimization', color=color2, markersize=4) + axes[0].plot(alg2_training_time_cost, algorithm2['acc_recorder'], '-', label='Joint Optimization', color=color1, markersize=4) + axes[0].legend() + axes[0].grid() + axes[0].set_xlabel('Training Time (s)', fontdict=font) + axes[0].set_ylabel('Accuracy', fontdict=font) + axes[0].set_title('Comparison of the Convergence Speed of the three methods [Accuracy]', fontdict=font) + + plt.show() + +############################################################################################################################## + +# if __name__ == '__main__': +# random = np.load('./output/random_with_sche_info.npz', allow_pickle=True) +# algorithm1 = np.load('./output/algorithm1_with_sche_info.npz', allow_pickle=True) +# algorithm2 = np.load('./output/algorithm2_with_sche_info.npz', allow_pickle=True) +# random_1 = np.load('./output/random_with_sche_info_1.npz', allow_pickle=True) +# algorithm1_1 = np.load('./output/algorithm1_with_sche_info_1.npz', allow_pickle=True) +# algorithm2_1 = np.load('./output/algorithm2_with_sche_info_1.npz', allow_pickle=True) +# random_2 = np.load('./output/random_with_sche_info_2.npz', allow_pickle=True) +# algorithm1_2 = np.load('./output/algorithm1_with_sche_info_2.npz', allow_pickle=True) +# algorithm2_2 = np.load('./output/algorithm2_with_sche_info_2.npz', allow_pickle=True) + +# font = { +# 'size': 10 +# } + +# f, axes = plt.subplots(1, 2) + +# positions = random['schedualed_user_info'].item() +# positions_1 = random_1['schedualed_user_info'].item() +# positions_2 = random_2['schedualed_user_info'].item() +# all_positions = [] +# for iter_info in positions.values(): +# for user_info in iter_info.values(): +# all_positions.append(user_info['position']) +# for iter_info in positions_1.values(): +# for user_info in iter_info.values(): +# all_positions.append(user_info['position']) +# for iter_info in positions_2.values(): +# for user_info in iter_info.values(): +# all_positions.append(user_info['position']) +# position, counts = np.unique(np.round(all_positions, decimals=-1), axis=0, return_counts=True) +# position /= 10 +# position[:, 0] += 10 +# position[:, 1] -= 10 +# position[:, 1] *= -1 +# print(position) +# print(counts) +# heatmap = np.zeros((21, 21)) +# for i, p in enumerate(position): +# heatmap[int(p[0]), int(p[1])] = counts[i] +# im = axes[0].imshow(heatmap / 3, vmin=0, vmax=10, cmap='Greys') +# axes[0].set_xticks(np.arange(0, 21, 1)) +# axes[0].set_yticks(np.arange(0, 21, 1)) +# axes[0].set_xticklabels(np.arange(-100, 110, 10)) +# axes[0].set_yticklabels(np.arange(100, -110, -10)) +# plt.setp(axes[0].get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor") + +# thetas = np.linspace(0, np.math.pi*2, 200) +# x = 10 * np.cos(thetas) + 10 +# y = 10 * np.sin(thetas) + 10 +# axes[0].plot(x, y, '--k') +# axes[0].set_title('Heatmap of Scheduled User Location [Random]') + +# axes[0].figure.colorbar(im, ax=axes[0], fraction=0.046, pad=0.04) + +# #############################33 + +# positions = algorithm1['schedualed_user_info'].item() +# positions_1 = algorithm1_1['schedualed_user_info'].item() +# positions_2 = algorithm1_2['schedualed_user_info'].item() +# all_positions = [] +# for iter_info in positions.values(): +# for user_info in iter_info.values(): +# all_positions.append(user_info['position']) +# for iter_info in positions_1.values(): +# for user_info in iter_info.values(): +# all_positions.append(user_info['position']) +# for iter_info in positions_2.values(): +# for user_info in iter_info.values(): +# all_positions.append(user_info['position']) +# position, counts = np.unique(np.round(all_positions, decimals=-1), axis=0, return_counts=True) +# position /= 10 +# position[:, 0] += 10 +# position[:, 1] -= 10 +# position[:, 1] *= -1 +# print(position) +# print(counts) +# heatmap = np.zeros((21, 21)) +# for i, p in enumerate(position): +# heatmap[int(p[0]), int(p[1])] = counts[i] +# im = axes[1].imshow(heatmap / 3, vmin=0, vmax=10, cmap='Greys') +# axes[1].set_xticks(np.arange(0, 21, 1)) +# axes[1].set_yticks(np.arange(0, 21, 1)) +# axes[1].set_xticklabels(np.arange(-100, 110, 10)) +# axes[1].set_yticklabels(np.arange(100, -110, -10)) +# plt.setp(axes[1].get_xticklabels(), rotation=45, ha="right", rotation_mode="anchor") + +# thetas = np.linspace(0, np.math.pi*2, 200) +# x = 10 * np.cos(thetas) + 10 +# y = 10 * np.sin(thetas) + 10 +# axes[1].plot(x, y, '--k') +# axes[1].set_title('Heatmap of Scheduled User Location [Separated Optimization]') + +# axes[1].figure.colorbar(im, ax=axes[1], fraction=0.046, pad=0.04) + + + + +# plt.show() + +####################################################################################################################### + +# def calculate_delay(): +# client_num = 20 # 用户数量 +# total_dataset_size = 60000 # 数据集总大小 +# dataset_size_per_client = total_dataset_size / client_num # 每个用户的数据集大小 +# D = dataset_size_per_client * np.ones((1, client_num)) # 每个用户的数据集大小 +# K = 100000 * np.ones((1, client_num)) # 处理一个样本所需的CPU周期 +# L = 1 * np.ones((1, client_num)) # 本地迭代次数 +# F = 2 * 1e3 * 1e3 * 1e3 * np.ones((1, client_num)) # 每个用户的CPU频率 2 +# computing_delay = D * K * L / F # 计算延时 + +# M = 1024 * 1024 * 3 # 模型大小 bit +# bandwidth_per_user = 50 * 1e3 * 1e3 # 频谱带宽 Hz +# f = bandwidth_per_user * np.ones((1, client_num)) +# S = 1 * np.ones((1, client_num)) # 发送功率 1 +# N0 = 10**(-10) # 噪声功率谱密度 +# g = np.ones((1, client_num)) * (50**(-2)) # 信道增益 +# I = np.random.uniform(1e-4, 1e-2, (1, client_num)) # 干扰 +# rate = (f * np.log2(1 + g * S / (N0 * f + I))) # 速率 +# communication_delay = M / rate +# print('***************') +# print(rate / 8 / 1024 / 1024) +# print(N0 * f) +# print(I) +# print(communication_delay, computing_delay) + +# total_delay = np.max(computing_delay + communication_delay) +# return total_delay + +# 变化计算频率,发射功率,频谱带宽 + +# if __name__ == '__main__': +# f = np.arange(1, 41) / 10 +# delay_ = [] +# for item in f: +# delay = calculate_delay(item) +# delay_.append(delay) + +# font = { +# 'size': 10 +# } +# plt.plot(f, delay_, 'o-', color=color1, label='Iteration Delay') +# plt.xlabel('User Computing Frequency (GHz)', fontdict=font) +# plt.ylabel('Iteration Delay (s)', fontdict=font) +# plt.title('The Relationship between Iteration Delay and Computing Frequency', fontdict=font) +# plt.legend() +# plt.grid() +# plt.show() + +# if __name__ == '__main__': +# # f = [10**(item) for item in range(-6, 2)] +# f = [0.2*1e-5, 0.5*1e-5, 1e-5, +# 0.2*1e-4, 0.5*1e-4, 1e-4, +# 0.2*1e-3, 0.5*1e-3, 1e-3, +# 0.2*1e-2, 0.5*1e-2, 1e-2, +# 0.2*1e-1, 0.5*1e-1, 1e-1, +# 0.2*1e0, 0.5, 1, +# 0.2*1e1, 5, 10, +# 0.2*1e2, 50, 1e2] +# delay_ = [] +# for item in f: +# delay = calculate_delay(item) +# delay_.append(delay) + +# font = { +# 'size': 10 +# } +# plt.loglog(f, delay_, 'o-', color=color1, label='Iteration Delay') +# # plt.semilogx(f, delay_, 'o-', color=color1, label='Iteration Delay') +# # plt.plot(f, delay_, 'o-', color=color1, label='Iteration Delay') +# plt.xlabel('User Transmit Power (W)', fontdict=font) +# plt.ylabel('Iteration Delay (s)', fontdict=font) +# plt.title('The Relationship between Iteration Delay and Transmit Power', fontdict=font) +# plt.legend() +# plt.grid() +# plt.show() + +# if __name__ == '__main__': +# f = np.arange(1, 51, 2) +# delay_ = [] +# for item in f: +# delay = calculate_delay(item) +# delay_.append(delay) + +# font = { +# 'size': 10 +# } +# plt.plot(f, delay_, 'o-', color=color1, label='Iteration Delay') +# plt.xlabel('User Uplink Bandwidth (MHz)', fontdict=font) +# plt.ylabel('Iteration Delay (s)', fontdict=font) +# plt.title('The Relationship between Iteration Delay and Uplink Bandwidth', fontdict=font) +# plt.legend() +# plt.grid() +# plt.show() +