我正在寻找在 Pyomo 中创建一个简单的指示变量。假设我有一个变量 x,如果 x > 0,则该指示函数将取值 1,否则取 0。
我尝试这样做的方法如下:
model = ConcreteModel()
model.A = Set(initialize=[1,2,3])
model.B = Set(initialize=['J', 'K'])
model.x = Var(model.A, model.B, domain = NonNegativeIntegers)
model.ix = Var(model.A, model.B, domain = Binary)
def ix_indicator_rule(model, a, b):
return model.ix[a, b] == int(model.x[a, b] > 0)
model.ix_constraint = Constraint(model.A, model.B,
rule = ix_indicator_rule)
我收到的错误消息是这样的Avoid this error by using Pyomo-provided math functions
,根据这个链接 https://stackoverflow.com/questions/48171498/finding-pyomo-provided-math-functions被发现于pyomo.environ
...但我不知道该怎么做。我尝试过使用validate_PositiveValues()
, 像这样:
def ix_indicator_rule(model, a, b):
return model.ix[a, b] == validate_PositiveValues(model.x[a, b])
model.ix_constraint = Constraint(model.A, model.B,
rule = ix_indicator_rule)
没有运气。任何帮助表示赞赏!