from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range ) author = 'Wang Song' doc = """ NTUEXP: three_ring for Task 5 (assortative) """ class Constants(BaseConstants): name_in_url = 'three_ring_t5' players_per_group = 3 num_rounds = 5 deletelink = c(110) remainlink = c(100) totime = 60 player1 = 'J' player2 = 'N' player3 = 'T' class Subsession(BaseSubsession): def assortative_grouping(self): player_list = self.get_players() n = len(player_list) score_list = [c(0)] * n for i in range(n): score = player_list[i].participant.vars['score'] score_list[i] = sum(score) # https://codereview.stackexchange.com/questions/65031/creating-a-list-containing-the-rank-of-the-elements-in-the-original-list indices = list(range(n)) indices.sort(key=lambda x: score_list[x]) score_rank = [0] * n for i, x in enumerate(indices): score_rank[x] = i matrix = [[0] * Constants.players_per_group for _ in range(int(n / Constants.players_per_group))] # dimension: [n/players_per_group] x [players_per_group] for i in range(n): group_num = int(score_rank[i] / Constants.players_per_group) player_num = int(score_rank[i] % Constants.players_per_group) matrix[group_num][player_num] = player_list[i] self.set_group_matrix(matrix) print("score_list: ", score_list) print("score_rank: ", score_rank) print("group_matrix: ", self.get_group_matrix()) def creating_session(self): if self.round_number == 1: self.group_randomly() else: self.group_like_round(1) print("creating_session: group matrix = ", self.get_group_matrix()) # Initialize the network for g in self.get_groups(): # print("initialize the network for groups") g.AB = 1 g.BA = 1 g.AC = 1 g.CA = 1 g.BC = 1 g.CB = 1 # initialize the counter to see how many people have chosen not to delete any link g.CO = 0 g.done = 0 for p in self.get_players(): # print("initialize the payoff for players") p.rpayoff = c(0) p.participant.vars['PayoffT5'] = c(0) class Group(BaseGroup): AB = models.IntegerField(initial=1) AC = models.IntegerField(initial=1) # AD = models.IntegerField(initial=1) BA = models.IntegerField(initial=1) BC = models.IntegerField(initial=1) # BD = models.IntegerField(initial=1) CA = models.IntegerField(initial=1) CB = models.IntegerField(initial=1) # CD = models.IntegerField(initial=1) # DA = models.IntegerField(initial=1) # DB = models.IntegerField(initial=1) # DC = models.IntegerField(initial=1) CO = models.IntegerField(initial=0) done = models.IntegerField(initial=0) p1sel = models.StringField( widget=widgets.RadioSelect, blank=True, ) p2sel = models.StringField( widget=widgets.RadioSelect, blank=True, ) p3sel = models.StringField( widget=widgets.RadioSelect, blank=True, ) def check_empty(self): if self.AB == 0 and self.AC == 0 and self.BC == 0: return 1 else: return 0 def set_var_1(self): p1 = self.get_player_by_id(1) if self.p1sel == 'Delete the link with Participant ' + Constants.player2: p1.participant.vars['PayoffT5'] += Constants.deletelink for g in self.in_rounds(self.round_number, Constants.num_rounds): g.CO = 0 g.AB = 0 g.BA = 0 elif self.p1sel == 'Delete the link with Participant ' + Constants.player3: p1.participant.vars['PayoffT5'] += Constants.deletelink for g in self.in_rounds(self.round_number, Constants.num_rounds): g.CO = 0 g.AC = 0 g.CA = 0 else: for g in self.in_rounds(self.round_number, Constants.num_rounds): g.CO += 1 # check for empty network if self.check_empty() == 1: for g in self.in_rounds(self.round_number, Constants.num_rounds): g.CO = Constants.players_per_group def set_var_2(self): p2 = self.get_player_by_id(2) if self.p2sel == 'Delete the link with Participant ' + Constants.player1: p2.participant.vars['PayoffT5'] += Constants.deletelink for g in self.in_rounds(self.round_number, Constants.num_rounds): g.CO = 0 g.AB = 0 g.BA = 0 elif self.p2sel == 'Delete the link with Participant ' + Constants.player3: p2.participant.vars['PayoffT5'] += Constants.deletelink for g in self.in_rounds(self.round_number, Constants.num_rounds): g.CO = 0 g.BC = 0 g.CB = 0 else: for g in self.in_rounds(self.round_number, Constants.num_rounds): g.CO += 1 if self.check_empty() == 1: for g in self.in_rounds(self.round_number, Constants.num_rounds): g.CO = Constants.players_per_group def set_var_3(self): p3 = self.get_player_by_id(3) if self.p3sel == 'Delete the link with Participant ' + Constants.player1: p3.participant.vars['PayoffT5'] += Constants.deletelink for g in self.in_rounds(self.round_number, Constants.num_rounds): g.CO = 0 g.AC = 0 g.CA = 0 elif self.p3sel == 'Delete the link with Participant ' + Constants.player2: p3.participant.vars['PayoffT5'] += Constants.deletelink for g in self.in_rounds(self.round_number, Constants.num_rounds): g.CO = 0 g.BC = 0 g.CB = 0 else: for g in self.in_rounds(self.round_number, Constants.num_rounds): g.CO += 1 if self.check_empty() == 1: for g in self.in_rounds(self.round_number, Constants.num_rounds): g.CO = Constants.players_per_group def set_payoff(self): p1 = self.get_player_by_id(1) p2 = self.get_player_by_id(2) p3 = self.get_player_by_id(3) if self.AB == 1: p1.participant.vars['PayoffT5'] += Constants.remainlink p2.participant.vars['PayoffT5'] += Constants.remainlink if self.AC == 1: p1.participant.vars['PayoffT5'] += Constants.remainlink p3.participant.vars['PayoffT5'] += Constants.remainlink if self.BC == 1: p2.participant.vars['PayoffT5'] += Constants.remainlink p3.participant.vars['PayoffT5'] += Constants.remainlink for p in self.get_players(): p.rpayoff = p.participant.vars['PayoffT5'] p.payoff = p.rpayoff # - ws Apr def set_done(self): pass class Player(BasePlayer): rpayoff = models.CurrencyField()