-
Notifications
You must be signed in to change notification settings - Fork 0
/
scenarios.py
139 lines (108 loc) · 4.46 KB
/
scenarios.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import popularities as pop
from da import deferred_acceptance
from matplotlib.backends.backend_pdf import PdfPages
"""npPositions is the number of open positions in Ministries"""
nbPositions, nbStudents = 85, 80
"""
In each scenario:
- we let students apply to their top X choices
- we let positions keep their top Y applicants
"""
scenarios = [
("Scenario 1", int(0.15 * nbPositions), 8),
("Scenario 2", int(0.15 * nbPositions), nbStudents),
("Scenario 3", nbPositions, 8),
("Scenario 4", nbPositions, nbStudents),
]
def truncate(prefA, prefB, sz):
prA = [pr[:sz] for pr in prefA]
prB = [
[i for i in pr if j in prA[i]] # todo: not efficient
for j, pr in enumerate(prefB)
]
return prA, prB
if __name__ == "__main__":
logpop = pop.generate_logpop(nbPositions, nbStudents)
prefP1, prefS1 = pop.draw_profile(logpop)
with PdfPages("fig.pdf") as pdf:
for iScenario, (name, sz1, sz2) in enumerate(scenarios):
# truncate the preference of students
prefS2, prefP2 = truncate(prefS1, prefP1, sz1)
# truncate the preference of positions
prefP3, prefS3 = truncate(prefP2, prefS2, sz2)
# run deferred acceptance
matchP, matchS = deferred_acceptance(prefP3, prefS3)
#####################################
cmap = mpl.cm.viridis
norm = mpl.colors.Normalize(vmin=logpop.min(), vmax=logpop.max())
txt1 = "First, students apply to %d positions" % sz1
txt2 = "Then, positions keep at most %d applicants" % sz2
#####################################
plt.figure(figsize=(6, 5), tight_layout=True)
plt.title(name)
plt.imshow(logpop, origin="lower", norm=norm, cmap=cmap)
plt.xlabel("students")
plt.ylabel("positions")
plt.colorbar()
for s in range(nbStudents):
if matchS[s] == None:
plt.plot([s], [nbPositions], "r.", markersize=3)
else:
plt.plot([s], [matchS[s]], "r.", markersize=3)
for p in range(nbPositions):
if matchP[p] == None:
plt.plot([nbStudents], [p], "r.", markersize=3)
plt.xlim((-0.5, nbStudents + 0.5))
plt.ylim((-0.5, nbPositions + 0.5))
pdf.savefig()
plt.close()
#####################################
plt.figure(figsize=(10, 5), tight_layout=True)
ax = plt.subplot(1, 2, 1)
ax.title.set_text(txt1)
tmp = logpop.copy()
for p in range(nbPositions):
l = set(range(nbStudents)) - set(prefP2[p])
for s in l:
tmp[p, s] = None
if prefP2[p] == []:
plt.axhline(p, color="r")
plt.imshow(tmp, origin="lower", norm=norm, cmap=cmap)
plt.xlabel("students")
plt.ylabel("positions")
ax = plt.subplot(1, 2, 2)
ax.title.set_text(txt2)
for s in range(nbStudents):
l = set(range(nbPositions)) - set(prefS3[s])
for p in l:
tmp[p, s] = None
if prefS3[s] == []:
plt.axvline(s, color="r")
plt.imshow(tmp, origin="lower", norm=norm, cmap=cmap)
plt.xlabel("students")
plt.ylabel("positions")
pdf.savefig()
plt.close()
#####################################
plt.figure(figsize=(10, 3), tight_layout=True)
ax = plt.subplot(1, 2, 1)
ax.title.set_text(txt1)
YS = sorted([len(pr) for pr in prefS2], reverse=True)
YP = sorted([len(pr) for pr in prefP2], reverse=True)
plt.plot(YS, label="length of students' lists")
plt.plot(YP, label="length of positions' lists")
plt.ylim(bottom=0)
plt.legend()
ax = plt.subplot(1, 2, 2)
ax.title.set_text(txt2)
YS = sorted([len(pr) for pr in prefS3], reverse=True)
YP = sorted([len(pr) for pr in prefP3], reverse=True)
plt.plot(YS, label="length of students' lists")
plt.plot(YP, label="length of positions' lists")
plt.ylim(bottom=0)
plt.legend()
pdf.savefig()
plt.close()