简体   繁体   中英

For loop iteration - Syntax Error in Pyomo Expression

I'm trying to do a for loop in a pyomo Expression

here are my initializations:

N = range(len(df))
model.N             = Param(N, mutable=False)
model.r             = Var(initialize=1.0 , within=NonNegativeReals)
model.alpha         = Var(initialize=1.0 , within=NonNegativeReals)
model.a             = Var(initialize=1.0 , within=NonNegativeReals)
model.b             = Var(initialize=1.0 , within=NonNegativeReals)
model.rCon          = Constraint(expr= model.r >= 0.000001)
model.alphaCon      = Constraint(expr= model.alpha >= 0.000001)
model.aCon          = Constraint(expr= model.a >= 0.000001)
model.bCon          = Constraint(expr= model.b >= 0.000001)

and when i try to define an expression like this:

model.v1 = Expression(expr= model.r + df['Xi'][0])

its compiled well, but when i try to do a For loop iteration like this:

model.v2 = Expression(expr= model.r + df['Xi'][i] for i in N)

i get an error like this:

pyomo

would appreciate your help

It looks like you are trying to pass a generator through the parameter expr to the Expression constructor. In which case, you need extra brackets like this:

model.v2 = Expression(expr=(model.r + df['Xi'][i] for i in N))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM