I would like to establish a precedence constraint to ensure the precedence relation of tasks. I have a set of tasks available, but required tasks are not always the same. It means, the tasks executed will depend of the demand.
When I searched for examples of precedence constraints in CPLEX/DOCPLEX and also OR-TOOLS, all examples I found use interval variables for establishing precedence constraints. In my case, I don't have interval variables, just binary ones.
I tried to implement the following precedence constraint:
for t1 in required_tasks:
for t2 in required_tasks:
for w in range(1, len(task_cost) + 1):
for j1 in range(1, number_tasks + 1):
for j2 in range(1, number_tasks + 1):
if j1 < j2:
if t2 in preced_task[t1]:
opt_model.add(
X[t1, w, j1] *
precedence[t1, t2]
>= X[t2, w, j2]
)
Some definitions:
X
is a binary variablew
is the worker indext1
andt2
are task indexesj1
andj2
are the positions of the tasks in the sequencepreced_task
is a dictionary containing the precedence relation.
Example: In this case, task 2 must be done after task 1, task 3 must be done after task 2. Task 1, 4, etc do not require any tasks before them.
preced_task = {1: [], 2: [1], 3: [2], 4: [], 5: [], 6: [], 7: []}
When I consider j1
and j2
in range(1, number_tasks + 1)
it does not return a solution. However, if I consider j1
and j2
in range(2, number_tasks + 1)
it returns a solution respecting precedence for one task (2, for example), but not for task 3.
Could someone help me with that?
preced_task
keys are (1, 2, ..) and I assumeX
index starts from 0. Are you sure then you're correctly indexingX
? Also, this is just a suggestion about implementing your constraint. You have 5 loops and you only have a handful of values inpreced_task
. So, I suggest changing the order of your loops. Loop over yourpreced_task
which gives you only those keys and values that you need and then for the desiredj1
andj2
indices coming frompreced_task
, you can create your constraint. $\endgroup$X
is a decision variable and its index is based ont
,w
andj
. All of them start from 1. $\endgroup$x
. That was just a check to make sure the index problem is not coming fromx
being defined on a different range. Anyway, it seems you figured out a solution. $\endgroup$