0

I am trying to maximize a sequence of shifts for 5 workers in a year. Let's say I have 3 shifts(1, 2, 3) and break(B). Then I get as an input a sequence of shifts, let's say I get '111222333BBBBBB'.

What I need to do with it is maximize the number of appearences of that sequence in my workers schedule. I definied my workers schedule as shifts[(w, d, s)] = 1 meaning worker w in day d works the shift s.

What I tried to do: create bools (w_d) meaning for worker w the sequence will start in day d. Then I try to maximize the number of positive bools.

The problem: This takes too long and doesn't stop running even after 1 day, I did set the number of cores to 8. If anyone got a better idea on how to do this, please let me know!

The code:

worker = 5
days = 365
required_sequence_bools = []
required_sequence = "111222333BBBBBB"
for w in range(worker):
    required_sequence_bools.append([])
    for d in range(1, days - len(required_sequence)):
        required_sequence_bools[w].append(model.NewBoolVar(f"{w}_{d}"))

for w in range(worker):
    for d in range(0, days - len(required_sequence) - 1):
        day = d + 1
        for letter in required_sequence:
            if letter == '1':
                model.Add(shifts[(w, day, 0)] == 1).OnlyEnforceIf(required_sequence_bools[w][d])
            elif letter == '2':
                model.Add(shifts[(w, day, 1)] == 1).OnlyEnforceIf(required_sequence_bools[w][d])
            elif letter == '3':
                model.Add(shifts[(w, day, 2)] == 1).OnlyEnforceIf(required_sequence_bools[w][d])
            elif letter == 'B':
                model.Add(shifts[(w, day, 3)] == 1).OnlyEnforceIf(required_sequence_bools[w][d])
            day += 1
model.Maximize(sum(required_sequence_bools[w][d] for w in range(worker) for d in range(0, days - len(required_sequence) - 1)))

Example: required_sequence="112233BB" 4 workers

        | day1 | day2 | day3 | day4 | day5 | day6 | day7 | day8 | day9 | day10 | day11| day12 | day13 | day14 |
|worker1| 1      1      2      2     3       3      B      B
|worker2|               1      1     2       2      3      3       B       B
|worker3|                            1       1      2      2       3       3       B      B
|worker4|                                           1      1       2       2       3      3      B      B
ARE MERE
  • 21
  • 4
  • I wasn't able to fully understand your problem statement. Can you please explain what does it mean to "maximize the number of appearances of a sequence"? or even better, give a small example of inputs and outputs (say for a week, not for a year). I also assume there are some other constraints, e.g.: all shifts have to filled in every day(?) – etov Apr 08 '21 at 12:55
  • Yes there are many more constraints that need to be fulfilled. What I was trying to say is that with all other constraints fullfilled then I try to maximize that the workers work the shifts in a specific way that is given in the input. Say for example the required sequence is "112233BB" then for the output after the other constraints are fulfilled, I want to have maximum appearances of of that sequence in my workers schedule. So for like 2 weeks it should look like the example I added in the question. The days where I dont have something written are filled so that the constraints are respected – ARE MERE Apr 16 '21 at 08:30

0 Answers0