Exemplo n.º 1
0
def upstream_edge_info(wg, n):
    upstream_edges = list(nx.edge_dfs(wg, n, orientation='reverse'))
    # print(upstream_edges)
    up_switch_dict = {}
    e_file = gv.filepaths["edges"]
    for u in upstream_edges:
        # edge info:
        p = u[0].lstrip()
        q = u[1].lstrip()
        edge_of_path_name_1 = p + "_to_" + event_intensity(n)
        edge_of_path_name_2 = q + "_to_" + p
        edge_search_result_1 = db._edge_search(edge_of_path_name_1)
        edge_search_result_2 = db._edge_search(edge_of_path_name_2)
        try:
            with open(e_file, 'r') as f:
                csvr = csv.reader(f)
                csvr = list(csvr)
                for row in csvr:
                    if row[0].lstrip() == edge_of_path_name_1 or row[0].lstrip(
                    ) == edge_of_path_name_2:
                        if edge_search_result_1 != 0 or edge_search_result_2 != 0:
                            if row[1].lstrip() == "switch":
                                # print("[i] Upstream Switch: {}, Status: {}, Availability: {}".format(row[0], row[4],
                                # row[13]))
                                up_switch_dict[row[0]] = row[4]
                            if row[1].lstrip() == "transformer":
                                pass
                                # print(
                                # " [i] Upstream Transformer: {}, Status: {}, Availability: {}".format(row[0], row[4],
                                # row[13]))

        except:
            print("[x] Error in up-stream edge search.")
    return up_switch_dict
Exemplo n.º 2
0
def downstream_edge_info(wg, n):
    downstream_edges = list(nx.dfs_edges(wg, n))
    down_switch_dict = {}
    e_file = gv.filepaths["edges"]
    for d in downstream_edges:
        # edge info:
        p = d[0].lstrip()
        q = d[1].lstrip()
        edge_of_path_name_1 = p + "_to_" + q
        edge_of_path_name_2 = q + "_to_" + p
        edge_search_result_1 = db._edge_search(edge_of_path_name_1)
        edge_search_result_2 = db._edge_search(edge_of_path_name_2)
        # try:
        with open(e_file, 'r+') as f:
            csvr = csv.reader(f)
            csvr = list(csvr)
            for row in csvr:
                if row[0].lstrip() == edge_of_path_name_1 or row[0].lstrip(
                ) == edge_of_path_name_2:
                    if edge_search_result_1 != 0 or edge_search_result_2 != 0:
                        if row[1].lstrip() == "switch":
                            # print("[i] Downstream Switch: {}, Status: {}, Availability: {}".format(row[0], row[4],
                            # row[13]))
                            down_switch_dict[row[0]] = row[4]
                        if row[1].lstrip() == "transformer":
                            pass
                            # print("[i] Downstream Transformer: {}, Status: {}, Availability: {}".format(row[0],
                            # row[4],
                            # row[13]))

        # except:
        #     print("[x] Error in downstream edge search.")

    return down_switch_dict
Exemplo n.º 3
0
def load_and_demand_query(path, verbose=True):
    loads = 0
    gens = 0
    demand_kw = 0.0
    gen_kw = 0.0
    e_file = gv.filepaths["edges"]
    if type(path) is str:
        n = node_object_from_node_name(path)
    else:
        for p in path:
            n = node_object_from_node_name(p)
            i = path.index(p)
            if i < len(path) - 1:
                q = path[i + 1]
                edge_of_path_name_1 = p + "_to_" + q
                edge_of_path_name_2 = q + "_to_" + p
                edge_search_result_1 = db._edge_search(edge_of_path_name_1)
                edge_search_result_2 = db._edge_search(edge_of_path_name_2)
                try:
                    with open(e_file, 'r+') as f:
                        csvr = csv.reader(f)
                        csvr = list(csvr)
                        for row in csvr:
                            if row[0].lstrip() == edge_of_path_name_1 or row[
                                    0].lstrip() == edge_of_path_name_2:
                                if edge_search_result_1 != 0 or edge_search_result_2 != 0:
                                    edge_status_list[row[0]] = row[4]
                                    if row[1].lstrip() == "switch":
                                        # print("[i] Switch {} found!".format(row[0]))
                                        edge_switches[row[0]] = row[4]
                except:
                    print(
                        "[x] Edge could not be queried for status for the nodes."
                    )

            if int(n.load) > 0:
                loads += 1
                demand_kw += float(n.load)
            if n.gen.lstrip() != "inf":
                gen_kw += float(n.gen)

    if verbose:
        print("[i] Number of loads = {}, Demand = {} kW, Generation = {} kW".
              format(loads, demand_kw, gen_kw))
        print("[i] Edge Statuses of the path:")
        pp.pprint(edge_status_list)

        if verbose:
            print("[i] Switch Status:")
            pp.pprint(edge_switches)

    load_and_demand_query_dict = {}
    load_and_demand_query_dict["edge_status"] = edge_status_list
    load_and_demand_query_dict["switch_status"] = edge_switches

    return load_and_demand_query_dict
