├── LICENSE ├── README.md └── main.py /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 FlyGreyWolf 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CodeCraft2022-part 2 | open source gradually... 3 | -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | data_folder = './data/data1' 4 | solution_file = './solution/solution.txt' 5 | 6 | max_qos_config = 'config.ini' 7 | demand_file = 'demand.csv' 8 | qos_file = 'qos.csv' 9 | sitebandwidth_file = 'site_bandwidth.csv' 10 | 11 | 12 | with open (os.path.join(data_folder, max_qos_config)) as f: 13 | f.readline() 14 | max_qos = int(f.readline().split('=')[1]) 15 | 16 | with open (os.path.join(data_folder, demand_file)) as f: 17 | # 用户:{时间:demand} 18 | demands = {} 19 | 20 | # 时间 21 | times = [] 22 | users = f.readline().rstrip().split(",") 23 | 24 | i = 1 25 | while(i < len(users)): 26 | demands[users[i]] = {} 27 | i = i + 1 28 | 29 | s = f.readlines() 30 | i = 0 31 | while(i= 2): 111 | bian_yuan = value[j].split('<')[-1] 112 | fen_pei = int(value[j+1].split('>')[0]) 113 | if(yong_hu not in fenpei_info) : 114 | fenpei_info[yong_hu] = {} 115 | if(bian_yuan not in fenpei_info[yong_hu]) : 116 | fenpei_info[yong_hu][bian_yuan] = 0 117 | 118 | fenpei_info[yong_hu][bian_yuan] = fenpei_info[yong_hu][bian_yuan] + fen_pei 119 | else: 120 | if(yong_hu not in fenpei_info) : 121 | fenpei_info[yong_hu] = {} 122 | 123 | site_sets = set() 124 | for yonghu in fenpei_info.keys(): 125 | yonghu_demand = 0 126 | for bian_yuan in fenpei_info[yonghu]: 127 | yonghu_demand = yonghu_demand + fenpei_info[yonghu][bian_yuan] 128 | if bian_yuan not in site_info: 129 | site_info[bian_yuan] = {} 130 | 131 | if(time not in site_info[bian_yuan]): 132 | site_info[bian_yuan][time] = 0 133 | 134 | if (qos[yonghu][bian_yuan] >= max_qos): 135 | print("超过qos上限") 136 | 137 | site_info[bian_yuan][time] = site_info[bian_yuan][time] + fenpei_info[yonghu][bian_yuan] 138 | site_sets.add(bian_yuan) 139 | 140 | if(yonghu_demand != demands[yonghu][time]) : 141 | print("用户:" + yonghu) 142 | print("分配的需求:" + str(yonghu_demand)) 143 | print("实际需求:" + str(demands[yonghu][time])) 144 | print("用户需求不匹配") 145 | 146 | # print(site_sets) 147 | # print(site_info) 148 | for site_name in site_sets: 149 | if(site_info[site_name][time] > sitebandwidth[site_name]): 150 | print(site_info[bian_yuan][time]) 151 | print("超过边缘节点带宽上限") 152 | 153 | for site_name in site_info: 154 | site_info[site_name] = sorted(site_info[site_name].items(), key=lambda x: x[1]) 155 | 156 | 157 | import math 158 | 159 | score = 0 160 | for site_name in site_info: 161 | score = score + site_info[site_name][math.ceil(len(times)*0.95) -1][1] 162 | 163 | print("score:" , score) 164 | 165 | 166 | from pyecharts import * 167 | 168 | page = Page() # 实例化page类 169 | attr = [] 170 | v1 = [] 171 | v2 = [] 172 | 173 | bar = Bar("score:" + str(score) + '\n' + 174 | 'x轴:' + "第t时刻" + '\n' + 175 | 'y轴:' + '带宽' + '\n' + 176 | '时刻总数:' + str(len(times)) + '\n' + 177 | 'dataset:' + data_folder + '\n' + 178 | '顺序: 按边缘节点占用时刻总数' + '\n' 179 | 'solution_file:' + solution_file, width=1000,height=200) 180 | 181 | page.add(bar) # 添加要展示的图表,并设置显示位置 182 | 183 | site_names_timenum_order = {} 184 | for site_name in site_info: 185 | for t in site_info[site_name]: 186 | if(site_name not in site_names_timenum_order): 187 | site_names_timenum_order[site_name] = 0 188 | 189 | if(t[1] != 0): 190 | site_names_timenum_order[site_name] = 1 + site_names_timenum_order[site_name] 191 | 192 | site_names_timenum_order = sorted(site_names_timenum_order.items(), key=lambda x: x[1], reverse=True) 193 | 194 | i = 0 195 | for site in site_names_timenum_order: 196 | site_name = site[0] 197 | attr = [] 198 | v1 = [] 199 | v2 = [] 200 | attr = [str(time2index[t[0]]) for t in site_info[site_name]] 201 | 202 | v1 = [t[1] for t in site_info[site_name]] 203 | v2 = [sitebandwidth[site_name] - t[1] for t in site_info[site_name]] 204 | bar = Bar( "边缘节点:" + site_name + '\n' + 205 | '共' + str(site_names_timenum_order[i][1]) + '时刻有分配' '\n' , title_pos="60%",width=1900) 206 | bar.add("已用", attr, v1, is_stack=True, xaxis_rotate = 30,xaxis_label_textsize = 8) 207 | bar.add("剩余", attr, v2, is_stack=True, xaxis_rotate = 30,xaxis_label_textsize = 8) 208 | page.add(bar) 209 | i = i + 1 210 | 211 | print("===============") 212 | import matplotlib.pyplot as plt 213 | import numpy as np 214 | 215 | plt.figure(figsize=(35,20)) 216 | 217 | sitename2index = {} 218 | print(len(sitebandwidth)) 219 | print(len(times)) 220 | 221 | sb = np.empty((len(sitebandwidth), len(times))) 222 | sn_i = 0 223 | for sn in site_info: 224 | sitename2index[sn] = sn_i 225 | sn_i = sn_i + 1 226 | 227 | for sn in sitename2index: 228 | sn_i = sitename2index[sn] 229 | for t in site_info[sn]: 230 | t_i = time2index[t[0]] 231 | v = t[1] 232 | sb[sn_i][t_i] = v 233 | 234 | 235 | sitenames = list(sitename2index.keys()) 236 | times = [time2index[t] for t in time2index.keys()] 237 | 238 | import seaborn as sns 239 | 240 | print(len(sb)) 241 | ax = sns.heatmap(sb, linewidths=0.03, linecolor='white', cbar=True) # 画热力图 242 | 243 | ax.set_xlabel('times') 244 | ax.set_ylabel('site_name') 245 | ax.set_xticklabels(times) 246 | print(len(sitenames)) 247 | print(len(times)) 248 | ax.set_yticklabels(sitenames, rotation=0) 249 | 250 | 251 | 252 | 253 | --------------------------------------------------------------------------------