如何解决如果条件包含Pyomo约束中的变量,如何使用
我正在尝试为我的Pyomo模型添加一个简单的约束:
最大{x [1] -a,b} +最大{x [2] -a,b}> = c
其中a,b,c被赋予了预定义的参数,x是由设置模型索引的决策变量。I初始化为[1,2]。
为了实现这一目标,我尝试了以下方法,但均未成功:
from pyomo.environ import *
from pyomo.opt import SolverFactory
from pyomo.environ import *
model = AbstractModel()
model.I = Set()
model.a = Param()
model.b = Param()
model.c = Param()
model.x = Var(model.I)
def my_rule(model):
total = 0
for i in model.I:
val = model.x[i] - model.a
if val < model.b:
val = model.b
total = total + val
return total >= model.c
model.my_constraint = Constraint(rule=my_rule)
data = {None:{
'I': {None: [1,2]},'a': {None: 3},'b': {None: 5},'c': {None: 4}}
}
real_model = model.create_instance(data)
给出的错误是: 警告:不建议使用:链式不等式不建议使用。使用inequality() 表示范围不等式的函数。 错误:为约束my_constraint生成表达式时,规则失败: TypeError:尝试形成具有两个下限的复合不等式
The inequality expression:
x[1] - 3 < 5.0
contains non-constant terms (variables) that were evaluated in an
unexpected Boolean context at
File '<ipython-input-11-e3a6c44c5b8e>',line 14:
if val < model.b:
Evaluating Pyomo variables in a Boolean context,e.g.
if expression <= 5:
is generally invalid. If you want to obtain the Boolean value of
the expression based on the current variable values,explicitly
evaluate the expression using the value() function:
if value(expression) <= 5:
or
if value(expression <= 5):
有人可以帮助我吗?非常感谢。
解决方法
夫妇问题...
- 您不能在条件取决于变量求值的约束构造中埋入“ if”语句。 如果您要这么做,
- max和min函数不能直接在LP / IP中表示。也许有一些方法可以改写...但是...
您的约束有点荒谬。如果b
和c
是固定值,则您所显示的约束始终为true,因为如上所述,b
> c
:
max(任何b
)+ max(其他b
)> c
始终为真。
是时候重新检查您的问题表达了。 :)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。