-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstable_matcher.py
35 lines (26 loc) · 1017 Bytes
/
stable_matcher.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import slot_indiv_classes
def stable_matcher(slots, individuals, N):
"""
Input: slots with their preferences and candidates with their preferences. Matches to be interviewee-optimal using Gale-Shapley algorithm.
"""
proposals_queue = list(range(N))
slots_queue = set()
while proposals_queue:
# proposal stage
for cand_id in proposals_queue:
slot = individuals[cand_id].propose()
slots[slot].receive_offer(cand_id)
slots_queue.add(slot)
proposals_queue = []
# rejection/maybe stage
for s in slots_queue:
slot = slots[s]
for candidate in slot.proposals:
if slot.current_match != candidate:
proposals_queue.append(candidate)
slot.proposals = [slot.current_match]
slots_queue = set()
final_ans = [None] * N
for i in range(N):
final_ans[i] = slots[i].current_match
return final_ans