from otree.api import ( models, widgets, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, currency_range, Page, WaitPage ) cu = c doc = '错峰出行的激励机制研究——基于通勤者出发时间选择实验(空)' class Constants(BaseConstants): name_in_url = 'OffPeakTravel4_6people' players_per_group = 6 num_rounds = 1 ticket_price = 2 class Subsession(BaseSubsession): pass def next_day(group): import random players = group.get_players() ''' 计算优先级、出行扣费、改变出发班次扣费 ''' for p in players: p.priority = random.randint(1,100) # 对之前的扣费清零 p.money_ticket = 0 p.money_crowd = 0 p.money_late = 0 p.money_wait = 0 p.money_change = 0 p.money -= Constants.ticket_price # 出行一次扣费 p.money_ticket += Constants.ticket_price p.money -= abs(p.pre_flight - p.current_flight) # 改变出发班次扣费 p.money_change += abs(p.pre_flight - p.current_flight) p.pre_flight = p.current_flight # 对下次来说,这次的班次是上次班次 p.real_flight = p.current_flight # 实际班次,将根据人数多少进行改变 ''' 计算拥挤扣费、实际班次 ''' people = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] pris = [] # 第1-7班车 for index in range(1,8): for p in players: if (p.real_flight == index): people[index] += 1 pris.append(p.priority) pris.sort() # 按优先级升序排列 if people[index] == 2: # 第index班车人数为2,拥挤扣费 for p in players: if (p.real_flight == index): p.money -= 1 p.money_crowd += 1 if people[index] >= 3: # 第index班车人数3以上,拥挤扣费 ###挤到下一班的等待扣费 for p in players: if (p.real_flight == index): if (p.priority < pris[2]): # 优先级为前2 p.money -= 1 p.money_crowd += 1 else: p.real_flight += 1 # 优先级为第3及以后 pris = [] # 第8班车 for p in players: if (p.real_flight == 8): people[8] += 1 pris.append(p.priority) pris.sort() # 按优先级升序排列 if people[8] == 2: # 第8班车人数为2,拥挤扣费 for p in players: if (p.real_flight == 8): p.money -= 1 p.money_crowd += 1 if people[8] >= 3: # 第8班车人数3以上,拥挤扣费 ###挤到下一班的等待扣费 for p in players: if (p.real_flight == 8): if (p.priority < pris[2]): # 优先级为前2 p.money -= 1 p.money_crowd += 1 else: p.real_flight += 1 # 优先级为第3及以后 # 第9班车 for p in players: if (p.real_flight == 9): people[9] += 1 if people[9] == 2: # 第8班车人数为2,拥挤扣费 for p in players: if (p.real_flight == 9): p.money -= 1 p.money_crowd += 1 ''' 计算等待扣费、迟到扣费 ''' for p in players: p.money -= p.real_flight-p.current_flight p.money_wait += p.real_flight-p.current_flight if p.real_flight > 5: p.money -= p.real_flight-5 p.money_late += p.real_flight-5 def next_day_encourage4(group): import random players = group.get_players() ''' 计算优先级、出行扣费、改变出发班次扣费 ''' for p in players: p.priority = random.randint(1,100) # 对之前的扣费清零 p.money_ticket = 0 p.money_crowd = 0 p.money_late = 0 p.money_wait = 0 p.money_change = 0 p.money -= Constants.ticket_price # 出行一次扣费 p.money_ticket += Constants.ticket_price p.money -= abs(p.pre_flight - p.current_flight) # 改变出发班次扣费 p.money_change += abs(p.pre_flight - p.current_flight) p.pre_flight = p.current_flight # 对下次来说,这次的班次是上次班次 p.real_flight = p.current_flight # 实际班次,将根据人数多少进行改变 ''' 计算拥挤扣费、实际班次 ''' people = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] pris = [] # 第1-7班车 for index in range(1,8): for p in players: if (p.real_flight == index): people[index] += 1 pris.append(p.priority) pris.sort() # 按优先级升序排列 if people[index] == 2: # 第index班车人数为2,拥挤扣费 for p in players: if (p.real_flight == index): p.money -= 1 p.money_crowd += 1 if people[index] >= 3: # 第index班车人数3以上,拥挤扣费 ###挤到下一班的等待扣费 for p in players: if (p.real_flight == index): if (p.priority < pris[2]): # 优先级为前2 p.money -= 1 p.money_crowd += 1 else: p.real_flight += 1 # 优先级为第3及以后 pris = [] # 第8班车 for p in players: if (p.real_flight == 8): people[8] += 1 pris.append(p.priority) pris.sort() # 按优先级升序排列 if people[8] == 2: # 第8班车人数为2,拥挤扣费 for p in players: if (p.real_flight == 8): p.money -= 1 p.money_crowd += 1 if people[8] >= 3: # 第8班车人数3以上,拥挤扣费 ###挤到下一班的等待扣费 for p in players: if (p.real_flight == 8): if (p.priority < pris[2]): # 优先级为前2 p.money -= 1 p.money_crowd += 1 else: p.real_flight += 1 # 优先级为第3及以后 # 第9班车 for p in players: if (p.real_flight == 9): people[9] += 1 if people[9] == 2: # 第8班车人数为2,拥挤扣费 for p in players: if (p.real_flight == 9): p.money -= 1 p.money_crowd += 1 ''' 计算等待扣费、迟到扣费 ''' for p in players: p.money -= p.real_flight-p.current_flight p.money_wait += p.real_flight-p.current_flight if p.real_flight > 5: p.money -= p.real_flight-5 p.money_late += p.real_flight-5 ''' 以下是激励机制4的新增内容: ''' for p in players: if p.current_flight<=2: p.saved_days+=1 if p.saved_days==3 and p.flag==1: p.saved_money+=5 p.flag=0 if p.current_flight>2: p.waste_days+=1 if p.waste_days==2 and p.flag2==1: p.waste_money+=5 p.flag2=0 def e4_week3_end(group): import random players = group.get_players() ''' 计算优先级、出行扣费、改变出发班次扣费 ''' for p in players: p.priority = random.randint(1,100) # 对之前的扣费清零 p.money_ticket = 0 p.money_crowd = 0 p.money_late = 0 p.money_wait = 0 p.money_change = 0 ''' 以下是week3_end的新增内容: ''' p.saved_days = 0 p.waste_days = 0 p.money -= Constants.ticket_price # 出行一次扣费 p.money_ticket += Constants.ticket_price p.money -= abs(p.pre_flight - p.current_flight) # 改变出发班次扣费 p.money_change += abs(p.pre_flight - p.current_flight) p.pre_flight = p.current_flight # 对下次来说,这次的班次是上次班次 p.real_flight = p.current_flight # 实际班次,将根据人数多少进行改变 ''' 计算拥挤扣费、实际班次 ''' people = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] pris = [] # 第1-7班车 for index in range(1,8): for p in players: if (p.real_flight == index): people[index] += 1 pris.append(p.priority) pris.sort() # 按优先级升序排列 if people[index] == 2: # 第index班车人数为2,拥挤扣费 for p in players: if (p.real_flight == index): p.money -= 1 p.money_crowd += 1 if people[index] >= 3: # 第index班车人数3以上,拥挤扣费 ###挤到下一班的等待扣费 for p in players: if (p.real_flight == index): if (p.priority < pris[2]): # 优先级为前2 p.money -= 1 p.money_crowd += 1 else: p.real_flight += 1 # 优先级为第3及以后 pris = [] # 第8班车 for p in players: if (p.real_flight == 8): people[8] += 1 pris.append(p.priority) pris.sort() # 按优先级升序排列 if people[8] == 2: # 第8班车人数为2,拥挤扣费 for p in players: if (p.real_flight == 8): p.money -= 1 p.money_crowd += 1 if people[8] >= 3: # 第8班车人数3以上,拥挤扣费 ###挤到下一班的等待扣费 for p in players: if (p.real_flight == 8): if (p.priority < pris[2]): # 优先级为前2 p.money -= 1 p.money_crowd += 1 else: p.real_flight += 1 # 优先级为第3及以后 # 第9班车 for p in players: if (p.real_flight == 9): people[9] += 1 if people[9] == 2: # 第8班车人数为2,拥挤扣费 for p in players: if (p.real_flight == 9): p.money -= 1 p.money_crowd += 1 ''' 计算等待扣费、迟到扣费 ''' for p in players: p.money -= p.real_flight-p.current_flight p.money_wait += p.real_flight-p.current_flight if p.real_flight > 5: p.money -= p.real_flight-5 p.money_late += p.real_flight-5 ''' 以下是激励机制4的新增内容: ''' for p in players: if p.current_flight<=2: p.saved_days+=1 if p.saved_days==3 and p.flag==1: p.saved_money+=5 p.flag=0 if p.current_flight>2: p.waste_days+=1 if p.waste_days==2 and p.flag2==1: p.waste_money+=5 p.flag2=0 def e4_week2_end(group): import random players = group.get_players() ''' 计算优先级、出行扣费、改变出发班次扣费 ''' for p in players: p.priority = random.randint(1,100) # 对之前的扣费清零 p.money_ticket = 0 p.money_crowd = 0 p.money_late = 0 p.money_wait = 0 p.money_change = 0 ''' 以下是week2_end的新增内容 ''' p.money = 50 p.money -= Constants.ticket_price # 出行一次扣费 p.money_ticket += Constants.ticket_price p.money -= abs(p.pre_flight - p.current_flight) # 改变出发班次扣费 p.money_change += abs(p.pre_flight - p.current_flight) p.pre_flight = p.current_flight # 对下次来说,这次的班次是上次班次 p.real_flight = p.current_flight # 实际班次,将根据人数多少进行改变 ''' 计算拥挤扣费、实际班次 ''' people = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] pris = [] # 第1-7班车 for index in range(1,8): for p in players: if (p.real_flight == index): people[index] += 1 pris.append(p.priority) pris.sort() # 按优先级升序排列 if people[index] == 2: # 第index班车人数为2,拥挤扣费 for p in players: if (p.real_flight == index): p.money -= 1 p.money_crowd += 1 if people[index] >= 3: # 第index班车人数3以上,拥挤扣费 ###挤到下一班的等待扣费 for p in players: if (p.real_flight == index): if (p.priority < pris[2]): # 优先级为前2 p.money -= 1 p.money_crowd += 1 else: p.real_flight += 1 # 优先级为第3及以后 pris = [] # 第8班车 for p in players: if (p.real_flight == 8): people[8] += 1 pris.append(p.priority) pris.sort() # 按优先级升序排列 if people[8] == 2: # 第8班车人数为2,拥挤扣费 for p in players: if (p.real_flight == 8): p.money -= 1 p.money_crowd += 1 if people[8] >= 3: # 第8班车人数3以上,拥挤扣费 ###挤到下一班的等待扣费 for p in players: if (p.real_flight == 8): if (p.priority < pris[2]): # 优先级为前2 p.money -= 1 p.money_crowd += 1 else: p.real_flight += 1 # 优先级为第3及以后 # 第9班车 for p in players: if (p.real_flight == 9): people[9] += 1 if people[9] == 2: # 第8班车人数为2,拥挤扣费 for p in players: if (p.real_flight == 9): p.money -= 1 p.money_crowd += 1 ''' 计算等待扣费、迟到扣费 ''' for p in players: p.money -= p.real_flight-p.current_flight p.money_wait += p.real_flight-p.current_flight if p.real_flight > 5: p.money -= p.real_flight-5 p.money_late += p.real_flight-5 ''' 以下是激励机制4的新增内容: ''' for p in players: if p.current_flight<=2: p.saved_days+=1 if p.saved_days==3 and p.flag==1: p.saved_money+=5 p.flag=0 if p.current_flight>2: p.waste_days+=1 if p.waste_days==2 and p.flag2==1: p.waste_money+=5 p.flag2=0 def weekend(group): players = group.get_players() for p in players: if p.saved_days >= 3: p.money += 5 p.saved_days = 0 p.waste_days = 0 p.flag=1 # 下周省钱数可以+5了 p.flag2=1 class Group(BaseGroup): next_day = next_day next_day_encourage4 = next_day_encourage4 e4_week3_end = e4_week3_end e4_week2_end = e4_week2_end weekend = weekend class Player(BasePlayer): pre_flight = models.IntegerField(label='您的习惯到达教室时间是(请输入1-7中的一项):', max=7, min=1) current_flight = models.IntegerField(label='今天您选择的班次是?(请输入1-7中的一项)', max=7, min=1) real_flight = models.IntegerField() money = models.IntegerField(initial=50) money_ticket = models.IntegerField(initial=0) money_crowd = models.IntegerField(initial=0) money_wait = models.IntegerField(initial=0) money_late = models.IntegerField(initial=0) money_change = models.IntegerField(initial=0) saved_money = models.IntegerField(initial=0) saved_days = models.IntegerField(initial=0) waste_days = models.IntegerField(initial=0) priority = models.IntegerField() flag = models.IntegerField(initial=1) flag2 = models.IntegerField(initial=1) waste_money = models.IntegerField(initial=0) def custom_export(players): yield ['participant_code', 'id_in_group'] for p in players: pp = p.participant yield [pp.code, p.id_in_group]