Pyomo 变量下界

发布于 2025-01-18 22:39:17 字数 1328 浏览 3 评论 0原文

大家好,我有以下问题。我有两个优化问题,第一个的输出值是第二个变量的下限。

我尝试按以下方式编写:

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)

我为下一个优化问题设置以下决策变量:

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)

但我收到以下错误:

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

如何解决此问题?

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多情癖 2025-01-25 22:39:17

模型中的变量似乎是索引的,因此您必须提供一些索引的下界索引工具。您可以通过(如显示)传递一个可以执行此操作的函数,就像使用规则来制定约束一样。或者,您可以将其作为模型中的约束构建,而不是在模型空间中使用下限。

我会强烈地鼓励您为您所索引的每件事制作pyomo.set,它将为您节省大量的时间故障排除等。

代码:

# 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()

输出(第二阶段模型打印):

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]

Your variables in the model appear to be indexed, so you have to provide some lower bound indexing tool that is indexed. You can pass a function (as show) that will do this, same as if you were using a rule to make constraints. Or you could just build this in as a constraint in the model and not use lower bound...same thing in model space.

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.

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):

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文