def continue_nearest_simulation(system_end_time, MEC_resource, device_num, continue_distance, device_allocation_method, path_w): df = pd.read_csv( "/Users/sugimurayuuki/Desktop/mecsimulator/CloudletSimulator/base_station/kddi_okayama_city2.csv", dtype={ 'lon': 'float', 'lat': 'float' }) server_type = "LTE" cover_range = 500 n = len(df) print("Number of MEC server:", n) mec = [MEC_server(0, 00, " ", 00.00, 00.00, 0, 0)] * n for index, series in df.iterrows(): mec[index] = MEC_server(MEC_resource, index + 1, server_type, series["lon"], series["lat"], cover_range, system_end_time) mec_num = len(df) # 集約局を対応するMECに設定する set_aggregation_station(mec) # 到着順 if device_allocation_method == 0: d = open( '/Users/sugimurayuuki/Desktop/mecsimulator/CloudletSimulator/dataset/device.clone_binaryfile', 'rb') devices = pickle.load(d) devices = devices[0:device_num] num = len(devices) for i in range(num): devices[i].startup_time = float( devices[i].plan[0].time) # 各デバイスの起動時間を設定する devices[i].set_congestion_status(system_end_time) devices[i].set_MEC_distance(len(df)) devices[i]._first_flag = True devices[i]._allocation_plan = [None] * system_end_time # 順序をシャッフル random.shuffle(devices) sorted_devices = [devices] * system_end_time for t in range(system_end_time): random.shuffle(devices) sorted_devices[t] = devices # リソース順 elif device_allocation_method == 1: d = open( '/Users/sugimurayuuki/Desktop/mecsimulator/CloudletSimulator/dataset/device.congestion_binaryfile', 'rb') devices = pickle.load(d) devices = devices[0:device_num] num = len(devices) devices = reverse_resource_sort(devices) for i in range(num): devices[i].startup_time = float( devices[i].plan[0].time) # 各デバイスの起動時間を設定する devices[i].set_congestion_status(system_end_time) devices[i].set_MEC_distance(len(df)) devices[i]._first_flag = True sorted_devices = [devices] * system_end_time # 混雑度順 else: # 混雑度計算 # traffic_congestion(mec, devices, system_end_time, 1000) cd = open( '/Users/sugimurayuuki/Desktop/mecsimulator/CloudletSimulator/dataset/congestion_checked_devices.binaryfile', 'rb') cd = pickle.load(cd) devices = cd num = len(devices) print("device_num", num) # 混雑度順で毎秒ごとのdevicesをソートする sorted_devices = devices_congestion_sort(devices, system_end_time) keep_count = 0 # save_devices = [] * data_length # --- # ここからメインの処理 for t in range(system_end_time): print("[TIME:", t, "]") # ある時刻tのMECに割り当てらえたデバイスを一時的に保存する用の変数 save_devices = [None] * mec_num for i in range(num): print("---new device---", sorted_devices[t][i].name) # plan_indexがデバイスの稼働時間外なら処理をスキップ if (check_plan_index(sorted_devices[t][i].plan_index, len(sorted_devices[t][i].plan)) == False): print("skip") continue # plan_indexが稼働時間内なら処理開始 if check_between_time(sorted_devices[t][i], t) == True: print("plan_index", sorted_devices[t][i].plan_index, "max_index", len(sorted_devices[t][i].plan)) # 継続割り当て device_flag, allocation_MEC_name = continue_search( sorted_devices[t][i], mec, sorted_devices[t][i].plan_index, cover_range, t, continue_distance) if device_flag == True: # 継続回数 keep_count = keep_count + 1 # 最近傍選択 if device_flag == False: device_flag, allocation_MEC_name = nearest_search2( sorted_devices[t][i], mec, sorted_devices[t][i].plan_index, cover_range, t) # 割り当てが成功したら表示する if device_flag == True: # deviceが直前で割り当てたMECを取得 mec_index = search_mec_index(mec, allocation_MEC_name) # print("device:", sorted_devices[t][i].name, ", use_resource:", sorted_devices[t][i].use_resource, "--->", "MEC_ID:", mec[mec_index].name, ", index:", i) # print(sorted_devices[t][i].mec_name, mec[mec_index].resource) # print(mec_index, len(save_devices)) # --- # なぜindexがmec_indexなの? <- mec用のリストだから if save_devices[mec_index] == None: save_devices[mec_index] = [sorted_devices[t][i].name] else: save_devices[mec_index].append( sorted_devices[t][i].name) mec_index = 0 else: print("NOT FIND") # plan_indexをインクリメント sorted_devices[t][ i]._plan_index = sorted_devices[t][i]._plan_index + 1 # デバイスの稼働時間を超えた時の処理 else: # もしデバイスの終了時間を超えた時のみ(1回だけ)、デバイスに直前に割り当てたMECのリソースをリカバリーする。 if sorted_devices[t][i].mec_name != [] and sorted_devices[t][ i]._lost_flag == False: print("DECREASE") sorted_devices[t][i].set_mode = "decrease" print(sorted_devices[t][i].mec_name) mec[sorted_devices[t][i].mec_name - 1].custom_resource_adjustment(sorted_devices[t][i], t) mec[sorted_devices[t][i].mec_name - 1].save_resource(t) sorted_devices[t][i].set_mode = "add" sorted_devices[t][i]._lost_flag = True # ある時刻tのMECに一時的に保存していた割り当てたデバイスをコピーする。 copy_to_mec(mec, save_devices, t) #----- # リソース消費量がそれぞれで違う時のテスト用関数を作成する # 各秒でMECが持っているデバイスのインデックスと数がわかるものとする sum = 0 mec_sum = 0 having_device_resource_sum = 0 sum = 0 mec_sum = 0 having_device_resource_sum = 0 """ for t in range(system_end_time): # print("time:", t) for m in range(mec_num): # if t == 16: # print("MEC_ID:", mec[m].name, ", having devices:", mec[m]._having_devices[t], mec[m]._having_devices_count[t], # ", mec_resouce:", mec[m]._resource_per_second[t], ", current time:", t) # sum = sum + mec[m]._having_devices_count[t] # mec_sum = mec_sum + mec[m]._resource_per_second[t] # sum = sum + mec[m]._having_devices_count[t] mec_sum = mec_sum + mec[m]._resource_per_second[t] if mec[m]._having_devices[t] is not None: # print("check", mec[m]._having_devices[t]) device_index = device_index_search(sorted_devices[t], mec[m]._having_devices[t]) # print(mec[m]._having_devices[t], device_index) having_device_resource_sum = having_device_resource_sum + device_resource_calc(sorted_devices[t], device_index) check_allocation(t, 150, MEC_resource, having_device_resource_sum, mec_sum) print((150 * MEC_resource - having_device_resource_sum), mec_sum) having_device_resource_sum = 0 sum = 0 mec_sum = 0 """ # print(sum, (150*100-sum), mec_sum) num = len(sorted_devices[-1]) print("system_time: ", system_end_time) print("MEC_num: ", mec_num) print("device_num: ", num) sorted_devices = sorted_devices[0:system_end_time] maximum = max_hop_search(sorted_devices[-1]) print("max_hop: ", maximum) minimum = min_hop_search(sorted_devices[-1]) print("min_hop: ", minimum) average_hop = average_hop_calc(sorted_devices[-1]) print("average_hop: ", average_hop) reboot_rate = application_reboot_rate(mec, system_end_time) print("AP reboot rate:", reboot_rate) max_distance = max_distance_search(sorted_devices[-1]) print("max_distance:", max_distance) min_distance = min_distance_search(sorted_devices[-1]) print("min_distance:", min_distance) average_distance = average_distance_calc(sorted_devices[-1]) print("average_distance: ", average_distance) result = [system_end_time] result.append(mec_num) result.append(MEC_resource) result.append(num) result.append(maximum) result.append(minimum) result.append(average_hop) result.append(reboot_rate) result.append(max_distance) result.append(min_distance) result.append(average_distance) # pathを動的に変えることで毎回新しいファイルを作成することができる write_csv(path_w, result) print("finish") return average_hop, reboot_rate
devices = pickle.load(f) num = len(devices) # デバイスの総数 print(num) moving_time = [0] * num plan_index = [0] * num # 各deviceのplanリストのindex用リスト for i in range(num): devices[i].startup_time = float(devices[i].plan[0].time) # 各デバイスの起動時間を設定する moving_time[i] = len(devices[i].plan) # 各デバイスのplanの長さ(稼働時間)をリストに格納する same = None # 割り当てに被りがないように直前に割り当てたMECのIDを保存する変数 mode = "add" # リソースの割り当て状態、初期状態はadd old_id = None # t-1秒前のMECのIDを保存するための変数 for t in range(20): print("[TIME:", t, "]") for i in range(10): print("---new device---", devices[i].name) if (check_plan_index(plan_index[i], moving_time[i]) == True): print("skip") continue if check_between_time(devices[i], t) == True: for index in range(100): #ここのアルゴリズムが間違っている if mec[index]._mode == "keep": print(mec[index].name, devices[i].name) print("mode:", mec[index]._mode) if mec[index].check_resource(devices[i].use_resource) == True: mec[index].mode_adjustment(devices[i], plan_index[i], t) memo, device_flag = mec[index].cover_range_search( devices[i], plan_index[i], t) if memo > 0 and device_flag == True: memo = 0 #mec[m]._resouce_per_resouce[t] = mec[m].resource
def nearest_simulation(system_end_time, MEC_resource, device_num, device_allocation_method, path_w, how_compare): # --- # MECの準備 df = pd.read_csv( "/home/tomokishiraga/PycharmProjects/simulation/CloudletSimulator/base_station/okayama_kddi.csv", dtype={ 'lon': 'float', 'lat': 'float' }) server_type = "LTE" cover_range = 500 n = len(df) print("Number of MEC server:", n) mec = [MEC_server(0, 00, " ", 00.00, 00.00, 0, 0)] * n #MECインスタンスをCSVを元に生成 data_length = len(df) for index, series in df.iterrows(): mec[index] = MEC_server(MEC_resource, index + 1, server_type, series["lon"], series["lat"], cover_range, system_end_time) #mec = delete_mec(mec) #MECサーバにAPの情報を付与(乱数を取得し実行可能なAPを設定する) count = 1 for t in range(data_length): if count == 1: mec[t]._app_ver = [1, 2] count = 2 elif count == 2: mec[t]._app_ver = [1, 3] count = 3 else: mec[t]._app_ver = [2, 3] count = 1 mec_num = len(mec) #print("MECs", mec_num) # 集約局を対応するMECに設定する set_aggregation_station(mec) # 到着順 if device_allocation_method == 0: if how_compare == "hop": d = open( '/home/tomokishiraga/PycharmProjects/simulation/CloudletSimulator/dataset/device.clone_binaryfile2', 'rb') else: d = open( '/home/tomokishiraga/PycharmProjects/simulation/CloudletSimulator/dataset/device.clone_binaryfile', 'rb') devices = pickle.load(d) devices = devices[0:device_num] num = len(devices) sum = 0 for i in range(num): devices[i].startup_time = float( devices[i].plan[0].time) # 各デバイスの起動時間を設定する devices[i].set_congestion_status(system_end_time) devices[i].set_MEC_distance(len(df)) devices[i]._first_flag = True devices[i]._allocation_plan = [None] * system_end_time sum = sum + devices[i].use_resource #print(sum) # 順序をシャッフル(各時間ごとに到着順をランダムで決める) random.shuffle(devices) sorted_devices = [devices] * system_end_time for t in range(system_end_time): random.shuffle(devices) sorted_devices[t] = devices # リソース順 elif device_allocation_method == 1: if how_compare == "hop": d = open( '/home/tomokishiraga/PycharmProjects/simulation/CloudletSimulator/dataset/device.clone_binaryfile2', 'rb') else: d = open( '/home/tomokishiraga/PycharmProjects/simulation/CloudletSimulator/dataset/device.clone_binaryfile', 'rb') devices = pickle.load(d) devices = devices[0:device_num] num = len(devices) devices = reverse_resource_sort(devices) for i in range(num): devices[i].startup_time = float( devices[i].plan[0].time) # 各デバイスの起動時間を設定する devices[i].set_congestion_status(system_end_time) devices[i].set_MEC_distance(len(df)) devices[i]._first_flag = True sorted_devices = [devices] * system_end_time #デバイスをリソース順にソートする # 混雑度順 else: # 混雑度計算 # traffic_congestion(mec, devices, system_end_time, 1000) if how_compare == "hop": cd = open( '/home/tomokishiraga/PycharmProjects/simulation/CloudletSimulator/script/normal/device_compare/congestion_checked_devices2.binaryfile', 'rb') else: cd = open( '/home/tomokishiraga/PycharmProjects/simulation/CloudletSimulator/script/normal/device_compare/congestion_checked_devices1.binaryfile', 'rb') cd = pickle.load(cd) devices = cd num = len(devices) #print("device_num", num) # 混雑度順で毎秒ごとのdevicesをソートする sorted_devices = devices_ap_congestion_sort(devices, system_end_time) # ---------------------------------------------------------------------------------------------------------------------------------- # ここからメインの処理 for t in range(system_end_time): #print("[TIME:", t, "]") # ある時刻tのMECに割り当てらえたデバイス名を一時的に保存する用の変数 save_devices = [None] * mec_num for i in range(num): #print("---new device---", sorted_devices[t][i].name) # plan_indexがデバイスの稼働時間外なら処理をスキップ if (check_plan_index(sorted_devices[t][i].plan_index, len(sorted_devices[t][i].plan)) == False): #print("skip") continue # plan_indexが稼働時間内なら処理開始 if check_between_time(sorted_devices[t][i], t) == True: #print(sorted_devices[t][i].plan_index) # 最近傍割り当て処理 device_flag, allocation_MEC_name = nearest_search( sorted_devices[t][i], mec, sorted_devices[t][i].plan_index, cover_range, t) # 最近傍割り当てが成功したら表示する if device_flag == True: # deviceが直前で割り当てたMECのインデックスを取得 mec_index = search_mec_index(mec, allocation_MEC_name) # print("device:", sorted_devices[t][i].name, ", use_resource:", sorted_devices[t][i].use_resource, "--->", "MEC_ID:", mec[mec_index].name, ", index:", i) # print(sorted_devices[t][i].mec_name, mec[mec_index].resource) # print(mec_index, len(save_devices)) # なぜindexがmec_indexなの? <- mec用のリストだから if save_devices[ mec_index] == None: #ある時刻tにMECに割り当てたデバイス名を保存 save_devices[mec_index] = [sorted_devices[t][i].name] else: save_devices[mec_index].append( sorted_devices[t][i].name) #デバイス名を追加 # --- # print(t, mec_index, index) # 実行時間外の時 #else: #デバイスがMECを見つけられないかった時 #print(devices[i].name) #print("NOT FIND") # plan_indexをインクリメント sorted_devices[t][i]._plan_index = sorted_devices[t][ i]._plan_index + 1 #sorted_devicesのインスタンスのplan_indexをインクリメント else: # デバイスの稼働時間を超えた時の処理(デバイスが移動し終わって消滅した場合) # 前回割り当てたMECのリソースをリカバリする。 if sorted_devices[t][i].mec_name != [] and sorted_devices[t][ i]._lost_flag == False: #deviceを割り当てていたMECの名前が空でなく、lost_flag == False(割り当て完了の意味) previous_index = search_mec_index( mec, sorted_devices[t] [i].mec_name) #deviceを割り当ててているMECの名前からインデックスを取得 #print("DECREASE") sorted_devices[t][ i].set_mode = "decrease" #移動し終わったデバイスのmodeを変更 #print(sorted_devices[t][i].mec_name) mec[previous_index].custom_resource_adjustment( sorted_devices[t][i], t) #MECのリソースを調整(増やす) mec[previous_index].save_resource( t ) #時刻tにおけるリソースの状態を保存するメソッド:resource_per_second[time]に保存 sorted_devices[t][i].set_mode = "add" #初期値に更新 sorted_devices[t][i]._lost_flag = True #初期値に更新 # ある時刻tのMECに一時的に保存していた割り当てたデバイスをコピーする。 copy_to_mec(mec, save_devices, t) #having_devicesに(time,save_devices[])を追加 #----------------------------------------------------------------------------------------------------------------- # リソース消費量がそれぞれで違う時のテスト用関数を作成する # 各秒でMECが持っているデバイスのインデックスと数がわかるものとする sum = 0 mec_sum = 0 #mecのリソース容量の合計 having_device_resource_sum = 0 # for t in range(system_end_time): # print("time:", t) #1つ目のMECから順に for m in range(mec_num): mec_sum = mec_sum + mec[m]._resource_per_second[ t] #resource_per_second[t]は時刻tにおけるリソースの状態 if mec[m]._having_devices[t] is not None: #時刻tに割り当てたデバイスが空でなければ # print("check", mec[m]._having_devices[t]) device_index = device_index_search( sorted_devices[t], mec[m]._having_devices[t]) #割り当てたデバイスのインデックスを取得 # print(mec[m]._having_devices[t], device_index) having_device_resource_sum = having_device_resource_sum + device_resource_calc( sorted_devices[t], device_index) #要求リソース量の和を計算 #ある時刻tのMECへの割り当てができているかを確認するメソッド check_allocation(t, mec_num, MEC_resource, having_device_resource_sum, mec_sum) #print((mec_num * MEC_resource - having_device_resource_sum), mec_sum) sum = 0 mec_sum = 0 having_device_resource_sum = 0 # print(sum, (150*100-sum), mec_sum) # print("resource",resource_sum) print("system_time: ", system_end_time) print("MEC_num: ", mec_num) print("device_num: ", num) sorted_devices = sorted_devices[0:system_end_time] maximum = max_hop_search(sorted_devices[-1]) print("max_hop: ", maximum) minimum = min_hop_search(sorted_devices[-1]) print("min_hop: ", minimum) average_hop = average_hop_calc(sorted_devices[-1]) print("average_hop: ", average_hop) reboot_rate = application_reboot_rate(mec, system_end_time) print("AP reboot rate:", reboot_rate) max_distance = max_distance_search(sorted_devices[-1]) print("max_distance:", max_distance) min_distance = min_distance_search(sorted_devices[-1]) print("min_distance:", min_distance) average_distance = average_distance_calc(sorted_devices[-1]) print("average_distance: ", average_distance) # for d in range(device_num): # print(devices[d].hop) result = [system_end_time] result.append(mec_num) result.append(MEC_resource) result.append(num) result.append(maximum) result.append(minimum) result.append(average_hop) result.append(reboot_rate) result.append(max_distance) result.append(min_distance) result.append(average_distance) # 結果をcsvへ書き込み write_csv(path_w, result) return average_hop, reboot_rate
def nearest_simulation(system_end_time, MEC_resource, device_num, device_allocation_method, path_w, how_compare): # --- # MECの準備 df = pd.read_csv( "/Users/sugimurayuuki/Desktop/mecsimulator/CloudletSimulator/base_station/kddi_okayama_city2.csv", dtype={ 'lon': 'float', 'lat': 'float' }) server_type = "LTE" cover_range = 500 n = len(df) print("Number of MEC server:", n) mec = [MEC_server(0, 00, " ", 00.00, 00.00, 0, 0)] * n for index, series in df.iterrows(): mec[index] = MEC_server(MEC_resource, index + 1, server_type, series["lon"], series["lat"], cover_range, system_end_time) #mec = delete_mec(mec) mec_num = len(mec) print("MECs", mec_num) # 集約局を対応するMECに設定する set_aggregation_station(mec) # 到着順 if device_allocation_method == 0: if how_compare == "hop": d = open( '/Users/sugimurayuuki/Desktop/mecsimulator/CloudletSimulator/dataset/device.simple_congestion_binaryfile_500', 'rb') else: d = open( '/Users/sugimurayuuki/Desktop/mecsimulator/CloudletSimulator/dataset/device.clone_binaryfile', 'rb') devices = pickle.load(d) devices = devices[0:device_num] num = len(devices) sum = 0 for i in range(num): devices[i].startup_time = float( devices[i].plan[0].time) # 各デバイスの起動時間を設定する devices[i].set_congestion_status(system_end_time) devices[i].set_MEC_distance(len(df)) devices[i]._first_flag = True devices[i]._allocation_plan = [None] * system_end_time sum = sum + devices[i].use_resource print(sum) # 順序をシャッフル random.shuffle(devices) sorted_devices = [devices] * system_end_time for t in range(system_end_time): random.shuffle(devices) sorted_devices[t] = devices # リソース順 elif device_allocation_method == 1: if how_compare == "hop": d = open( '/Users/sugimurayuuki/Desktop/mecsimulator/CloudletSimulator/dataset/device.simple_congestion_binaryfile_500', 'rb') else: d = open( '/Users/sugimurayuuki/Desktop/mecsimulator/CloudletSimulator/dataset/device.clone_binaryfile', 'rb') devices = pickle.load(d) devices = devices[0:device_num] num = len(devices) devices = reverse_resource_sort(devices) for i in range(num): devices[i].startup_time = float( devices[i].plan[0].time) # 各デバイスの起動時間を設定する devices[i].set_congestion_status(system_end_time) devices[i].set_MEC_distance(len(df)) devices[i]._first_flag = True sorted_devices = [devices] * system_end_time # 混雑度順 else: # 混雑度 # traffic_congestion(mec, devices, system_end_time, 1000) if how_compare == "hop": cd = open( '/Users/sugimurayuuki/Desktop/mecsimulator/CloudletSimulator/script/simple/device_compare/congestion_checked_devices2.binaryfile', 'rb') else: cd = open( '/Users/sugimurayuuki/Desktop/mecsimulator/CloudletSimulator/script/simple/device_compare/congestion_checked_devices1.binaryfile', 'rb') cd = pickle.load(cd) devices = cd num = len(devices) print("device_num", num) # 混雑度順で毎秒ごとのdevicesをソートする sorted_devices = devices_congestion_sort(devices, system_end_time) # --- # ここからメインの処理 for t in range(system_end_time): print("[TIME:", t, "]") # ある時刻tのMECに割り当てらえたデバイスを一時的に保存する用の変数 save_devices = [None] * mec_num for i in range(num): print("---new device---", sorted_devices[t][i].name) # plan_indexがデバイスの稼働時間外なら処理をスキップ if (check_plan_index(sorted_devices[t][i].plan_index, len(sorted_devices[t][i].plan)) == False): print("skip") continue # plan_indexが稼働時間内なら処理開始 if check_between_time(sorted_devices[t][i], t) == True: print(sorted_devices[t][i].plan_index) # 最近傍割り当て処理 device_flag, allocation_MEC_name = nearest_search2( sorted_devices[t][i], mec, sorted_devices[t][i].plan_index, cover_range, t) # 最近傍割り当てが成功したら表示する if device_flag == True: # deviceが直前で割り当てたMECを取得 mec_index = search_mec_index(mec, allocation_MEC_name) # print("device:", sorted_devices[t][i].name, ", use_resource:", sorted_devices[t][i].use_resource, "--->", "MEC_ID:", mec[mec_index].name, ", index:", i) # print(sorted_devices[t][i].mec_name, mec[mec_index].resource) # print(mec_index, len(save_devices)) # なぜindexがmec_indexなの? <- mec用のリストだから if save_devices[mec_index] == None: save_devices[mec_index] = [sorted_devices[t][i].name] else: save_devices[mec_index].append( sorted_devices[t][i].name) # --- # print(t, mec_index, index) # 実行時間外の時 else: # デバイスがMECを見つけられないかった時 print(devices[i].name) print("NOT FIND") # plan_indexをインクリメント sorted_devices[t][ i]._plan_index = sorted_devices[t][i]._plan_index + 1 else: # デバイスの稼働時間を超えた時の処理 # 前回割り当てたMECのリソースをリカバリーする。 if sorted_devices[t][i].mec_name != [] and sorted_devices[t][ i]._lost_flag == False: previous_index = search_mec_index( mec, sorted_devices[t][i].mec_name) print("DECREASE") sorted_devices[t][i].set_mode = "decrease" print(sorted_devices[t][i].mec_name) mec[previous_index].custom_resource_adjustment( sorted_devices[t][i], t) mec[previous_index].save_resource(t) sorted_devices[t][i].set_mode = "add" sorted_devices[t][i]._lost_flag = True # ある時刻tのMECに一時的に保存していた割り当てたデバイスをコピーする。 copy_to_mec(mec, save_devices, t) #----- # リソース消費量がそれぞれで違う時のテスト用関数を作成する # 各秒でMECが持っているデバイスのインデックスと数がわかるものとする sum = 0 mec_sum = 0 having_device_resource_sum = 0 for t in range(system_end_time): # print("time:", t) for m in range(mec_num): mec_sum = mec_sum + mec[m]._resource_per_second[t] if mec[m]._having_devices[t] is not None: # print("check", mec[m]._having_devices[t]) device_index = device_index_search(sorted_devices[t], mec[m]._having_devices[t]) # print(mec[m]._having_devices[t], device_index) having_device_resource_sum = having_device_resource_sum + device_resource_calc( sorted_devices[t], device_index) check_allocation(t, mec_num, MEC_resource, having_device_resource_sum, mec_sum) print((mec_num * MEC_resource - having_device_resource_sum), mec_sum) having_device_resource_sum = 0 sum = 0 mec_sum = 0 # print(sum, (150*100-sum), mec_sum) # print("resource",resource_sum) print("system_time: ", system_end_time) print("MEC_num: ", mec_num) print("device_num: ", num) sorted_devices = sorted_devices[0:system_end_time] maximum = max_hop_search(sorted_devices[-1]) print("max_hop: ", maximum) minimum = min_hop_search(sorted_devices[-1]) print("min_hop: ", minimum) average_hop = average_hop_calc(sorted_devices[-1]) print("average_hop: ", average_hop) reboot_rate = application_reboot_rate(mec, system_end_time) print("AP reboot rate:", reboot_rate) max_distance = max_distance_search(sorted_devices[-1]) print("max_distance:", max_distance) min_distance = min_distance_search(sorted_devices[-1]) print("min_distance:", min_distance) average_distance = average_distance_calc(sorted_devices[-1]) print("average_distance: ", average_distance) # for d in range(device_num): # print(devices[d].hop) result = [system_end_time] result.append(mec_num) result.append(MEC_resource) result.append(num) result.append(maximum) result.append(minimum) result.append(average_hop) result.append(reboot_rate) result.append(max_distance) result.append(min_distance) result.append(average_distance) # 結果をcsvへ書き込み write_csv(path_w, result) return average_hop, reboot_rate
def continue_priority_simulation(system_end_time, MEC_resource, device_num, continue_distance, f_time, device_allocation_method, path_w): df = pd.read_csv( "/Users/sugimurayuuki/Desktop/mecsimulator/CloudletSimulator/base_station/kddi_okayama_city2.csv", dtype={ 'lon': 'float', 'lat': 'float' }) server_type = "LTE" cover_range = 500 n = len(df) print("Number of MEC server:", n) mec = [MEC_server(0, 00, " ", 00.00, 00.00, 0, 0)] * n for index, series in df.iterrows(): mec[index] = MEC_server(MEC_resource, index + 1, server_type, series["lon"], series["lat"], cover_range, system_end_time) mec_num = len(df) # 集約局を対応するMECに設定する set_aggregation_station(mec) # 到着順 if device_allocation_method == 0: d = open( '/Users/sugimurayuuki/Desktop/mecsimulator/CloudletSimulator/dataset/device.clone_binaryfile', 'rb') devices = pickle.load(d) devices = devices[0:device_num] num = len(devices) for i in range(num): devices[i].startup_time = float( devices[i].plan[0].time) # 各デバイスの起動時間を設定する devices[i].set_congestion_status(system_end_time) devices[i].set_MEC_distance(len(df)) devices[i]._first_flag = True devices[i]._allocation_plan = [None] * system_end_time # 順序をシャッフル random.shuffle(devices) sorted_devices = [devices] * system_end_time for t in range(system_end_time): random.shuffle(devices) sorted_devices[t] = devices # リソース順 elif device_allocation_method == 1: d = open( '/Users/sugimurayuuki/Desktop/mecsimulator/CloudletSimulator/dataset/device.congestion_binaryfile', 'rb') devices = pickle.load(d) devices = devices[0:device_num] num = len(devices) devices = reverse_resource_sort(devices) for i in range(num): devices[i].startup_time = float( devices[i].plan[0].time) # 各デバイスの起動時間を設定する devices[i].set_congestion_status(system_end_time) devices[i].set_MEC_distance(len(df)) devices[i]._first_flag = True sorted_devices = [devices] * system_end_time # 混雑度順 else: # 混雑度計算 # traffic_congestion(mec, devices, system_end_time, 1000) cd = open( '/Users/sugimurayuuki/Desktop/mecsimulator/CloudletSimulator/dataset/congestion_checked_devices.binaryfile', 'rb') cd = pickle.load(cd) devices = cd num = len(devices) print("device_num", num) # 混雑度順で毎秒ごとのdevicesをソートする sorted_devices = devices_congestion_sort(devices, system_end_time) keep_count = 0 # save_devices = [] * data_length # --- # ここからメインの処理 for t in range(system_end_time): print("[TIME:", t, "]") # ある時刻tのMECに割り当てらえたデバイスを一時的に保存する用の変数 save_devices = [None] * mec_num for i in range(num): print("---new device---", sorted_devices[t][i].name) # plan_indexがデバイスの稼働時間外なら処理をスキップ if (check_plan_index(sorted_devices[t][i].plan_index, len(sorted_devices[t][i].plan)) == False): print("skip") continue # plan_indexが稼働時間内なら処理開始 if check_between_time(sorted_devices[t][i], t) == True: print(sorted_devices[t][i].plan_index) # 継続割り当て continue_flag, allocation_MEC_name = continue_search( sorted_devices[t][i], mec, sorted_devices[t][i].plan_index, cover_range, t, continue_distance) if continue_flag == True: keep_count = keep_count + 1 if continue_flag == False: print("移動経路優先度選択") loop_count = 0 # 移動経路優先で使う探索範囲の初期化 search_distance = continue_distance # 移動経路優先で使う未来の時刻の初期化 ftime = f_time # 優先度割り当て用のFLAGの初期化 property_flag = False # 優先度を割り当てられるまで完了するまでループ(探索範囲内にMECがなければ、距離を500つずつ増やす) # 車の平均速度を50km/hと仮定すると13m/s while (property_flag == False): # 予測時間の時にデバイスが稼働時間内の場合 if sorted_devices[t][i].plan_index + ftime < len( sorted_devices[t][i].plan): print("if") # 優先度ソート用のFLAGの初期化 sort_flag = False # 移動経路優先度計算が完了するまで繰り返す calc_count = 0 while (sort_flag == False): print("calc") calc_count = calc_count + 1 print("ソート直前ののMECの長さ", len(mec)) # 移動経路優先度にMECをソート sort_flag, sorted_mecs, sort_finish_index = move_plan_priority_calc( mec, sorted_devices[t][i], sorted_devices[t][i].plan_index, t, ftime, search_distance) print("ソート直後ののMECの長さ", len(sorted_mecs)) # 探索範囲の加算 search_distance = search_distance + 500 print(sort_flag, search_distance, sorted_devices[t][i].plan_index + ftime, len(sorted_devices[t][i].plan)) # 優先度順にソートしたMECを選択 property_flag, allocation_MEC_name = priority_allocation( sorted_mecs, sorted_devices[t][i], sorted_devices[t][i].plan_index, t, sort_finish_index) # MECを更新 if property_flag == True: mec = sorted_mecs # 予測時間がデバイスの稼働時間を超えてしまった場合 else: print("else") # デバイスの終了時間のインデックスを調べる shutdown_index = sorted_devices[t][ i].shutdown_time - sorted_devices[t][ i].startup_time # デバイスの終了時間のインデックスを予測時間に代入 ftime = shutdown_index - sorted_devices[t][ i].plan_index # 継続割り当て or 移動経路優先割り当てに成功した場合 # 割り当てたときのMECとデバイスの情報を表示 if continue_flag == True or property_flag == True: # 移動経路優先割り当てでソートした場合 # MECを更新 # if property_flag == True: # mec = sorted_mecs # ID順に昇順ソート mec = sorted(mec, key=lambda m: m.name, reverse=False) # deviceが直前で割り当てたMECを取得 mec_index = search_mec_index(mec, allocation_MEC_name) print(mec_index, mec[mec_index].name) print("device:", sorted_devices[t][i].name, ", use_resource:", sorted_devices[t][i].use_resource, "--->", "MEC_ID:", mec[mec_index].name, ", index:", i) print("割り振られたMEC:", sorted_devices[t][i].mec_name, ", MEC残量リソース:", mec[mec_index].resource) print(mec_index, len(save_devices)) # --- # 毎秒ごとの割り当てたデバイスを保存 # なぜindexがmec_indexなの? <- mec用のリストだから if save_devices[mec_index] == None: save_devices[mec_index] = [sorted_devices[t][i].name] else: save_devices[mec_index].append( sorted_devices[t][i].name) # --- # print(t, mec_index, index) allocation_MEC_name = 0 else: # デバイスが割り振れなかった時 # ここが実行されるのは本来ありえない print("NOT FIND") print("not find MEC error") sys.exit() # plan_indexをインクリメント(planを読み込むため) sorted_devices[t][ i]._plan_index = sorted_devices[t][i]._plan_index + 1 else: # デバイスの稼働時間を超えた時の処理 if sorted_devices[t][i].mec_name != [] and sorted_devices[t][ i]._lost_flag == False: mec = sorted(mec, key=lambda m: m.name, reverse=False) print("device", sorted_devices[t][i].mec_name, ": shutdown") print("DECREASE") sorted_devices[t][i].set_mode = "decrease" print(sorted_devices[t][i].mec_name) mec[sorted_devices[t][i].mec_name - 1].custom_resource_adjustment(sorted_devices[t][i], t) mec[sorted_devices[t][i].mec_name - 1].save_resource(t) sorted_devices[t][i].set_mode = "add" sorted_devices[t][i]._lost_flag = True # ある時刻tのMECに一時的に保存していた割り当てたデバイスをコピーする。 copy_to_mec(mec, save_devices, t) # MECを昇順に直す mec = sorted(mec, key=lambda m: m.name, reverse=False) sum = 0 mec_sum = 0 having_device_resource_sum = 0 """ for t in range(system_end_time): # print("time:", t) for m in range(mec_num): #if t == 95: # if mec[m]._having_devices_count[t] != (MEC_resource- mec[m]._resource_per_second[t]): # if mec[m].name == 11: #print("MEC_ID:", mec[m].name, ", having devices:", mec[m]._having_devices[t], #mec[m]._having_devices_count[t], #", mec_resouce:", mec[m]._resource_per_second[t], ", current time:", t, mec[m]._test) # sum = sum + mec[m]._having_devices_count[t] # mec_sum = mec_sum + mec[m]._resource_per_second[t] # sum = sum + mec[m]._having_devices_count[t] mec_sum = mec_sum + mec[m]._resource_per_second[t] if mec[m]._having_devices[t] is not None: # print("check", mec[m]._having_devices[t]) device_index = device_index_search(sorted_devices[t], mec[m]._having_devices[t]) # print(mec[m]._having_devices[t], device_index) having_device_resource_sum = having_device_resource_sum + device_resource_calc(sorted_devices[t], device_index) check_allocation(t, mec_num, MEC_resource, having_device_resource_sum, mec_sum) print((mec_num * MEC_resource - having_device_resource_sum), mec_sum) having_device_resource_sum = 0 sum = 0 mec_sum = 0 # print(sum, (150*100-sum), mec_sum) """ num = len(devices) print("system_time: ", system_end_time) print("MEC_num: ", mec_num) print("device_num: ", num) sorted_devices = sorted_devices[0:system_end_time] maximum = max_hop_search(sorted_devices[-1]) print("max_hop: ", maximum) minimum = min_hop_search(sorted_devices[-1]) print("min_hop: ", minimum) average_hop = average_hop_calc(sorted_devices[-1]) print("average_hop: ", average_hop) reboot_rate = application_reboot_rate(mec, system_end_time) print("AP reboot rate:", reboot_rate) print("continue_count", keep_count) max_distance = max_distance_search(sorted_devices[-1]) print("max_distance:", max_distance) min_distance = min_distance_search(sorted_devices[-1]) print("min_distance:", min_distance) average_distance = average_distance_calc(sorted_devices[-1]) print("average_distance: ", average_distance) result = [system_end_time] result.append(mec_num) result.append(MEC_resource) result.append(num) result.append(maximum) result.append(minimum) result.append(average_hop) result.append(reboot_rate) result.append(max_distance) result.append(min_distance) result.append(average_distance) # pathを動的に変えることで毎回新しいファイルを作成することができる write_csv(path_w, result) print("finish") return average_hop, reboot_rate
def continue_nearest_simulation(system_end_time, MEC_resource, device_num, continue_distance, device_allocation_method, path_w, how_compare): df = pd.read_csv( "/home/tomokishiraga/PycharmProjects/simulation/CloudletSimulator/base_station/okayama_kddi.csv", dtype={ 'lon': 'float', 'lat': 'float' }) server_type = "LTE" cover_range = 500 n = len(df) print("Number of MEC server:", n) mec = [MEC_server(0, 00, " ", 00.00, 00.00, 0, 0)] * n #MECインスタンスをCSVを元に生成 data_length = len(df) for index, series in df.iterrows(): mec[index] = MEC_server(MEC_resource, index + 1, server_type, series["lon"], series["lat"], cover_range, system_end_time) #mec = delete_mec(mec) #MECサーバにAPの情報を付与(乱数を取得し実行可能なAPを設定する) count = 1 for t in range(data_length): if count == 1: mec[t]._app_ver = [1, 2] count = 2 elif count == 2: mec[t]._app_ver = [1, 3] count = 3 else: mec[t]._app_ver = [2, 3] count = 1 mec_num = len(df) # 集約局を対応するMECに設定する set_aggregation_station(mec) # 到着順 if device_allocation_method == 0: if how_compare == "hop": d = open( '/home/tomokishiraga/PycharmProjects/simulation/CloudletSimulator/dataset/device.ns_clone_binaryfile2', 'rb') else: d = open( '/home/tomokishiraga/PycharmProjects/simulation/CloudletSimulator/dataset/device.ns_clone_binaryfile', 'rb') devices = pickle.load(d) devices = devices[0:device_num] num = len(devices) for i in range(num): devices[i].startup_time = float( devices[i].plan[0].time) # 各デバイスの起動時間を設定する devices[i].set_congestion_status(system_end_time) devices[i].set_MEC_distance(len(df)) devices[i]._first_flag = True devices[i]._allocation_plan = [None] * system_end_time # 順序をシャッフル(各時間ごとに到着順をランダムで決める) random.shuffle(devices) sorted_devices = [devices] * system_end_time for t in range(system_end_time): random.shuffle(devices) sorted_devices[t] = devices # リソース順 elif device_allocation_method == 1: if how_compare == "hop": d = open( '/home/tomokishiraga/PycharmProjects/simulation/CloudletSimulator/dataset/device.clone_binaryfile2', 'rb') else: d = open( '/home/tomokishiraga/PycharmProjects/simulation/CloudletSimulator/dataset/device.clone_binaryfile', 'rb') devices = pickle.load(d) devices = devices[0:device_num] num = len(devices) devices = reverse_resource_sort(devices) for i in range(num): devices[i].startup_time = float( devices[i].plan[0].time) # 各デバイスの起動時間を設定する devices[i].set_congestion_status(system_end_time) devices[i].set_MEC_distance(len(df)) devices[i]._first_flag = True sorted_devices = [devices] * system_end_time #デバイスをリソース順にソートする # 混雑度順 else: # 混雑度計算 # traffic_congestion(mec, devices, system_end_time, 1000) if how_compare == "hop": cd = open( '/home/tomokishiraga/PycharmProjects/simulation/CloudletSimulator/script/normal/device_compare/congestion_checked_devices2.binaryfile', 'rb') else: cd = open( '/home/tomokishiraga/PycharmProjects/simulation/CloudletSimulator/script/normal/device_compare/congestion_checked_devices1.binaryfile', 'rb') cd = pickle.load(cd) devices = cd num = len(devices) print("device_num", num) # 混雑度順で毎秒ごとのdevicesをソートする sorted_devices = devices_ap_congestion_sort(devices, system_end_time) # save_devices = [] * data_length # ここからメインの処理---------------------------------------------------------------------------------------- keep_count = 0 for t in range(system_end_time): #print("[TIME:", t, "]") # ある時刻tのMECに割り当てらえたデバイスを一時的に保存する用の変数 save_devices = [None] * mec_num for i in range(num): #print("---new device---", sorted_devices[t][i].name) # plan_indexがデバイスの稼働時間外なら処理をスキップ if (check_plan_index(sorted_devices[t][i].plan_index, len(sorted_devices[t][i].plan)) == False): #print("skip") continue # plan_indexが稼働時間内なら処理開始 if check_between_time(sorted_devices[t][i], t) == True: #print("plan_index", sorted_devices[t][i].plan_index, "max_index", len(sorted_devices[t][i].plan)) # 継続割り当て device_flag, allocation_MEC_name = continue_search( sorted_devices[t][i], mec, sorted_devices[t][i].plan_index, cover_range, t, continue_distance) if device_flag == True: # 継続回数 keep_count = keep_count + 1 # 最近傍選択 if device_flag == False: device_flag, allocation_MEC_name = nearest_search( sorted_devices[t][i], mec, sorted_devices[t][i].plan_index, cover_range, t) # 割り当てが成功したら表示する if device_flag == True: # deviceが直前で割り当てたMECのインデックスを取得 mec_index = search_mec_index(mec, allocation_MEC_name) # print("device:", sorted_devices[t][i].name, ", use_resource:", sorted_devices[t][i].use_resource, "--->", "MEC_ID:", mec[mec_index].name, ", index:", i) # print(sorted_devices[t][i].mec_name, mec[mec_index].resource) # print(mec_index, len(save_devices)) # --- # なぜindexがmec_indexなの? <- mec用のリストだから if save_devices[mec_index] == None: save_devices[mec_index] = [sorted_devices[t][i].name ] #ある時刻tにMECに割り当てたデバイス名を保存 else: save_devices[mec_index].append( sorted_devices[t][i].name) #デバイス名を追加 mec_index = 0 #初期化 #else: #print("NOT FIND") # plan_indexをインクリメント sorted_devices[t][i]._plan_index = sorted_devices[t][ i]._plan_index + 1 #sorted_devicesのインスタンスのplan_indexをインクリメント # デバイスの稼働時間を超えた時の処理 else: # もしデバイスの終了時間を超えた時のみ(1回だけ)、デバイスに直前に割り当てたMECのリソースをリカバリーする。 if sorted_devices[t][i].mec_name != [] and sorted_devices[t][ i]._lost_flag == False: #print("DECREASE") sorted_devices[t][ i].set_mode = "decrease" #移動し終わったデバイスのmodeを変更 #print(sorted_devices[t][i].mec_name) mec[sorted_devices[t][i].mec_name - 1].custom_resource_adjustment(sorted_devices[t][i], t) #MECのリソースを調整(増やす) mec[sorted_devices[t][i].mec_name - 1].save_resource( t ) #時刻tにおけるリソースの状態を保存するメソッド:resource_per_second[time]に保存 sorted_devices[t][i].set_mode = "add" #初期値に更新 sorted_devices[t][i]._lost_flag = True #初期値に更新 # ある時刻tのMECに一時的に保存していた割り当てたデバイスをコピーする。 copy_to_mec(mec, save_devices, t) #----- # リソース消費量がそれぞれで違う時のテスト用関数を作成する # 各秒でMECが持っているデバイスのインデックスと数がわかるものとする #sum = 0 #mec_sum = 0 #having_device_resource_sum = 0 """ for t in range(system_end_time): # print("time:", t) for m in range(mec_num): # if t == 16: # print("MEC_ID:", mec[m].name, ", having devices:", mec[m]._having_devices[t], mec[m]._having_devices_count[t], # ", mec_resouce:", mec[m]._resource_per_second[t], ", current time:", t) # sum = sum + mec[m]._having_devices_count[t] # mec_sum = mec_sum + mec[m]._resource_per_second[t] # sum = sum + mec[m]._having_devices_count[t] mec_sum = mec_sum + mec[m]._resource_per_second[t] if mec[m]._having_devices[t] is not None: # print("check", mec[m]._having_devices[t]) device_index = device_index_search(sorted_devices[t], mec[m]._having_devices[t]) # print(mec[m]._having_devices[t], device_index) having_device_resource_sum = having_device_resource_sum + device_resource_calc(sorted_devices[t], device_index) check_allocation(t, 150, MEC_resource, having_device_resource_sum, mec_sum) print((150 * MEC_resource - having_device_resource_sum), mec_sum) having_device_resource_sum = 0 sum = 0 mec_sum = 0 """ # print(sum, (150*100-sum), mec_sum) num = len(sorted_devices[-1]) print("system_time: ", system_end_time) print("MEC_num: ", mec_num) print("device_num: ", num) sorted_devices = sorted_devices[0:system_end_time] maximum = max_hop_search(sorted_devices[-1]) print("max_hop: ", maximum) minimum = min_hop_search(sorted_devices[-1]) print("min_hop: ", minimum) average_hop = average_hop_calc(sorted_devices[-1]) print("average_hop: ", average_hop) reboot_rate = application_reboot_rate(mec, system_end_time) print("AP reboot rate:", reboot_rate) max_distance = max_distance_search(sorted_devices[-1]) print("max_distance:", max_distance) min_distance = min_distance_search(sorted_devices[-1]) print("min_distance:", min_distance) average_distance = average_distance_calc(sorted_devices[-1]) print("average_distance: ", average_distance) result = [system_end_time] result.append(mec_num) result.append(MEC_resource) result.append(num) result.append(maximum) result.append(minimum) result.append(average_hop) result.append(reboot_rate) result.append(max_distance) result.append(min_distance) result.append(average_distance) # pathを動的に変えることで毎回新しいファイルを作成することができる write_csv(path_w, result) print("finish") return average_hop, reboot_rate
sorted_devices[t][i].startup_time) # デバイスの終了した時、リソースを回復するまで確かめるための変数 lost_flag = False #save_devices = [] * data_length # --- # ここからメインの処理 for t in range(system_end_time): print("[TIME:", t, "]") # ある時刻tのMECに割り当てらえたデバイスを一時的に保存する用の変数 save_devices = [None] * mec_num for i in range(num): print("---new device---", sorted_devices[t][i].name) # plan_indexがデバイスの稼働時間外なら処理をスキップ if (check_plan_index(sorted_devices[t][i].plan_index, len(sorted_devices[t][i].plan)) == False): print("skip") continue # plan_indexが稼働時間内なら処理開始 if check_between_time(sorted_devices[t][i], t) == True: print(sorted_devices[t][i].plan_index) # 最近傍割り当て処理 device_flag, memo = nearest_search2( sorted_devices[t][i], mec, sorted_devices[t][i].plan_index, cover_range, t) # 最近傍割り当てが成功したら表示する if device_flag == True: print("device:", sorted_devices[t][i].name, ", use_resource:", sorted_devices[t][i].use_resource, "--->", "MEC_ID:", mec[memo].name, ", index:", i) # print(sorted_devices[t][i].mec_name, mec[memo].resource)