1

I am using CP-SAT solver from ortools https://developers.google.com/optimization/cp/cp_solver

I am executing the solver with a callback object

solver = cp_model.CpSolver()
solution_agg = SolutionCollector(data, self.variables, self.products, self.vehicles)
status = solver.SearchForAllSolutions(self.model, callback=solution_agg)

Solution agg is supposed to filter out all solutions that have some wrong assignments, I could not model these as linear inequalities.

What I know is that generated solutions can be converged faster and the "hits" on the verifier can be made less. If I can add constraints on-the-go inside the callback.

I tried to do this inside the callback, adding a constraint to look for solutions in lesser volume than minimum volume till now.

self.__model.Add(volume_expression <= min_found_yet)

This doesn't give error, but the number of times the verifier rejected a solution is still same.

Is it possible to form constraints while solving? If not in Ortools, then any other solver which provides?

Trion
  • 500
  • 5
  • 11

1 Answers1

2

Not directly. The solver is stateless and read the cp_model once at the start of the solve.

What you are describing seems just a minimization property. Why don't you just minimize volume_expression?

Laurent Perron
  • 8,594
  • 1
  • 8
  • 22
  • I wanted to but SearchForAllSolutions doesn't support any minimization. I need all the feasible solutions so that function was most suitable. Can I get all feasible solutions that minimize the expression via the Solve method of CP SAT or MIP solver? – Trion Mar 12 '19 at 13:47
  • 3
    If you solve with an objective and a solution callback, you will collect all solutions found, which may be only one. Now, if you minimize the objective, store the optimal value, then search for all solutions of a model without any objective, but with a constraint on the volume_expression, you will find all solutions close to the optimal. – Laurent Perron Mar 12 '19 at 14:08