Beispiel #1
0
def simu(request_num=15, req_num_eachtime=4, sigma=500000, max_iter=1, bs_num=6):
    # bs_num = 6
    # BSC:base station capacity
    # RBSC: residuary base station capacity
    # SR: slice request
    # max_iter = 1  # ------------------------
    delta = 0.000000001
    pc = 0.8
    pm = 0.01
    # req_num_eachtime = 4
    # 构造request_num次请求
    # request_num = 15  # --------------------------
    values = np.zeros((request_num), dtype=np.float)
    solutions = []
    sr_all = []
    rbscs = []
    # 每轮处理请求失败的切片请求数,fails[0]是遗传、fails[1]是贪心总代价、fails[2]是贪心下行带宽、fails[3]是贪心上行带宽、fails[4]是贪心计算资源
    fails = np.zeros((7, request_num))
    # 记录7中算法每次迭代得到下行,上行,计算,总代价
    cost_result = np.zeros((7, request_num, 4), dtype=np.float)
    resource_used_radio = np.zeros((7, bs_num, 3), dtype=np.float)
    time_resouce_used = request_num - 1
    # sigma = 50000
    # 构造m个切片请求
    m = req_num_eachtime * request_num
    sr_total = np.zeros((m, 3), dtype=np.float)
    for i in range(m):
        s = np.abs(np.random.normal(1000, sigma, 3))
        s = 1.5 * s / (sum(s))
        sr_total[i] = s
    for iter in range(request_num):
        # 随机构造每次请求的切片数
        m = (iter + 1) * req_num_eachtime
        # 构造基站资源
        rbsc = getRbsc(bs_num)
        total_rbsc = np.sum(rbsc, 0)  # 求每列之和,得到1*3向量,分别表示下行,上行,计算资源总量
        # 构造m个切片请求
        sr = np.zeros((m, 3), dtype=np.float)
        for i in range(m):
            s = sr_total[i]
            sr[i] = s

        rbscs.append(rbsc)
        print("rbsc:")
        print(rbsc)
        print("sr:")
        print(sr)
        sr_all.append(sr)  # 记录请求,为其他算法提供相同的请求环境
        populationSize = min(50, m * bs_num)
        solution, value = ga(sr, rbsc, max_iter, delta, pc, pm, populationSize)

        # 资源紧张的时候,采用greedy算法,得到可以满足的情况
        while solution == "failed" and np.size(sr, 0) >= 2:
            cost, rbsc_r, solution = greedy.greedy_min_cost(sr, rbsc, delta)
            x1 = np.sum(solution, 1)  # 求每行之和

            cost, rbsc_r, solution = greedy_resource.greedy_min_cost(sr, rbsc, delta)
            x2 = np.sum(solution, 1)  # 求每行之和

            cost, rbsc_r, solution = greedy_down_bandwidth.greedy_min_down_bandwidth_cost(sr, rbsc, delta)
            x3 = np.sum(solution, 1)  # 求每行之和

            cost, rbsc_r, solution = greedy_up_bandwidth.greedy_min_up_bandwidth_cost(sr, rbsc, delta)
            x4 = np.sum(solution, 1)  # 求每行之和

            cost, rbsc_r, solution = greedy_computing.greedy_min_compute_cost(sr, rbsc, delta)
            x5 = np.sum(solution, 1)  # 求每行之和
            XX = np.array((x1, x2, x3, x4, x5))
            X = np.array((np.sum(x1), np.sum(x2), np.sum(x3), np.sum(x4), np.sum(x5)))
            x = np.max(X)
            if x == 0:
                solution == "failed"
                value = 0
                sr = np.array([])
                break
            index = np.where(X == x)
            x = XX[index[0][0]]
            sr_list = []
            for s in range(np.size(x)):
                if x[s] == 1:
                    sr_list.append(sr[s])
            sr = np.array(sr_list)
            solution, value = ga(sr, rbsc, max_iter, delta, pc, pm, populationSize)
        # 记录失败数目
        fails[0][iter] = np.size(sr_all[iter], 0) - np.sum(np.sum(solution))
        print('最优目标函数值:', value)
        values[iter] = value
        print('solution:')
        print(solution)
        ##############################
        # 持久化结果
        fit = getFitnessValue(sr, rbsc, [solution], delta)
        o = [fit[0, 0], fit[0, 1], fit[0, 2], fit[0, 3]]
        cost_result[0][iter][0] = fit[0, 0]
        cost_result[0][iter][1] = fit[0, 1]
        cost_result[0][iter][2] = fit[0, 2]
        cost_result[0][iter][3] = fit[0, 0] + fit[0, 1] + fit[0, 2]
        ##############################
        solutions.append(np.copy(solution))
        if iter == time_resouce_used:
            rbsc_init = getRbsc(bs_num)
            rbsc = update_rbsc(sr, rbsc_init, solution)
            for bs in range(bs_num):
                for resource_type in range(3):
                    resource_used_radio[0][bs][resource_type] = rbsc[bs][resource_type] / rbsc_init[bs][resource_type]
    print("ga总结果")
    print(values)
    # print(rbsc)
    ###########################################################################################################
    for i in range(request_num):
        sr = sr_all[i]
        rbsc = rbscs[i]
        cost, rbsc, solution = greedy.greedy_min_cost(sr, rbsc, delta)
        values[i] = cost
        ##############################
        # 持久化结果
        fit = getFitnessValue(sr, rbscs[i], [solution], delta)
        o = [fit[0, 0], fit[0, 1], fit[0, 2], fit[0, 3]]
        cost_result[1][i][0] = fit[0, 0]
        cost_result[1][i][1] = fit[0, 1]
        cost_result[1][i][2] = fit[0, 2]
        cost_result[1][i][3] = fit[0, 0] + fit[0, 1] + fit[0, 2]
        ##############################
        # 记录失败数
        fails[1][i] = np.size(sr, 0) - np.sum(np.sum(solution, 0), 0)
        if i == time_resouce_used:
            rbsc_init = getRbsc(bs_num)
            rbsc = update_rbsc(sr, rbsc_init, solution)
            for bs in range(bs_num):
                for resource_type in range(3):
                    resource_used_radio[1][bs][resource_type] = rbsc[bs][resource_type] / rbsc_init[bs][resource_type]
    print("greedy_min_cost总结果")
    print(values)
    ##############################################################################################################
    for i in range(request_num):
        sr = sr_all[i]
        rbsc = rbscs[i]
        # cost, rbsc, solution = greedy_down_bandwidth.greedy_min_down_bandwidth_cost(sr, rbsc, delta)
        cost, rbsc, solution = greedy_bandwidth.greedy_min_bandwidth_cost(sr, rbsc, delta)
        values[i] = cost
        ##############################
        # 持久化结果
        fit = getFitnessValue(sr, rbscs[i], [solution], delta)
        o = [fit[0, 0], fit[0, 1], fit[0, 2], fit[0, 3]]
        cost_result[2][i][0] = fit[0, 0]
        cost_result[2][i][1] = fit[0, 1]
        cost_result[2][i][2] = fit[0, 2]
        cost_result[2][i][3] = fit[0, 0] + fit[0, 1] + fit[0, 2]
        ##############################
        # 记录失败数
        fails[2][i] = np.size(sr, 0) - np.sum(np.sum(solution, 0), 0)
        if i == time_resouce_used:
            rbsc_init = getRbsc(bs_num)
            rbsc = update_rbsc(sr, rbsc_init, solution)
            for bs in range(bs_num):
                for resource_type in range(3):
                    resource_used_radio[2][bs][resource_type] = rbsc[bs][resource_type] / rbsc_init[bs][resource_type]
    print("greedy_min_bandwidth_cost总结果")
    print(values)
    ##############################################################################################################
    for i in range(request_num):
        sr = sr_all[i]
        rbsc = rbscs[i]
        cost, rbsc, solution = greedy_up_bandwidth.greedy_min_up_bandwidth_cost(sr, rbsc, delta)
        values[i] = cost
        ##############################
        # 持久化结果
        fit = getFitnessValue(sr, rbscs[i], [solution], delta)
        o = [fit[0, 0], fit[0, 1], fit[0, 2], fit[0, 3]]
        cost_result[3][i][0] = fit[0, 0]
        cost_result[3][i][1] = fit[0, 1]
        cost_result[3][i][2] = fit[0, 2]
        cost_result[3][i][3] = fit[0, 0] + fit[0, 1] + fit[0, 2]
        ##############################
        # 记录失败数
        fails[3][i] = np.size(sr, 0) - np.sum(np.sum(solution, 0), 0)
        if i == time_resouce_used:
            rbsc_init = getRbsc(bs_num)
            rbsc = update_rbsc(sr, rbsc_init, solution)
            for bs in range(bs_num):
                for resource_type in range(3):
                    resource_used_radio[3][bs][resource_type] = rbsc[bs][resource_type] / rbsc_init[bs][resource_type]
    print("greedy_min_up_bandwidth_cost总结果")
    print(values)
    ##############################################################################################################
    for i in range(request_num):
        sr = sr_all[i]
        rbsc = rbscs[i]
        cost, rbsc, solution = greedy_computing.greedy_min_compute_cost(sr, rbsc, delta)
        values[i] = cost
        ##############################
        # 持久化结果
        fit = getFitnessValue(sr, rbscs[i], [solution], delta)
        o = [fit[0, 0], fit[0, 1], fit[0, 2], fit[0, 3]]
        cost_result[4][i][0] = fit[0, 0]
        cost_result[4][i][1] = fit[0, 1]
        cost_result[4][i][2] = fit[0, 2]
        cost_result[4][i][3] = fit[0, 0] + fit[0, 1] + fit[0, 2]
        ##############################
        # 记录失败数
        fails[4][i] = np.size(sr, 0) - np.sum(np.sum(solution, 0))
        if i == time_resouce_used:
            rbsc_init = getRbsc(bs_num)
            rbsc = update_rbsc(sr, rbsc_init, solution)
            for bs in range(bs_num):
                for resource_type in range(3):
                    resource_used_radio[4][bs][resource_type] = rbsc[bs][resource_type] / rbsc_init[bs][resource_type]
    print("greedy_min_compute_cost总结果")
    print(values)
    ##############################################################################################################
    for i in range(request_num):
        sr = sr_all[i]
        rbsc = rbscs[i]
        cost, rbsc, solution = greedy_resource.greedy_min_cost(sr, rbsc, delta)
        values[i] = cost
        ##############################
        # 持久化结果
        fit = getFitnessValue(sr, rbscs[i], [solution], delta)
        o = [fit[0, 0], fit[0, 1], fit[0, 2], fit[0, 3]]
        cost_result[5][i][0] = fit[0, 0]
        cost_result[5][i][1] = fit[0, 1]
        cost_result[5][i][2] = fit[0, 2]
        cost_result[5][i][3] = fit[0, 0] + fit[0, 1] + fit[0, 2]
        # 记录失败数
        fails[5][i] = np.size(sr, 0) - np.sum(np.sum(solution, 0), 0)
        if i == time_resouce_used:
            rbsc_init = getRbsc(bs_num)
            rbsc = update_rbsc(sr, rbsc_init, solution)
            for bs in range(bs_num):
                for resource_type in range(3):
                    resource_used_radio[5][bs][resource_type] = rbsc[bs][resource_type] / rbsc_init[bs][resource_type]
    print("greedy_min_max_cost总结果")
    print(values)
    ##############################################################################################################
    for i in range(request_num):
        sr = sr_all[i]
        rbsc = rbscs[i]
        cost, rbsc, solution = random_select.random_select(sr, rbsc, delta)
        values[i] = cost
        ##############################
        # 持久化结果
        fit = getFitnessValue(sr, rbscs[i], [solution], delta)
        o = [fit[0, 0], fit[0, 1], fit[0, 2], fit[0, 3]]
        cost_result[6][i][0] = fit[0, 0]
        cost_result[6][i][1] = fit[0, 1]
        cost_result[6][i][2] = fit[0, 2]
        cost_result[6][i][3] = fit[0, 0] + fit[0, 1] + fit[0, 2]
        # 记录失败数
        fails[6][i] = np.size(sr, 0) - np.sum(np.sum(solution, 0), 0)
        if i == time_resouce_used:
            rbsc_init = getRbsc(bs_num)
            rbsc = update_rbsc(sr, rbsc_init, solution)
            for bs in range(bs_num):
                for resource_type in range(3):
                    resource_used_radio[6][bs][resource_type] = rbsc[bs][resource_type] / rbsc_init[bs][resource_type]
    print("random总结果")
    print(values)
    ##############################################################################################################
    print(fails)
    return cost_result, fails, resource_used_radio
