pythonconstraintsconstraint-programmingor-tools

Are multiple objectives possible? (OR-TOOLS Constraint Programming)


I have a problem where I have a set of warehouses with a given production capacity that send some product to a list of customers at a given cost. I'm trying to minimize the total cost of sending the products so that each customer's demand is satisfied. That part is sorted.

Now I need to add a new objective (or constraint) where I try to satisfy all the clients demand at a minimum cost but also using the minimum number of warehouses possible. Say start with 5 warehouses, if the problem is impossible then try 6, 7, 8 etc. until a solution is found were I satisfy all the demand using the minimum number of warehouses possible.

How could I go about this using or-tool constraint programming module? Is it even possible? I've had a good look at the documentation but couldn't find any constraint or function that seemed to cater for this idea.


Solution

  • Solve with the first objective, constraint the objective with the solution, hint and solve with the new objective.

    from ortools.sat.python import cp_model
    
    model = cp_model.CpModel()
    solver = cp_model.CpSolver()
    x = model.NewIntVar(0, 10, "x")
    y = model.NewIntVar(0, 10, "y")
    
    # Maximize x
    model.Maximize(x)
    solver.Solve(model)
    print("x", solver.Value(x))
    print("y", solver.Value(y))
    print()
    
    # Hint (speed up solving)
    model.AddHint(x, solver.Value(x))
    model.AddHint(y, solver.Value(y))
    
    # Maximize y (and constraint prev objective)
    model.Add(x == round(solver.ObjectiveValue()))  # use <= or >= if not optimal
    model.Maximize(y)
    
    solver.Solve(model)
    print("x", solver.Value(x))
    print("y", solver.Value(y))
    

    Reference (github issue)