# How to implement a difference equation in Pyomo

I would like to define a difference equation in pyomo for a temperature variable. The equation (simplified) looks like: $$Temperature(t) = Temperature (t-1) + x(t)$$ , for t>0

$$Temperature (0) = 20$$

So this is what I tried in Pyomo

model.set_timeslots = pyo.RangeSet(0,95)

model.variable_x = pyo.Var(model.set_timeslots, within=pyo.Binary)
model.variable_temperature[0].fix(20)

def temperatureConstraintRule(model):
model.variable_temperature(t) = model.variable_temperature(t-1) + model.variable_x(t)
model.constraint_temperature = pyo.Constraint (model.set_timeslots, rule=temperatureConstraintRule)

However, I get the error message "SyntaxError: can't assign to function call"

Can anybody tell me how I can implement this difference equation in Pyomo?

• I was wondering whether this could be solved using CPLEX or what kind of solver did you use? Commented Apr 22, 2021 at 15:10
• Hi Snowflake. Yes you could use CPLEX or GUROBI. I used both for this problem. Basically it is still a mixed-integer linear optimization problem which can be solved by any solver which supports these types of problems Commented Apr 22, 2021 at 15:14
• If you do not have any integer variables then it would of course be a linear optimization problem. Commented Apr 22, 2021 at 15:16
• Hey PeterBe, thank you for your response, however I have an error using Pyomo using CPLEX regarding nonlinear item problem (see: or.stackexchange.com/questions/6134/…). Could you perhaps give some pointers how one could solve this? Commented Apr 22, 2021 at 15:18
• Well, CPLEX can't solve non-linear problems (if they are non-quadratic). So I guess you have to use another solver for that (I have no experience whatsoever with non-linear solvers). Or maybe you can try to formulate as a linear problem if that is possible. Commented Apr 22, 2021 at 15:27

try:

model.set_timeslots = pyo.RangeSet(0,95)

model.variable_x = pyo.Var(model.set_timeslots, within=pyo.Binary)
i0 = 20

def temperatureConstraintRule(model, t):
if t == model.set_timeslots.first():
return model.variable_x[t] == i0 + model.variable_x[t]
return model.variable_x[t] == model.variable_x[t-1] + model.variable_x[t]
model.constraint_temperature = Constraint(model.set_timeslots, rule=temperatureConstraintRule)
• Cool. Thank you Steven. Your suggested code seems to work. I upvoted and accepted your answer. Commented Feb 22, 2021 at 17:37
• Glad it helped @PeterBe Commented Feb 22, 2021 at 17:50