Long Time Spend in Model Construction
AnsweredHello
Thanks for reading and thanks for your help, I really appreciate your suggestions.
I am solving the model with gurobi using Bender's Cutting Plane method. So I have a subproblem that needs to be solved multiple times, and an optimality cut needs to be added in every iteration.
When I am tracking the solving time, I notice that it takes a lot of time to construct the model of the subproblem while the m.runtime is small. For example, it can take 553.229 seconds to build the model and solve it while m.runtime is only 0.187 seconds. (The time for adding an optimality cut is also very long, like 598.925 seconds, I guess most time is used in constructing the expression). Might I know if is there any way to shorten the time used in this part?
I've tried to pre-calculate the coefficient of the subproblem objective coefficient with Python only rather than using gurobi quicksum. And it did help a little bit. However, I am wondering if there are any other ways that can further help shorten the time used in model construction. Would it be possible to rerun the subproblem with a different objective coefficient without reconstructing the model? (And similarly, would it be possible to add the optimality cur repeatedly with different coefficient?) Or are there any other ways?
Thanks for reading
Yin Can
-
Hi Yin Can,
Would it be possible to rerun the subproblem with a different objective coefficient without reconstructing the model?
Yes, typically this is the way you would do it. I guess you're constructing the dual form of the subproblem since you've mentioned the objective coefficient. How you modify the objective coefficients can depend on which API you are using with Gurobi, for example with gurobipy you can use Model.setAttr, or if you were using the guroipy Matrix API it may just be quicker to replace the objective, e.g. model.setObjective(newcoeffs@myvariables)
- Riley
0 -
Hello Riley
Thanks for your answer, it's really helpful and I really appreciate it.
I am using Python to get access to Gurobi, and I am trying to reset the model objective with the below code while obj is the updated obj expression.
mSub.setObjective(obj, GRB.MAXIMIZE)However, I got an error message as belowNameError: cannot access free variable 'delta_kit' where it is not associated with a value in enclosing scopeI guess the reason for this is because my subproblem was created with a self-defined function like def SubProblem():Then when I tried to reset the model objective function in 2nd iteration without rebuilding it, The variable wasn't saved and thus can not refer to it.If my guess is wrong, might I know what might be the reason for it?If yes, does it mean I need to create my subproblem globally If I want to rerun it later? If it's doesn't need to become global, what kind of object do I need to return in order to modify the objective only?Thanks for reading and thank you again for your helpYin Can0 -
Hi Yin Can,
Then when I tried to reset the model objective function in 2nd iteration without rebuilding it, The variable wasn't saved and thus can not refer to it.
Yes, you are correct.
does it mean I need to create my subproblem globally If I want to rerun it later? If it's doesn't need to become global, what kind of object do I need to return in order to modify the objective only?
I'm guessing you're returning the model from the SubProblem function? If you return the variables too then you will be able to assign them to a Python variable and you can then use them. Or alternatively you can use model.getVars() in the global scope to obtain a reference to the variables.
Are you using an iterative approach, which alternates between master and subproblem solves, or are you using a callback approach which uses MIPSOL callbacks to run the subproblem? If it is the latter you can use a callback class and it would make sense to store the subproblem model in the callback class.
- Riley
0 -
Hello Riley
Thank you very much for your help.
I am using an iterative approach and I am trying to return the subproblem variables together with the subproblem function. And now it works.
Thanks again for your effort, I really appreciate it
Yin Can
0 -
Hello Riley
Sorry to bother you again, but I just realized there is another issue, could you please help me with it?
So the issue is, I am using an iterative approach, I try to build the model "m" in the 1st iteration, and then in the following iteration, reset the objective function, and then rerun the model "m".
The code now is running when I return model "m" and variables
However, when I compare the objective value obtained from this approach, with rebuilding the model in every iteration. Their objective value is not the same, Which indicates it probably still has some issues, but I am not sure how it happened. Could you please help identify it?
Thanks for reading and sorry again for keep bothering you
Yin Can
0 -
Hi Yin Can,
This may only be an issue if the end result is different - is it different?
If the two approaches do end up with different results then I would try writing the models to LP files then comparing the two. Do they have the same number of variables and constraints? Are the coefficient statistics the same? Try to find some differences that gives you clues.
- Riley
0
Please sign in to leave a comment.
Comments
6 comments