-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
308 lines (247 loc) · 14.3 KB
/
models.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
# coding=utf-8
from otree.api import *
import random
#import csv
#import os
import copy
from itertools import chain
from SHttc8.user_settings import *
author = 'Benjamin Pichl'
doc = """
This app is intended to model a school choice problem. It implements the Gale's Top Trading Cycles for School Choice
Mechanism within the oTree framework. If you have any questions, comments, feature requests, or bug reports,
please write me an eMail: benjamin.pichl@outlook.com.
If you intend to use this code for your own research purposes, please cite:
t.b.d.
"""
class Subsession(BaseSubsession):
# METHOD: =================================================================================== #
# THINGS TO DO BEFORE THE SESSION STARTS =================================================== #
# =========================================================================================== #
def creating_session(self):
print(f"Starting session for SHttc8: {self.__class__.__name__}")
# Reset other participant.vars as needed
# CREATE INDICES FOR MOST IMPORTANT VARS ================================================ #
# indices = [j for j in range(1, Constants.nr_courses + 1)]
players = self.get_players()
num_players = len(players)
# Debugging: Print number of players
print(f"Number of players: {num_players}")
# Randomly shuffle players and form new groups of 4
self.group_randomly(fixed_id_in_group=True)
#group_matrix = [players[i:i + 4] for i in range(0, num_players, 4)]
#elf.set_group_matrix(group_matrix)
for group in self.get_groups():
players_in_group = group.get_players()
for p in players_in_group:
p.participant.vars['role'] = p.role()
# Other participant vars as needed
# Store group ID in participant vars
p.participant.vars['group_id'] = group.id
print(f"Player {p.id_in_group}'s role: {p.participant.vars['role']}")
print(f"Player {p.id_in_group} is in group {group.id}")
indices = [j for j in range(1, Constants.nr_courses + 1)]
# for p in players:
# p.participant.vars['role'] = p.role() # This stores the result of p.role() in participant.vars
#print(f"Player {p.id_in_group}'s role: {p.participant.vars['role']}")
# CREATE FORM TEMPLATES FOR DECISION.HTML ============================================== #
form_fields = ['pref_c' + str(j) for j in indices]
for p in players:
p.participant.vars['form_fields_plus_index'] = list(zip(indices, form_fields))
p.participant.vars['player_prefs'] = [None for n in indices]
p.participant.vars['success8'] = [False for n in indices]
#p.participant.vars['role'] = p.role()
# ALLOCATE THE CORRECT VALUATIONS VECTOR TO PLAYER (DEPENDING ON TYPE) ================== #
# AND GET OTHER PLAYERS' VALUATIONS AND TYPES TO DISPLAY IF DESIRED #
type_names = ['Type ' + str(i) for i in range(1, Constants.nr_types + 1)]
for p in players:
p.participant.vars['val8_others'] = []
p.participant.vars['other_types_names'] = []
for t in type_names:
if p.role() == t:
p.participant.vars['val8'] = Constants.val8[type_names.index(t)]
else:
if Constants.nr_types > 1:
p.participant.vars['val8_others'].append(Constants.val8[type_names.index(t)])
p.participant.vars['other_types_names'] = [t for t in type_names if p.role() != t]
print(f"others' vals: {p.participant.vars['val8_others']}")
print(f"own vals: {p.participant.vars['val8']}")
# ALLOCATE THE CORRECT prio8 VECTOR TO PLAYER (DEPENDING ON ID) ==================== #
for p in players:
p.participant.vars['prio8'] = []
for i in Constants.prio8:
p.participant.vars['prio8'].extend([(i.index(j) + 1) for j in i if j == p.id_in_group])
print(f"prio8 in models: {p.participant.vars['prio8']}")
# METHOD: =================================================================================== #
# PREPARE ADMIN REPORT ====================================================================== #
# =========================================================================================== #
def vars_for_admin_report(self):
indices = [j for j in range(1, Constants.nr_courses + 1)]
players = self.get_players()
table_nr_tds_decisions = Constants.nr_courses + 2
table_nr_tds_prio8 = Constants.nr_courses + 1
player_prefs = [p.participant.vars['player_prefs'] for p in players]
last_player_per_group = [i[-1] for i in self.get_group_matrix()]
player_val8 = [p.participant.vars['val8'] for p in players]
player_prio8 = [p.participant.vars['prio8'] for p in players]
types = ['Type ' + str(i) for i in range(1, Constants.nr_types + 1)]
val8 = [i for i in Constants.val8]
capacities = [i for i in Constants.capacities]
decisions = zip(players, player_prefs)
success8 = [p.participant.vars['success8'] for p in players]
success8_with_id = zip(players, success8)
val8_all_types = zip(types, val8)
prio8_all_players = zip(players, player_prio8)
data_all = zip(players, player_val8, player_prefs, success8)
return {
'indices': indices,
'players': players,
'table_nr_tds_decisions': table_nr_tds_decisions,
'table_nr_tds_prio8': table_nr_tds_prio8,
'player_prefs': player_prefs,
'last_player_per_group': last_player_per_group,
'player_prio8': player_prio8,
'capacities': capacities,
'decisions': decisions,
'success8': success8,
'success8_with_id': success8_with_id,
'val8_all_types': val8_all_types,
'prio8_all_players': prio8_all_players,
'data_all': data_all
}
class Group(BaseGroup):
# METHOD: =================================================================================== #
# GET ALLOCATION (EXECUTED AFTER ALL PLAYERS SUBMITTED DECISION.HTML ======================== #
# =========================================================================================== #
def deep_copy_priorities(self, original):
# Manually create a deep copy of a list of lists
return [list(sublist) for sublist in original]
def get_allocation(self):
# CREATE INDICES FOR MOST IMPORTANT VARS ================================================ #
players = self.get_players()
indices = [j for j in range(1, Constants.nr_courses + 1)]
# COLLECT PREPARED_LIST FROM ALL PLAYERS AND ORDER THEM IN ONE SINGLE LIST IN =========== #
# DESCENDING ORDER OF PREFS. #
for p in players:
for n in indices:
p.participant.vars['player_prefs'][n - 1].append(p.id_in_group)
p.participant.vars['player_prefs'][n - 1].append(n)
all_prefs = [p.participant.vars['player_prefs'] for p in players]
all_prefs = list(chain.from_iterable(all_prefs))
all_prefs.sort(key=lambda sublist: sublist[0], reverse=False)
# CREATE EMPTY LISTS AND COUNTERS FOR THE ALLOCATION PROCESS ============================ #
cycles_found = []
size_counter = 1
pass_nr = 1
players_in_round = players
print(f"players_in_round1: {players_in_round}")
player_resource = [[] for p in players]
seats_left = Constants.capacities.copy()
prio8_left = self.deep_copy_priorities(Constants.prio8)
print(f"priorities left first time: {prio8_left}")
#prio8_left = Constants.prio8.copy()
#print(f"priorities left first time: {prio8_left}")
#(Constants.prio8.copy())
# IMPLEMENTATION OF THE TTC MECHANISM =================================================== #
while size_counter <= len(players_in_round) +10:
# DETERMINE WHICH PLAYERS ARE PLAYING IN THIS ROUND ================================= #
players_in_round = []
print(f"players_in_round2: {players_in_round}")
for i in all_prefs:
players_in_round.append([p for p in players if p.id_in_group == i[1]])
players_in_round = set(list(chain.from_iterable(players_in_round)))
print(f"players_in_round3: {players_in_round}")
# DETERMINE THE TOP PREFERENCES IN THIS ROUND ======================================= #
top_prefs_in_round = []
for p in players_in_round:
top_prefs_in_round.append([[b[1], b[2]] for b in all_prefs if
b[0] == (min(i[0] for i in all_prefs if i[1] == p.id_in_group)) and b[
1] == p.id_in_group])
top_prefs_in_round = list(chain.from_iterable(top_prefs_in_round))
print(f"top_preferences_in_round: {top_prefs_in_round}")
# DETERMINE THE TOP prio8 IN THIS ROUND ======================================== #
top_prio8_in_round = []
print(f"prio8 left: {prio8_left}")
for i, j in zip(indices, prio8_left):
if seats_left[i - 1] > 0:
top_prio8_in_round.append([i, j[0]])
print(f"top_prio8_in_round: {top_prio8_in_round}")
# MAKE AN EMPTY LIST THAT CATCHES ALL FOUND CYCLES AND APPEND ALL CYCLES ============ #
cycles_check = []
for i in top_prefs_in_round:
j = next(j for j in top_prio8_in_round if j[0] == i[1])
cycles_check.append([i, j])
print(f"cycles: {cycles_check}")
# IF A CYCLE IS LARGER THAN 1 WE NEED TO APPEND MORE THAN 2 ITEMS TO THE CYCLE ====== #
if size_counter > 1:
m = 0
while m < size_counter:
for i in cycles_check:
j = next(j for j in top_prefs_in_round if j[0] == i[-1][1])
k = next(k for k in top_prio8_in_round if k[0] == j[-1])
cycles_check[cycles_check.index(i)].append(j)
cycles_check[cycles_check.index(i)].append(k)
m += 1
cycles_found = [i for i in cycles_check if i[0][0] == i[-1][-1]]
print(f"cycles_found: {cycles_found}")
# IF A CYCLE WAS FOUND, WE NEED TO DELETE ALL PLAYERS IN THE CYCLE AND REDUCE THE === #
# CAPACITIES OF RESOURCES IN THE CYCLE BY 1. WE ALSO UPDATE THE PREF_LIST. #
if cycles_found:
remove_seats = [j[0][1] for j in cycles_found]
for i in remove_seats:
seats_left[i - 1] -= 1
for i in cycles_found:
for j in prio8_left:
j.remove(i[0][0])
print(f"prio8 left: {prio8_left}")
all_prefs = [k for k in all_prefs if not i[0][0] == k[1]]
if seats_left[(i[0][1] - 1)] < 1:
all_prefs = [k for k in all_prefs if i[0][1] != k[2]]
for b in top_prefs_in_round:
if i[0] == b:
player_resource[b[0] - 1] = b
print(f"player resource?: {player_resource}")
# UPDATE COUNTERS =============================================================== #
size_counter = 1
else:
size_counter += 1
print(f"size_counter: {size_counter}")
if not all_prefs:
break
pass_nr += 1
print(f"pass_nr: {pass_nr}")
# ASSIGN RESOURCES TO PLAYERS' PARTICIPANT VARS ========================================= #
for p in players:
p.participant.vars['player_resource'] = player_resource[p.id_in_group - 1]
print(f"Player {p.id_in_group} is matched with resource: {p.participant.vars['player_resource']}")
# METHOD: =================================================================================== #
# SET PAYOFFS =============================================================================== #
# =========================================================================================== #
def set_payoffs(self):
players = self.get_players()
for p in players:
print(f"Player {p.id_in_group} is matched with resource: {p.participant.vars['player_resource']}")
# Assuming the second element in player_resource is the course number
matched_course = p.participant.vars['player_resource'][1]
p.payoff = p.participant.vars['val8'][matched_course - 1]
p.participant.vars['success8'][matched_course - 1] = True
print(
f" Matched with Course {matched_course}. Payoff: {p.payoff}. success8: {p.participant.vars['success8'][matched_course - 1]}")
class Player(BasePlayer):
# METHOD: =================================================================================== #
# DEFINE ROLES ACCORDING TO INPUT IN USER_SETTINGS.PY ======================================= #
# =========================================================================================== #
def role(self):
# DESIGN TYPES ========================================================================== #
all_ids = list(range(1, Constants.players_per_group + 1))
nr_ids_per_type = int(Constants.players_per_group / Constants.nr_types)
type_for_id = list(chain.from_iterable([[i] * nr_ids_per_type for i in range(1, Constants.nr_types + 1)]))
type_matrix = [[i, j] for i, j in zip(all_ids, type_for_id)]
for i in range(0, len(type_matrix)):
if self.id_in_group == type_matrix[i][0]:
return 'Type ' + str(type_matrix[i][1])
# DYNAMICALLY CREATE N FORM TEMPLATES FOR PREFS ============================================= #
for j in range(1, Constants.nr_courses + 1):
locals()['pref_c' + str(j)] = models.IntegerField()
del j
group_membership = models.IntegerField()