2

I am attempting to implement a simple CP-SAT where the objective is to minimize the largest value assigned across all decision variables. I can minimize any individual variable or a linear function of variables, but it seems I am unable to minimize the maximum of variables. Is there a way to achieve this? Perhaps a way to linearize a max() function?

Note: I do have constraints in my model, but I'm omitting them here as I do not believe they are relevant to my question.

from ortools.sat.python import cp_model

model = cp_model.CpModel()

num_vars = 50
variables = {}
for i in range(num_vars):
     variables[i] = model.NewIntVar(0,i,'n_%i'% i)

The following line always results in an error, as does alternative arguments, e.g., an iterator.

model.Minimize(max(variables))
Gord B
  • 51
  • 5
  • The second half of your question, where you describe how you fixed the error, can be used as an answer. – Zach Valenta Apr 25 '20 at 16:21
  • 1
    Sorry, I'm new to Stack Overflow :). Wasn't sure what the correct protocol was when I discover an answer to my own question. I've entered the answer below, instead of as an edit to the question. – Gord B Apr 26 '20 at 17:05

1 Answers1

3

I've discovered a solution to this problem. I needed to declare a new decision variable, representing the objective value, and then I needed an AddMaxEquality constraint, equating the new variable to the max of other decision variables. Finally, I pass the new objective variable to the model.Minimize() command.

obj = model.NewIntVar(0,num_vars,'obj')

# Impose a constraint equating the new variable to the max of other vars.
model.AddMaxEquality(obj, [variables[i] for i in range(num_vars)])

# Minimize objective.
model.Minimize(obj)
Gord B
  • 51
  • 5