Consequential task assignments with possible soft constraint #2266
-
Hi, I am modeling a flexible job-shop problem that plans X jobs consisting of two tasks whilst taking job deadlines into account. Possible allocation of jobs to N suitable workers is done prior to modeling, and I am using the CP-SAT solver in Python.
Thanks in advance, I am curious to see some ideas! Kind regards, |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 12 replies
-
Are you using OptionalIntervalVar? |
Beta Was this translation helpful? Give feedback.
-
Yes, from ortools.sat.python import cp_model
model = cp_model.CpModel()
workers = range(3)
tasks = range(2)
prefer_task1 = {
# if worker 0 does task 0 prefer worker 1 for task 1
0: [1],
1: [2],
2: [0, 1],
}
assigments = {(t, w): model.NewBoolVar("") for t in tasks for w in workers}
# 1 worker per task
for t in tasks:
model.Add(sum(assigments[t, w] for w in workers) == 1)
pref_booleans = []
for w0, prefs in prefer_task1.items():
tmp = model.NewBoolVar("")
# worker does task[0]
model.AddImplication(tmp, assigments[tasks[0], w0])
# a prefered worker does task[1]
model.AddBoolOr([tmp.Not()] + [assigments[tasks[1], w1] for w1 in prefs])
pref_booleans.append(tmp)
# Test
# model.Add(assigments[0, 0] == 1)
model.Maximize(sum(pref_booleans))
solver = cp_model.CpSolver()
solver.Solve(model)
for task in tasks:
for w in workers:
if solver.Value(assigments[task, w]):
print("Task", task, "Worker", w) |
Beta Was this translation helpful? Give feedback.
Are you using OptionalIntervalVar?
If so, you can create a new boolean that is true iff worker1 does task1 and workerx does task2, and maximize those booleans in the objective