I'm programming a optimization in pyomo.environ for timeseries. Now I have the need for using piecewise function. Using the
pyomo.environ.Piecewise(...)
I get the error
TypeError: Cannot apply a Set operator to an indexed Var component (I)
To find a solution e reduzed the model to a minimized version:
import pyomo.environ as pe
import numpy as np
# Modell erstellen
model = pe.ConcreteModel()
model.steps = range(0,10)
# Variablen
model.P = pe.Var(model.steps, within=pe.Reals) # P kann positiv oder negativ sein
model.I = pe.Var(model.steps ) # I(P) als abhängige Variable
# Parameter
model.a1 = pe.Param(initialize=1) # Beispielwerte
model.m1 = pe.Param(initialize=2)
model.a2 = pe.Param(initialize=-1)
model.m2 = pe.Param(initialize=3)
for i in range(0, 10):
# Stückweise lineare Funktion definieren
points_p = np.array([-10000, -0.01, 0.01, 10000])
points_i = np.where(points_p > 0, 0.01 + 0.5 * points_p / 48,
0.01 + 0.45 * points_p / 48)
model.piecewise_pi = pe.Piecewise(model.I, model.P, list(points_p), list(points_i),
pw_constr_type='EQ')
# Zielfunktion (optional)
model.obj = pe.Objective(expr=model.I, sense=pe.minimize)
# Modell lösen
solver = pe.SolverFactory('cplex')
solver.solve(model)
I allready made a test with loop over the indext Variables:
for i in range(0, 10):
# Stückweise lineare Funktion definieren
points_p = np.array([-10000, -0.01, 0.01, 10000])
points_i = np.where(points_p > 0, 0.01 + 0.5 * points_p / 48,
0.01 + 0.45 * points_p / 48)
model.piecewise_pi = pe.Piecewise(model.I[i], model.P[i], list(points_p), list(points_i),
pw_constr_type='EQ')
but it still leads to te same error:
TypeError: Cannot apply a Set operator to a non-Set component data (I[0])
Without the dimension of the 11 steps the code is running.
Maybe that there is a solution without usingPiecewise in this case, but I also ned the approximation of a 6th degree polynomial within the code and therefore I need the Piecewise-function.
Does anyone have a hint for me, how to treat with it?
I'm programming a optimization in pyomo.environ for timeseries. Now I have the need for using piecewise function. Using the
pyomo.environ.Piecewise(...)
I get the error
TypeError: Cannot apply a Set operator to an indexed Var component (I)
To find a solution e reduzed the model to a minimized version:
import pyomo.environ as pe
import numpy as np
# Modell erstellen
model = pe.ConcreteModel()
model.steps = range(0,10)
# Variablen
model.P = pe.Var(model.steps, within=pe.Reals) # P kann positiv oder negativ sein
model.I = pe.Var(model.steps ) # I(P) als abhängige Variable
# Parameter
model.a1 = pe.Param(initialize=1) # Beispielwerte
model.m1 = pe.Param(initialize=2)
model.a2 = pe.Param(initialize=-1)
model.m2 = pe.Param(initialize=3)
for i in range(0, 10):
# Stückweise lineare Funktion definieren
points_p = np.array([-10000, -0.01, 0.01, 10000])
points_i = np.where(points_p > 0, 0.01 + 0.5 * points_p / 48,
0.01 + 0.45 * points_p / 48)
model.piecewise_pi = pe.Piecewise(model.I, model.P, list(points_p), list(points_i),
pw_constr_type='EQ')
# Zielfunktion (optional)
model.obj = pe.Objective(expr=model.I, sense=pe.minimize)
# Modell lösen
solver = pe.SolverFactory('cplex')
solver.solve(model)
I allready made a test with loop over the indext Variables:
for i in range(0, 10):
# Stückweise lineare Funktion definieren
points_p = np.array([-10000, -0.01, 0.01, 10000])
points_i = np.where(points_p > 0, 0.01 + 0.5 * points_p / 48,
0.01 + 0.45 * points_p / 48)
model.piecewise_pi = pe.Piecewise(model.I[i], model.P[i], list(points_p), list(points_i),
pw_constr_type='EQ')
but it still leads to te same error:
TypeError: Cannot apply a Set operator to a non-Set component data (I[0])
Without the dimension of the 11 steps the code is running.
Maybe that there is a solution without usingPiecewise in this case, but I also ned the approximation of a 6th degree polynomial within the code and therefore I need the Piecewise-function.
Does anyone have a hint for me, how to treat with it?
Share Improve this question asked Mar 18 at 7:43 mq_ogmq_og 111 Answer
Reset to default 0The pyomo.environ.Piecewise
component is meant for defining a single piecewise function between two individual (scalar) variables. The error arises because you're attempting to apply it to an indexed variable (model.I
) or a specific element of it (model.I[i]
).
You need to define a separate Piecewise
constraint for each time step. You can define the piecewise function parameters (points_p
and points_i
) outside the loop.
model.steps = range(0, 10)
# Piecewise constraints for each time step
model.piecewise_pi = pe.ConstraintList()
for i in model.steps:
model.piecewise_pi.add(
pe.Piecewise(
model.I[i],
model.P[i],
list(points_p),
list(points_i),
pw_constr_type='EQ'
)
)
For each time step i
in model.steps
, we create a Piecewise
constraint that links model.I[i]
and model.P[i]
specifically for that time. This individual constraint is then added to the model.piecewise_pi
ConstraintList
.