Reputation: 405
I would schedule shifts in a way that people works in pair.
1. The principal constraint is that from time to time each person should not work with the person which he have worked in the previous shift.
2. There aren't constraint about the time of the shifts, I just need to match a pair with a day.
For example if {A,B.. F} representing people then
Day 1: A-B
Day 2: C-D
Day 3: E-F
Day 4: A-B <--- Wrong, because so (1.) is violated
My solution is:
Define a fitness function T, so then T(A) is equal to 0 if A worked today or T(A)=c if A worked for the last time c days ago.
The steps would be:
There are a better way to solve a problem? There is something similar in literature so I can consult? Like similar problems for examples? Is the nurse shitfs problems similar to this?
Thank you.
Upvotes: 1
Views: 955
Reputation: 58310
Your problem is underspecified, leading to trivial solutions. For example, pick any three people, {A, B, C}, and schedule AB, AC, BC (repeating).
If you want a fairer solution: keep picking a random pair each day until you find a viable pair. There's at most N non-viable pairs and N(N-1)/2 possible pairs.
Here's one way to do it:
import random
def schedule(folk):
excluded = dict((p, None) for p in folk)
while True:
while True:
a, b = random.sample(folk, 2)
if excluded[a] != b and excluded[b] != a:
break
excluded[a], excluded[b] = b, a
yield a, b
for a, b in schedule('ABCDE'):
print '%s%s' % (a, b),
Upvotes: 1