def play_round(self): case = self.case if self.subsession.round_number == 1: yield (views.Introduction) if 'seller' in self.player.role(): yield ( views.Production, { 'seller_proposed_price': Constants.initial_endowment, 'seller_proposed_quality': c(10) }) if case == 'purchase': assert 'at a price of <strong>{}'.format( Constants.initial_endowment) in self.html else: assert 'The buyer bought nothing' in self.html else: # can't make a null purchase yield SubmissionMustFail(views.Purchase) if case == 'purchase': yield (views.Purchase, {'seller_id': 1}) assert 'The quality grade of your purchase is <strong>Low' in self.html assert 'your period payoff is <strong>{}'.format( c(15)) in self.html else: yield (views.Purchase, {'seller_id': 0}) assert 'You bought nothing' in self.html assert "your period payoff is {}".format(Constants.initial_endowment) yield (views.Results) if self.subsession.round_number == Constants.num_rounds: yield (views.FinalResults)
def set_payoffs_in_rub(self): if self.round_number < self.paying_round_num: for p in self.get_players(): p.payoff = c(0) if self.round_number >= self.paying_round_num: p1, p2 = self.get_players()[0], self.get_players()[1] p1.payoff = c((self.S_final_payoff + self.S_final_prediction_payoff)*Constants.unitcost) + c(150) p2.payoff = c((self.R_final_payoff + self.R_final_prediction_payoff)*Constants.unitcost) + c(150)
def calc_payoff(self): proposer = self.get_player_by_role('proposer') responder = self.get_player_by_role('responder') if self.accepted: proposer.payoff=Constants.pot_size-self.offer responder.payoff=self.offer else: proposer.payoff=c(0) responder.payoff=c(0)
def play_round(self): yield (views.Introduction) if self.player.id_in_group == 1: yield (views.Offer, {"kept": c(99)}) assert self.player.payoff == c(99) else: assert self.player.payoff == c(1) yield (views.Results)
def no_yellow_errors(self): '''None of these should be highlighted in yellow''' _ = self.f_currency + 1 _ = self.f_currency + c(1) _ = c(1) + 1 _ = c(1) / c(1) _ = self.f_int + 1 _ = self.f_int + c(1) _ = self.f_bool + 1 _ = self.group.f_posint
def play_round(self): yield (pages.Introduction) if self.case == 'min': yield (pages.Decide, {'units': 0}) # if player produces 0, nothing is sold and they make 0 assert self.player.payoff == c(0) if self.case == 'max': yield (pages.Decide, {'units': Constants.max_units_per_player}) # if everyone produces max, price is driven to 0 assert self.player.payoff == c(0) yield (pages.Results)
def set_growth(self): if self.dead_remove is not True: self.herd_size_initial = self.participant.vars['herd_size'] growth_rate = random.gauss(self.session.config['growth_rate_mean'], self.session.config['growth_rate_sd']) self.participant.vars['herd_size'] = \ self.participant.vars['herd_size'] + (growth_rate * self.participant.vars['herd_size']) if self.participant.vars['herd_size'] > c(self.session.config['maxherd']): self.participant.vars['herd_size'] = c(self.session.config['maxherd']) if self.participant.vars['herd_size'] <= c(0): self.participant.vars['herd_size'] = c(0) self.herd_size_after_growth = self.participant.vars['herd_size'] if self.herd_size_after_growth < c(self.session.config['minherd']): self.under_minimum = True else: self.under_minimum = False
def set_payoffs(self): if any(p.volunteer for p in self.get_players()): baseline_amount = Constants.general_benefit else: baseline_amount = c(0) for p in self.get_players(): p.payoff = baseline_amount if p.volunteer: p.payoff -= Constants.volunteer_cost
def set_shock(self): if self.dead_remove is not True: if random.uniform(0, 1) < self.session.config['shock_rate']: self.shock_occurrence = True shock_size = random.gauss(self.session.config['shock_size_mean'], self.session.config['shock_size_sd']) self.participant.vars['herd_size'] = \ self.participant.vars['herd_size'] - (shock_size * self.participant.vars['herd_size']) else: self.shock_occurrence = False if self.participant.vars['herd_size'] > c(self.session.config['maxherd']): self.participant.vars['herd_size'] = c(self.session.config['maxherd']) if self.participant.vars['herd_size'] <= c(0): self.participant.vars['herd_size'] = c(0) self.herd_size_after_shock = self.participant.vars['herd_size'] if self.herd_size_after_shock < c(self.session.config['minherd']): self.under_minimum = True else: self.under_minimum = False
def set_payoffs(self): players = self.get_players() self.total_requests = sum([p.request for p in players]) if self.total_requests <= Constants.amount_shared: for p in players: p.payoff = p.request else: for p in players: p.payoff = c(0)
def play_round(self): case = self.case yield (views.Introduction) if case == '0_volunteer': yield (views.Decision, {'volunteer': False}) assert self.player.payoff == c(0) assert 'You did not volunteer and no one did' in self.html elif case == '1_volunteer': yield (views.Decision, {'volunteer': self.player.id_in_group == 1}) if self.player.id_in_group == 1: assert 'You volunteered' in self.html assert self.player.payoff == Constants.general_benefit - Constants.volunteer_cost else: assert 'You did not volunteer but some did' in self.html assert self.player.payoff == c(100) yield (views.Results)
def play_round(self): yield (views.Introduction) if self.player.id_in_group == 1: yield (views.Offer, {'amount_offered': c(10)}) else: if self.group.strategy: yield (views.AcceptStrategy, {'response_{}'.format(int(offer)): True for offer in Constants.offer_choices}) else: yield (views.Accept, {'offer_accepted': True}) yield (views.Results)
def set_payoffs(self): players = self.get_players() winning_price = min([p.price for p in players]) winners = [p for p in players if p.price == winning_price] winner = random.choice(winners) for p in players: p.payoff = c(0) if p == winner: p.is_a_winner = True p.payoff += p.price
def set_payoffs(self): players = self.get_players() self.num_volunteers = sum([p.volunteer for p in players]) if self.num_volunteers > 0: baseline_amount = Constants.general_benefit else: baseline_amount = c(0) for p in players: p.payoff = baseline_amount if p.volunteer: p.payoff -= Constants.volunteer_cost
def set_payoffs(self): matcher = self.get_player_by_role('Matcher') mismatcher = self.get_player_by_role('Mismatcher') if matcher.penny_side == mismatcher.penny_side: matcher.is_winner = True mismatcher.is_winner = False else: matcher.is_winner = False mismatcher.is_winner = True for player in [mismatcher, matcher]: if self.subsession.round_number == self.session.vars['paying_round'] and player.is_winner: player.payoff = Constants.stakes else: player.payoff = c(0)
def play_round(self): # start yield (views.Introduction) if self.case == 'success': request_amount = c(10) yield (views.Request, {"request_amount": request_amount}) yield (views.Results) assert self.player.payoff == request_amount if self.case == 'greedy': yield (views.Request, {"request_amount": Constants.amount_shared}) yield (views.Results) assert self.player.payoff == 0
def set_payoff(self): # determine round_result as (potential) payoff per round if self.bomb: self.round_result = c(0) else: self.round_result = self.boxes_collected * Constants.box_value # set payoffs if <random_payoff = True> to round_result of randomly chosen round # randomly determine round to pay on player level if self.subsession.round_number == 1: self.participant.vars['round_to_pay'] = random.randint(1,Constants.num_rounds) if Constants.random_payoff: if self.subsession.round_number == self.participant.vars['round_to_pay']: self.pay_this_round = True self.payoff = self.round_result else: self.pay_this_round = False self.payoff = c(0) # set payoffs to round_result if <random_payoff = False> else: self.payoff = self.round_result
def set_payoffs(self): p1, p2 = self.get_players() if p1.claim == p2.claim: self.lower_claim = p1.claim for p in [p1, p2]: p.payoff = self.lower_claim p.adjustment = c(0) else: if p1.claim < p2.claim: winner, loser = p1, p2 else: winner, loser = p2, p1 self.lower_claim = winner.claim winner.adjustment = Constants.adjustment_abs loser.adjustment = -Constants.adjustment_abs winner.payoff = self.lower_claim + winner.adjustment loser.payoff = self.lower_claim + loser.adjustment
def final_herd_size(self): for n in self.get_players(): if n.participant.vars['herd_size'] > c(self.session.config['maxherd']): n.participant.vars['herd_size'] = c(self.session.config['maxherd']) if n.participant.vars['herd_size'] <= c(0): n.participant.vars['herd_size'] = c(0) if n.dead_remove is not True: n.herd_size_after_transfers = c(n.participant.vars['herd_size']) if n.herd_size_after_transfers < c(self.session.config['minherd']): n.under_minimum = True n.participant.vars['under_minimum_years_left'] -= 1 else: n.under_minimum = False n.participant.vars['under_minimum_years_left'] = self.session.config['years_before_death'] n.under_minimum_years_left_end = n.participant.vars['under_minimum_years_left'] if n.under_minimum_years_left_end == 0: n.dead = True else: n.dead = False
def cost_from_effort(effort): return c(Constants.EFFORT_TO_COST[effort])
def set_payoff(self): if self.isWinner: self.payoff = self.value - self.group.second else: self.payoff = c(0)
def vars_for_template(self): email = '*****@*****.**' return {'email': email, 'participation': self.session.config['participation_fee'], 'conversion': c(1 / self.session.config['real_world_currency_per_point']), 'dollar': '$1'}
def vars_for_template(self): risk = self.session.vars['control_risks'][self.round_number-1][self.player.id_in_group - 1] return {'payoff_when_flood_without_adapt' : c(50), 'total_payoff': c(Constants.stakes), 'payoff_with_adapt': c(300) }
def vars_for_template(self): sender1 = self.group.get_player_by_role('sender 1') sender2 = self.group.get_player_by_role('sender 2') sender3 = self.group.get_player_by_role('sender 3') if self.player.role() == 'sender 1': sender_allocation = sender1.sender_allocation sender_kept = Constants.endowment - sender_allocation returned_sender = self.group.returned1_points payoff = Constants.endowment - sender_allocation + returned_sender allocated_low = (returned_sender - 10) allocated_high = (returned_sender + 10) e_test = sender1.sender_expectation if allocated_low <= e_test <= allocated_high: e_verdict = 'You have correctly guessed the number of points allocated to you' payoff += c(20) else: e_verdict = 'You have incorrectly guessed the number of points allocated to you' return { 'e_test': e_test, 'e_verdict': e_verdict, 'sender_allocation': sender_allocation, 'sender_kept': sender_kept, 'returned_sender': returned_sender, 'payoff': payoff } elif self.player.role() == 'sender 2': sender_allocation = sender2.sender_allocation sender_kept = Constants.endowment - sender_allocation returned_sender = self.group.returned2_points payoff = Constants.endowment - sender_allocation + returned_sender allocated_low = (returned_sender - 10) allocated_high = (returned_sender + 10) e_test = sender2.sender_expectation if allocated_low <= e_test <= allocated_high: e_verdict = 'You have correctly guessed the number of points allocated to you' payoff += c(20) else: e_verdict = 'You have incorrectly guessed the number of points allocated to you' return { 'allocated_low': allocated_low, 'allocated_high': allocated_high, 'e_test': e_test, 'e_verdict': e_verdict, 'sender_allocation': sender_allocation, 'sender_kept': sender_kept, 'returned_sender': returned_sender, 'payoff': payoff } elif self.player.role() == 'sender 3': sender_allocation = sender3.sender_allocation sender_kept = Constants.endowment - sender_allocation returned_sender = self.group.returned3_points payoff = Constants.endowment - sender_allocation + returned_sender allocated_low = (returned_sender - 10) allocated_high = (returned_sender + 10) e_test = sender3.sender_expectation if allocated_low <= e_test <= allocated_high: e_verdict = 'You have correctly guessed the number of points allocated to you' payoff += c(20) else: e_verdict = 'You have incorrectly guessed the number of points allocated to you' return { 'allocated_low': allocated_low, 'allocated_high': allocated_high, 'e_test': e_test, 'e_verdict': e_verdict, 'sender_allocation': sender_allocation, 'sender_kept': sender_kept, 'returned_sender': returned_sender, 'payoff': payoff } else: endowment = Constants.endowment sender_allocation = self.group.total_sender_allocation total_allocated = sender_allocation * Constants.multiplier payoff = Constants.endowment + self.group.kept_amount return { 'endowment': endowment, 'sender_allocation': sender_allocation, 'total_allocated': total_allocated, 'payoff': payoff }
def to_c(self, value): return c(value).to_real_world_currency(self.session)
def check_color_answers(self): print('\n\nFOR ROUND', self.round_number) controller = self.get_player_by_role('Controller') controller_color_answers = [ controller.word1, controller.word2, controller.word3, controller.word4, controller.word5, controller.word6, controller.word7, controller.word8, controller.word9, controller.word10, controller.word11, controller.word12, controller.word13, controller.word14, controller.word15, controller.word16, controller.word17, controller.word18, controller.word19, controller.word20 ] for p in self.get_players(): if self.round_number < 4: if p.participant.vars['treatment_group'] == "A": for i in range(20): if controller_color_answers[i] == self.session.vars[ 'color_goals_key_A' + str(self.round_number)][i]: controller.total_words_correct += 1 controller.payoff += c(10) print( 'For word', i + 1, 'color was correct. Controller.total_words_correct is', controller.total_words_correct, 'and controller.payoff is', controller.payoff) print( 'color_goals_key_A[', i, '] was', self.session.vars['color_goals_key_A' + str(self.round_number)][i], 'and controller_color_answers[', i, '] was', controller_color_answers[i], '\n') else: print( 'For word', i + 1, 'color was incorrect. Controller.total_words_correct is still', controller.total_words_correct, 'and controller.payoff is still', controller.payoff) print( 'color_goals_key_A[', i, '] was', self.session.vars['color_goals_key_A' + str(self.round_number)][i], 'and controller_color_answers[', i, '] was', controller_color_answers[i], '\n') if p.participant.vars['treatment_group'] == "B": for i in range(20): if controller_color_answers[i] == self.session.vars[ 'color_goals_key_B' + str(self.round_number)][i]: controller.total_words_correct += 1 controller.payoff += c(10) print( 'For word', i + 1, 'color was correct. Controller.total_words_correct is', controller.total_words_correct, 'and controller.payoff is', controller.payoff) print( 'color_goals_key_B[', i, '] was', self.session.vars['color_goals_key_B' + str(self.round_number)][i], 'and controller_color_answers[', i, '] was', controller_color_answers[i], '\n') else: print( 'For word', i + 1, 'color was incorrect. Controller.total_words_correct is still', controller.total_words_correct, 'and controller.payoff is still', controller.payoff) print( 'color_goals_key_B[', i, '] was', self.session.vars['color_goals_key_B' + str(self.round_number)][i], 'and controller_color_answers[', i, '] was', controller_color_answers[i], '\n') if p.participant.vars['treatment_group'] == "C": for i in range(20): if controller_color_answers[i] == self.session.vars[ 'color_goals_key_C' + str(self.round_number)][i]: controller.total_words_correct += 1 controller.payoff += c(10) print( 'For word', i + 1, 'color was correct. Controller.total_words_correct is', controller.total_words_correct, 'and controller.payoff is', controller.payoff) print( 'color_goals_key_C[', i, '] was', self.session.vars['color_goals_key_C' + str(self.round_number)][i], 'and controller_color_answers[', i, '] was', controller_color_answers[i], '\n') else: print( 'For word', i + 1, 'color was incorrect. Controller.total_words_correct is still', controller.total_words_correct, 'and controller.payoff is still', controller.payoff) print( 'color_goals_key_C[', i, '] was', self.session.vars['color_goals_key_C' + str(self.round_number)][i], 'and controller_color_answers[', i, '] was', controller_color_answers[i], '\n') if self.round_number >= 4: if p.participant.vars['treatment_group2'] == "A": for i in range(20): if controller_color_answers[i] == self.session.vars[ 'color_goals_key_A' + str(self.round_number - 3)][i]: controller.total_words_correct += 1 controller.payoff += c(10) print( 'For word', i + 1, 'color was correct. Controller.total_words_correct is', controller.total_words_correct, 'and controller.payoff is', controller.payoff) print( 'color_goals_key_A[', i, '] was', self.session.vars['color_goals_key_A' + str(self.round_number - 3)][i], 'and controller_color_answers[', i, '] was', controller_color_answers[i], '\n') else: print( 'For word', i + 1, 'color was incorrect. Controller.total_words_correct is still', controller.total_words_correct, 'and controller.payoff is still', controller.payoff) print( 'color_goals_key_A[', i, '] was', self.session.vars['color_goals_key_A' + str(self.round_number - 3)][i], 'and controller_color_answers[', i, '] was', controller_color_answers[i], '\n') if p.participant.vars['treatment_group2'] == "B": for i in range(20): if controller_color_answers[i] == self.session.vars[ 'color_goals_key_B' + str(self.round_number - 3)][i]: controller.total_words_correct += 1 controller.payoff += c(10) print( 'For word', i + 1, 'color was correct. Controller.total_words_correct is', controller.total_words_correct, 'and controller.payoff is', controller.payoff) print( 'color_goals_key_B[', i, '] was', self.session.vars['color_goals_key_B' + str(self.round_number - 3)][i], 'and controller_color_answers[', i, '] was', controller_color_answers[i], '\n') else: print( 'For word', i + 1, 'color was incorrect. Controller.total_words_correct is still', controller.total_words_correct, 'and controller.payoff is still', controller.payoff) print( 'color_goals_key_B[', i, '] was', self.session.vars['color_goals_key_B' + str(self.round_number - 3)][i], 'and controller_color_answers[', i, '] was', controller_color_answers[i], '\n') if p.participant.vars['treatment_group2'] == "C": for i in range(20): if controller_color_answers[i] == self.session.vars[ 'color_goals_key_C' + str(self.round_number - 3)][i]: controller.total_words_correct += 1 controller.payoff += c(10) print( 'For word', i + 1, 'color was correct. Controller.total_words_correct is', controller.total_words_correct, 'and controller.payoff is', controller.payoff) print( 'color_goals_key_C[', i, '] was', self.session.vars['color_goals_key_C' + str(self.round_number - 3)][i], 'and controller_color_answers[', i, '] was', controller_color_answers[i], '\n') else: print( 'For word', i + 1, 'color was incorrect. Controller.total_words_correct is still', controller.total_words_correct, 'and controller.payoff is still', controller.payoff) print( 'color_goals_key_C[', i, '] was', self.session.vars['color_goals_key_C' + str(self.round_number - 3)][i], 'and controller_color_answers[', i, '] was', controller_color_answers[i], '\n')
def play_round(self): yield (pages.VReceiveContributions, {'contribution': c(57)}) yield (pages.VShowResults)
def creating_session(self): if self.round_number == 1: n = Constants.num_choices for p in self.get_players(): decision_d = ['D' + str(d) for d in range(1, 22)] # create list of lottery indices # ---------------------------------------------------------------------------------------------------- indices = [j for j in range(1, n + 1)] # create list corresponding to form_field variables including all choices # ---------------------------------------------------------------------------------------------------- form_fields = ['choice_' + str(k) for k in indices] # create list of probabilities # ---------------------------------------------------------------------------------------------------- if Constants.variation == 'probability': probabilities = [ Constants.probability + (k - 1) * Constants.step_size for k in indices ] else: probabilities = [Constants.probability for k in indices] # create list of high lottery payoffs # ---------------------------------------------------------------------------------------------------- if Constants.variation == 'lottery_hi': lottery_hi = [ c(Constants.lottery_hi + (k - 1) * Constants.step_size) for k in indices ] else: lottery_hi = [c(Constants.lottery_hi) for k in indices] # create list of low lottery payoffs # ---------------------------------------------------------------------------------------------------- if Constants.variation == 'lottery_lo': lottery_lo = [ c(Constants.lottery_lo - (k - 1) * Constants.step_size) for k in indices ] else: lottery_lo = [c(Constants.lottery_lo) for k in indices] # create list of sure payoffs # ---------------------------------------------------------------------------------------------------- if Constants.variation == 'sure_payoff': sure_payoffs = [ c(Constants.sure_payoff + (k - 1) * Constants.step_size) for k in indices ] else: sure_payoffs = [c(Constants.sure_payoff) for k in indices] # create list of choices # ---------------------------------------------------------------------------------------------------- p.participant.vars['cem_choices'] = list( zip(indices, form_fields, probabilities, lottery_hi, lottery_lo, sure_payoffs, decision_d)) # randomly determine index/choice of binary decision to pay # ---------------------------------------------------------------------------------------------------- p.participant.vars['cem_index_to_pay'] = random.choice(indices) p.participant.vars['cem_choice_to_pay'] = 'choice_' + str( p.participant.vars['cem_index_to_pay']) # randomize order of lotteries if <random_order = True> # ---------------------------------------------------------------------------------------------------- if Constants.random_order: random.shuffle(p.participant.vars['cem_choices']) # initiate list for choices made # ---------------------------------------------------------------------------------------------------- p.participant.vars['cem_choices_made'] = [ None for j in range(1, n + 1) ] # generate random switching point for PlayerBot in tests.py # -------------------------------------------------------------------------------------------------------- for participant in self.session.get_participants(): # participant.vars['cem-bot_switching_point'] = random.randint(1, n) participant.vars['cem-bot_switching_point'] = 10
def total_payoff(self): self.sum_payoff=self.in_round(1).payoff+self.payoff self.sum_pay_euro=c(self.sum_payoff.to_real_world_currency(self.session))
class Constants(BaseConstants): name_in_url = 'matching_pennies' players_per_group = 2 num_rounds = 4 stakes = c(100)
def checkzero(self): player = self.get_players() for p in player: if p.price == 0: p.price = c(0.3)
class Player(BasePlayer): def set_under_minimum_years_left(self): self.under_minimum_years_left = self.participant.vars[ 'under_minimum_years_left'] def set_growth(self): if self.dead_remove is not True: self.herd_size_initial = self.participant.vars['herd_size'] growth_rate = random.gauss(self.session.config['growth_rate_mean'], self.session.config['growth_rate_sd']) self.participant.vars['herd_size'] = \ self.participant.vars['herd_size'] + (growth_rate * self.participant.vars['herd_size']) if self.participant.vars['herd_size'] > c( self.session.config['maxherd']): self.participant.vars['herd_size'] = c( self.session.config['maxherd']) if self.participant.vars['herd_size'] <= c(0): self.participant.vars['herd_size'] = c(0) self.herd_size_after_growth = self.participant.vars['herd_size'] if self.herd_size_after_growth < c(self.session.config['minherd']): self.under_minimum = True else: self.under_minimum = False def set_shock(self): if self.dead_remove is not True: if random.uniform(0, 1) < self.session.config['shock_rate']: self.shock_occurrence = True shock_size = random.gauss( self.session.config['shock_size_mean'], self.session.config['shock_size_sd']) self.participant.vars['herd_size'] = \ self.participant.vars['herd_size'] - (shock_size * self.participant.vars['herd_size']) else: self.shock_occurrence = False if self.participant.vars['herd_size'] > c( self.session.config['maxherd']): self.participant.vars['herd_size'] = c( self.session.config['maxherd']) if self.participant.vars['herd_size'] <= c(0): self.participant.vars['herd_size'] = c(0) self.herd_size_after_shock = self.participant.vars['herd_size'] if self.herd_size_after_shock < c(self.session.config['minherd']): self.under_minimum = True else: self.under_minimum = False def set_request_player(self): if self.group.num_playing == 2: for o in self.get_others_in_group(): if o.dead_remove is not True: self.request_player = o.id_in_group def set_dead(self): if self.dead: self.participant.vars['dead'] = True def set_remove_from_game(self): for n in range(1, Constants.num_rounds + 1): if self.in_round(n).dead is None: self.in_round(n).dead_remove = True self.in_round(Constants.num_rounds).rounds_survived = self.round_number self.set_payoff_and_dvs() def set_payoff_and_dvs(self): self.participant.payoff = sum( [p.herd_size_after_transfers for p in self.in_all_rounds()]) / Constants.num_rounds n = 0 for r in range(1, Constants.num_rounds + 1): if self.in_round(r).dead_remove is not True: n += 1 self.in_round( Constants.num_rounds).overall_total_amount_requested = sum( filter(None, [p.request_amount for p in self.in_all_rounds()])) self.in_round(Constants.num_rounds).overall_total_amount_given = sum( filter( None, [(p.sender.aggregate(tot_sent=Sum('amount_sent'))['tot_sent'] or 0) for p in self.in_all_rounds()])) self.in_round(Constants.num_rounds).overall_requested_given_diff = \ self.in_round(Constants.num_rounds).overall_total_amount_requested - \ self.in_round(Constants.num_rounds).overall_total_amount_given self.in_round( Constants.num_rounds).overall_mean_amount_requested = sum( filter(None, [p.request_amount for p in self.in_all_rounds()])) / n self.in_round(Constants.num_rounds).overall_mean_amount_given = sum( filter(None, [(p.sender.aggregate( tot_sent=Sum('amount_sent'))['tot_sent'] or 0) for p in self.in_all_rounds()])) / n a = 0 for r in range(1, Constants.num_rounds + 1): if self.in_round(r).request: a += 1 self.in_round(Constants.num_rounds).overall_num_requests_made = a # still need: # # overall_num_requests_responded_to # overall_repetitive_giving # overall_repetitive_asking def is_playing(self): return self.participant.vars['dead'] is False under_minimum_years_left = models.PositiveIntegerField() herd_size_initial = models.CurrencyField() herd_size_after_growth = models.CurrencyField() shock_occurrence = models.BooleanField() herd_size_after_shock = models.CurrencyField() request = models.BooleanField( choices=[ [True, 'Yes'], [False, 'No'], ], widget=widgets.RadioSelect(), verbose_name="Would you like to make a request for cattle?") request_player = models.IntegerField( widget=widgets.RadioSelect(), verbose_name="Which player would you like to request cattle from?") request_amount = models.CurrencyField( min=c(1), verbose_name= "How many cattle would you like to request from this player?") sr_dump = models.CharField() received = models.CurrencyField() herd_size_after_transfers = models.CurrencyField() under_minimum_years_left_end = models.PositiveIntegerField() under_minimum = models.BooleanField() dead = models.BooleanField() dead_remove = models.BooleanField() rounds_survived = models.IntegerField() overall_total_amount_requested = models.CurrencyField() overall_total_amount_given = models.CurrencyField() overall_requested_given_diff = models.CurrencyField() overall_mean_amount_requested = models.CurrencyField() overall_mean_amount_given = models.CurrencyField() overall_num_requests_made = models.IntegerField() overall_num_requests_responded_to = models.IntegerField() overall_repetitive_giving = models.IntegerField() overall_repetitive_asking = models.IntegerField()
def question(amount): return 'Would you accept an offer of {}?'.format(c(amount))
class Constants(BaseConstants): name_in_url = 'ultimatum_trial' players_per_group = 2 num_rounds = 1 endowment = c(10)
def vars_for_template(self): # Set Paid Active Round ############### if 'paid_round' not in self.participant.vars: self.participant.vars['paid_round'] = random.choice( range(self.session.config['stage_round_count'])) + 1 else: pass self.player.paid_round = self.participant.vars['paid_round'] # Get passive player rounds ### # Build Table ################# table_rows = [] for prev_player in self.player.in_all_rounds(): if prev_player.round_number != None: row = { 'round_number': prev_player.round_number, 'terminal_choice': prev_player.terminal_choice, 'score': prev_player.postStage_round_points, } table_rows.append(row) # Set active player round payoffs if prev_player.round_number == self.participant.vars[ 'paid_round']: self.player.paid_active_round_score = prev_player.postStage_round_points # set passive player round payoffs. passive_player_earnings = [] for p in self.subsession.get_players(): for pp in p.in_all_rounds(): if pp.passive_Player_Earnings != None: passive_player_earnings.append(pp.passive_Player_Earnings) if 'earnings_from_passivePlayerRound' not in self.participant.vars: self.participant.vars[ 'earnings_from_passivePlayerRound'] = random.choice( passive_player_earnings) else: pass total_points = ( self.participant.vars['final_score'] + self.participant.vars['earnings_from_passivePlayerRound'] + self.player.paid_active_round_score ) * self.participant.vars['final_score_discounter'] total_points = c(total_points).to_real_world_currency(self.session) #this logs payoffs into the otree "SessionPayments" screen, # it needs to come after prev_player.payoff is set self.session.config['participation_fee'] = c( 600).to_real_world_currency(self.session) # self.session.config['real_world_currency_per_point'] = decimal.Decimal(1.0) return { 'debug': settings.DEBUG, 'paid_round': self.participant.vars['paid_round'], 'part2_cash': self.participant.vars['final_score'], 'part3_passive': self.participant.vars['earnings_from_passivePlayerRound'], 'part3_passive2': passive_player_earnings, 'total_points': total_points, 'table_rows': table_rows, 'Role_self': self.player.player_role, 'showupfee': self.session.config['participation_fee'], 'point_aed_convert': round(1 / prev_player.participant.vars['final_score_discounter'], 2), 'final_cash': c(self.player.payoff).to_real_world_currency(self.session) + c(600).to_real_world_currency(self.session) }
def vars_for_template(self): table_rows = [] roundNum = 1 final_score = 0 for prev_player in self.player.in_all_rounds(): if prev_player.round_payoff != None: prev_player.payoff = c( prev_player.round_payoff ) * prev_player.participant.vars['final_score_discounter'] final_score += prev_player.round_payoff row = { '00_round_number': roundNum, '01_A_stage1': prev_player.A_stage1, '02_F_stage2': prev_player.F_stage2, '03_A_stage3': prev_player.A_stage3, "04_Nature": prev_player.Nature, '05_terminal_choice': prev_player.terminal_choice, '06_payoff': prev_player.payoff, } table_rows.append(row) roundNum += 1 self.player.payoff = self.player.payoff + ( self.participant.vars['final_score'] * self.participant.vars['final_score_discounter']) self.player.payoff = round_up(self.player.payoff, 5) #this logs payoffs into the otree "SessionPayments" screen, # it needs to come after prev_player.payoff is set self.session.config['participation_fee'] = c( 30).to_real_world_currency(self.session) self.session.config['real_world_currency_per_point'] = decimal.Decimal( 1.0) return { 'debug': settings.DEBUG, 'part1_score': self.participant.vars["ret_score"], 'part2_score': self.participant.vars['final_score'], 'part2_cash': (self.participant.vars['final_score'] * self.participant.vars['final_score_discounter']), 'final_score': c(round(final_score, 1)), 'part3_cash': self.player.payoff - (self.participant.vars['final_score'] * self.participant.vars['final_score_discounter']), 'table_rows': table_rows, 'Role_self': self.player.player_role, 'showupfee': self.session.config['participation_fee'], 'point_aed_convert': round(1 / prev_player.participant.vars['final_score_discounter'], 2), 'final_cash': (c(self.player.payoff).to_real_world_currency(self.session) + self.session.config['participation_fee']) }
class Constants(BaseConstants): name_in_url = 'my_public_goods' players_per_group = 3 num_rounds = 1 endowment = c(1000) # c() means it's a currency multiplier = 2
class Constants(BaseConstants): name_in_url = 'my_simple_survey2' players_per_group =3 num_rounds = 1 endowment = c(1000) multiplier = 2
def vars_for_template(self): return {'aud_per_point': c(1).to_real_world_currency(self.session)}
class Constants(BaseConstants): name_in_url = 'cynical_practice' players_per_group = None num_rounds = 5 endowment = c(100)
class Constants(BaseConstants): name_in_url = 'dictator_trial' players_per_group = 2 # 2人プレイヤー num_rounds = 1 # 1shotゲーム endowment = c(10) # 提案者の初期保有額は10ポイント
class Constants(BaseConstants): name_in_url = 'Persona5' players_per_group = None num_rounds = 10 stakes = c(100) # amount of payoff for winning
class Constants(BaseConstants): name_in_url = 'my_public_goods' players_per_group = 5 num_rounds = 12 endowment = c(100) efficiency_factor = 2
def set_payoffs(self): for p in self.get_players(): p.payoff = c(50)
def play_round(self): yield (views.Contribute, {'contribution': c(1)}) yield (views.Results)
def sent_back_amount_choices(self): return currency_range( c(0), self.sent_amount * Constants.multiplication_factor, c(1) )
def play_round(self): # compete price yield (views.Introduction) yield (views.Decide, {'price': c(30)}) yield (views.Results)
def set_payoffs(self): self.total_quantity = sum(player.quantity for player in self.get_players()) self.price = c(Constants.total_capacity - self.total_quantity) for player in self.get_players(): player.payoff = self.price * player.quantity
def tokens_to_dollars(self, value): return c(value).to_real_world_currency(self.session)
def return_from_effort(effort): return c(Constants.EFFORT_TO_RETURN[effort])
def savings_choices(self): return [[c,c.to_real_world_currency(self.session)] for c in [c(0),c(.5),c(1)]]
def creating_session(self): for p in self.get_players(): p.participant.vars['herd_size'] = c(self.session.config['initialherd']) p.participant.vars['under_minimum_years_left'] = self.session.config['years_before_death'] p.participant.vars['dead'] = False
class Constants(BaseConstants): # ---------------------------------------------------------------------------------------------------------------- # # --- Task-specific Settings --- # # ---------------------------------------------------------------------------------------------------------------- # # lottery payoffs # "high" and "low" outcomes (in currency units set in settings.py) of "lottery A" and "lottery B" # note that payoffs are identical for all choices and only probabilities of "high" and "low" outcomes change lottery_a_hi = c(90) lottery_a_lo = c(60) lottery_b_hi = c(140) lottery_b_lo = c(20) # number of binary choices between "lottery A" and "lottery B" # note that the number of choices determines the probabilities of high and low outcomes of lotteries "A" and "B" # for <num_choices = X>, the probability of outcome "high" is 1/X for the first choice, 2/X for the second, etc. num_choices = 10 # include 'certain' choice (** only applies if <variation_type = 'probability'> **) # if <certain_choice = True>, the binary choice with probability of the outcome "high" being equal to 1 is included # if <certain_choice = False>, the list only contains (<num_choices> - 1) binary decision pairs # note, however, that the probability of outcome "high" is set by <num_choices>, not (<num_choices> - 1), though # i.e., if <certain_choice = False>, the last choice implies a probability of (X - 1)/X (given <num_choices = X>) certain_choice = True # ---------------------------------------------------------------------------------------------------------------- # # --- Overall Settings and Appearance --- # # ---------------------------------------------------------------------------------------------------------------- # # show each lottery pair on a separate page # if <one_choice_per_page = True>, each single binary choice between lottery "A" and "B" is shown on a separate page # if <one_choice_per_page = False>, all <num_choices> choices are displayed in a table on one page one_choice_per_page = False # order choices between lottery pairs randomly # if <random_order = True>, the ordering of binary decisions is randomized for display # if <random_order = False>, binary choices are listed in ascending order of the probability of the "high" outcome random_order = False # enforce consistency, i.e. only allow for a single switching point # if <enforce_consistency = True>, all options "A" above a selected option "A" are automatically selected # similarly, all options "B" below a selected option "B" are automatically checked, implying consistent choices # note that <enforce_consistency> is only implemented if <one_choice_per_page = False> and <random_order = False> enforce_consistency = True # depict probabilities as percentage numbers # if <percentage = True>, the probability of outcome "high" will be displayed as percentage number # if <percentage = False>, the probabilities will be displayed as fractions, i.e. "1/X", "2/X", etc. percentage = True # show small pie charts for each lottery # if <small_pies = True>, a pie chart depicting the probabilities of outcomes is rendered next to each lottery # if <small_pies = False>, no graphical representation of probabilities is displayed small_pies = True # display lotteries in terms of large pie charts # if <large_pies = True>, lotteries are depicted as pie charts; if <large_pies = False> lotteries are list items # note that <large_pies = True> only affects the task's appearance if <one_choice_per_page = True> large_pies = True # show progress bar # if <progress_bar = True> and <one_choice_per_page = True>, a progress bar is rendered # if <progress_bar = False>, no information with respect to the advance within the task is displayed # the progress bar graphically depicts the advance within the task in terms of how many decision have been made # further, information in terms of "page x out of <num_choices>" (with x denoting the current choice) is provided progress_bar = True # show instructions page # if <instructions = True>, a separate template "Instructions.html" is rendered prior to the task # if <instructions = False>, the task starts immediately (e.g. in case of printed instructions) instructions = True # show results page summarizing the task's outcome including payoff information # if <results = True>, a separate page containing all relevant information is displayed after finishing the task # if <results = False>, the template "Decision.html" will not be rendered results = True # ---------------------------------------------------------------------------------------------------------------- # # --- oTree Settings (Don't Modify) --- # # ---------------------------------------------------------------------------------------------------------------- # name_in_url = 'risk_part2' players_per_group = None if one_choice_per_page: if certain_choice: num_rounds = num_choices else: num_rounds = num_choices - 1 else: num_rounds = 1
def set_payoff(self): self.payoff = c((min(self.demand, self.order) * Constants.price) - (self.order * self.cost))
def sent_back_amount_choices(self): return currency_range( c(0), self.group.sent_amount * Constants.multiplier, c(1) )
def set_payoffs(self, round_payoff, token_color): self.payoff = round_payoff if self.participant.vars['group_color'] == token_color: self.payoff -= c(Constants.token_store_cost_homogeneous) elif token_color != 'None': self.payoff -= c(Constants.token_store_cost_heterogeneous)
def vars_for_template(self): quiz_bonus = self.player.participant.vars["quiz_bonus"] player_contributed = self.participant.vars['player_contributed'] player_withheld = self.participant.vars['player_witholdings_total'] game_total_contrib = self.participant.vars['game_total_contrib'] carbonfund_total = self.participant.vars['carbonfund_total'] suvery_payoff = self.participant.vars["survey_payout"] participation_pay = Constants.participation_pay if game_total_contrib >= Constants.group_goal: result_message = 'Congratulations, you met the 60% group energy conservation goal of 900 energy tokens.' goal_meet = True bonus_tokens = int(game_total_contrib * 2 / 25) else: result_message = 'Sorry, you did not meet the 60% group energy conservation goal of 900 energy tokens.' goal_meet = False bonus_tokens = 0 player_contributed_usd = self.player.tokens_to_dollars( player_contributed) game_total_usd = self.player.tokens_to_dollars(game_total_contrib) quiz_bonus_usd = self.player.tokens_to_dollars(quiz_bonus) suvery_payoff_usd = self.player.tokens_to_dollars(suvery_payoff) bonus_tokens_usd = self.player.tokens_to_dollars(bonus_tokens) player_withheld_usd = self.player.tokens_to_dollars(player_withheld) participation_pay_usd = self.player.tokens_to_dollars( participation_pay) carbonfund_total_usd = self.player.tokens_to_dollars(carbonfund_total) return { 'page_title': 'Final Game Result', 'progress': 'Game', 'result_message': result_message, "goal_meet": goal_meet, "game_total_contrib": c(game_total_contrib), "game_total_usd": game_total_usd, 'bonus_tokens': c(bonus_tokens), 'bonus_tokens_usd': bonus_tokens_usd, 'quiz_bonus': c(quiz_bonus), 'quiz_bonus_usd': quiz_bonus_usd, 'suvery_payoff_usd': suvery_payoff_usd, 'player_withheld': c(player_withheld), 'player_withheld_usd': player_withheld_usd, 'player_contributed': c(player_contributed), 'player_contributed_usd': player_contributed_usd, 'carbonfund_total': c(carbonfund_total), 'carbonfund_total_usd': carbonfund_total_usd, 'participation_pay_usd': participation_pay_usd, "total_pay": quiz_bonus_usd + player_withheld_usd + participation_pay_usd + bonus_tokens_usd + suvery_payoff_usd, }
def make_field(amount): return models.BooleanField( widget=widgets.RadioSelectHorizontal, label='Would you accept an offer of {}?'.format(c(amount)))