How to generate a constraints via a loop

198 views
Skip to first unread message

ran tao

unread,
Mar 6, 2020, 12:31:19 AM3/6/20
to Pyomo Forum

hi, all

i want to create a set of constraints, as following:

Pg[0] <= Pmax[0]

Pg[1] <= Pmax[1]

Pg[3] <= Pmax[3]

Pg[4] <= Pmax[4]

Pg[5] <= Pmax[5]

and, my code is as following:

import pyomo.environ as pyo

model = pyo.ConcreteModel()
Ngen = 5
Pmax = [40, 170, 520, 200, 100]
model.Ngen = pyo.Set(dimen=Ngen)
model.Pg = pyo.Var(model.Ngen)

def Co1(model):
    return ((model.Pg[ii] for ii in model.Ngen) <= (Pmax[jj]  for jj in range(Ngen)))

model.Co1 = pyo.Constraint(rule=Co1)

but, the Python Console tells me:

" TypeError: '<=' not supported between instances of 'generator' and 'generator' "

HOW DO I RECORRECT this?

and the other quesiton, if the Pmax is not a list, but a numpy-ndarray. somethings will different?

thanks a lot!

Lucas Ford

unread,
Mar 6, 2020, 7:59:55 AM3/6/20
to Pyomo Forum
Hi Ran, 

I believe what you are looking for is something like the following:
def Co1(model,ii):
   
return model.Pg[ii] <= model.Pmax[ii]
model
.Co1 = pyo.Constraint(model.Ngen, rule=Co1)

Where you will define model.Pmax as a parameter with dimension Ngen and initialize it to Pmax. This will allow pyomo to create the constraint for all indices in model.Ngen. 

Hope this helps. 

Rob

unread,
Mar 6, 2020, 8:03:30 AM3/6/20
to Pyomo Forum
You're going to want to change your constraint to

def Co1(model, ii):
    return model.Pg[ii] <= Pmax[ii]
model.Co1 = pyo.Constraint(model.Ngen, rule=Co1)

Another way to do it, similar to your logic, would be:
model.Co1 = pyo.ConstraintList()
for ii in model.Ngen:
    model.Co1.add(model.Pg[ii] <= Pmax[ii])


On Friday, March 6, 2020 at 12:31:19 AM UTC-5, ran tao wrote:

ran tao

unread,
Mar 6, 2020, 9:18:25 AM3/6/20
to Pyomo Forum
Hi Lucas
thanks for your answer!
something about obj and constraints may be correct in your way when i run the obj or constraints code singlely.
but when i try to solve this model, another question i encounter.
the Python Console tells me:

" ValueError: ERROR: No objectives defined for input model. Cannot write legal LP file. "

and my whole code is as following:

import pyomo.environ as pyo

import pyomo.opt as SolverFactory


model = pyo.AbstractModel()


Ngen = 5

pg = [10, 12, 13, 12, 15]

rg = [0., 0., 0., 0., 0.]

sumPd = 1000

Pmax = [40, 170, 520, 200, 100]

Pmin = [5, 20, 50, 20, 10]

model.Ngen = pyo.Set(dimen=Ngen)

model.Pg = pyo.Var(model.Ngen,domain=pyo.NonNegativeReals)

model.Rg = pyo.Var(model.Ngen,domain=pyo.NonNegativeReals)

model.Pmax = pyo.Param(model.Ngen,initialize=Pmax)

model.Pmin = pyo.Param(model.Ngen,initialize=Pmin)

model.pg = pyo.Param(model.Ngen,initialize=pg)

model.rg = pyo.Param(model.Ngen,initialize=rg)

def ObjRule(model, ii):  

  return (sum(model.Pg[ii] * model.pg[ii] + model.Rg[ii] * model.rg[ii]))

model.Obj = pyo.Objective(model.Ngen, rule=ObjRule)

 

def Co1(model, ii):  

  return (model.Pmin[ii] <= model.Pg[ii] <= model.Pmax[ii])

