EDIT: The block of code below is working now, I may have missed something in my IDE, but I am having issues with other similar constraints. I believe the problem is using endogenous if statements, but I'm not sure of another way to write the constraint that if a value exists in one column/variable, the value in another column/variable must be 0.
I'm attempting to implement a simple constraint wherein a battery cannot discharge and charge within the same hour. model.p_BAT_discharge and charge are both variables within the pyomo model.
I seem to receive this error a lot when trying to implement constraints with pyomo variables instead of parameters. I am wondering if there is a trick/work around for simple constraints like this using pyomo variables?
I'm ignoring the boundary conditions for now. See below:
model.p_BAT_charge = pyo.Var(model.t, within=pyo.NonNegativeReals)
model.p_BAT_discharge = pyo.Var(model.t, within=pyo.NonNegativeReals)
def no_discharge_charge_rule(model, t):
if t == model.t.first():
return pyo.Constraint.Skip
elif t == model.t.last():
return pyo.Constraint.Skip
else:
if pyo.value(model.p_BAT_discharge[t]) != 0:
return pyo.value(model.p_BAT_charge[t]) == 0
elif pyo.value(model.p_BAT_charge[t]) != 0:
return pyo.value(model.p_BAT_discharge[t]) == 0
else:
return pyo.Constraint.Skip
model.no_discharge_charge_constraint = pyo.Constraint(model.t, rule=no_discharge_charge_rule)
I am receiving the error: No value for uninitialized NumericValue object p_BAT_discharge[2]
As I understand it, in Pyomo, the list of conditions is not iterated over for each timestamp in the loop, but rather is looked at during the model initialization (I'm still learning Pyomo). I have an issue where the model is charging and discharging the battery at the same moment to optimize revenue, but I can't find a constraint that works to prevent that.
Any insight at all would be appreciated. Thank you.