[英]Why Does Pyomo Behavior Change When Passing parameters to constraint function?
This code works correctly. 此代码正常工作。
def m(model, i):
return model.fd_amt[i] <= getattr(model, 'br_sa_ind')[i] * global_m
setattr(model, ind+"_m",Constraint(model.br_standalone_I, rule=m))
But this 但是这个
def m(model, i, ind_name):
return model.fd_amt[i] <= getattr(model, ind_name)[i] * global_m
setattr(model, ind+"_m",Constraint(rule=m(model,model.model.br_standalone_I, 'br_sa_ind') ))
results in this error: 导致此错误:
ERROR: evaluating expression: No value for uninitialized NumericValue object fd_amt[br_standalone_I] (expression: fd_amt[br_standalone_I] <= 13 * br_sa_ind[br_standalone_I]) ERROR: Rule failed when generating expression for constraint br_sa_ind_m: ValueError: No value for uninitialized NumericValue object fd_amt[br_standalone_I] ERROR: Constructing component 'br_sa_ind_m' from data=None failed: ValueError: No value for uninitialized NumericValue object fd_amt[br_standalone_I]
错误:计算表达式:未初始化的NumericValue对象fd_amt [br_standalone_I]没有值(表达式:fd_amt [br_standalone_I] <= 13 * br_sa_ind [br_standalone_I])错误:为约束br_sa_ind_m生成表达式时规则失败:未初始化的NumericValue对象没有值fd_amt [br_standalone_I]错误:从数据构造组件'br_sa_ind_m'=没有失败:ValueError:未初始化的NumericValue对象没有值fd_amt [br_standalone_I]
Is there a reason Pyomo constraints behave this way with explicit parameters? Pyomo约束对显式参数有这种行为吗?
You can achieve the desired behavior by replacing rule=
with expr=
: 您可以通过将
rule=
替换为expr=
来实现所需的行为:
setattr(model, ind+"_m",Constraint(model.br_standalone_I))
for i in model.br_standalone_I:
getattr(model, ind+"_m")[i].set_value(expr=m(model, i, 'br_sa_ind'))
The purpose of rule
is so that indexed constraint expressions can be constructed using a common rule. rule
的目的是使索引约束表达式可以使用通用规则来构造。 If you have a singleton constraint, you can specify the expression using expr
. 如果您有单例约束,则可以使用
expr
指定expr
。
Your code doesn't work because you use a function call rule=m(...)
instead of a function reference rule=m
. 您的代码不起作用,因为您使用了函数调用
rule=m(...)
而不是函数引用rule=m
。
While this solution may not answer to your problem directly, it may offer a workarround. 尽管此解决方案可能无法直接解决您的问题,但可能会带来麻烦。 I still don't know if Pyomo allows what you are asking (passing a parameter to a rule).
我仍然不知道Pyomo是否允许您询问(将参数传递给规则)。
Create a new set with your parameter that you want to pass as your only element. 使用您要传递的参数作为唯一元素创建一个新集合。 You could add more elements later if you want, if you handle your parameters in your rule's function adequately.
如果需要,可以在规则函数中适当处理参数的情况下稍后添加更多元素。 For simplicity, let's begin with only one element.
为简单起见,让我们仅从一个元素开始。
model.S = Set(initialize=['br_sa_ind'])
Then use this set as your parameter to your rule. 然后将此集用作规则的参数。 It's like using a for all notation, with only one element.
就像对所有符号都使用a一样,它只有一个元素。 (For all element in set S and in set br_standalone_I, apply rule m).
(对于集合S和集合br_standalone_I中的所有元素,应用规则m)。 You should create your constraint using
您应该使用以下方式创建约束
Constraint(model.br_standalone_I, model.S, rule=m)
So your entire code would look like 所以你的整个代码看起来像
def m(model, i, ind_name):
return model.fd_amt[i] <= getattr(model, ind_name)[i] * global_m
setattr(model, ind+"_m",Constraint(model.br_standalone_I, model.S, rule=m))
This is not entirely elegant, but it should work. 这并不完全优雅,但应该可以。 I yet want to hear if you can specify parameters to a rule while creating a constraint.
我还想听听您是否可以在创建约束时为规则指定参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.