简体   繁体   English

Pyomo 变量下界

[英]Pyomo lower bounds of variables

Hi guys I have the following problem.大家好,我有以下问题。 I have two optimization problems and the output values of the first are the lower bounds in second's variables.我有两个优化问题,第一个的 output 值是第二个变量的下限。

I try to write it the following way:我尝试按以下方式编写它:

model_low=ConcreteModel()

#Decision Variables
model_low.p=Var((tech for tech in fuels+['hydro_big']+renew),hours,within=NonNegativeReals,initialize=2000)
model_low.C=Var(techs,within=NonNegativeReals,initialize=0)

I set for the next optimization problem the following decision variables:我为下一个优化问题设置了以下决策变量:

model_high=ConcreteModel()

#Decision Variables
model_high.p=Var((tech for tech in fuels+['hydro_big']+renew),hours,within=NonNegativeReals,lb=value(model_low.p),initialize=2000)
model_high.C=Var(techs,within=NonNegativeReals,lb=value(model_low.C),initialize=0)

But I got the following Error:但我收到以下错误:

ERROR: evaluating object as numeric value: p
(object: \<class 'pyomo.core.base.var.IndexedVar'\>)
'IndexedVar' object is not callable
Traceback (most recent call last):
File "C:/Final_python.py", line 144, in \<module\>
model_high.p=Var((tech for tech in fuels+\['hydro_big'\]+renew),hours,within=NonNegativeReals,lb=value(model_low.p),initialize=2000)
File "pyomo\\core\\expr\\numvalue.pyx", line 156, in pyomo.core.expr.numvalue.value
File "pyomo\\core\\expr\\numvalue.pyx", line 141, in pyomo.core.expr.numvalue.value
TypeError: 'IndexedVar' object is not callable

How could I fix this?我该如何解决这个问题?

Your variables in the model appear to be indexed, so you have to provide some lower bound indexing tool that is indexed.您在 model 中的变量似乎已被索引,因此您必须提供一些被索引的下限索引工具。 You can pass a function (as show) that will do this, same as if you were using a rule to make constraints.您可以传递将执行此操作的 function(如图所示),就像您使用规则进行约束一样。 Or you could just build this in as a constraint in the model and not use lower bound...same thing in model space.或者您可以将其构建为 model 中的约束,而不使用下限...在 model 空间中也是如此。

I would strongly encourage you to make a pyomo.Set for each of the things you are indexing with, it will save you a ton of time troubleshooting and such.强烈建议您为您要索引的每个事物制作一个pyomo.Set ,它将为您节省大量时间进行故障排除等。

Code:代码:

# 2-stage 

import pyomo.environ as pyo

m1 = pyo.ConcreteModel('stage 1')

m1.S = pyo.Set(initialize=[1, 2, 3])
m1.X = pyo.Var(m1.S, domain=pyo.NonNegativeReals)

# set some arbitrary values that would normally be done by the solver...
m1.X[1] = 4.2
m1.X[2] = 1.4
m1.X[3] = 8.0

# now lets set up the 2nd model...

m2 = pyo.ConcreteModel('stage 2')

m2.S = pyo.Set(initialize=m1.S)

# rule to initialize
def stage_2_initializer(m, s):
    return (pyo.value(m1.X[s]), None)  # (lower bound, upper bound)

m2.X = pyo.Var(m2.S, bounds=stage_2_initializer, domain=pyo.NonNegativeReals)

m2.pprint()

Output (the 2nd stage model print): Output(第二阶段model打印):

1 Set Declarations
    S : Size=1, Index=None, Ordered=Insertion
        Key  : Dimen : Domain : Size : Members
        None :     1 :    Any :    3 : {1, 2, 3}

1 Var Declarations
    X : Size=3, Index=S
        Key : Lower : Value : Upper : Fixed : Stale : Domain
          1 :   4.2 :  None :  None : False :  True : NonNegativeReals
          2 :   1.4 :  None :  None : False :  True : NonNegativeReals
          3 :   8.0 :  None :  None : False :  True : NonNegativeReals

2 Declarations: S X
[Finished in 420ms]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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