model.co1 = pyo.Constraint(model.Ngen, rule=Co1)


def Co2(model, ii):  

  return (model.Pg[ii] + model.Rg[ii] <= model.Pmax[ii])

model.co2 = pyo.Constraint(model.Ngen, rule=Co2)


def Co3(model, ii):  

 return (sum(model.Pg[ii]) == sumPd)

model.co3 = pyo.Constraint(model.Ngen, rule=Co3)


instance = model.create_instance()

opt = pyo.SolverFactory('glpk')

opt.solve(instance)





在 2020年3月6日星期五 UTC+8下午8:59:55,Lucas Ford写道:

Cesar Silva

unread,
Mar 6, 2020, 9:23:41 AM3/6/20
to pyomo...@googlegroups.com
I think the problem is your objective function definition. You should define the summation within the function that is returned, different from an indexed constraint. It should be:

def ObjRule(model):  

  return (sum(model.Pg[ii] * model.pg[ii] + model.Rg[ii] * model.rg[ii] for ii in model.Ngen))

model.Obj = pyo.Objective(rule=ObjRule)


--
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/a0e040a0-2103-4998-baa5-c0f4c7b67334%40googlegroups.com.

ran tao

unread,
Mar 6, 2020, 9:39:02 AM3/6/20
to Pyomo Forum
Hi Cesar
thanks for your reply!
i try it in your way, and now the Python tells me :(it is NOT a error)
"
WARNING: Constant objective detected, replacing with a placeholder to prevent
    solver failure.
WARNING: Empty constraint block written in LP format - solver may error
"

so, i fix my all constraint as your method, but it may be wrong:
the error message is:

"
ValueError: Constraint 'co1' does not have a proper value. Found '<generator object Co1.<locals>.<genexpr> at 0x0000020B02BAAA98>'
Expecting a tuple or equation. Examples:
   sum(model.costs) == model.income
   (0, model.price[item], 50)
"

and the python function, i.e. def Co1(model, ii). the parameter ii makes me confused

i think i see this form in the Pyomo Document, but i cannot understand how it works, because no value pass to the parameter ii.

在 2020年3月6日星期五 UTC+8下午10:23:41,Cesar Silva写道:
To unsubscribe from this group and stop receiving emails from it, send an email to pyomo...@googlegroups.com.

Siirola, John D

unread,
Mar 6, 2020, 9:59:00 AM3/6/20
to pyomo...@googlegroups.com

The problem is that your entire model is indexed by an empty Set.  You are declaring

 

model.Ngen = pyo.Set(dimen=5)

 

The “dimen” keyword tells Set to expect that each member of the Set will be a tuple with 5 elements.  However, you never actually provide any data to the Set.  I think what you want is a Set with 5 members, 0 through 4.  You can get that one of two ways:

 

                model.Ngen = pyo.Set(initialize=range(5))

or

                model.Ngen = pyo.RangeSet(0,4)

 

Also note that you are not initializing your Params incorrectly.  The Param initialize keyword expects a dictionary mapping index to value and not a list.  As written, each Param will get a value that is itself a list.

 

john

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/b8358f70-d9cb-45e8-a9d0-9153a046098b%40googlegroups.com.

陶然(TAO Ran)

unread,
Mar 6, 2020, 10:23:24 AM3/6/20
to pyomo...@googlegroups.com
finally,i success!
thank you vvvvvvery much!

'Siirola, John D' via Pyomo Forum <pyomo...@googlegroups.com> 于2020年3月6日周五 下午10:59写道:
You received this message because you are subscribed to a topic in the Google Groups "Pyomo Forum" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/pyomo-forum/Sb6BMYibbNc/unsubscribe.
To unsubscribe from this group and all its topics, send an email to pyomo-forum...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pyomo-forum/ef803b51a8a844179a7cacbb822ec044%40ES02AMSNLNT.srn.sandia.gov.
Reply all
Reply to author
Forward
0 new messages