Exemplo n.º 4
0
def impact_on_edge(e):
    """
    Risk = (foliage_risk*elemental_risk*event_intensity*max(risk_of_from_node, risk_of_to_node))/hardening
    :param e: Key of edge dictionary for global edge risk dictionary
    :return:
    """
    edge_name = e  # e[1].lstrip() + "_to_" + e[0].lstrip()
    edge = edge_object_from_edge_name(edge_name)
    elemental_risk = (float(edge.wind_risk) + float(edge.fire_risk) +
                      float(edge.water_risk)) / 30.0
    foliage_risk = 1.0  # <-- todo: need to implement the foliage risk factor
    # where's the closest threat node?
    e_file = gv.filepaths["edges"]
    edge_search_result = db._edge_search(edge_name)
    hardening = 1
    try:
        with open(e_file, 'r+') as f:
            csvr = csv.reader(f)
            csvr = list(csvr)
            for row in csvr:
                if row[0].lstrip() == edge_name and edge_search_result != 0:
                    from_node_of_e = row[2].lstrip()
                    to_node_of_e = row[3].lstrip()
                    hardening = float(row[12])
    except:
        print(
            "[x] Cannot find the requested edge.\n[i] Calculating impact on substation PCC instead."
        )
        from_node_of_e = "S1"
        to_node_of_e = "F1_1"
        return

    ev_intensity_from = float(event_intensity(from_node_of_e))
    ev_intensity_to = float(event_intensity(to_node_of_e))
    ev_intensity = 0.5 * (ev_intensity_from + ev_intensity_to)
    # primary_threat_anchor = primary_threat_anchor_of_node(from_node_of_e)
    risk_of_from_node = impact_on_node(from_node_of_e)
    risk_of_to_node = impact_on_node(to_node_of_e)
    x = (foliage_risk * ev_intensity *
         max(risk_of_from_node, risk_of_to_node)) / hardening
    return x
