I am defining a Pyomo model that should have some mutable Big-M parameters whose values should be dynamically assigned (once). However, I am having a problem with a difference equation as described in this post Error message in difference equation in Pyomo. Here you can see my extremel simplified model in pyomo (that does not make much sense from a logical point of view, but I just want to adress the error):
import pyomo.environ as pyo
import pandas as pd
from pyomo.util.infeasible import log_infeasible_constraints
#Define the model
model = pyo.ConcreteModel()
#Define the sets
model.set_timeslots = pyo.RangeSet(1, 288)
#Define the bigM parameter
model.param_BigM_Surplus_Positive = pyo.Param(model.set_timeslots, mutable=True, default =0)
#Define the variables
model.variable_temperatureBufferStorage = pyo.Var(model.set_timeslots, bounds=(20, 22))
model.variable_surplusPowerTotal = pyo.Var(model.set_timeslots)
#Equations for the Big-M parameters
'''When commenting this Big-M parameter equation out, the model can be solved. Otherwise I get an error for each value of the variable model.variable_temperatureBufferStorage
variable ERROR: evaluating object as numeric value:
variable_temperatureBufferStorage[274]
(object: <class 'pyomo.core.base.var._GeneralVarData'>)
No value for uninitialized NumericValue object
variable_temperatureBufferStorage[274]
'''
def BigM_Surplus_PositiveRule (model, t):
return model.param_BigM_Surplus_Positive [t] == 10
model.constraint_BigM_Surplus_Positive = pyo.Constraint(model.set_timeslots, rule = BigM_Surplus_PositiveRule)
# Defining the constraints
#Temperature constraint for the buffer storage (space heating) with energetic difference equation
def temperatureBufferStorageConstraintRule(model, t):
if t == model.set_timeslots.first():
return model.variable_temperatureBufferStorage[t] == 21
return model.variable_temperatureBufferStorage[t] == model.variable_temperatureBufferStorage[t-1]
model.constraint_temperatureBufferStorage = pyo.Constraint (model.set_timeslots, rule=temperatureBufferStorageConstraintRule)
#Equations for the surplus power
def surplusPowerTotalRule (model, t):
return model.variable_surplusPowerTotal [t] == 1
model.constraint_surplusPowerTotal = pyo.Constraint(model.set_timeslots, rule = surplusPowerTotalRule)
#Objectives
def objectiveRule_combined_general (model):
return sum(model.variable_surplusPowerTotal[t] for t in model.set_timeslots)
model.objective_combined_general = pyo.Objective( rule=objectiveRule_combined_general, sense =pyo.minimize)
print("Start of solving")
solver = pyo.SolverFactory('gurobi')
solver.options['MIPGap'] = 1
solver.options['TimeLimit'] = 20
solution = solver.solve(model, tee=True)
log_infeasible_constraints(model)
The problematic part is when I define the constraints for the mutable Big-M parameter model.param_BigM_Surplus_Positive = pyo.Param(model.set_timeslots, mutable=True, default =0)
. When they are active and not commented out I get an error from Gurobi telling that the problem is infeasible or unbounded:
Infeasible or unbounded model
WARNING: Loading a SolverResults object with a warning status into
model.name="unknown";
- termination condition: infeasibleOrUnbounded
- message from solver: Problem proven to be infeasible or unbounded.
When looking what is causing this problem with from pyomo.util.infeasible import log_infeasible_constraints
I can see an error for each value of the variable model.variable_temperatureBufferStorage
ERROR: evaluating object as numeric value:
variable_temperatureBufferStorage[2]
(object: <class 'pyomo.core.base.var._GeneralVarData'>)
No value for uninitialized NumericValue object
variable_temperatureBufferStorage[2]
ERROR: evaluating object as numeric value:
variable_temperatureBufferStorage[3]
(object: <class 'pyomo.core.base.var._GeneralVarData'>)
No value for uninitialized NumericValue object
variable_temperatureBufferStorage[3]
ERROR: evaluating object as numeric value:
variable_temperatureBufferStorage[4]
(object: <class 'pyomo.core.base.var._GeneralVarData'>)
No value for uninitialized NumericValue object
variable_temperatureBufferStorage[4]`
Without the Big-M parameter constraints the model is solvable. So my question is why does this error occur when having a mutable parameter and how can I tackle this problem?
model.constraint_BigM_Surplus_Positive
(for simplicity I just assign the value 10 to it in this simplified example). So the value of the parameter Big-M should be calculated for every timeslot (t). $\endgroup$model.param_BigM_Surplus_Positive = pyo.Param(model.set_timeslots, mutable=True, default =0)
I have to specify a default value otherwise I get a Value Error "ValueError: Error evaluating Param value (param_BigM_Surplus_Positive[1]): The Param value is currently set to an invalid value. This is typically from a scalar Param or mutable Indexed Param without an initial or default value." $\endgroup$parameter
$\endgroup$