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:
Xis a binary variablewis the worker indext1andt2are task indexesj1andj2are the positions of the tasks in the sequencepreced_taskis 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_taskkeys are (1, 2, ..) and I assumeXindex 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_taskwhich gives you only those keys and values that you need and then for the desiredj1andj2indices coming frompreced_task, you can create your constraint. – EhsanK Nov 28 '19 at 15:56Xis a decision variable and its index is based ont,wandj. All of them start from 1. – campioni Dec 01 '19 at 13:42x. That was just a check to make sure the index problem is not coming fromxbeing defined on a different range. Anyway, it seems you figured out a solution. – EhsanK Dec 01 '19 at 14:56