automatically putting output into networkx and node attributes

39 views
Skip to first unread message

ellen mariman

unread,
Apr 1, 2024, 11:00:54 AMApr 1
to networkx-discuss
Hi,


I am starting to work on networkx for the first time. Right now, I have the code in the screenshot below. I want to immediately reform my data output from another optimization model that I programmed in Pulp that comes like this y_(2,_3,_1,_2) = 0.0
y_(2,_3,_1,_3) = 0.0, for example, into immediately making it so that I can use it for networkx. Right now, I constantly need to manually reform this to a data list (see the second photo). How can I automatically do this?

Another question I have is, I want to add other things from my output to the network, such as the arrival time at a node ('m_(2,_1,_2) = 1140.0') to the network. But I can not seem to get it right so that it shows the network with the arrival times with the current code I have.This is the input arrival_times = {
    (0, 1, 0): 0.0,
    (0, 1, 1): 0.0,
    (0, 1, 2): 0.0,
    (0, 1, 3): 0.0,
    (1, 1, 0): 0.0,
    (1, 1, 1): 15.333333,
    (1, 1, 2): 15.333333,
    (1, 1, 3): 15.333333,
    (2, 1, 0): 0.0,
    (2, 1, 1): 1260.0,
    (2, 1, 2): 1260.0,
    (2, 1, 3): 1260.0,
    (3, 1, 0): 0.0,
    (3, 1, 1): 30.666667,
    (3, 1, 2): 30.666667,
    (3, 1, 3): 30.666667,
}
-> What is the best way to do this?


Any tips are welcome!

Ellen
Schermafbeelding 2024-04-01 om 16.24.01.png
Schermafbeelding 2024-04-01 om 16.22.33.png

Dan Schult

unread,
Apr 1, 2024, 11:19:58 AMApr 1
to networkx...@googlegroups.com
Two useful functions are: `add_edges_from` and `set_edge_attributes`

In [8]: G=nx.MultiGraph()


In [9]: G.add_edges_from(arrival_times.keys())

Out[9]: [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]


In [10]: nx.set_edge_attributes(G, arrival_times, "time")


In [11]: G.edges(data=True)

Out[11]: MultiEdgeDataView([(0, 1, {'time': 0.0}), (0, 1, {'time': 0.0}), (0, 1, {'time': 0.0}), (0, 1, {'time': 0.0}), (1, 1, {'time': 0.0}), (1, 1, {'time': 15.333333}), (1, 1, {'time': 15.333333}), (1, 1, {'time': 15.333333}), (1, 2, {'time': 0.0}), (1, 2, {'time': 1260.0}), (1, 2, {'time': 1260.0}), (1, 2, {'time': 1260.0}), (1, 3, {'time': 0.0}), (1, 3, {'time': 30.666667}), (1, 3, {'time': 30.666667}), (1, 3, {'time': 30.666667})])


