Generate parameters in Abstract Model in Pyomo

76 views
Skip to first unread message

MUHAMMAD WAQAS SWATI

unread,
Sep 5, 2022, 6:00:28 AM9/5/22
to Pyomo Forum

I am using Networkx to define a graph G and later use it in my abstract model of pyomo.

The graph contains few nodes. I want to create pyomo abstract constraints equal to the number of nodes in graph. The graph G is:

def mygraph():
    G = nx.Graph() #Some nodes are defined here

Later, I am using my graph in my Pyomo Abstract Model as:

def mymodel(): #Abstract Model Creation
     model = AbstractModel()
     g=mygraph() #Graph passed to pyomo function

 

I want to create constraints for each node of the graph and pprint() it to verify. Like, suppose my graph has 6 nodes then there must be 6 constraints. I am trying to do so as:

    model.nodes_range = RangeSet(1, len(g)) # this creates a parameter with same size of the graph

    model.C = Param(model.nodes_range, within=NonNegativeIntegers) # one parameter for each node

    model.D = Param(model.nodes_range, within=NonNegativeIntegers)

    def const1(model):# Dummy constraint
           return sum(model.C[u] for u in model.nodes_range)<=model.D[u]

    model.constraint1=Constraint(model.nodes_range, rule=const1)

    model.constraint1.pprint()

mymodel()

However, I am not getting constraints with this for each node. Can you guide me with this?
Kind Regards

David Woodruff

unread,
Sep 5, 2022, 3:19:23 PM9/5/22
to pyomo...@googlegroups.com
These days, most people are creating a ConcreteModel so you might be happier doing that.

But either way, I am surprised you don't get an error message because

  model.constraint1=Constraint(model.nodes_range, rule=const1)

implies that const1 will take two arguments: the model and a node.

Dave

--
You received this message because you are subscribed to the Google Groups "Pyomo Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo-forum...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyomo-forum/2a3ae572-1244-41aa-863d-c479db03f120n%40googlegroups.com.

MUHAMMAD WAQAS SWATI

unread,
Sep 5, 2022, 3:34:27 PM9/5/22
to Pyomo Forum

Dave, thank you. You've always been extremely helpful. Instead of doing things hard-coded in Concrete Model, I'm using Abstract Model so that I can read different graphs and optimize them.
Could you please explain my coding error? I didn't understand what you were saying.

David Woodruff

unread,
Sep 5, 2022, 7:06:06 PM9/5/22
to pyomo...@googlegroups.com
If you, or your library, can obtain a copy of the third edition of the Pyomo book, it will show you how to do what you want to do using a ConcreteModel:

ISBN: 3030689301

ISBN13: 9783030689308

To quickly answer your question:

Constraint(model.nodes_range, rule=const1)

tells Pyomo to call the function const1 once for each member of model.nodes_range and pass that in as an argument (for the first function argument is always the model). This is explained much better in the book.

  Dave


Reply all
Reply to author
Forward
0 new messages