MIP modelling using matrix-form variables/constraints
AnsweredHI! Recently I built a MILP model and planned to solve it using Gurobi. However the solving takes a really really long long time though it's a quite small-scale and simple model.
I'm wondering if I use MVar to create variables in 1-D or 2-D array form, and write the constraints in matrix form, will this help a little? I don' think there are many sparse matrices involved in my model. So just want to check if it's worthy for me to rewrite the code.
My original version of addVar and addConstr:
for t in T:
for tb in TB:
energy[(t,tb)] = m.addVar(lb=0, ub=gb.GRB.INFINITY)
profit[(t,tb)] = m.addVar(lb=0, ub=gb.GRB.INFINITY)
for t in T:
for tb in TB:
m.addConstr(profit[(t,tb)], gb.GRB.EQUAL, energy[(t,tb)] * data_EPrice[(t)]['Price'])
The "matrix-form" variables and constraints I'm planning to make:
energy = m.addMVar(nTime, lb=0, ub=gb.GRB.INFINITY, name='Energy')
profit = m.addMVar(nTime, lb=0, ub=gb.GRB.INFINITY, name='Profit')
m.addConstr(profit == energy * np.array(data_EPrice,nTB,axis=1))
The key question is if this will fasten the solving process. The numbers of vars and constraints are quite small so the time of building model and data inputting is OK even if I use "for t in T" all the time.
Many thx.
-
Switching to the Python Matrix API would only affect the model construction time but since as you said, you model is quite small, this should not be an issue. An effect using the Python Matrix API might have is that different ordering of constraints/variables can affect solver's performance.
You could try out different parameters, running the Gurobi tuning tool, and maybe rethink the final MIPGap value you need in the case that the optimizer struggles to reach a very low MIPGap in considerable time.
Best regards,
Jaromił0
Please sign in to leave a comment.
Comments
1 comment