我想在 pyomo 中实现以下目标函数:
def alpha_beta(x):
a = 0.0019727939
b = 0.0078887
Lmin, Lnom, Lmax = 0.8035, 2.3811, 3.084
return np.piecewise(x, [np.logical_and(Lmin <= x, x < Lnom),
np.logical_and(Lnom <= x, x <= Lmax)],
[lambda x: a * ((x - Lnom)**2) + 0.006226,
lambda x: b * ((x - Lnom)**2) + 0.006226, 0])
def obj_rule(model):
return (sum(0.00873*Dindf[i]*(w1*alpha_beta(model.x[i])+w2*alpha_beta(model.y[i]))+\
k*(w1*abs(model.x[i+1]-model.x[i])+w2*abs(model.y[i+1]-model.y[i])) for i in model.B))+\
k*(w1*abs(model.x[0]-L10)+w2*abs(model.y[0]-L20))+Rfc1*w1+Rfc2*w2
obj_rule 是我在 pyomo 中的目标函数,函数 alpha_beta 是 obj_rule 调用的外部函数。我收到一个错误(错误消息的一部分):
/tmp/ipykernel_4287/3372018693.py in alpha_beta(x)
40 b = 0.0078887
41 Lmin, Lnom, Lmax = 0.8035, 2.3811, 3.084
---> 42 return np.piecewise(x, [np.logical_and(Lmin <= x, x < Lnom),
43 np.logical_and(Lnom <= x, x <= Lmax)],
44 [lambda x: a * ((x - Lnom)**2) + 0.006226,
pyomo/core/expr/numvalue.pyx in pyomo.core.expr.numvalue.NumericValue.__array_ufunc__()
pyomo/core/expr/numvalue.pyx in pyomo.core.expr.numvalue.NumericNDArray.__array_ufunc__()
pyomo/core/expr/logical_expr.pyx in pyomo.core.expr.logical_expr.InequalityExpression.__bool__()
PyomoException: Cannot convert non-constant expression to bool. This error is usually caused by using an expression in a boolean context such as an if statement. For example,
m.x = Var()
if m.x <= 0:
...
would cause this exception.
如何解决这个问题并以正确的方式实现目标函数?谢谢!