Does mathopt support warmup start for the second solve in this situation? #4151
-
Hello, from ortools.math_opt.python.mathopt import Model, solve, SolverType, fast_sum
model = Model()
n=100
setIJ = {(i,j) for i in range(n) for j in range(n)}
x = {(i,j): model.add_integer_variable(lb=0.0, name=f"x_{i}_{j}") for (i,j) in setIJ}
for i in range(n):
model.add_linear_constraint(fast_sum(x[i,j] for j in range(n)) >=1)
print('first solve')
model.minimize(0)
solve(model, SolverType.CP_SAT)
print('second solve')
model.minimize(fast_sum(x[i,j] for (i,j) in setIJ))
solve(model, SolverType.CP_SAT) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
MathOpt supports incremental solving. But instead of calling math.solve(), you need to make an IncrementalSolver object: https://github.com/google/or-tools/blob/stable/ortools/math_opt/python/solve.py#L137 If you change n nonzeros between solves, mathopt will do O(n log n) work in python to send the data to C++ and then underlying solver. Updating the solver may require work, proportionate to model size/number of variables/number of constraints, this is solver dependent. A (not particularly minimal) example of this is here: https://github.com/google/or-tools/blob/stable/ortools/math_opt/samples/python/cutting_stock.py#L220 However, the amount of incrementalism you are going to get to some extent depends on the underlying solver and the problem class. CP-SAT in particular has no incremental support in mathopt and there is no difference with solving from scratch. In general, LP tends to benefit more than MIP from solving incrementally, but every problem is different. For MIP with SCIP or Gurobi, when solving incrementally, you will save on copying the model from python to c++ (small), initializing the solver (small), and solutions (and some cuts) you found will get reused in the second solve. I think Gurobi does a little bit extra, not sure. Generally the impact of this is not large, but it can be when feasibility is hard. You can get much of the benefit by providing the solution(s) to the first solve as hint(s) to the second solve (see ModelSolveParameters, an argument to solve()). SCIP, Gurobi, and CP_SAT all support hint. |
Beta Was this translation helpful? Give feedback.
MathOpt supports incremental solving. But instead of calling math.solve(), you need to make an IncrementalSolver object:
https://github.com/google/or-tools/blob/stable/ortools/math_opt/python/solve.py#L137
If you change n nonzeros between solves, mathopt will do O(n log n) work in python to send the data to C++ and then underlying solver. Updating the solver may require work, proportionate to model size/number of variables/number of constraints, this is solver dependent.
A (not particularly minimal) example of this is here:
https://github.com/google/or-tools/blob/stable/ortools/math_opt/samples/python/cutting_stock.py#L220
However, the amount of incrementalism you are going to get to some…