Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply a constraint if condition with OR-Tools CP Solver

Tags:

or-tools

I'm implementing a similar solution for the Job-Shop problem with one difference: I don't know the machine that has to perform each task. Solving that is a part of the problem too. We can say, in fact, that I'm trying to solve a combination of the Nurse Problem and the Job-Shop Problem.

More concretely, I have some tasks T with duration D that have to be performed by some specific employees E due to their nature N (let's say fron-end tasks, back-end, and so on) in a specific order O.

I have:

  • An array of T int vars that can acquire a value in the range of E (employee that's going to perform a task).
  • An array of fixed duration interval vars (to scheduling them knowing it's duration D).
  • E sequence vars which should be the sequence of tasks (interval vars) that an employee is going to perform.
  • Some constraints for the relation N between tasks and employees.
  • Some other constraints for the order O.

A way to solve this is: first solve the assignment problem, then schedule the tasks. I have achieved this.

However I want to implement it as an unique solution.

I am stuck at this: How to create a disjunctive constraint that depends on the int vars I created before?

To those who need to see code:

for i in range(number_employees):
    disj = solver.DisjunctiveConstraint([interval_var[task_id] if int_var[task_id] == i] ,'i_name')
   [...]

Of course, that doesn't work.

I would really appreciate any suggestion.

like image 308
Claudio Verdú Ruiz Avatar asked Jul 16 '18 08:07

Claudio Verdú Ruiz


People also ask

What is a constraint based solver?

Restrictions formulated as constraints make it possible to assign a value to an arbitrary variable. A constraint solver can propagate consequences of assignments to other variables and the order of variable assignments does not affect constraints.

What does or tools stand for?

Dismiss Got it. OR-Tools is open source software for combinatorial optimization, which seeks to find the best solution to a problem out of a very large set of possible solutions.

What is Pywraplp?

Module pywraplpThe class for constraints of a Mathematical Programming (MP) model. A constraint is represented as a linear equation or inequality. Expand source code. A constraint is represented as a linear equation or inequality. """


1 Answers

Should should have a look at the CP-SAT solver. It supports half-reified constraints.

That is, (in python):

model.AddNoOverlap([list of intervals]).OnlyEnforceIf(boolvar)

model.Add(x == i).OnlyEnforceIf(boolvar)

model.Add(x != i).OnlyEnforceIf(boolvar.Not())

See: https://github.com/google/or-tools/blob/master/ortools/sat/doc/index.md

like image 152
Laurent Perron Avatar answered Jan 02 '23 22:01

Laurent Perron