Slow rate of adding large number of constraints
進行中Hi, I am trying to add a large number of constraints to my model. The problem is an electric vehicle charging coordination problem where I need to add constraints for each time slot, for each vehicle for each scenario (solar generation scenario). The shape of my variable is (100, 300, 48) which results in > 1mil constraints.
I am facing difficulties in adding the constraints, particularly the performance is very slow. Below is the code used:
for s in range(nbScenarios):
# time the loop
start = time.time()
for i in range(nbStations):
expr = SOC[s, i, tHin[i]]
for t in range(nbTime):
if t <= tHin[i]:
model.addConstr(SOC[s, i, t] == SOC_vec[i])
if t > tHin[i] and t <= tHout[i]:
model.addConstr(
SOC[s, i, t]
== SOC[s, i, t - 1]
+ DayAheadChargingPower[s, i, t] * timeInterval / BatteryCapacity[i]
)
if t > tHout[i]:
model.addConstr(SOC[s, i, t] == SOC[s, i, tHout[i]])
expr += DayAheadChargingPower[s, i, t] * timeInterval / BatteryCapacity[i]
model.addConstr(expr == 1)
-
正式なコメント
This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
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 -
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 Yi0
投稿コメントは受け付けていません。
コメント
3件のコメント