def weight_test(inp):
    wqs = WaterQualitySim(inp)
    node_dirt = wqs.parallel_compute_time_dirt(wqs.nodeList,
                                               is_json=False)  # 水质模拟
    new_dirt = WaterQualitySimData(node_dirt=node_dirt).change_number(
        is_out=False)  # 数据处理

    # 权重数据
    fn = FilterNode(inp)
    data1 = fn.compute_water_data()
    data2 = fn.data_normalization(data1)
    print("管网节点数:", len(wqs.nodeList))
    print("污染数据的键数:", len(new_dirt.keys()))
    print(len(data2["diff_demand"]))
    pipe_list = list(data2["degree_list"])
    #print(len(pipe_list))
    for i, j in enumerate(new_dirt.values()):
        j.append(pipe_list[i])
    print(new_dirt)
    sp = WeightSensorPlacement(node_dirt=new_dirt,
                               individuals_num=8,
                               iterations_num=500)  # 算法迭代
    node_result = sp.iteration()  # 结果输出
    sp.draw_node(node_result)  # 帕累托解集显示
    result_list = index_to_node(node_result, wqs.nodeList)  # 将已转化为索引的节点再还原回来
    for i in result_list:
        print(i)
def no_file2(inp_path):
    """
    不生成结果文件,数据全部由上个函数返回值组成
    """

    # 管网加载模块,改善自WNTR库,添加水质模拟模块
    wqs = WaterQualitySim(inp_path)

    # 水质模拟 is_json=False表示不产生json文件
    """
    产生dirt结果:
    {注入污染节点1:{影响节点1:初次污染时间,影响节点2:初次污染时间,...},
     注入污染节点2:{影响节点1:初次污染时间,影响节点2:初次污染时间,...},
     ...}
    """
    ky2_paixu = "F://AWorkSpace//2020data//节点重要性排序数据//node_centrality_ky2.json"
    # node_list = read_paixu(ky2_paixu, cen, large)
    #node_dirt = wqs.parallel_compute_time_dirt(node_list, is_json=False)
    node_dirt = wqs.parallel_compute_time_dirt(wqs.nodeList, is_json=False)

    # 数据处理
    """
    将水质模拟的结果改为如下形式,并且节点映射为0 1 2 3...方便算法计算
    {节点1:[[污染事件1,污染事件2,...], 平均污染时间],
     节点2:[[污染事件1,污染事件2,...], 平均污染时间],
     ...}
    """
    dirt_data = WaterQualitySimData(node_dirt=node_dirt).change_number(
        is_out=False)

    # 算法计算
    sp = SensorPlacement(node_dirt=dirt_data,
                         individuals_num=100,
                         iterations_num=500,
                         cross_probability=0.8,
                         mutation_probability=0.4)

    # 迭代主程序 产生结果
    """
    node_result样式
    [[节点1,节点2..]
     [节点1,节点2..]
     ...]
    """
    node_result = sp.iteration()  # 迭代主程序,输出结果
    sp.draw_node(node_result)  # 绘图
    result_list = index_to_node(node_result, wqs.nodeList)  # 还原原来的节点名称
    for i in result_list:
        print(i)
    return node_result
Exemplo n.º 3
0
def NSGA2():
    """
    算法迭代
    """
    wqs = WaterQualitySim("F:/AWorkSpace/Python-Learning-Data/Net3.inp")

    final_json = "final_json.json"
    sp = SensorPlacement(json_path=final_json,
                         individuals_num=10,
                         iterations_num=20)
    node_result = sp.iteration()  # 迭代主程序,输出结果
    sp.draw_node(node_result)  # 绘图
    result_list = index_to_node(node_result, wqs.nodeList)  # 还原原来的节点名称
    for i in result_list:
        print(i)
def ky8_json_test():
    """
    ky8的普通读json文件的算法迭代结果
    :return:
    """
    ky8_inp = "F:/AWorkSpace/Python-Learning-Data/ky8.inp"
    wqs = WaterQualitySim(ky8_inp)
    final_json = "D:/Git/SensorPlacementInDistributionNetworks/simulation/final_json.json"
    sp = SensorPlacement(json_path=final_json,
                         individuals_num=100,
                         iterations_num=500)  # 算法迭代
    node_result = sp.iteration()  # 结果输出
    sp.draw_node(node_result)  # 帕累托解集显示
    result_list = index_to_node(node_result, wqs.nodeList)  # 将已转化为索引的节点再还原回来
    for i in result_list:
        print(i)
