我的Pyomo模型具有以下约束:
def rule1(model, r, t):
return model.v[r,t] == 0 if (model.w[r,t] < 0) else model.v[r,t] == 10*model.w[r,t]^1.5
model.r1 = Constraint(model.R, model.T, rule=rule1)
它会引发以下错误消息:
TypeError: Relational expression used in an unexpected Boolean context.
The inequality expression:
w[test1,0] < 0.0
contains non-constant terms (variables) that were evaluated in an
unexpected Boolean context at
File '<ipython-input-311-60394d2f36da>', line 2:
return model.v[r,t] == 0 if (model.w[r,t] < 0) else model.v[r,t] == 10*model.w[r,t]^1.5
从错误消息中我了解到,由于在表达式中使用了变量model.w
,因此发生了错误。目前尚不清楚该如何解决?
model.w
的值是在另一个约束中计算的,如下所示(它编译时没有错误):
def balance_equation(model, r, t):
if t == 0:
return Constraint.Skip
return model.w[r,t] == model.w[r,t-1].value + model.v[r,t] - model.z[r,t].value
model.flow = Constraint(model.R, model.T, rule=balance_equation)