-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulation.py
373 lines (320 loc) · 12.5 KB
/
simulation.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import pandas as pd
import numpy as np
import random as rnd
import json as json
import sys as sys
import da
import popularities as pop
"""convert a string rank to a numeric rank"""
def to_rank(rang_str):
if str(rang_str).isnumeric():
return int(str(rang_str))
else:
return -1
def select_wishes(pr, params):
nb_favorite = params["nb_favorite"]
nb_medium = params["nb_medium"]
nb_safe = params["nb_safe"]
min_rank_medium = params["min_rank_medium"]
min_rank_safe = params["min_rank_safe"]
# truncate the preference of candidates, (nb_favorite+nb_medium+nb_safe) wishes each
# nb_favorite "favorite" wishes
result = [pr[i] for i in range(nb_favorite)]
# nb_medium "medium" wishes, meaning with rank in the popularities order of jobs
# in the interval [min_rank_medium, min_rank_safe]
#
# remark that jobs in the model are sorted by decreasing popularity hence job_id
# is the rank in the popularity order
for i in range(nb_favorite + 1, len(pr)):
job_id = pr[i]
if not job_id in result and min_rank_medium <= job_id <= min_rank_safe:
result.append(job_id)
if len(result) >= nb_favorite + nb_medium:
break
# min_rank_safe "secure" wishes with with rank in the popularities order of jobs
# strictly above min_rank_safe
for i in range(nb_favorite + 1, len(pr)):
job_id = pr[i]
if not job_id in result and min_rank_safe < job_id:
result.append(job_id)
if len(result) >= nb_favorite + nb_medium + nb_safe:
break
return result
def select_auditions(job_id, nb_jobs, pref, prefC, params):
# more auditions for less popular jobs
# from 8 auditions for the most popular to 16 auditions for the least popular
nb_min_auditions = params["nb_min_auditions"]
auditions_nb = nb_min_auditions + (nb_min_auditions * job_id) // nb_jobs
# we add candidates who made a wishes, until the nb of auditions is reached
result = []
for i in range(len(pref)):
st_id = pref[i]
if job_id in prefC[st_id]:
result.append(st_id)
if len(result) >= auditions_nb:
break
return result
def truncate(prefJ, prefC, params):
prefCtrunc = [select_wishes(pr, params) for pr in prefC]
prefJtrunc = []
for job_id in range(len(prefJ)):
auditions = select_auditions(job_id, len(prefJ), prefJ[job_id], prefC, params)
prefJtrunc.append(auditions)
return prefJtrunc, prefCtrunc
def print_result(model, matchJ, matchC, prefJshort, prefCshort, params, nb_dev):
print("\n\n****************************************************************")
print("Paramètres du modèle")
percent = int(100 * params["nb_voeux"] / params["nb_jobs"])
print(
"\tLes candidats choisissent {} voeux (soit {}% du nombre de postes), ".format(
params["nb_favorite"] + params["nb_medium"] + params["nb_safe"], percent
)
+ "en sélectionnant leurs {} favoris plus {} voeux plus accessibles plus {} voeux de secours.".format(
params["nb_favorite"], params["nb_medium"], params["nb_safe"]
)
)
print(
"\tLes employeurs auditionnent pour chaque position entre {} et {} candidats, selon leur popularité.".format(
params["nb_min_auditions"], 2 * params["nb_min_auditions"]
)
)
print(
"\tA l'issue des classements des candidats auditionnés, les candidats sont affectés en utilisant l'algorithme de Gale-Shapley"
)
nb_cand = len(matchC)
nb_cand_unmatched = matchC.count(None)
nb_cand_matched = nb_cand - nb_cand_unmatched
nb_jobs = len(matchJ)
nb_jobs_unmatched = matchJ.count(None)
nb_jobs_matched = nb_jobs - nb_jobs_unmatched
rank_pref_match = [
None if matchC[id] == None else prefCshort[id].index(matchC[id])
for id in range(nb_cand)
]
matched_to_favourite = len(
list(filter(lambda rank: rank is not None and rank == 0, rank_pref_match))
)
matched_to_second = len(
list(filter(lambda rank: rank is not None and rank == 1, rank_pref_match))
)
matched_to_third = len(
list(filter(lambda rank: rank is not None and rank == 2, rank_pref_match))
)
matched_to_favourites = len(
list(filter(lambda rank: rank is not None and rank <= 2, rank_pref_match))
)
avg_match_rank = 1 + np.average(
list(filter(lambda x: x is not None, rank_pref_match))
)
rank_pref_match_j = [
None if matchJ[id] == None else prefJshort[id].index(matchJ[id])
for id in range(nb_jobs)
]
matched_to_favourite_j = len(
list(filter(lambda rank: rank == 1, rank_pref_match_j))
)
matched_to_favourites_j = len(
list(filter(lambda rank: rank is not None and rank <= 3, rank_pref_match_j))
)
avg_match_rank_j = np.average(
list(filter(lambda x: x is not None, rank_pref_match_j))
)
print("\n")
print("Adéquation préférences formations")
print(
"\tNombre de postes recrutant leur candidat préféré {}.".format(
matched_to_favourite_j
)
)
print(
"\tNombre de postes recrutant un de leurs trois premiers candidats préférés {}.".format(
matched_to_favourites_j
)
)
print(
"\tRang moyen dans les préférences des postes du candidat recruté {}.".format(
int(avg_match_rank_j)
)
)
print("\n")
print("Liste des employeurs et postes pourvus au premier tour:\n")
stats = {}
employers = model["employers_names"]
pops = model["employers_popularities"]
for name in employers:
stats[name] = 0
for job_id in range(nb_jobs):
is_matched = matchJ[job_id] != None
if is_matched:
name = model["jobs_names"][job_id]
stats[name] = stats[name] + 1
sorted_employers_ids = sorted(range(len(employers)), key=lambda x: -pops[x])
for employer_id in sorted_employers_ids:
nb_matched = stats[employers[employer_id]]
nb_to_match = model["employers_capacities"][employer_id]
name = employers[employer_id]
if nb_matched > 0:
print(
"'{}' \t: {} / {}.".format(name[:60].ljust(60), nb_matched, nb_to_match)
)
print("\n")
print("Liste des employeurs et postes à pourvoir au second tour:\n")
sorted_employers_ids = sorted(range(len(employers)), key=lambda x: -pops[x])
for employer_id in sorted_employers_ids:
nb_matched = stats[employers[employer_id]]
nb_to_match = model["employers_capacities"][employer_id] - nb_matched
name = employers[employer_id]
if nb_to_match > 0:
print(
"'{}' \t: {} position(s) à pourvoir.".format(
name[:60].ljust(60), nb_to_match
)
)
print("Efficacité du premier tour")
print(
"\tNombre de candidats avec affectation à l'issue du 1er tour {} / {}.".format(
nb_cand_matched, nb_cand
)
)
print(
"\tNombre de postes pourvues à l'issue du 1er tour {} / {}.".format(
nb_jobs_matched, nb_jobs
)
)
print(
"\tNombre de candidats participant au second tour {}.".format(nb_cand_unmatched)
)
print("\tNombre de postes proposées au second tour {}.".format(nb_jobs_unmatched))
print("\n")
print("Adéquation préférences candidats")
print(
"\tNombre de candidats affectés à leur poste préféré {}.".format(
matched_to_favourite
)
)
print(
"\tNombre de candidats affectés à leur second poste préféré {}.".format(
matched_to_second
)
)
print(
"\tNombre de candidats affectés à leur troisième poste préféré {}.".format(
matched_to_third
)
)
print(
"\tNombre de candidats affectés à un de leurs trois premiers postes préférés {}.".format(
matched_to_favourites
)
)
print(
"\tRang moyen dans les préférences candidats de la position obtenue {}.".format(
int(avg_match_rank)
)
)
rangs = list(
map(lambda rank: 1 + rank if rank is not None else "", rank_pref_match)
)
print(
"\t Nombre de candidats matchés au premier tour et ayant une incitation à tenter leur chance au second tour {}/{}.".format(
nb_dev, nb_cand
)
)
print(
"\tListe des rangs du matching au premier tour dans les listes de préférence des candidats {}.".format(
rangs
)
)
def run_experiment(model, params, silent):
# generates a matrix of log pop
logpop = pop.generate_logpop_from_model(model)
# draws full preferences
prefJ, prefC = pop.draw_profile(logpop)
# truncate the preference of jobs
prefJshort, prefCshort = truncate(prefJ, prefC, params)
# run deferred acceptance
matchJ, matchC = da.deferred_acceptance(prefJshort, prefCshort)
nb_cand = len(matchC)
nb_cand_unmatched = matchC.count(None)
nb_cand_matched = nb_cand - nb_cand_unmatched
rank_pref_match = [
None if matchC[id] == None else prefCshort[id].index(matchC[id])
for id in range(nb_cand)
]
nb_dev = 0
is_matched = list(map(lambda r: 1 if r is not None else 0, matchJ))
for id in range(nb_cand):
r = rank_pref_match[id]
if r is not None:
for rank in range(r):
job_preferred = prefC[id][rank]
if is_matched[job_preferred] == 0:
nb_dev = nb_dev + 1
break
# print the results
if not silent:
print_result(model, matchJ, matchC, prefJshort, prefCshort, params, nb_dev)
return nb_cand_matched, nb_dev
if __name__ == "__main__":
if len(sys.argv) <= 1 or len(sys.argv) > 2:
print("Usage: {} [model_filename] ".format(sys.argv[0]), file=sys.stderr)
model_name = "models/model_medium"
# sys.exit(0)
else:
model_name = sys.argv[1]
popularities = pop.deserialize(model_name)
nb_jobs = len(popularities["jobs_names"])
# nombre minimal de voeux tel que fixé dans le projet de décret
min_nb_voeux = int(np.ceil(0.15 * nb_jobs))
# nombre minimal d'auditions tel que fixé dans le projet de décret
min_nb_auditions = 8
# for nb_voeux in (min_nb_voeux, 2 * min_nb_voeux):
# for nb_min_auditions in (min_nb_auditions, 2 * min_nb_auditions - 1):
for nb_voeux in [min_nb_voeux]:
for nb_min_auditions in [min_nb_auditions]:
nb_favorite = nb_voeux // 2
nb_medium = int(np.ceil(nb_voeux / 3))
nb_safe = nb_voeux - nb_medium - nb_favorite
params = {
"nb_jobs": nb_jobs,
"min_nb_voeux": min_nb_voeux,
"nb_voeux": nb_voeux,
"nb_favorite": nb_favorite,
"nb_medium": nb_medium,
"nb_safe": nb_safe,
"min_rank_medium": nb_favorite + 1,
"min_rank_safe": 1 + nb_jobs // 2,
"nb_min_auditions": nb_min_auditions,
}
nb_matched_stats = []
nb_dev_stats = []
for i in range(500):
silent = i != 0
nb_matched, nb_dev = run_experiment(popularities, params, silent)
nb_matched_stats.append(nb_matched)
nb_dev_stats.append(nb_dev)
nb_candidates = popularities["nb_candidates"]
avg_matched = int(np.average(nb_matched_stats))
min_matched = np.min(nb_matched_stats)
max_matched = np.max(nb_matched_stats)
avg_dev = int(np.average(nb_dev_stats))
min_dev = np.min(nb_dev_stats)
max_dev = np.max(nb_dev_stats)
print(
"\n******\nnb_voeux {} nb_min_auditions {} ".format(
nb_voeux, nb_min_auditions
),
file=sys.stderr,
)
print(
"Statistics on 500 experiments [min,avg,max] matched after first step [{},{},{}] for {} candidates\n".format(
min_matched, avg_matched, max_matched, nb_candidates
),
file=sys.stderr,
)
print(
"Statistics on 500 experiments [min,avg,max] candidates matched at first run wanting to participate again to the second run [{},{},{}] for {} candidates\n".format(
min_dev, avg_dev, max_dev, nb_candidates
),
file=sys.stderr,
)