def dirt_test():
    ky8_inp = "F:/AWorkSpace/Python-Learning-Data/ky2.inp"
    wqs = WaterQualitySim(ky8_inp)  # 加载管网
    node_dirt = wqs.parallel_compute_time_dirt(wqs.nodeList,
                                               is_json=False)  # 水质模拟

    new_dirt = WaterQualitySimData(node_dirt=node_dirt).change_number(
        is_out=False)  # 数据处理

    sp = SensorPlacement(node_dirt=new_dirt,
                         individuals_num=8,
                         iterations_num=500)  # 算法迭代
    node_result = sp.iteration()  # 结果输出
    sp.draw_node(node_result)  # 帕累托解集显示
    result_list = index_to_node(node_result, wqs.nodeList)  # 将已转化为索引的节点再还原回来
    for i in result_list:
        print(i)
def no_file(inp_path):
    """
    不生成结果文件,数据全部由上个函数返回值组成
    """

    # 管网加载模块,改善自WNTR库,添加水质模拟模块
    wqs = WaterQualitySim(inp_path)

    # 水质模拟 is_json=False表示不产生json文件
    """
    产生dirt结果:
    {注入污染节点1:{影响节点1:初次污染时间,影响节点2:初次污染时间,...},
     注入污染节点2:{影响节点1:初次污染时间,影响节点2:初次污染时间,...},
     ...}
    """
    node_dirt = wqs.parallel_compute_time_dirt(wqs.nodeList, is_json=False)

    # 数据处理
    """
    将水质模拟的结果改为如下形式,并且节点映射为0 1 2 3...方便算法计算
    {节点1:[[污染事件1,污染事件2,...], 平均污染时间],
     节点2:[[污染事件1,污染事件2,...], 平均污染时间],
     ...}
    """
    dirt_data = WaterQualitySimData(node_dirt=node_dirt).change_number(
        is_out=False)

    # 算法计算
    sp = SensorPlacement(node_dirt=dirt_data,
                         individuals_num=10,
                         iterations_num=500)

    # 迭代主程序 产生结果
    """
    node_result样式
    [[节点1,节点2..]
     [节点1,节点2..]
     ...]
    """
    node_result = sp.iteration()  # 迭代主程序,输出结果
    sp.draw_node(node_result)  # 绘图
    result_list = index_to_node(node_result, wqs.nodeList)  # 还原原来的节点名称
    for i in result_list:
        print(i)
    return node_result
def ky8_weight_json_test():
    """
    ky8添加权重的结果
    :return:
    """
    ky8_inp = "F:/AWorkSpace/Python-Learning-Data/ky8.inp"
    wqs = WaterQualitySim(ky8_inp)
    #quality_path = "D:/Git/SensorPlacementInDistributionNetworks/simulation/"
    #wqs = WaterQualitySim(ky8_inp)  # 加载管网
    #wqs.parallel_compute_time_dirt(wqs.nodeList, rpt_file=quality_path)  # 水质模拟

    #json_path = "D:/Git/SensorPlacementInDistributionNetworks/simulation/waterQuality.json"
    #new_dirt = WaterQualitySimData(json_path=json_path).change_number(is_out=True)

    final_json = "D:/Git/SensorPlacementInDistributionNetworks/simulation/final_json.json"
    with open(final_json, "r") as f:
        json_data = json.load(f)
    dirt_data = json.loads(json_data)

    # 进行指标收集
    fn = FilterNode(ky8_inp)
    data1 = fn.compute_water_data()
    data2 = fn.data_normalization(data1)
    pipe_list = list(data2["volume_list"])
    print(len(dirt_data.keys()))
    print(len(pipe_list))
    for i, j in enumerate(dirt_data.values()):  # 指标结果代入dirt
        j.append(pipe_list[i])

    sp = WeightSensorPlacement(node_dirt=dirt_data,
                               individuals_num=100,
                               iterations_num=500)
    node_result = sp.iteration()  # 结果输出
    sp.draw_node(node_result)  # 帕累托解集显示
    result_list = index_to_node(node_result, wqs.nodeList)  # 将已转化为索引的节点再还原回来
    for i in result_list:
        print(i)