Home > front end >  How to declare objective function and constraints in Pyomo (Abstract model)
How to declare objective function and constraints in Pyomo (Abstract model)

Time:12-24

I am learning how to create Abstract models in Pyomo and I did the modelling, however, when I visualize the pprint, there is no declaration of objective function and constraints. How can I fix this? I attached the code

Thanks in advance.

import pyomo as pyo
from pyomo.environ import *

model = AbstractModel()

model.I = Set()
model.J = Set()

model.a = Param(model.I, model.J)
model.b = Param(model.I)
model.c = Param(model.J)

model.x = Var(model.J, domain = NonNegativeReals)

def obj (model):
  return sum(model.c[j]*model.x[j] for j in model.J)

  model.obj = Objective(rule = obj)

def ax_constraint_rule (model, i):
  return sum(model.a[i,j]*model.x[j] for j in model.J) >= model.b[i]

  model.AxbConstraint = Constraint(model.I, rule = ax_constraint_rule)

model.pprint()

3 Set Declarations
    I : Size=0, Index=None, Ordered=Insertion
        Not constructed
    J : Size=0, Index=None, Ordered=Insertion
        Not constructed
    a_index : Size=0, Index=None, Ordered=True
        Not constructed

3 Param Declarations
    a : Size=0, Index=a_index, Domain=Any, Default=None, Mutable=False
        Not constructed
    b : Size=0, Index=I, Domain=Any, Default=None, Mutable=False
        Not constructed
    c : Size=0, Index=J, Domain=Any, Default=None, Mutable=False
        Not constructed

1 Var Declarations
    x : Size=0, Index=J
        Not constructed

7 Declarations: I J a_index a b c x

CodePudding user response:

You appear to have mistakenly indented the statements below, which would make them part of the functions. They are not. They call the functions... by making them part of the function and after the return statement they are dead code.

That should at least get something to print. Then, you will want to make an instance of your model by loading data (see the documentation) and pprint the instance instead of the model. Good luck!

enter image description here

  • Related