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.
これを修正し、目的関数を正しい方法で実装するにはどうすればよいですか? ありがとう!