I want to implement the following objective function in 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
The obj_rule is my objective function in pyomo, the function alpha_beta is an external function called by obj_rule. I got an error (part of error message):
/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.
How to fix this and implement the objective function in the right way? Thanks!