-3

I am using two python libraries for my task.

Networkx: I am creating a simple directed graph of 2 nodes & 1 link as follows:

G=nx.DiGraph()
G.add_node('N1', N1_demand=13)
D1_N1=nx.get_node_attributes(G, 'N1_demand')
G.add_node('N2', N2_demand=10)
D2_N2=nx.get_node_attributes(G, 'N2_demand')
G.add_edge("N1", "N2", b_demand=3)
VNF_edge_labels = nx.get_edge_attributes(G, "b_demand")

Pyomo

I want to pass the graph G created using networkX to another function which implements a Pyomo model for optimization. The reason for passing the graph G to a function of the Pyomo library is that Pyomo function will create the number of variables equivalent to the number of nodes in the networkX graph function.

Please help.

  • G can be simply passed to another function directly as an argument. refer the networkx documentation to get the nodes and edges – arjunsiva Aug 19 '22 at 09:57
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Aug 19 '22 at 13:21
  • @Community, I appreciate your time, I have explained my question and I hope it's more understandable now. – Waqas Swati Aug 19 '22 at 14:39

1 Answers1

1

As written by @arjunsiva, this can be accomplished by simply calling the graph function inside the pyomo function as follows:

def mymodel(): #Model Creation
model = ConcreteModel()
g=mygraph() #Graph passed to function
#Accessing the nodes's attributes in pyomo model
print("N1:",g.nodes["N1"]["N1_demand"])
print("N2:",g.nodes["N2"]["N2_demand"])