Ejemplo n.º 1
0
    def Processing(self, agent, index_t):
        agent_id_requests = self.requests.Time_t_Requests_of_Agent_i(
            agent.ag_id, index_t)

        # 本地命中
        loc_hit = np.zeros(param.F, dtype=np.int32)
        for j in range(len(agent.state.cache_files)):
            tmp = agent.state.cache_files[j]
            # 真正已缓存的文件tmp,本地命中
            if tmp != -1 and agent_id_requests[tmp] != 0:
                loc_hit[tmp] = 1
        agent.action.loc = loc_hit

        # 请求cdn服务器
        neighbor_hit = np.zeros(param.F, dtype=np.int32)
        for j in range(len(agent_id_requests)):
            # 本地未命中
            if agent_id_requests[j] != 0 and agent.action.loc[j] == 0:
                # j 文件由 cdn 存储
                neighbor_hit[j] = 1

        agent.action.c = neighbor_hit

        # 奖励值
        alpha = param.alpha
        beta = param.beta
        l_e_j_t = param.cdn_latency(agent.ag_id)
        p_e_j_t = param.beta * param.traffic_cost_cdn
        isreplace = 1

        reward = 0
        for i in range(len(agent.action.c)):
            if agent.action.c[i] == 1:
                d_f_e_t = agent_id_requests[i]
                reward += 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)

        # 将cdn服务器内容取出来放入缓存中
        for i in range(len(agent.action.c)):
            if agent.action.c[i] == 1:
                agent.state.tocache(j)  #最近最少使用替换

        reward = 0 - reward
        hit = float(sum(agent.action.loc) / len(agent.state.cache_files))
        cdn_num = sum(agent.action.c)
        # 返回奖励值、命中率、请求cdn服务器的次数
        return reward, hit, cdn_num
Ejemplo n.º 2
0
    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