Skip to main content

Slow rate of adding large number of constraints

Ongoing

Comments

2 comments

  • Jaromił Najman
    Gurobi Staff Gurobi Staff

    Could you please share a minimal reproducible example? Right now, the code snippet is not reproducible.

    You should try avoiding the \(\texttt{+=}\) operator when building many possibly large linear expressions. Instead, you could use the addTerms method, where you could add all terms w.r.t. the \(\texttt{t}\) index at once.

    1
  • JIN YI Yong
    Gurobi-versary
    First Comment
    First Question

    Hi Jaromił Najman ,

    Thank you for your help!

    I realized that using MVars made it slow, switching to addVars instead has made adding the above provided constraints in 0.12s. However I still have slow model building with another constraint that involves linearizing the constraint. Provided below is a minimal reproducible example.

    import gurobipy as gb
    from gurobipy import GRB
    import time

    if __name__ == "__main__":
    nbScenarios = 100
      model: gb.Model = gb.Model("coordination")

        nbStations = 300
        nbTime = 48
        timeInterval = 0.5

        # maximum charging power of each EV, in kW, using level 2 charging
        maxChargingPower = 19

      DayAheadOnOffChargingStatus = model.addVars(
            nbScenarios,
            nbStations,
            nbTime,
            vtype=gb.GRB.BINARY,
            name="DayAheadOnOffChargingStatus",
        )

        DayAheadChargingPower = model.addVars(
            nbScenarios,
            nbStations,
            nbTime,
            lb=0,
            ub=maxChargingPower,
            vtype=gb.GRB.CONTINUOUS,
            name="DayAheadChargingPower",
        )

        # time to make charging power linear
        start = time.time()

        DayAheadChargingPowerLinearized = model.addVars(
            nbScenarios,
            nbStations,
            nbTime,
            lb=0,
            ub=maxChargingPower,
            vtype=GRB.CONTINUOUS,
            name="DayAheadChargingPowerLinearized",
        )

        for s in range(nbScenarios):
            for i in range(nbStations):
                for t in range(nbTime):
                    DayAheadChargingPowerLinearized_s_i_t = DayAheadChargingPowerLinearized[
                        s, i, t
                    ]
                    DayAheadOnOffChargingStatus_s_i_t = DayAheadOnOffChargingStatus[s, i, t]
                  DayAheadChargingPower_s_i_t = DayAheadChargingPower[s, i, t]
                 
    model.addConstr(
                        DayAheadChargingPowerLinearized_s_i_t
                        <= maxChargingPower * DayAheadOnOffChargingStatus_s_i_t
                    )
                    model.addConstr(
                        DayAheadChargingPowerLinearized_s_i_t
                        <= DayAheadChargingPower_s_i_t
                    )
                    model.addConstr(DayAheadChargingPowerLinearized_s_i_t >= 0)
                    model.addConstr(
                        DayAheadChargingPowerLinearized_s_i_t
                        >= DayAheadChargingPower_s_i_t
                        - (1 - DayAheadOnOffChargingStatus_s_i_t) * maxChargingPower
                    )
        end = time.time()
        print("Time to add charging power linearization constraints: " + str(end - start))
      model.update()

     

    Thank you for your time and I appreciate the help!

    On a side note: How do I add code blocks with copy and pasting from my editor? It is hard to format the code in the forum itself.

    Best,
    Jin Yi

    0

Please sign in to leave a comment.