Exemplo n.º 5
0
def reconfigure(node_to_restore,
                damaged_graph,
                criteria="least_impact",
                filename=None):
    """
    Algorithm: Kruskal Minimum Spanning Tree
    Damaged graph.
    Path search from node_to_restore, in complete graph to generators
    :return:
    """

    generator_nodes = []
    complete_graph = gv.graph_collection[3]
    undir_complete_graph = nx.Graph()  # complete_graph.to_undirected()
    if filename is None:
        e_file = gv.filepaths["edges"]
    else:
        e_file = filename
    # Calculate the impact on edge and attribute it to undir_complete_graph:
    complete_graph_edgelist = complete_graph.edges()
    temp_edge_impact_dict = {}
    with open("edge_risk_profile_during_reconfiguration.csv", "w+",
              newline='') as risk_profile_data:
        for m in complete_graph_edgelist:
            edge_of_path_name_1 = m[0] + "_to_" + m[1]
            edge_of_path_name_2 = m[1] + "_to_" + m[0]
            edge_search_result_1 = db._edge_search(edge_of_path_name_1)
            edge_search_result_2 = db._edge_search(edge_of_path_name_2)
            with open(e_file, 'r+') as f:
                csvr = csv.reader(f)
                csvr = list(csvr)

                for row in csvr:
                    if row[0].lstrip() == edge_of_path_name_1 or row[0].lstrip(
                    ) == edge_of_path_name_2:
                        if edge_search_result_1 != 0:
                            impact = impact_on_edge(edge_of_path_name_1)
                            temp_edge_impact_dict[edge_of_path_name_1] = impact
                            undir_complete_graph.add_edge(m[0],
                                                          m[1],
                                                          weight=impact)
                            write_string = m[0] + "_to_" + m[1] + "," + str(
                                impact) + "\n"
                            risk_profile_data.write(write_string)
                        elif edge_search_result_2 != 0:
                            impact = impact_on_edge(edge_of_path_name_2)
                            temp_edge_impact_dict[edge_of_path_name_1] = impact
                            undir_complete_graph.add_edge(m[1],
                                                          m[0],
                                                          weight=impact)
                            write_string = m[1] + "_to_" + m[0] + "," + str(
                                impact) + "\n"
                            risk_profile_data.write(write_string)

    cn.add_node_attr(undir_complete_graph)
    for k, v in undir_complete_graph.nodes(data=True):
        if float(v['gen']) > 0 or v['gen'].lstrip() == "inf":
            generator_nodes.append(k)
    print("Available generator nodes are {}".format(generator_nodes))
    logging.info("Reconfiguration function called")
    logging.info("Available generator nodes are {}".format(generator_nodes))

    minimum_spanning_edges = nx.minimum_spanning_edges(undir_complete_graph,
                                                       data=False)
    new_graph: DiGraph = nx.DiGraph()

    for m in minimum_spanning_edges:
        edge_of_path_name_1 = m[0] + "_to_" + m[1]
        edge_of_path_name_2 = m[1] + "_to_" + m[0]
        edge_search_result_1 = db._edge_search(edge_of_path_name_1)
        edge_search_result_2 = db._edge_search(edge_of_path_name_2)
        with open(e_file, 'r+') as f:
            csvr = csv.reader(f)
            csvr = list(csvr)

            for row in csvr:
                if row[0].lstrip() == edge_of_path_name_1 or row[0].lstrip(
                ) == edge_of_path_name_2:
                    try:
                        impact = temp_edge_impact_dict[edge_of_path_name_1]
                    except:
                        impact = temp_edge_impact_dict[edge_of_path_name_2]

                    if edge_search_result_1 != 0:
                        new_graph.add_edge(m[0], m[1], weight=float(impact))

                    elif edge_search_result_2 != 0:
                        new_graph.add_edge(m[1], m[0], weight=float(impact))

    print("Edges in Restored Path:")
    print(new_graph.edges())

    g_new_edges = set(new_graph.edges())

    # Edge status comparison:
    g_old = gv.graph_collection[0]
    g_old_edges = set(g_old.edges())

    turn_on = g_new_edges.difference(g_old_edges)
    controllable_switches = {}
    for t in turn_on:
        edge_of_path_name_1 = t[0] + "_to_" + t[1]
        edge_of_path_name_2 = t[1] + "_to_" + t[0]
        edge_search_result_1 = db._edge_search(edge_of_path_name_1)
        edge_search_result_2 = db._edge_search(edge_of_path_name_2)
        try:
            with open(e_file, 'r+') as f:
                csvr = csv.reader(f)
                csvr = list(csvr)
                for row in csvr:
                    if row[0].lstrip() == edge_of_path_name_1 or row[0].lstrip(
                    ) == edge_of_path_name_2:
                        if edge_search_result_1 != 0 or edge_search_result_2 != 0:
                            edge_status_list[row[0]] = row[4]
                            if row[1].lstrip() == "switch":
                                # print("[i] Switch {} found!".format(row[0]))
                                controllable_switches[row[0]] = row[4]
        except:
            print("[x] Edge could not be queried for status for the nodes.")

    cn.add_node_attr(new_graph)
    cn.lat_long_layout(new_graph,
                       show=True,
                       save=False,
                       title="Minimum Spanning Tree Restoration")

    try:
        cyclic_path = list(
            nx.find_cycle(new_graph, node_to_restore, orientation='ignore'))
        print(cyclic_path)
    except nx.NetworkXNoCycle:
        print("[i] All loads could be picked without loop elimination.")

    return controllable_switches