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)  

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?

  • $\begingroup$ I was wondering whether this could be solved using CPLEX or what kind of solver did you use? $\endgroup$ – Snowflake Apr 22 at 15:10
  • $\begingroup$ 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 $\endgroup$ – PeterBe Apr 22 at 15:14
  • $\begingroup$ If you do not have any integer variables then it would of course be a linear optimization problem. $\endgroup$ – PeterBe Apr 22 at 15:16
  • $\begingroup$ 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? $\endgroup$ – Snowflake Apr 22 at 15:18
  • $\begingroup$ 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. $\endgroup$ – PeterBe Apr 22 at 15:27


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)
  • 1
    $\begingroup$ Cool. Thank you Steven. Your suggested code seems to work. I upvoted and accepted your answer. $\endgroup$ – PeterBe Feb 22 at 17:37
  • $\begingroup$ Glad it helped @PeterBe $\endgroup$ – Steven01123581321 Feb 22 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.