Treating Gurobi as a local solver?
AnsweredHi all,
As Gurobi is a global solver, is it possible to make Gurobi solve a problem and stop when it finds a local optimum by e.g., adding model.Param?
I intend to make an iterative algorithm using the output of the local optima. I know there are a few local solvers out there, but I am too comfortable modeling with Gurobi :)
Thank you,
Best, Panggah.
-
Hi Panggah,
You can use the SolutionLimit parameter to tell Gurobi to terminate after SolutionLimit=N solutions have been found.
Best regards,
Jaromił0 -
Hi, Jaromił. Thank you for your reply.
For now, I have a continuous nonlinear problem with multiple bilinear terms. I believe that this problem may have multiple local optima. However, the SolutionLimit parameter states that it only affects MIP problem. Is there other parameters that I can set?
Please take a look at the reproducible example code below:import numpy as np
import gurobipy as gp
from gurobipy import GRB, tuplelist
idx_x = np.arange(1,9,1)
set_x = tuplelist([i for i in idx_x])
def define_variables(m):
# X
x = m.addVars(set_x, name='x')
x[1].LB, x[1].UB = 100, 10000
for i in [2,3]:
x[i].LB, x[i].UB = 1000, 10000
for i in [4,5,6,7,8]:
x[i].LB, x[i].UB = 10, 1000
return x
def define_objective(m,x):
sum_x = sum(x[i] for i in [1,2,3])
m.setObjective(sum_x, GRB.MINIMIZE)
def nonlinear_constraints(m,x):
m.addConstr(0.0025*(x[4] + x[6]) - 1 <= 0)
m.addConstr(0.0025*(-x[4] + x[5] + x[7]) - 1 <= 0)
m.addConstr(0.01*(-x[5] + x[8]) - 1 <= 0)
m.addConstr(100*x[1] - x[1]*x[6] + 833.33252*x[4] - 83333.333 <= 0)
m.addConstr(x[2]*x[4] - x[2]*x[7] - 1250*x[4] + 1250*x[5] <= 0)
m.addConstr(x[3]*x[5] - x[3]*x[8] - 2500*x[5] + 1250000 <= 0)
## Nonlinear - nonconvex
m = gp.Model('NonLinear')
x = define_variables(m)
m.update()
define_objective(m,x)
nonlinear_constraints(m,x)
#m.Params.SolutionLimit = 1
m.optimize()
print('Objective value =', m.getObjective().getValue())Please let me know if there is a mistake.
Thank you. Best,
Panggah0 -
Hi Panggah,
If your model is recognized to be convex, then it has only one optimal solution anyway. If Gurobi does not recognize your model to be convex, then Gurobi treats the model as a MIP, i.e., the SolutionLimit parameter applies.
For more information about non-convexity in Gurobi I recommend having a look at our Tech Talk about Nonconvexity.
Best regards,
Jaromił0 -
Hi Jaromił,
To compare with the previous example (problem A), I implemented another example (problem B) below:
Both problems have the same log:
"Continuous model is non-convex -- solving as a MIP"However, there is a difference in the result:
- Problem A: "Solution count 1: 7049.25"
- Problem B: "Solution count 3: -1.08333 -0.75 0"
Then we can conclude that problem A has one local minimum as a global minimum, and problem B has 3 local minima. Thus, the parameter SolutionLimit=1 is only applicable to the problem B.
Thank you for your advice and have a good day!
Best, Panggah.0 -
Then we can conclude that problem A has one local minimum as a global minimum, and problem B has 3 local minima. Thus, the parameter SolutionLimit=1 is only applicable to the problem B.
This is not fully correct.
Both problems were recognized as non-convex by Gurobi. Thus both models may have multiple local optima and the SolutionLimit parameter can be applied to both problems.
For problem A, it happens that Gurobi only finds one solution on its optimization path. For problem B, Gurobi finds 3 solutions on its optimization path.
Best regards,
Jaromił0 -
Hi Jaromił,
Thank you for the information.
Anyways, is it possible to set a starting point like a local solver does?Best, Panggah.
0 -
Hi Panggah,
You can provide a MIPstart for models with discrete variables. For nonconvex models, you can also use the PStart variable attribute.
Best regards,
Jaromił1
Please sign in to leave a comment.
Comments
7 comments