2022-02-28

Formatting to get desired constraint in python-constraint

This is a constraint problem being solved with python-constraint. I have 10 hours with the following constraint: no 2 activities/tasks can occur at the same 1hr slot. I have 9 activities to be taken by 9 hours of the schedule. Then I have 3 tasks, 'x', 'y', and 'z'. I want one of those tasks to be chosen for the other 1hr slot.

This is what I have. Here I am adding the 9 activities as variables:

problem.addVariables(['Activity1','Activity2', 'Activity3', 'Activity4', 'Activity5', 'Activity6', 'Activity7', 'Activity8', 'Activity9'], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

Here I am trying to add one of the three tasks as a variable:

d = {'x': 1, 'y': 2, 'z': 3}
for k,v in d.items():
    problem.addVariables(["%s" %(k)], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

Here is for adding the constraint:

problem.addConstraint(AllDifferentConstraint(),['Activity1','Activity2', 'Activity3', 'Activity4', 'Activity5', 'Activity6', 'Activity7', 'Activity8', 'Activity9', "%s" %(k)])

Line to get solution:

solution = problem.getSolutions()

This is one of the outputs for the schedule, formatted as a pandas series of solution:

Activity4       1
x               2
Activity7       3
Activity3       4
Activity2       5
Activity1       6
Activity8       7
Activity9       8
Activity6       9
Activity5       10
y               10
z               10

How can I get just one of the tasks to show up in the output? I know this code is working for the one task assigned, in this case x, but then y and z were still added to the end, violating the constraint for just 1 thing to be assigned to each hour. I don't want y and z to show up in this example output because x was supposed to be the one task chosen.



No comments:

Post a Comment