You should be able to build what you need from what you've got. 
(Your screenshots aren't nearly as helpful as copy/past of text because then the reader can try out the code/data without retyping it.)
(and you have 4-tuples in your data, but you don't explain what the 4 elements are.)
(finally -- more useful functions appear in the docs' tutorial which could be helpful if you haven't gone through it before.) 

ellen mariman

unread,
Apr 1, 2024, 3:26:34 PMApr 1
to networkx-discuss
Hi, I rewrote my question so that it is more clear: 
This is the code I have now. I want to get a label outside of the node with the arrival times (see the data in the code) for example node 0 in period 0: 1200 and for node 1 period 0: 720. What am I doing wrong here? I want to work with a for loop because then I can easily just change the input data and arrival times. These are outputs of linear program I am writing. I added these lines to get this: G.nodes[i]['arrival_time'] = arrival_times[(i, j, v)]
                    G.nodes[j]['arrival_time'] = arrival_times[(i, j, v)]
Thank you in advance


This is the code:
import networkx as nx
import matplotlib.pyplot as plt


I = 4
#(this stands for (i,j,v,t): value comes from y_ijvt)
data = [
    ((0, 1, 1, 0), 0.0),
    ((0, 1, 1, 1), 0.0),
    ((0, 1, 1, 2), 0.0),
    ((0, 1, 1, 3), 0.0),
    ((0, 2, 1, 0), 0.0),
    ((0, 2, 1, 1), 0.0),
    ((0, 2, 1, 2), 0.0),
    ((0, 2, 1, 3), 0.0),
    ((0, 3, 1, 0), 0.0),
    ((0, 3, 1, 1), 1.0),
    ((0, 3, 1, 2), 1.0),
    ((0, 3, 1, 3), 1.0),
    ((1, 0, 1, 0), 0.0),
    ((1, 0, 1, 1), 0.0),
    ((1, 0, 1, 2), 0.0),
    ((1, 0, 1, 3), 0.0),
    ((1, 2, 1, 0), 0.0),
    ((1, 2, 1, 1), 1.0),
    ((1, 2, 1, 2), 1.0),
    ((1, 2, 1, 3), 1.0),
    ((1, 3, 1, 0), 0.0),
    ((1, 3, 1, 1), 0.0),
    ((1, 3, 1, 2), 0.0),
    ((1, 3, 1, 3), 0.0),
    ((2, 0, 1, 0), 0.0),
    ((2, 0, 1, 1), 1.0),
    ((2, 0, 1, 2), 1.0),
    ((2, 0, 1, 3), 1.0),
    ((2, 1, 1, 0), 0.0),
    ((2, 1, 1, 1), 0.0),
    ((2, 1, 1, 2), 0.0),
    ((2, 1, 1, 3), 0.0),
    ((2, 3, 1, 0), 0.0),
    ((2, 3, 1, 1), 0.0),
    ((2, 3, 1, 2), 0.0),
    ((2, 3, 1, 3), 0.0),
    ((3, 0, 1, 0), 0.0),
    ((3, 0, 1, 1), 0.0),
    ((3, 0, 1, 2), 0.0),
    ((3, 0, 1, 3), 0.0),
    ((3, 1, 1, 0), 0.0),
    ((3, 1, 1, 1), 1.0),
    ((3, 1, 1, 2), 1.0),
    ((3, 1, 1, 3), 1.0),
    ((3, 2, 1, 0), 0.0),
    ((3, 2, 1, 1), 0.0),
    ((3, 2, 1, 2), 0.0),
    ((3, 2, 1, 3), 0.0)
]

arrival_times = {
    (0, 1, 0): 0.0,
    (0, 1, 1): 1200.0,
    (0, 1, 2): 1200.0,
    (0, 1, 3): 1200.0,
    (1, 1, 0): 0.0,
    (1, 1, 1): 720.0,
    (1, 1, 2): 720.0,
    (1, 1, 3): 720.0,
    (2, 1, 0): 0.0,
    (2, 1, 1): 1140.0,
    (2, 1, 2): 1140.0,
    (2, 1, 3): 1140.0,
    (3, 1, 0): 0.0,
    (3, 1, 1): 704.6666666666666,
    (3, 1, 2): 704.6666666666666,
    (3, 1, 3): 704.6666666666666
}






# Iterate through periods
for t in range(4):  # Assuming there are 4 periods
    # Create a directed graph for the current period
    G = nx.DiGraph()

    # Iterate through data and add edges where y[(i, j, v, t)] == 1 for the current period
    for (i, j, v, curr_t), val in data:
        if v == 1 and curr_t == t:
            try:
                if val == 1:  # Check if value is
                   # G.add_node(i,weight = arrival_times[(i)])
                    G.add_edge(i, j)
                    G.nodes[i]['arrival_time'] = arrival_times[(i, j, v)]
                    G.nodes[j]['arrival_time'] = arrival_times[(i, j, v)]

            except KeyError:
                pass  # Handle case where key does not exist in y



    plt.figure()
    plt.title(f"Period {t}")  # Set the title before drawing the graph

    nx.draw(G, with_labels=True, node_size=1000, node_color="skyblue", font_size=10)
    plt.show()
Op maandag 1 april 2024 om 17:19:58 UTC+2 schreef Dan Schult:
Reply all
Reply to author
Forward
0 new messages