Soft constrain #2488
-
|
Greetings to eveyone But I can not find a way to add a soft constrain in scheduling problem, from what I understand I should crate a new boolean var, has explained. That is true only in the soft constrain is violated: Here is a example: from ortools.sat.python import cp_model
# Creates the model.
model = cp_model.CpModel()
# Creates the variables.
num_vals = 3
x = model.NewIntVar(0, 10 , 'x')
y = model.NewIntVar(0, 10 , 'y')
z = model.NewIntVar(0, 10 , 'z')
var = model.NewBoolVar('var')
var == (x <= 5) ### I am having trouble with this definition, how to assign true at var if x is less or equal to 5
# Create the constraints.
model.Add(x != y)
model.Add(y != z)
model.Add(x != z)
model.Minimize( x + y + z+ var*10 )
# Create a solver and solve.
solver = cp_model.CpSolver()
solution_printer = VarArraySolutionPrinter([x, y, z])
status = solver.Solve(model)
print('Status = %s' % solver.StatusName(status))
#print('Number of solutions found: %i' % solution_printer.solution_count())
var1=[x, y, z, var]
for v in var1:
print(solver.Value(v))Thank you for your time |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
See: https://github.com/google/or-tools/blob/stable/ortools/sat/doc/channeling.md model.Add(x <= 5).OnlyEnforceIf(var)
model.Add(x > 5).OnlyEnforceIf(var.Not()) |
Beta Was this translation helpful? Give feedback.
See: https://github.com/google/or-tools/blob/stable/ortools/sat/doc/channeling.md
but basically: