# VARIABLES preferences = { # TODO: These are students' true preferences 1: ['A', 'C', 'D', 'B', 'E'], 2: ['B', 'D', 'A', 'E', 'C'], 3: ['D', 'A', 'B', 'C', 'E'], 4: ['C', 'A', 'B', 'E', 'D'], 5: ['C', 'B', 'A', 'D', 'E'] } given_preferences = { # TODO: These are students' expressed preferences 1: ['A', 'C', 'D', 'B', 'E'], 2: ['B', 'D', 'A', 'E', 'C'], 3: ['D', 'A', 'B', 'C', 'E'], 4: ['C', 'A', 'B', 'E', 'D'], 5: ['C', 'B', 'A', 'D', 'E'] } priorities = { # TODO: Change to have different priorities for the students 'A': [2, 4, 1, 5, 3], 'B': [4, 1, 2, 3, 5], 'C': [3, 2, 4, 5, 1], 'D': [4, 5, 3, 2, 1], 'E': [1, 3, 2, 5, 4] } consent_list = { 1: True, 2: True, 3: True, 4: True, 5: True } current_treatment = 2 ################################ # NEEDED METHODS class Player: def __init__(self, id_in_group): self.id_in_group = id_in_group self.preferences = preferences[id_in_group] self.given_preferences = given_preferences[id_in_group] self.consent = consent_list[id_in_group] player_1 = Player(1) player_2 = Player(2) player_3 = Player(3) player_4 = Player(4) player_5 = Player(5) players = [player_1, player_2, player_3, player_4, player_5] def get_player_by_id(id_in_group): for p in players: if p.id_in_group == id_in_group: return p def index_of(array, element): index = 0 for a in array: if a == element: return index index += 1 def deferred_acceptance(given_preferences, consent_list=None): students_app = {} schools_app = {} given_preferences_copy = {} flag = True interrupters_list = [] # Set everything to -1: At the beginning, nobody is matched # Save the preferences given by the players for student in preferences: students_app[student] = -1 given_preferences_copy[student] = given_preferences[student].copy() for school in priorities: schools_app[school] = -1 step = 1 while flag: flag = False for student_id in students_app: # For every student if students_app[student_id] == -1 and len(given_preferences_copy[student_id]) > 0: # If the student is not matched and still has preferences flag = True # Take the first element out of his preferences, and delete it from the list liked_school = given_preferences_copy[student_id].pop(0) if schools_app[liked_school] == -1: # If this school is not matched students_app[student_id] = liked_school # Match the student with the school schools_app[liked_school] = student_id # Match the school with this student elif index_of(priorities[liked_school], schools_app[liked_school]) > \ index_of(priorities[liked_school], student_id): # If the current student matched by the school has a larger index in the priority list than me if consent_list and consent_list[schools_app[liked_school]]: interrupters_list.append((schools_app[liked_school], liked_school)) students_app[schools_app[liked_school]] = -1 # Set the school to unmatched students_app[student_id] = liked_school # Match the student with the school schools_app[liked_school] = student_id # Match the school with this student print("Step " + str(step) + ": ", end="") print(schools_app) # Print the result at every round, needed for debug step += 1 return students_app, interrupters_list def deferred_acceptance_consent(given_preferences, consent_list): while True: print("New run DA") ranking, interrupters = deferred_acceptance(given_preferences, consent_list) print("interrupter: ", end="") print(interrupters) if len(interrupters) == 0: break interrupting_student = interrupters[-1][0] # Find the id of the student interrupter interrupting_schools = [pair[1] for pair in interrupters if pair[0] == interrupting_student] for school in interrupting_schools: given_preferences[interrupting_student].remove(school) print("Given preferences after removing: ", end="") print(given_preferences) return ranking def set_payoffs(): given_preferences = {} # Save the preferences given by the players for student in preferences: given_preferences[student] = get_player_by_id(student).given_preferences.copy() # Create consent list consent_list = {} for p in players: if p.consent: consent_list[p.id_in_group] = 1 else: consent_list[p.id_in_group] = 0 # Choose method according to treatment assignment if current_treatment == 1: students_ranking, none = deferred_acceptance(given_preferences) else: students_ranking = deferred_acceptance_consent(given_preferences, consent_list) schools_allocation = [students_ranking[i] for i in range(1, len(preferences) + 1)] print(students_ranking) ################################ # METHOD CALL if __name__ == "__main__": set_payoffs()