Beispiel #2
0
def simu(request_num=15, req_num_eachtime=4, sigma=50000, max_iter=1):
    bs_num = 6
    # BSC:base station capacity
    # RBSC: residuary base station capacity
    # SR: slice request
    max_iter = 1  # ------------------------
    delta = 0.000000001
    pc = 0.8
    pm = 0.01
    # req_num_eachtime = 4
    # 构造request_num次请求
    # request_num = 15  # --------------------------
    values = np.zeros((request_num), dtype=np.float)
    solutions = []
    sr_all = []
    rbscs = []
    # 每轮处理请求失败的切片请求数,fails[0]是遗传、fails[1]是贪心总代价、fails[2]是贪心下行带宽、fails[3]是贪心上行带宽、fails[4]是贪心计算资源
    fails = np.zeros((7, request_num))
    # 记录7中算法每次迭代得到下行,上行,计算,总代价
    cost_result = np.zeros((7, request_num, 4), dtype=np.float)
    # sigma = 50000
    # 构造m个切片请求
    m = req_num_eachtime * request_num
    sr_total = np.zeros((m, 3), dtype=np.float)
    for i in range(m):
        s = np.abs(np.random.normal(100, sigma, 3)) + 1
        s = s / (sum(s))
        sr_total[i] = s
    for iter in range(request_num):
        print('iter')
        print(iter)
        # 随机构造每次请求的切片数
        m = (iter + 1) * req_num_eachtime
        # 构造基站资源
        rbsc = getRbsc(bs_num)
        total_rbsc = np.sum(rbsc, 0)  # 求每列之和,得到1*3向量,分别表示下行,上行,计算资源总量
        # 构造m个切片请求
        sr = np.zeros((m, 3), dtype=np.float)
        for i in range(m):
            s = sr_total[i]
            sr[i] = s

        rbscs.append(rbsc)
        print("rbsc:")
        print(rbsc)
        print("sr:")
        print(sr)
        sr_all.append(sr)  # 记录请求,为其他算法提供相同的请求环境
        populationSize = min(m * bs_num, 50)
        solution, value = ga(sr, rbsc, max_iter, delta, pc, pm, populationSize)

        # 资源紧张的时候,采用greedy算法,得到可以满足的情况
        while solution == "failed" and np.size(sr, 0) >= 2:
            cost, rbsc_r, solution = greedy.greedy_min_cost(sr, rbsc, delta)
            x1 = np.sum(solution, 1)  # 求每行之和

            cost, rbsc_r, solution = greedy_resource.greedy_min_cost(
                sr, rbsc, delta)
            x2 = np.sum(solution, 1)  # 求每行之和

            cost, rbsc_r, solution = greedy_down_bandwidth.greedy_min_down_bandwidth_cost(
                sr, rbsc, delta)
            x3 = np.sum(solution, 1)  # 求每行之和

            cost, rbsc_r, solution = greedy_up_bandwidth.greedy_min_up_bandwidth_cost(
                sr, rbsc, delta)
            x4 = np.sum(solution, 1)  # 求每行之和

            cost, rbsc_r, solution = greedy_computing.greedy_min_compute_cost(
                sr, rbsc, delta)
            x5 = np.sum(solution, 1)  # 求每行之和
            XX = np.array((x1, x2, x3, x4, x5))
            X = np.array(
                (np.sum(x1), np.sum(x2), np.sum(x3), np.sum(x4), np.sum(x5)))
            x = np.max(X)
            if x == 0:
                solution == "failed"
                value = 0
                sr = np.array([])
                break
            index = np.where(X == x)
            x = XX[index[0][0]]
            sr_list = []
            for s in range(np.size(x)):
                if x[s] == 1:
                    sr_list.append(sr[s])
            sr = np.array(sr_list)
            solution, value = ga(sr, rbsc, max_iter, delta, pc, pm,
                                 populationSize)
        # 记录失败数目
        fails[0][iter] = np.size(sr_all[iter], 0) - np.sum(np.sum(solution))
        print('最优目标函数值:', value)
        values[iter] = value
        print('solution:')
        print(solution)
        ##############################
        # 持久化结果
        fit = getFitnessValue(sr, rbsc, [solution], delta)
        o = [fit[0, 0], fit[0, 1], fit[0, 2], fit[0, 3]]
        cost_result[0][iter][0] = fit[0, 0]
        cost_result[0][iter][1] = fit[0, 1]
        cost_result[0][iter][2] = fit[0, 2]
        cost_result[0][iter][3] = fit[0, 0] + fit[0, 1] + fit[0, 2]
        result = {iter: o}
        # json.dump(result, fp1)
        ##############################
        solutions.append(np.copy(solution))
        # rbsc = update_rbsc(sr, rbsc, solution)
    print("ga总结果")
    print(values)
    # print(rbsc)
    ###########################################################################################################
    for i in range(request_num):
        sr = sr_all[i]
        rbsc = rbscs[i]
        cost, rbsc, solution = greedy.greedy_min_cost(sr, rbsc, delta)
        values[i] = cost
        ##############################
        # 持久化结果
        fit = getFitnessValue(sr, rbscs[i], [solution], delta)
        o = [fit[0, 0], fit[0, 1], fit[0, 2], fit[0, 3]]
        cost_result[1][i][0] = fit[0, 0]
        cost_result[1][i][1] = fit[0, 1]
        cost_result[1][i][2] = fit[0, 2]
        cost_result[1][i][3] = fit[0, 0] + fit[0, 1] + fit[0, 2]
        result = {i: o}
        ##############################
        # 记录失败数
        fails[1][i] = np.size(sr, 0) - np.sum(np.sum(solution, 0), 0)
    print("greedy_min_cost总结果")
    print(values)
    ##############################################################################################################
    for i in range(request_num):
        sr = sr_all[i]
        rbsc = rbscs[i]
        # cost, rbsc, solution = greedy_down_bandwidth.greedy_min_down_bandwidth_cost(sr, rbsc, delta)
        cost, rbsc, solution = greedy_bandwidth.greedy_min_bandwidth_cost(
            sr, rbsc, delta)
        values[i] = cost
        ##############################
        # 持久化结果
        fit = getFitnessValue(sr, rbscs[i], [solution], delta)
        o = [fit[0, 0], fit[0, 1], fit[0, 2], fit[0, 3]]
        cost_result[2][i][0] = fit[0, 0]
        cost_result[2][i][1] = fit[0, 1]
        cost_result[2][i][2] = fit[0, 2]
        cost_result[2][i][3] = fit[0, 0] + fit[0, 1] + fit[0, 2]
        result = {i: o}
        ##############################
        # 记录失败数
        fails[2][i] = np.size(sr, 0) - np.sum(np.sum(solution, 0), 0)
    print("greedy_min_bandwidth_cost总结果")
    print(values)
    ##############################################################################################################
    for i in range(request_num):
        sr = sr_all[i]
        rbsc = rbscs[i]
        cost, rbsc, solution = greedy_up_bandwidth.greedy_min_up_bandwidth_cost(
            sr, rbsc, delta)
        values[i] = cost
        ##############################
        # 持久化结果
        fit = getFitnessValue(sr, rbscs[i], [solution], delta)
        o = [fit[0, 0], fit[0, 1], fit[0, 2], fit[0, 3]]
        cost_result[3][i][0] = fit[0, 0]
        cost_result[3][i][1] = fit[0, 1]
        cost_result[3][i][2] = fit[0, 2]
        cost_result[3][i][3] = fit[0, 0] + fit[0, 1] + fit[0, 2]
        result = {i: o}
        ##############################
        # 记录失败数
        fails[3][i] = np.size(sr, 0) - np.sum(np.sum(solution, 0), 0)
    print("greedy_min_up_bandwidth_cost总结果")
    print(values)
    ##############################################################################################################
    for i in range(request_num):
        sr = sr_all[i]
        rbsc = rbscs[i]
        cost, rbsc, solution = greedy_computing.greedy_min_compute_cost(
            sr, rbsc, delta)
        values[i] = cost
        ##############################
        # 持久化结果
        fit = getFitnessValue(sr, rbscs[i], [solution], delta)
        o = [fit[0, 0], fit[0, 1], fit[0, 2], fit[0, 3]]
        cost_result[4][i][0] = fit[0, 0]
        cost_result[4][i][1] = fit[0, 1]
        cost_result[4][i][2] = fit[0, 2]
        cost_result[4][i][3] = fit[0, 0] + fit[0, 1] + fit[0, 2]
        result = {i: o}
        ##############################
        # 记录失败数
        fails[4][i] = np.size(sr, 0) - np.sum(np.sum(solution, 0))
    print("greedy_min_compute_cost总结果")
    print(values)
    ##############################################################################################################
    for i in range(request_num):
        sr = sr_all[i]
        rbsc = rbscs[i]
        cost, rbsc, solution = greedy_resource.greedy_min_cost(sr, rbsc, delta)
        values[i] = cost
        ##############################
        # 持久化结果
        fit = getFitnessValue(sr, rbscs[i], [solution], delta)
        o = [fit[0, 0], fit[0, 1], fit[0, 2], fit[0, 3]]
        cost_result[5][i][0] = fit[0, 0]
        cost_result[5][i][1] = fit[0, 1]
        cost_result[5][i][2] = fit[0, 2]
        cost_result[5][i][3] = fit[0, 0] + fit[0, 1] + fit[0, 2]
        result = {i: o}
        # 记录失败数
        fails[5][i] = np.size(sr, 0) - np.sum(np.sum(solution, 0), 0)
    print("greedy_min_max_cost总结果")
    print(values)
    ##############################################################################################################
    for i in range(request_num):
        sr = sr_all[i]
        rbsc = rbscs[i]
        cost, rbsc, solution = random_select.random_select(sr, rbsc, delta)
        values[i] = cost
        ##############################
        # 持久化结果
        fit = getFitnessValue(sr, rbscs[i], [solution], delta)
        o = [fit[0, 0], fit[0, 1], fit[0, 2], fit[0, 3]]
        cost_result[6][i][0] = fit[0, 0]
        cost_result[6][i][1] = fit[0, 1]
        cost_result[6][i][2] = fit[0, 2]
        cost_result[6][i][3] = fit[0, 0] + fit[0, 1] + fit[0, 2]
        result = {i: o}
        # 记录失败数
        fails[6][i] = np.size(sr, 0) - np.sum(np.sum(solution, 0), 0)
    print("random总结果")
    print(values)
    ##############################################################################################################
    print(fails)
    # nowtime = (lambda: int(round(time.time() * 1000)))
    # nowtime = nowtime()
    # print(cost_result[:, :, 0])
    # print(cost_result[:, :, 1])
    # print(cost_result[:, :, 2])
    # print(cost_result[:, :, 3])
    # pt.plot_fun_slot(cost_result[:, :, 0], fails, req_num_eachtime, '切片请求数量(个)', '平均下行带宽映射代价',
    #                  str(nowtime) + '下行带宽映射代价' + '_' + str(sigma))
    # pt.plot_fun_slot(cost_result[:, :, 1], fails, req_num_eachtime, '切片请求数量(个)', '平均上行带宽映射代价',
    #                  str(nowtime) + '上行带宽映射代价' + '_' + str(sigma))
    # pt.plot_fun_slot(cost_result[:, :, 2], fails, req_num_eachtime, '切片请求数量(个)', '平均计算资源映射代价',
    #                  str(nowtime) + '计算资源映射代价' + '_' + str(sigma))
    # pt.plot_fun_slot(cost_result[:, :, 3], fails, req_num_eachtime, '切片请求数量(个)', '平均总映射代价',
    #                  str(nowtime) + '总映射代价' + '_' + str(sigma))
    # pt.plot_fun_fail_slot(fails, req_num_eachtime, '切片请求数量(个)', '失败率(%)', str(nowtime) + '失败率' + '_' + str(sigma))
    return cost_result, fails