def cdn_latency(ag_id1): neighbors1 = csr.Neighbors(ag_id1) sumlatency = 0 for n_id1 in neighbors1: sumlatency += ne_latency(ag_id1, n_id1) # return 5*int(sumlatency/(len(neighbors1))) return 5 * float(sumlatency / (len(neighbors1)))
def mincost(self, agent, world, f_id, action): # 得到agent的所有邻居 ndarray neighbors = csr.Neighbors(agent.ag_id) # 提供最小开销的近邻/cdn # 几个近邻都一样,选择id最小的近邻 min_n = -1 # Cost = param.MAX_INT Cost = param.MAX_FLOAT isnehit = False isreplace = 0 # f_id是否需要替换 for n_id in neighbors: n_id_lochit = self.neighbors_id_loc(n_id, world) cost = 0 for ii in n_id_lochit: if ii == f_id: isnehit = True cost = float(param.alpha * param.ne_latency(agent.ag_id, n_id) + param.beta * param.traffic_cost_neighbor) if Cost > cost: Cost = cost min_n = int(n_id) break d_f_e_t = world.requests.Time_t_Requests_of_Agent_i( agent.ag_id, world.t - 1)[f_id] alpha = param.alpha beta = param.beta # 近邻命中 if isnehit == True: l_e_j_t = param.ne_latency(agent.ag_id, min_n) p_e_j_t = param.beta * param.traffic_cost_neighbor else: # cdn命中 l_e_j_t = param.cdn_latency(agent.ag_id) p_e_j_t = param.beta * param.traffic_cost_cdn # 是否需要加入替换开销 for ff_id in action: if ff_id == f_id: isreplace = 1 break # cost1 = int(alpha*d_f_e_t*l_e_j_t + beta*d_f_e_t*p_e_j_t + beta*isreplace*p_e_j_t) cost1 = float(alpha * d_f_e_t * l_e_j_t + beta * d_f_e_t * p_e_j_t + beta * isreplace * p_e_j_t) return 0 - cost1
def observation(self, agent, world): # 近邻agent的id从小到大排列 re = [] if world.t >= 0 and world.t <= 167: neighbors = csr.Neighbors(agent.ag_id) agent_cache_state = agent.state.cache_state agent_request = world.requests.Time_t_Requests_of_Agent_i( agent.ag_id, world.t) re.append(agent_cache_state) re.append(agent_request) for n_id in neighbors: # 近邻id从小到大 re.append(world.agents[n_id].state.cache_state) re.append( world.requests.Time_t_Requests_of_Agent_i(n_id, world.t)) re = np.asarray(re) return re
def observation(self,agent,world): # 近邻agent的id从小到大排列 re = [] if world.t >=0 and world.t <=167: neighbors = csr.Neighbors(agent.ag_id) agent_cache_state = agent.state.cache_state agent_request = world.requests.Time_t_Requests_of_Agent_i(agent.ag_id,world.t) re.append(agent_cache_state) re.append(agent_request) for n_id in neighbors: # 近邻id从小到大 re.append(world.agents[n_id].state.cache_state) re.append(world.requests.Time_t_Requests_of_Agent_i(n_id,world.t)) # if len(re)<18: # for j in range(18-len(re)): # re.append(list(np.zeros(100,dtype=np.int32))) re = np.asarray(re) return re
def cdn_hit(self, agent, world, f_id): neighbors = csr.Neighbors(agent.ag_id) min_n = -1 Cost = param.MAX_FLOAT isnehit = False for n_id in neighbors: n_id_lochit = self.neighbors_id_loc(n_id, world) cost = 0 for ii in n_id_lochit: if ii == f_id: isnehit = True cost = float(param.alpha * param.ne_latency(agent.ag_id, n_id) + param.beta * param.traffic_cost_neighbor) if Cost > cost: Cost = cost min_n = int(n_id) break # 近邻命中 if isnehit == True: return False else: return True