Search code examples
pythonoptimizationpyomo

Pyomo constraint that appends new iteration to a variable when reaching specified values


I am having issues adapting a constraint in Pyomo. The original constraint is below:

m.tou = Var(domain=Reals)


def peak_power_rule(m,i):
    return m.pe_c[i] + m.pe_d[i] + load[i]  <=  m.tou   
m.peak_power = Constraint(time, rule=peak_power_rule)

"load" refers to given hourly electricity consumption data for a building, while the variable m.pe_c is what is charged to a battery system, while m.pe_d is what is discharged from the battery. m.tou is the peak consumption. The range for [i] is 8760 (iterating hourly over values for a year).

The constraint is currently working fine but I want to adapt it so that it makes a new m.tou[x]
everytime [i] reaches 730 (one month in hours). I want a new m.tou[x] for each month. I get errors when I do slices in pyomo and my limited knowledge of pyomo is restricting me at this point in time.

Update: I was able to solve this by increasing my limited knowledge. I created a new variable for each month.


Solution

  • I created a new variable for each month. Below is an example of January (M.jan).

    Jan_time =time[0:745]
    def peak_power_rule_jan(m,i):
       return m.pe_c[i] + m.pe_d[i] + load[i]  <=  m.jan
    m.peak_power_jan = Constraint(Jan_time, rule=peak_power_rule_jan)