华为2022年软挑赛初赛试题及初级代码(成渝赛区)

目录

1.赛题描述

2.全部代码


1.赛题描述

华为2022年软挑赛初赛试题及初级代码(成渝赛区)_第1张图片

 华为2022年软挑赛初赛试题及初级代码(成渝赛区)_第2张图片

 

2.全部代码


# import psutil  ##查看程序所占用的内存空间
# import os

import numpy as np
import configparser
import time

start =time.clock()

config_path = 'F:\\桌面\\data\\config.ini'
path1 = "F:\\桌面\\data\\demand.csv"
path2 = "F:\\桌面\\data\\site_bandwidth.csv"
path3 = "F:\\桌面\\data\\qos.csv"
path4 = "F:\\桌面\\data\\output\\solution.txt"
##路径
# config_path = '/data/config.ini'
# path1 = "/data/demand.csv"
# path2 = "/data/site_bandwidth.csv"
# path3 = "/data/qos.csv"
# path4 = "/output/solution.txt"
#读取配置文件
conf = configparser.ConfigParser()
conf.read(config_path, encoding="utf-8")
qos_constraint = conf.getint("config", "qos_constraint")

##获取时间度个数
data1 = np.loadtxt(path1, dtype=str, delimiter=',', skiprows=1)
t = len(data1)
# print(t)

##获取客户结点ID
data0 = np.loadtxt(path1, dtype=str, delimiter=',')
data2 = data0[0].tolist()
data2.pop(0)
n = data2

##获取边缘节点ID
data3 = np.loadtxt(path2, dtype=str, delimiter=',', skiprows=1)
m = []
for item3 in data3:
    m.append(item3[0].tolist()[0:2])
# print(data3)
# print(m)
##定义客户节点类
class custom:
    def __init__(self, name):
        self.name = name
##定义边缘节点类
class bianyuan(custom):
    # cj = 430080
    def __init__(self, name, data, cj):
        super(bianyuan, self).__init__(name)
        self.cj = cj
        self.data = data
    def distribute(self, slip): ###遍历每个客户结点更新
        self.data = self.data + slip


### 读取一个时间刻度的需求量数据
def read_demand_data(path, time_chip):
    data1 = np.loadtxt(path, dtype=str, delimiter=',', skiprows=1)
    m_demand = {}
    for item in range(1, len(n)+1):
        item_data = data1[time_chip-1][item]
        m_demand.update({n[item-1]: item_data})
    # print(m_demand)
    return m_demand

# read_demand_data("E:\\桌面\\华为云作业\\线下调试数据\\data\\demand.csv")

# 读取一个时间刻度的qos限制数据
def read_qos_data(path, i):
    data2 = np.loadtxt(path, dtype=str, delimiter=',', skiprows=1)
    # print(data2)
    m_qos = {}
    for item in data2:
        m_qos.update({"%s"%(item[0]):int(item[i])})
    # print(m_qos)
    return m_qos


# for i in range(1, len(n)+1): ##表示的是客户节点的循环,应该放在程序的开始
#     read_qos_data("E:\\桌面\\华为云作业\\线下调试数据\\data\\qos.csv", i)

# 读取一个时间刻度的单个边缘节点限制数据
def read_bianyuan_limit_data(path):
    data3= np.loadtxt(path, dtype=str, delimiter=',', skiprows=1)
    m_limit = {}
    for item in data3:
        m_limit.update({"%s"%(item[0]):int(item[1])})
        # locals()['bianyuan_limit_'+item[0]+'_data'] = m_limit.get("%s"%item[0])
    # print(locals()['bianyuan_limit_'+name+'_data'])
    return  m_limit

# read_bianyuan_limit_data("E:\\桌面\\华为云作业\\线下调试数据\\data\\site_bandwidth.csv", "B")

