def control_exp2(budget, taskgraph_type, **kwargs): from utils import get_network_info_now from utils import getRandomId from model.record import writeoffloadingpolicy from utils import check_task_done from utils import log_controle_info import time # budget = budget taskgraph_type = taskgraph_type requestdeviceid = 1 applicationid, taskidlist = get_application(taskgraph_type) '获取当前上传网络速率' upload_rate = get_network_info_now("10.21.23.134") if 'upload_rate' in kwargs: upload_rate = kwargs['upload_rate'] '调用调度函数并设置参数' Xc_sep, Xf_sep = exp1_sep(budget, taskgraph_type, upload_rate) # Xc_gcc, Xf_gcc = exp1_gcc(budget, taskgraph_type, upload_rate) '将Xf_gcc, Xc_gcc 转成迁移策略 并保存在离线文件当中' offloading_policy = transform_X_sep(Xf_sep[0], Xc_sep[0]) offloadingpolicyid = getRandomId() policy = [] for i in range(0, len(taskidlist)): tmpdict = {} tmpdict['taskid'] = taskidlist[i] tmpdict['excuteddeviceid'] = int(offloading_policy[i]) policy.append(tmpdict) # 将迁移策略写入文件固化层 writeoffloadingpolicy(requestdeviceid=requestdeviceid, applicationid=applicationid, offloadingpolicyid=offloadingpolicyid, offloadingpolicy=policy) '将应用信息保存在离线文件当中' log_controle_info(applicationid, budget=budget, taskgraphtypeid=taskgraph_type, networkinfo=upload_rate, offloadingpolicyid=offloading_policy) '发送任务' control_send_task(taskgraph_type, taskidlist, applicationid, offloadingpolicyid) '确认是否已经完成任务' start_time = time.time() while not check_task_done(applicationid) and time.time() - start_time < 350: pass if check_task_done(applicationid): print("应用 {0} {1}已经完成".format(applicationid, offloading_policy)) else: print("应用 {0} {1}没有完成".format(applicationid, offloading_policy))
def control_task_graph(task_graph_type, upload_rate, budget, algor_type, datasize): from utils import get_network_info_now from utils import getRandomId from model.record import writeoffloadingpolicy from utils import check_task_done from utils import log_controle_info import time requestdeviceid = 1 applicationid, taskidlist = get_application(task_graph_type) # 根据算法类型调用参数 Xc, Xf = None, None offloading_policy = None if algor_type == 'sep': Xc, Xf = exp1_sep(budget, task_graph_type, upload_rate, datasize) Xc, Xf = Xc[0], Xf[0] offloading_policy = transform_X_sep(Xf, Xc) elif algor_type == 'gcc': Xc, Xf = exp1_hermes(budget=budget, task_graph=task_graph_type, upload_rate=upload_rate, num_image=datasize) Xc, Xf = Xc[0], Xf[0] offloading_policy = transform_X_sep(Xf, Xc) elif algor_type == 'hermes': Xc, Xf = exp1_gcc(budget=budget, task_graph=task_graph_type, upload_rate=upload_rate, num_image=datasize) offloading_policy = transform_X_gcc(Xf, Xc) else: print("In control task graph, the algor type is wrong not in the tlist") # 将Xf Xc 转换成为调度策略 并保存在离线文件当中 offloadingpolicyid = getRandomId() policy = [] for i in range(0, len(taskidlist)): tmpdict = {} tmpdict['taskid'] = taskidlist[i] tmpdict['excuteddeviceid'] = int(offloading_policy[i]) policy.append(tmpdict) # 将迁移策略写入文件固化层 writeoffloadingpolicy(requestdeviceid=requestdeviceid, applicationid=applicationid, offloadingpolicyid=offloadingpolicyid, offloadingpolicy=policy) '将应用信息保存在离线文件当中' log_controle_info(applicationid, budget=budget, taskgraphtypeid=task_graph_type, networkinfo=upload_rate, offloadingpolicyid=offloading_policy) '发送任务' control_send_task(task_graph_type, taskidlist, applicationid, offloadingpolicyid) '确认是否已经完成任务' start_time = time.time() while not check_task_done(applicationid) and time.time() - start_time < 600: pass if check_task_done(applicationid): # print("应用 {0} {1}已经完成".format(applicationid, offloading_policy)) pass else: print("应用 {0} {1}没有完成".format(applicationid, offloading_policy)) # exit(-1) return applicationid
def control_exp4(budget, taskgraph_type): from utils import get_network_info_now from utils import getRandomId from model.record import writeoffloadingpolicy from utils import check_task_done from utils import log_controle_info import time import numpy as np # budget = budget taskgraph_type = taskgraph_type requestdeviceid = 1 applicationid, taskidlist = get_application(taskgraph_type) '获取当前上传网络速率' upload_rate = get_network_info_now("10.21.23.134") '调用调度函数并设置参数' if taskgraph_type == 3: if upload_rate >= 0.7: Xf_gcc, Xc_gcc = np.array([[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0]], dtype=np.int), \ np.array([[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]], dtype=np.int) else: Xf_gcc, Xc_gcc = np.zeros(shape=(1, len(taskidlist))), np.zeros(shape=(1, len(taskidlist))) else: Xf_gcc, Xc_gcc = exp1_hermes(budget, taskgraph_type, upload_rate) # if taskgraph_type == 3 or taskgraph_type == 4: # Xc_gcc, Xf_gcc = np.zeros(shape=(1, len(taskidlist))), np.zeros(shape=(1, len(taskidlist))) # else: # Xc_gcc, Xf_gcc = exp1_hermes(budget, taskgraph_type, upload_rate) '将Xf_gcc, Xc_gcc 转成迁移策略 并保存在离线文件当中' offloading_policy = transform_X_sep(Xf_sep=Xf_gcc[0], Xc_sep=Xc_gcc[0]) offloadingpolicyid = getRandomId() policy = [] for i in range(0, len(taskidlist)): tmpdict = {} tmpdict['taskid'] = taskidlist[i] tmpdict['excuteddeviceid'] = int(offloading_policy[i]) policy.append(tmpdict) # 将迁移策略写入文件固化层 writeoffloadingpolicy(requestdeviceid=requestdeviceid, applicationid=applicationid, offloadingpolicyid=offloadingpolicyid, offloadingpolicy=policy) '将应用信息保存在离线文件当中' log_controle_info(applicationid, budget=budget, taskgraphtypeid=taskgraph_type, networkinfo=upload_rate, offloadingpolicyid=offloading_policy) '发送任务' control_send_task(taskgraph_type, taskidlist, applicationid, offloadingpolicyid) '确认是否已经完成任务' start_time = time.time() while not check_task_done(applicationid) and time.time() - start_time < 2000: pass if check_task_done(applicationid): print("应用 {0} {1}已经完成".format(applicationid, offloading_policy)) else: print("应用 {0} {1}没有完成".format(applicationid, offloading_policy))
def control_exp3(budget, taskgraph_type, **kwargs): from utils import get_network_info_now from utils import getRandomId from model.record import writeoffloadingpolicy from utils import check_task_done from utils import log_controle_info import os import json import copy # budget = budget taskgraph_type = taskgraph_type requestdeviceid = 1 applicationid, taskidlist = get_application(taskgraph_type) '获取当前上传网络速率' upload_rate = get_network_info_now("10.21.23.134") if 'upload_rate' in kwargs: upload_rate = kwargs['upload_rate'] '调用调度函数并设置参数' Xc_sep, Xf_sep = exp1_hermes(budget, taskgraph_type, upload_rate) # Xc_gcc, Xf_gcc = exp1_gcc(budget, taskgraph_type, upload_rate) '将Xf_gcc, Xc_gcc 转成迁移策略 并保存在离线文件当中' offloading_policy = transform_X_sep(Xf_sep[0], Xc_sep[0]) offloadingpolicyid = getRandomId() policy = [] for i in range(0, len(taskidlist)): tmpdict = {} tmpdict['taskid'] = taskidlist[i] tmpdict['excuteddeviceid'] = int(offloading_policy[i]) policy.append(tmpdict) # 将迁移策略写入文件固化层 writeoffloadingpolicy(requestdeviceid=requestdeviceid, applicationid=applicationid, offloadingpolicyid=offloadingpolicyid, offloadingpolicy=policy) '将应用信息保存在离线文件当中' log_controle_info(applicationid, budget=budget, taskgraphtypeid=taskgraph_type, networkinfo=upload_rate, offloadingpolicyid=offloading_policy) '发送任务' control_send_task(taskgraph_type, taskidlist, applicationid, offloadingpolicyid) '确认是否已经完成任务' while not check_task_done(applicationid): pass '返回应用完成时间 及应用完成能耗 及应用时间戳' application_finish_time = -1 application_finish_cost = -1 application_task_completing_time = None local_dir = r"C:\Users\derfei\Desktop\TMS_Exp\Mobile\Mobile\network" file_path = os.path.join(local_dir, "log_{0}.txt".format(applicationid)) with open(file_path, 'r+') as file: line = file.readlines()[0] time_list = json.loads(line) application_finish_time = time_list[-1][1] - time_list[0][0] #computer the energy cost Ptx = 0.1 # 国标上限 P0 = 1 # edge 之间传输的功耗 Pc_edge = 65 Pc_cloud = 65 Pc_mobile = 3.37 # 计算计算产生的能耗 computation_energy_cost = 0 for i in range(len(offloading_policy)): if int(offloading_policy[i]) == 1: computation_energy_cost += (time_list[i+1][1]-time_list[i+1][0])* Pc_mobile elif int(offloading_policy[i]) == 3: computation_energy_cost += (time_list[i+1][1]-time_list[i+1][0])* Pc_cloud else: computation_energy_cost += (time_list[i+1][1]-time_list[i+1][0])* Pc_edge # 通讯产生的能耗 communication_energy_cost = 0 application_task_completing_time = copy.deepcopy(time_list) print("应用 {0} 已经完成".format(applicationid))