-
Notifications
You must be signed in to change notification settings - Fork 0
/
slot_agent_classes.py
90 lines (73 loc) · 2.5 KB
/
slot_agent_classes.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# Note: everything is 0-indexed!
class Slot:
"""
Class to track a slot and its preferences.
"""
N = 0
K = 0
def __init__(self):
self.preferences = [None] * (Slot.N + 1)
self.proposals = []
self.current_match = None
def note_candidate(self, cand_id, ranking):
"""
Note agent by how they rank you.
"""
if not self.preferences[ranking]:
self.preferences[ranking] = []
self.preferences[ranking].append(cand_id)
def consolidate_prefs(self):
"""
Consolidate prefs into a final list, then map.
"""
self.final_prefs = []
self.pref_mappings = [None] * Slot.N
for cand_lst in self.preferences:
self.final_prefs.extend(cand_lst) if cand_lst else None
for i in range(Slot.N):
self.pref_mappings[self.final_prefs[i]] = i
def receive_offer(self, cand_id):
"""
Receive an offer from an agent.
"""
self.proposals.append(cand_id)
if not self.current_match:
self.current_match = cand_id
elif self.pref_mappings[cand_id] < self.pref_mappings[self.current_match]:
self.current_match = cand_id
class Agent:
"""
Class to track an agent and their preferences.
"""
N = 0
K = 0
def __init__(self, cand_series):
"""
Read in the series of preferences for that agent into a list.
"""
self.unassigned_prefs = set(range(Agent.N))
self.final_prefs = [x[1] for x in cand_series.iteritems()]
for elem in self.final_prefs:
self.unassigned_prefs.remove(elem)
def consolidate_prefs(self):
"""
Assign whatever (N - K) de-preferred slots remain using an approximation of nearby slot 'goodness', then map
"""
for elem in self.unassigned_prefs:
self.final_prefs.append(elem)
self.pref_mappings = [None] * Agent.N
for i in range(Agent.N):
self.pref_mappings[self.final_prefs[i]] = i
self.pref_queue = list(self.final_prefs)
self.pref_queue.reverse()
def notify(self, slots, id):
"""
Update the slots with this agent's information.
"""
for i in range(Agent.N):
slots[self.final_prefs[i]].note_candidate(id, i + 1)
def propose(self):
"""
Propose to the highest-ranked, available slot.
"""
return self.pref_queue.pop()