if __name__ == "__main__":
    # time_chip = 1
    wj = 0 ##用来存方案整体成本
    for item7 in m:
        locals()["bianyuan_list_" + str(item7)] = []  ##创建以边缘节点命名的列表m个
    list_all = {}  ###用于存单个边缘节点所有时刻的带宽值
    for time_chip in range(1, t+1):
        for i in range(1, len(n) + 1):  ##表示的是客户节点的循环,应该放在程序的开始
            m_demand = read_demand_data(path1, time_chip)  # 读取需求,返回在一个时间度下每个客户节点需求
            m_qos = read_qos_data(path3, i)  # 读取qos,返回一个时间度下,每个客户节点与每个边缘节点的qos
            m_limit = read_bianyuan_limit_data(path2)  ##读取限制性数据,
            # 返回每个边缘节点限制

            m_qos_suit = {k:v for k, v in m_qos.items() if v<=qos_constraint}  ##判断符合条件的qos
            m_qos_nosuit = {k:v for k, v in m_qos.items() if v>qos_constraint}
            m_qos_suit_num = len(m_qos_suit)
            # print("合适结点数目:", m_qos_suit_num)
            m_qos_nosuit_num = len(m_qos_nosuit)
            # print(m_qos_suit)
            # print(m_qos_nosuit)
            Dj = m_demand.get("%s"%n[i-1])  ##获取每个节点的需求数据
            slip = int(Dj)//m_qos_suit_num + 1   ##平均分配数量
            # print(slip)

            m_qos_suit_keys = m_qos_suit.keys()  ##获取适合的键(边缘节点名字)
            m_qos_nosuit_keys = m_qos_nosuit.keys()  ##获取不适合的键
            # print(m_qos_nosuit_keys)

            # print(m_qos_suit_keys)
            # print(locals()["custom_suit_" + str(n[i-1])].name_list)
            if i == 1:  ##只在第一轮客户结点时,创建所有边缘结点对象
                # flag1 = 1  ##设置flag,用于批量命名合适结点和不合适结点的对象
                for name in m_qos_suit_keys:
                    # for i in range(1,101):
                    # if flag1<(m_qos_suit_num+1):
                    locals()["bianyuan_suit_"+name] = bianyuan(name, 0, m_limit.get(name))  ##批量命名满足要求结点对象
                    locals()["bianyuan_suit_"+name].distribute(slip)  ##更新其内部数据
                    # flag1 += 1
                    # print(locals()['bianyuan_suit_A'].data)
                # flag2 = 1
                for name1 in m_qos_nosuit_keys:
                    # for i in range(1,101):
                    # if flag2<(m_qos_nosuit_num+1):
                    locals()["bianyuan_suit_"+name1] = bianyuan(name1, 0, m_limit.get(name1))  ##批量命名不满足要求结点对象
                    locals()["bianyuan_suit_"+name1].distribute(0)
                    # flag2 += 1
            else:
                for item5 in m_qos_suit_keys:
                    locals()["bianyuan_suit_"+item5].distribute(slip)
                for item6 in m_qos_nosuit_keys:
                    locals()["bianyuan_suit_"+item6].distribute(0)
            ##打印结果
            str_content1 = "%s:" % n[i-1]
            str_content = "%s:" % n[i-1]
            # print(bianyuan_name)
            for item2 in m_qos_suit_keys:
                str_content1 += "<%s,%d>," % (item2, slip)
            with open(path4, 'a') as file_handle:
                if m_demand.get(n[i-1]) and i == 1 and time_chip == 1:
                        file_handle.write(str_content1)  # 写入
                elif m_demand.get(n[i-1]):
                        file_handle.write('\n')  # 有时放在循环里面需要自动转行,不然会覆盖上一条数据
                        file_handle.write(str_content1)  # 写入
                elif m_demand.get(n[i-1]) != true and i == 1 and time_chip == 1:
                        file_handle.write(str_content)  # 写入
                elif m_demand.get(n[i-1]) != true:
                        file_handle.write('\n')  # 有时放在循环里面需要自动转行,不然会覆盖上一条数据
                        file_handle.write(str_content)  # 写入
                # print(str_content)
        ##存一个时刻下边缘节点的的流量
        for item7 in m:  ###将一个时刻的所有边缘节点带宽存到字典里边
            list_all.update({item7:(locals()["bianyuan_suit_"+item7].data)})
            locals()["bianyuan_list_" + item7].append(list_all.get(item7))
        # print(list_all)
        # time_chip += 1
    ##计算成本
    for item8 in m:
        locals()["bianyuan_list_" + str(item8)].sort()
        ###获得每个边缘节点所有时刻的95带宽值
        print(locals()["bianyuan_list_" + str(item8)])
        wjj = locals()["bianyuan_list_" + str(item8)][int(np.ceil((len(locals()["bianyuan_list_" + str(item8)]) * 0.95))) - 1]
        print(wjj)
        wj += locals()["bianyuan_list_" + str(item8)][int(np.ceil((len(locals()["bianyuan_list_" + str(item8)]) * 0.95))) - 1]
        # wj = locals()["bianyuan_suit_"+str(item4)].return_wj()
    print("总成本为:", wj)
    # print(u'当前进程的内存使用:%.4f GB' % (psutil.Process(os.getpid()).memory_info().rss / 1024 / 1024 / 1024))

    end = time.clock()
    print('Running time: %s Seconds' % (end - start))

# main(path1, path2, path3, path4)

你可能感兴趣的:(python学习,算法数据结构,华为软挑,华为比赛,华为,python)