为什么将参数传递给约束函数时 Pyomo 行为会发生变化? [英] Why Does Pyomo Behavior Change When Passing parameters to constraint function?

查看:81
本文介绍了为什么将参数传递给约束函数时 Pyomo 行为会发生变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码工作正常.

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

但是这个

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

导致此错误:

错误:计算表达式:未初始化的 NumericValue 没有值目的fd_amt[br_standalone_I](表达式:fd_amt[br_standalone_I] <= 13 *br_sa_ind[br_standalone_I]) 错误:为约束 br_sa_ind_m 生成表达式时规则失败:ValueError:未初始化的 NumericValue 对象没有值fd_amt[br_standalone_I] 错误:从 data=None 构建组件 'br_sa_ind_m' 失败:ValueError:未初始化的 NumericValue 对象 fd_amt[br_standalone_I] 没有值

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]

Pyomo 约束是否有显式参数的这种行为方式?

Is there a reason Pyomo constraints behave this way with explicit parameters?

推荐答案

您可以通过将 rule= 替换为 expr= 来实现所需的行为:

You can achieve the desired behavior by replacing rule= with 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'))

rule 的目的是使索引约束表达式可以使用通用规则构建.如果您有单例约束,则可以使用 expr 指定表达式.

The purpose of rule is so that indexed constraint expressions can be constructed using a common rule. If you have a singleton constraint, you can specify the expression using expr.

这篇关于为什么将参数传递给约束函数时 Pyomo 行为会发生变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