0

I have a simple graph with few nodes and these nodes have attributes such as "type" and "demand".

def mygraph():
    G = nx.Graph()
    G.add_nodes_from([("N1", {"type":"parent","demand": 10}),
    ("N2"{"type":"parent","demand": 12}),
    ("N3", {"type":"parent","demand": 25}), 
    ("S1", {"type":"server","demand": 12}), 
    ("S2,{"type":"server","demand": 20})])

I am passing this graph to another function in pyomo library. The dummy pyomo function is as follows:

def mymodel():
    g=mygraph()
    **VARIABLES**
    model.const1 = Constraint(my constraint1)
    model.const2 = Constraint(my constraint2)
    model.obj1 = Objective(my objective)
    status = SolverFactory('glpk')
    results = status.solve(model)
    assert_optimal_termination(results)
    model.display()
mymodel()

I am trying to:

  1. In graph function mygraph():, I need to find the total number of nodes in the graph G with attribute type==parent.

  2. In pyomo function mymodel():, I need to create the number new of VARIABLES equal to the number of nodes with attribute type==parent. So in the case above, my program must create 3 new variables, since 3 nodes have attribute type==parent in my graph function. The values of these newly created variables will be accessed from the demand attribute of the same node thus, it should be something like this;

new_var1=demand of node1 (i.e., node1_demand=10 in this case)
new_var2=demand of node2 (i.e., node2_demand=12)
new_var3=demand of node3 (i.e., node2_demand=25)
SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41

1 Answers1

0

For the first part you can loop over the nodes:

sum(1 for n,attr in G.nodes(data=True) if attr['type']=='parent')
# 3

# or to get all types
from collections import Counter
c = Counter(attr['type'] for n,attr in G.nodes(data=True))
# {'parent': 3, 'server': 2}
c['parent']
# 3
c['server']
# 2

For the second part (which also gives you the answer of the first part of you check the length):

{n: attr['demand'] for n,attr in G.nodes(data=True) if attr['type']=='parent'}

# or
[attr['demand'] for n,attr in G.nodes(data=True) if attr['type']=='parent']

Output:

{'N1': 10, 'N2': 12, 'N3': 25}

# or
[10, 12, 25]

instanciating attributes

def mymodel():
    g = mygraph()
    nodes = [attr['demand']
             for n,attr in G.nodes(data=True)
             if attr['type']=='parent']

    # initialize model?

    for i,n in enumerate(nodes, start=1):
        setattr(model, f'const{1}', Constraint(something with n))
    
    # ...
mozway
  • 194,879
  • 13
  • 39
  • 75
  • Dear Mozway! Thank you for your detailed response. I guess I didn't express my second part well, let me re-explain it. A for loop will run till the number of times we have parent nodes and it will create dynamic variables such as create new variable1=demandvalue of node1, create new variable2=demandvalue of node2 and so on. This creation of variables and assignment of values will go till the number of the parent nodes. – Waqas Swati Aug 26 '22 at 22:29
  • @WaqasSwati I'm sorry but it's unclear. Why do you need a loop? Why dynamic variables? Can't you iterate over the values of the second part directly? Please provide context and a description of what you want to achieve. [Generating variables dynamically](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) is [never a good idea](https://stackoverflow.com/questions/35458691/is-it-a-good-idea-to-dynamically-create-variables). – mozway Aug 27 '22 at 01:55
  • Thank you @Mozway, I cannot disagree with what you wrote earlier. Actually, I'm making a graph in networkx, and later this graph will be used by a function in the Pyomo library. So I'll be passing the networkx graph to a pyomo function. In the pyomo function, I need to create new variables that are equivalent to the nodes in my graph with the parent attribute, and their values will be equal to their demands attribute. – Waqas Swati Aug 27 '22 at 20:30
  • It might be worth providing a minimal example of how you generate one graph with a defined set of nodes, then we can maybe see how to generalize? – mozway Aug 27 '22 at 20:37
  • I appreciate your time @mozway. I am grateful; I attempted to provide more information in the query. I hope that makes sense. – Waqas Swati Aug 27 '22 at 21:38