I am wondering whether it is possible to create constraints over years and hours at the same time in Pyomo.

For example, my current time variable is:

model.T = pyo.RangeSet(len(hourly_data.index))

However, this does not allow me to distinguish between hours and years. I do have a timestamp variable, that contains the date and the time. So, I thought perhaps I could do:

model.T2 = pyo.Set(initialize=hourly_data.DateTime)

Now the problem comes on how to manipulate this TimeStamp object. Consider that the parameters are given and the variables are outputs from the solver. Let's first assume that our objective function is a maximisation function. We would like to create the following constraint:

Get the maximum water used, in normal circumstances, if we would like to get the maximum water usage of during all hours, we can do:

model.c_maxWater = pyo.ConstraintList()
for t in model.T:
     model.c_maxWater.add(model.waterUsage[t] <=

With a penalty in the objective function associated with model.maxWater. The problem becomes what if we want to penalise every year differently, because we have different water costs? I can imagine that our constraint would be somewhat like:

model.c_maxWater = pyo.ConstraintList()
for t in model.T2:
     model.c_maxWater.add(model.waterUsage[t] <=

My problem is: how can I associate the t variable with certain years y. One index is hourly (in this case the t and the other is annually (y)?

Note: a multi index set is possible, but how to deal with leap years etc.? Can a multi index set have different lengths in it's hourly dimensions for leap years?

  • $\begingroup$ I am not sure understand your question as well but, what are you looking for is something like this? $\sum_{t=1}^{t=365}\ waterUsage(t) \leq maxWater(y), \quad \ y\in Y$ $\endgroup$
    – A.Omidi
    Jun 2 at 12:23
  • $\begingroup$ Ah, yes! I am sorry for the confusion. But this is indeed what I am looking for, but, we cannot just sum to 365, since we have leap years sometimes. $\endgroup$
    – Snowflake
    Jun 2 at 12:24
  • $\begingroup$ So, one possible way to split the planning horizon based on what you need is to define auxiliary binary variables and using big-M constraints. $\endgroup$
    – A.Omidi
    Jun 2 at 12:37

Your Answer

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

Browse other questions tagged or ask your own question.