jeudi 16 décembre 2021

Finding the optimal combination from a list of lists that maximally disperses repeating elements

I have a list of lists, where each individual list represents which people are available to take a work schedule shift. My actual data is for a list of 50 shifts:

[['P3', 'P2', 'P4', 'P5'], ['P1', 'P3', 'P2', 'P4'], ['P1', 'P3', 'P2', 'P4'], ['P1'], ['P5'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P3', 'P2', 'P4', 'P5'], ['P3', 'P2', 'P4', 'P5'], ['P2', 'P5'], ['P1', 'P2', 'P5'], ['P1', 'P2', 'P5'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P5'], ['P2'], ['P1', 'P3', 'P4', 'P5'], ['P1', 'P3', 'P4', 'P5'], ['P1', 'P3', 'P4', 'P5'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P2', 'P4'], ['P1', 'P3', 'P2', 'P4'], ['P1', 'P3', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P4'], ['P1', 'P3', 'P2', 'P4'], ['P1', 'P2', 'P4'], ['P1', 'P2', 'P4', 'P5'], ['P1', 'P2', 'P5'], ['P1', 'P3', 'P2', 'P5'], ['P1', 'P3', 'P2', 'P5'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P2'], ['P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P5', 'P2', 'P4'], ['P1', 'P3', 'P2', 'P4'], ['P1', 'P3', 'P2', 'P4']]

I am trying to end in a final list of 50 (i.e., ['P3', 'P2', 'P4', 'P1', 'P5', 'P2', ...] with either one of two criteria:

Option A: Repeating elements are maximally dispersed

Option B: Each repeating element is at a minimum spaced apart by 2 index spaces, i.e. that ['P1', 'P1'] or ['P1', 'P2', 'P1] are not acceptable, but ['P1, 'P2', 'P3', 'P1'] is ok.

I don't know how to approach Option A tbh. For Option B, here is what I have so far:

AssignList = []
for ix, x in enumerate(AvailableList):
    randx = random.choice(x)
    if len(AssignList)==1:
        while randx==AssignList[0]:
            randx=random.choice(x)
    if len(AssignList)>1:
        while (randx==AssignList[-1]) | (randx==AssignList[-2]):
            randx=random.choice(x)
    AssignList.append(randx)
print(AssignList)

But the problem with my approach is I think it reaches some lists where there is one choice that causes an infinite loop. Any tips for either approach much appreciated!




Aucun commentaire:

Enregistrer un commentaire