简体   繁体   中英

possibles permutations between 2 list of tuples (Scheduling algoritm)

I'm trying to solve a particular problem with permutations using python.

In general terms, I have a list with employees ID, a list with the shifts available and a list with the nums of days like this:

employees = [1, 5,34,234,233,534,33,2,11,3,7,6,8]

days = [0,1,2,3,4]

shift = [0,1,2]

I do the possible permutations between these three lists using these lines of code:

lists = [days, shift, days]
Compbinations  = list(itertools.product(*lists))

So far everything is fine but I have another problem... I Need to assign the possible permutations to the corresponding days based on availability.

I have this array with the availability per day per turn:

Shift_Availabilities = [
(1,2,3),
(3,3,3),
(0,1,2),
(3,1,2),
(0,0,1)
]

But I don't know how I can do the possible permutations based on this constraint.

Any idea how I can do this?

Thanks!

If you have conditions to apply, step away from itertools and just make a list comprehension... they are incredibly flexible in terms of conditions, etc. This will develop "valid" assignment options based on some made up criteria.

emp_ids = [55, 202, 70]
days    = list('MTWRF')
shifts  = [1, 2, 3]
emp_avail = { 55: ['M', 'T'],
              202: ['T', 'W', 'R', 'F'],
              70: ['W', 'R', 'F']}
shifts_avail = {'M': [1, 2],
                'T': [3],
                'W': [2, 3],
                'F': [1]}

poss_assignments = [(emp, day, shift) 
        for emp in emp_ids 
        for day in days 
        for shift in shifts 
        if day in emp_avail.get(emp, []) # employee avail on day
        and shift in shifts_avail.get(day, [])]  # shift is active on day


for asmt in poss_assignments:
    print(asmt)

Yields:

(55, 'M', 1)
(55, 'M', 2)
(55, 'T', 3)
(202, 'T', 3)
(202, 'W', 2)
(202, 'W', 3)
(202, 'F', 1)
(70, 'W', 2)
(70, 'W', 3)
(70, 'F', 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM