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: