简体   繁体   English

为什么将参数传递给约束函数时,Pyomo行为会发生变化?

[英]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.

相关问题 为什么大熊猫内使用的功能行为会发生变化? - Why does function behavior used within pandas apply change? 将函数参数作为变量传递时,可以更改它们吗? - Can I change function parameters when passing them as variables? PYOMO:约束没有合适的值 - PYOMO: Constraint does not have a proper value 为什么 Pyomo 无法识别“Constraint.Skip”? - How why is `Constraint.Skip` not recognized in Pyomo? 为什么通过我的函数传递这个数据帧会改变原始数据帧? - Why does passing this dataframe through my function change the original dataframe? 当将参数传递给python / opencv函数时,给出参数名称而不导致不同的结果,为什么 - when passing parameters to python/opencv function, giving parameters' name and not lead to different results, why Pyomo:约束在解决目标后不会改变变量值 - Pyomo: Constraint doesn't change variable values after solving the objective 为什么lambda函数将其行为从生成它的函数更改为主要函数? - Why does a lambda function change its behavior from in the function that generates it to the main? 为什么将 function 传递给装饰器时,AWS Lambda 会超时? - Why does AWS Lambda time out when passing a function to a decorator? 在Pyomo中,是否可以基于多个表达式编写目标函数或约束? - In Pyomo, Is it possible to write an objective function or a constraint based on several Expressions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM