When using the Gurobi optimizer, if a time limit is specified, can I still retreive the best solution that Gurobi has got so far? - gurobi

The Gurobi optimizer provides a time limit parameter. Given an optimization problem, Gurobi would stop when the specified time limit is reached. Is it possible to retrieve the current best solution that Gurobi has found so far?

If Gurobi reaches the time limit it will return the status code TIME_LIMIT. However, if a solution was found so far (you can check this via the SolCount attribute), you can retrieve the values of the current best feasible solution via the X attribute.

Related

GUROBI only uses single core to setup problem with cvxpy (python)

I have a large MILP that I build with cvxpy and want to solve with GUROBI. When I give use the solve() function of cvxpy it take a really really really long time to setup and does not start solving for hours. Whilest doing that only 1 core of my cluster is being used. It is used for 100%. I would like to use multiple cores to build the model so that the process of building the model does not take so long. Running grbprobe also shows that gurobi knows about the other cores and for solving the problem it uses multiple cores.
I have tried to run with different flags i.e. turning presolve off and on or giving the number of Threads to be used (this seemed like i didn't even for the solving.
I also have reduce the number of constraints in the problem and it start solving much faster which means that this is definitively not a problem of the model itself.
The problem in it's normal state should have 2200 constraints i reduce it to 150 and it took a couple of seconds until it started to search for a solution.
The problem is that I don't see anything since it takes so long to get the ""set username parameters"" flag and I don't get any information on what the computer does in the mean time.
Is there a way to tell GUROBI or CVXPY that it can take more cpus for the build-up?
Is there another way to solve this problem?
Sorry. The first part of the solve (cvxpy model generation, setup, presolving, scaling, solving the root, preprocessing) is almost completely serial. The parallel part is when it really starts working on the branch-and-bound tree. For many problems, the parallel part is by far the most expensive, but not for all.
This is not only the case for Gurobi. Other high-end solvers have the same behavior.
There are options to do less presolving and preprocessing. That may get you earlier in the B&B. However, usually, it is better not to touch these options.
Running things with verbose=True may give you more information. If you have more detailed questions, you may want to share the log.

The gap still keeps 100% in running Gurobi model

I am solving a mixed-integer linear programming model on Python platform using the solver Gurobi. However, The gap of the model still keeps 100 % during the long-running time.
I am trying to use the codes to limit the running time. The solution is found, but I don't know why the gap in the model doesn't reduce.
I have attached the file below.
Could you help me to fix this problem?
Thank you very much!
The gap is defined as:
gap = |bestfound - bestbound|/|bestfound|
(see the Gurobi documentation).
The best bound in your screenshot stays at 0, so the gap does not change and remains 100%.
You could also add a small objective offset to enforce a non-zero dual bound. This will enable a more expressive gap. You could use the ObjCon model attribute to achieve this.

Gurobi resume optimization after model modification

As far as i know Gurobi resumes optimizing where it left after calling Model.Terminate() and then calling Model.Optimize() again. So I can terminate and get the best solution so far and then proceed.Now I want to do the same, but since I want to use parts of the suboptimal solution I need to set some variables to fixed values before I call Model.Optimize() again and optimize the rest of the model. How can i do this so that gurobi does not start all over again?
First, it sounds like you're describing a mixed-integer program (MIP); model modification is different for continuous optimization (linear programming, quadratic programming).
When you modify a MIP model, the tree information is no longer helpful. Instead, you must resolve the continuous (LP) relaxation and create a new branch-and-cut tree. However, the prior solution may still be used as a MIP start, which can reduce the solve time for the second model.
However, your method may be redundant with the RINS algorithm, which is an automatic feature of Gurobi MIP. You can control the behavior of RINS via the parameters RINS, SubMIPNodes and Heuristics.

Optaplanner termination strategy

In the opta planner configuration ,there is a provision to specify the termination time out.
Is there a better way to handle the termination time out strategy? For example , my problem size is small and I have set the termination time out as 10 sec.
But I can see from the logs that the best score is obtained well within 2 - 3 seconds. Is there any means to exit once the best score is reached ?
Or should the program always run till the timeout is reached and then output the best score.
Take a look at the Termination chapter in the OptaPlanner documentation.
What you are referring to is called BestScoreTermination but it might not be what you actually want -- do note that OptaPlanner has no way of knowing if the score is "the optimal score"... unless you configure Exhaustive Search (which doesn't scale well).
Therefore, if you misjudge your problem and set the BestScoreTermination to something "better" than the optimal value, OptaPlanner will run until it tries out all combinations (which might take effectively forever on big problems). If you're looking for a compromise, take a look at "termination composition"

Gurobi Optimizer: determining feasibility without optimizing the model

In Gurobi, is it possible to see if a group of constraints and variables are feasible without actually optimizing the problem? It seems if the objective is a constant, Gurobi still does a lot of heavy computation to find an optimal solution, which I don't need!
Sorry. Except for very easy infeasibilities that can be detected in the presolver, Gurobi may have to do lots of work to prove whether the model is feasible or infeasible. No silver bullets here.
I am not sure if there is a better way to do this, but you can use either solution limits or time limits to stop the solver once a condition is met.
For example:
model.Params.SolutionLimit = n stops the solver once n solutions have been found.
model.Params.SolutionLimit = n stops the solver once it has run for n seconds too.
You can look at this gurobi parameters page for more info here