Skip to main content

Rolling Horizon Approach

Comments

6 comments

  • Marika Karbstein
    Gurobi Staff Gurobi Staff

    You can use the variable attribute Start to do this.

    See also How do I use MIP starts? and/or MIP starts for explanations.

    0
  • Marco Sp
    Gurobi-versary
    Conversationalist
    First Question

    Hi Marika, 

    thanks for your fast response. Could you maybe give me an example how I do this in python for two dimensional variables ?

    I tried with one variable "energy_consumption" which is dependend on a Period "t" and a device "i"

    I tried different options:

    m.energy_consumption.Start = 0 

    energy_consumption[0, j].Start = 0

    energy_consumption.Start = 0

    but it was not working 

    0
  • Marika Karbstein
    Gurobi Staff Gurobi Staff

    Could you share a minimum reproducible example and the error you are facing?

    It might be that m.update() is missing. 

    0
  • Marco Sp
    Gurobi-versary
    Conversationalist
    First Question

    I have a list of devices "j" and they have to supply energy in every period "i" depending on the energy demand

     

    for j in range(device_list):
    while (x2 <= 35040): # number of Periods (15 min steps)
    m = gp.Model("milp1)
    m.modelSense = GRB.Minimize

    energy_consumption = m.addVars(Period, Number_devices , lb=0, vtype=GRB.CONTINUOUS, name = "Power_c")
    heat_production = m.addVars(Period, Number_devices , lb=0, vtype=GRB.CONTINUOUS, name = "Heat_p")
    dhw_temperature = m.addVars(Period, Number_devices , lb=43, ub = 50, vtype=GRB.CONTINUOUS, name = "dhw_temp")
    ...
    ...

    m.addConstrs((dhw_temperature[i,j] = dhw_temperature[i-1,j] - losses + factor * heat_production[i,j] ) for i in range(x1+1, x2) for j Number_devices)
    m.optimize()
    solution_dhw = m.getAttr('X', dhw_temperature)
    solution_heat_production = m.getAttr('X', heat_production)
    ...

    #transfer solution to numpy array
    for i in Periode:
    heat_production_solution[i][j] = solution_heat_production[i,j]
    ...

    x1 = x2 - 96
    if(x2 < (35040 -1500)):
    x2 = x2 +1500
    if (x2 == 35040):
    x2 = 35041
    elif(x2 >= (35040 -1500)):
    x2 = 35040


       

    with x1 and x2 I tell the programe how big my period steps are for each optimization round. I now would like to take the solutions after optimizing and set it to the variables of the next optimzation round. Hope that makes it more clear ? :)

    0
  • Marika Karbstein
    Gurobi Staff Gurobi Staff

    Hi Marco,

    Your example is not reproducible, so I cannot see where exactly your problem is. 
    So far there are only continuous variables defined. But you mentioned binary variables in your first post as well. Setting the start attribute only affects MIP models. Do you only have continuous variables?
    Here is an example of defining in each round a MIP model that uses the solution computed in the last round

    import gurobipy as gp

    A = [2 ,4 ,5 ,8 ,9, 4]
    B = [3, 4, 3, 2 ,5, 5]

    # some start value for the first round
    xSol = [0]
    ySol = [0]

    for j in range(4):
      m = gp.Model("test1")

      x = m.addVars(j+1, lb=0, obj=1, vtype=gp.GRB.INTEGER, name = "x")
      y = m.addVars(j+1, lb=0, obj=1, vtype=gp.GRB.CONTINUOUS, name = "y")  

      m.addConstrs((x[i] + 2*y[i] >=  A[i]) for i in range(j+1))
        m.addConstrs((3*y[i] - x[i] <=  B[i]) for i in range(j+1))

      #set start values for first j variables
      for i in range(j):
         x[i].Start = xSol[i]
           y[i].Start = ySol[i]

      m.optimize()

      xSol = m.getAttr('X', x)
        ySol = m.getAttr('X', y)

    However, in this case, the model could also be built iteratively and then Gurobi automatically uses the MIP solution from the previous optimization:

    import gurobipy as gp

    A = [2 ,4 ,5 ,8 ,9, 4]
    B = [3, 4, 3, 2 ,5, 5]

    x = {}
    y = {}

    m = gp.Model("test2")

    x[0] = m.addVar(obj=1, vtype=gp.GRB.INTEGER, name='x0')
    y[0] = m.addVar(obj=1, vtype=gp.GRB.CONTINUOUS, name='y0')

    m.addConstr(x[0] + 2*y[0] >=  A[0])
    m.addConstr(3*y[0] - x[0] <=  B[0])

    m.optimize()

    for j in range(1,4):
      x[j] = m.addVar(obj=1, vtype=gp.GRB.INTEGER, name='x'+str(j))
        y[j] = m.addVar(obj=1, vtype=gp.GRB.CONTINUOUS, name='y'+str(j))

      m.addConstr(x[j] + 2*y[j] >=  A[j])
        m.addConstr(3*y[j] - x[j] <=  B[j])

      m.optimize()
        m.printAttr('X')

     

    Does this help? If not please be more specific in what problem you are facing.

    0
  • Marco Sp
    Gurobi-versary
    Conversationalist
    First Question

    Hi Marika, 

    thank you very much for your response. I am trying to modell heat devices. They should shift their power consumption to low price periods of the energy market. And when having energy devices you also have a hot water storage. When minimizing prices it leads always to a cooling down of the hot water storage at the end of the planned/modelled period. Therfore I want for example go back in the time periods (e.g. 96 periods a 15 min) and set the solution e.g. (x-96) as the new start solution. Is there a way to do so ? I hope it got more clear now :)

    0

Please sign in to leave a comment.