# --------------------------------------------------------- # Copyright (c) Microsoft Corporation. All rights reserved. # --------------------------------------------------------- import logging import random import pickle import joblib import numpy as np import pandas as pd import xgboost as xgb # from azureml.automl.core.shared import log_server # from azureml.telemetry import INSTRUMENTATION_KEY from inference_schema.parameter_types.numpy_parameter_type import NumpyParameterType from inference_schema.parameter_types.standard_py_parameter_type import StandardPythonParameterType from inference_schema.schema_decorators import input_schema, output_schema from otree.api import ( models, BaseConstants, BaseSubsession, BaseGroup, BasePlayer, Currency as c, ) author = ' ' doc = """ Public goods with punishment, roughly based on Fehr & Gaechter 2000. """ class Constants(BaseConstants): name_in_url = 'pgg' players_per_group = 4 num_rounds = 20 # ENDOWMENT = 20 endowment_low = c(20) endowment_high = c(40) ENDOWMENT = [20, 20, 40, 40] MULTIPLIER = 1.6 MAX_SCORE = 100 token_per_RMB = 3 belief_payment = 2 belief_error = 0 rank_punish = 10 WaitAIScore = 30 class Subsession(BaseSubsession): treatment = models.FloatField() def creating_session(self): # self.group_randomly() # 只在第1轮和第11轮随机分组 if self.round_number == 1 or self.round_number == 11: self.group_randomly() elif self.round_number <= 10: self.group_like_round(1) else: # assuming there are 20 rounds self.group_like_round(11) if self.session.config['treatment'] == 'Baseline': self.session.vars['treatment'] = 1 elif self.session.config['treatment'] == 'SocialScore': self.session.vars['treatment'] = 2 elif self.session.config['treatment'] == 'SocialScorePunish': self.session.vars['treatment'] = 3 elif self.session.config['treatment'] == 'SocialScorePunishRank': self.session.vars['treatment'] = 4 elif self.session.config['treatment'] == 'AIScore': self.session.vars['treatment'] = 5 elif self.session.config['treatment'] == 'AIScorePunish': self.session.vars['treatment'] = 6 self.treatment = self.session.vars['treatment'] class Group(BaseGroup): group_contribution_sum = models.CurrencyField() public_account_payoff = models.CurrencyField() random1 = models.IntegerField() random2 = models.IntegerField() random3 = models.IntegerField() random4 = models.IntegerField() random5 = models.IntegerField() # def set_endowment(self): # endowment_list = Constants.ENDOWMENT.copy() # random.shuffle(endowment_list) # # for p in self.get_players(): # p.endowment = endowment_list.pop() # print("@@@ 每个玩家的endowment", p.endowment) def set_endowment(self): if self.round_number == 1 or self.round_number == 11: endowment_list = Constants.ENDOWMENT.copy() random.shuffle(endowment_list) for p in self.get_players(): p.endowment = endowment_list.pop() elif self.round_number <= 10: for p in self.get_players(): p.endowment = self.in_round(1).get_player_by_id(p.id_in_group).endowment else: # assuming there are 20 rounds for p in self.get_players(): p.endowment = self.in_round(11).get_player_by_id(p.id_in_group).endowment def set_group_account(self): self.group_contribution_sum = sum([p.contribution for p in self.get_players()]) print("@@@ 小组贡献总和", self.group_contribution_sum) self.public_account_payoff = self.group_contribution_sum * Constants.MULTIPLIER / Constants.players_per_group print("@@@ 公共账户收益", self.public_account_payoff) # 如果只作为中介运算的话,就不需要self.但是如果后面还要调用这个值的话,就要加上self.(也就是data表里会显示的部分) for p in self.get_players(): p.group_contribution_sum = self.group_contribution_sum p.public_account_payoff = self.public_account_payoff def set_score(self): if self.subsession.treatment == 2 or self.subsession.treatment == 3 or self.subsession.treatment == 4: for p in self.get_players(): # Calculate average score received self_field = 'score_p{}'.format(p.id_in_group) scores_received = [getattr(other, self_field) for other in p.get_others_in_group()] average_scores_received1 = sum(scores_received) / 3 p.average_scores_received = round(float(average_scores_received1), 2) print("@@@ average_scores_received", p.average_scores_received) # Calculate scores received from each player other_players = p.get_others_in_group() for i, other in enumerate(other_players): score_field = 'score_received_from_p{}'.format(other.id_in_group) setattr(p, score_field, getattr(other, self_field)) if self.round_number in [1, 11, 20]: if p.id_in_group == 1: p.score_diff_p2 = abs(p.score_received_from_p2 - p.belief_p2_score) p.score_diff_p3 = abs(p.score_received_from_p3 - p.belief_p3_score) p.score_diff_p4 = abs(p.score_received_from_p4 - p.belief_p4_score) elif p.id_in_group == 2: p.score_diff_p1 = abs(p.score_received_from_p1 - p.belief_p1_score) p.score_diff_p3 = abs(p.score_received_from_p3 - p.belief_p3_score) p.score_diff_p4 = abs(p.score_received_from_p4 - p.belief_p4_score) elif p.id_in_group == 3: p.score_diff_p1 = abs(p.score_received_from_p1 - p.belief_p1_score) p.score_diff_p2 = abs(p.score_received_from_p2 - p.belief_p2_score) p.score_diff_p4 = abs(p.score_received_from_p4 - p.belief_p4_score) elif p.id_in_group == 4: p.score_diff_p1 = abs(p.score_received_from_p1 - p.belief_p1_score) p.score_diff_p2 = abs(p.score_received_from_p2 - p.belief_p2_score) p.score_diff_p3 = abs(p.score_received_from_p3 - p.belief_p3_score) # TODO: if self.subsession.treatment == 5 or self.subsession.treatment == 6: p.average_scores_received = Azure模型生成的预测得分 elif self.subsession.treatment == 5 or self.subsession.treatment == 6: for p in self.get_players(): p.group_contribution_sum = self.group_contribution_sum p.public_account_payoff = self.public_account_payoff if p.id_in_group == 1: p.endowment_other1 = self.get_player_by_id(2).endowment p.endowment_other2 = self.get_player_by_id(3).endowment p.endowment_other3 = self.get_player_by_id(4).endowment p.contribution_other1 = self.get_player_by_id(2).contribution p.contribution_other2 = self.get_player_by_id(3).contribution p.contribution_other3 = self.get_player_by_id(4).contribution p.contribution_prop_other1 = self.get_player_by_id(2).contribution_prop p.contribution_prop_other2 = self.get_player_by_id(3).contribution_prop p.contribution_prop_other3 = self.get_player_by_id(4).contribution_prop elif p.id_in_group == 2: p.endowment_other1 = self.get_player_by_id(1).endowment p.endowment_other2 = self.get_player_by_id(3).endowment p.endowment_other3 = self.get_player_by_id(4).endowment p.contribution_other1 = self.get_player_by_id(1).contribution p.contribution_other2 = self.get_player_by_id(3).contribution p.contribution_other3 = self.get_player_by_id(4).contribution p.contribution_prop_other1 = self.get_player_by_id(1).contribution_prop p.contribution_prop_other2 = self.get_player_by_id(3).contribution_prop p.contribution_prop_other3 = self.get_player_by_id(4).contribution_prop elif p.id_in_group == 3: p.endowment_other1 = self.get_player_by_id(1).endowment p.endowment_other2 = self.get_player_by_id(2).endowment p.endowment_other3 = self.get_player_by_id(4).endowment p.contribution_other1 = self.get_player_by_id(1).contribution p.contribution_other2 = self.get_player_by_id(2).contribution p.contribution_other3 = self.get_player_by_id(4).contribution p.contribution_prop_other1 = self.get_player_by_id(1).contribution_prop p.contribution_prop_other2 = self.get_player_by_id(2).contribution_prop p.contribution_prop_other3 = self.get_player_by_id(4).contribution_prop elif p.id_in_group == 4: p.endowment_other1 = self.get_player_by_id(1).endowment p.endowment_other2 = self.get_player_by_id(2).endowment p.endowment_other3 = self.get_player_by_id(3).endowment p.contribution_other1 = self.get_player_by_id(1).contribution p.contribution_other2 = self.get_player_by_id(2).contribution p.contribution_other3 = self.get_player_by_id(3).contribution p.contribution_prop_other1 = self.get_player_by_id(1).contribution_prop p.contribution_prop_other2 = self.get_player_by_id(2).contribution_prop p.contribution_prop_other3 = self.get_player_by_id(3).contribution_prop print("@@@endowment_other1", p.endowment_other1) print("@@@contribution_other2", p.contribution_other2) def set_AIscore_diff(self): if self.subsession.treatment == 5 or self.subsession.treatment == 6: for p in self.get_players(): other_players = p.get_others_in_group() for i, other in enumerate(other_players): score_field = 'p{}_score_received_from_AI'.format(other.id_in_group) setattr(p, score_field, other.average_AIscores_received) if self.round_number in [1, 11, 20]: if p.id_in_group == 1: p.score_diff_p1 = abs(p.average_AIscores_received - p.belief_AIscore_you) p.score_diff_p2 = abs(p.p2_score_received_from_AI - p.belief_AIscore_p2) p.score_diff_p3 = abs(p.p3_score_received_from_AI - p.belief_AIscore_p3) p.score_diff_p4 = abs(p.p4_score_received_from_AI - p.belief_AIscore_p4) elif p.id_in_group == 2: p.score_diff_p1 = abs(p.p1_score_received_from_AI - p.belief_AIscore_p1) p.score_diff_p2 = abs(p.average_AIscores_received - p.belief_AIscore_you) p.score_diff_p3 = abs(p.p3_score_received_from_AI - p.belief_AIscore_p3) p.score_diff_p4 = abs(p.p4_score_received_from_AI - p.belief_AIscore_p4) elif p.id_in_group == 3: p.score_diff_p1 = abs(p.p1_score_received_from_AI - p.belief_AIscore_p1) p.score_diff_p2 = abs(p.p2_score_received_from_AI - p.belief_AIscore_p2) p.score_diff_p3 = abs(p.average_AIscores_received - p.belief_AIscore_you) p.score_diff_p4 = abs(p.p4_score_received_from_AI - p.belief_AIscore_p4) elif p.id_in_group == 4: p.score_diff_p1 = abs(p.p1_score_received_from_AI - p.belief_AIscore_p1) p.score_diff_p2 = abs(p.p2_score_received_from_AI - p.belief_AIscore_p2) p.score_diff_p3 = abs(p.p3_score_received_from_AI - p.belief_AIscore_p3) p.score_diff_p4 = abs(p.average_AIscores_received - p.belief_AIscore_you) def set_rate(self): # 给小组内四个玩家的平均分进行排序 # TODO: 检查新的排名代码是否完全随机 # First, create a list of tuples with each player's id and their average score received player_scores = [(p.id_in_group, float(p.average_scores_received)) for p in self.get_players()] print("@@@ player_scores", player_scores) # Then, sort the list in descending order based on the second element of each tuple (the average score received) sorted_scores = sorted(player_scores, key=lambda x: x[1], reverse=True) print("@@@ sorted_scores", sorted_scores) # Finally, create a new variable 'rate' that assigns a rank to each player based on their sorted score rate = [] for p in self.get_players(): try: index = sorted_scores.index((p.id_in_group, float(p.average_scores_received))) rank = index + 1 except ValueError: rank = None rate.append(rank) p.score_rate = rate[-1] print("@@@ score_rate", p.score_rate) def set_payoff(self): # 这部分移到了set_score之前 # self.group_contribution_sum = sum([p.contribution for p in self.get_players()]) # print("@@@ 小组贡献总和", self.group_contribution_sum) # self.public_account_payoff = self.group_contribution_sum * Constants.MULTIPLIER / Constants.players_per_group # print("@@@ 公共账户收益", self.public_account_payoff) # # 如果只作为中介运算的话,就不需要self.但是如果后面还要调用这个值的话,就要加上self.(也就是data表里会显示的部分) for p in self.get_players(): if self.subsession.treatment == 1 or self.subsession.treatment == 2 or self.subsession.treatment == 5: p.pgg_payoff = p.endowment - p.contribution + self.public_account_payoff p.payoff = p.pgg_payoff print("@@@ 本轮payoff", p.payoff) elif self.subsession.treatment == 3: p.punish = (Constants.MAX_SCORE - p.average_scores_received)/10 p.pgg_payoff = p.endowment - p.contribution + self.public_account_payoff p.payoff = p.pgg_payoff - p.punish print("@@@ 本轮payoff", p.payoff) elif self.subsession.treatment == 6: p.punish = (Constants.MAX_SCORE - p.average_AIscores_received)/10 p.pgg_payoff = p.endowment - p.contribution + self.public_account_payoff p.payoff = p.pgg_payoff - p.punish print("@@@ 本轮payoff", p.payoff) elif self.subsession.treatment == 4: if p.score_rate == 4: p.punish = Constants.rank_punish elif p.score_rate < 4: p.punish = 0 p.pgg_payoff = p.endowment - p.contribution + self.public_account_payoff p.payoff = p.pgg_payoff - p.punish print("@@@ 本轮payoff", p.payoff) def set_random12(self): if self.round_number == Constants.num_rounds: self.random1 = random.randint(1, 10) self.random2 = random.randint(11, 20) for p in self.get_players(): p.random1_payoff = p.in_round(self.random1).payoff p.random2_payoff = p.in_round(self.random2).payoff p.total_payoff = p.random1_payoff + p.random2_payoff def set_random345(self): if self.round_number == Constants.num_rounds: if self.subsession.treatment == 2 or self.subsession.treatment == 3 or self.subsession.treatment == 4: self.random3 = random.randint(1, 3) print("@@@ random3", self.random3) self.random4 = random.randint(1, 3) print("@@@ random4", self.random4) self.random5 = random.randint(1, 3) print("@@@ random5", self.random5) for p in self.get_players(): if self.random3 == 1: if p.in_round(1).id_in_group <= 1: if p.in_round(1).score_diff_p2 == Constants.belief_error: p.random3_belief = 1 else: p.random3_belief = 0 elif p.in_round(1).id_in_group > 1: if p.in_round(1).score_diff_p1 == Constants.belief_error: p.random3_belief = 1 else: p.random3_belief = 0 elif self.random3 == 2: if p.in_round(1).id_in_group <= 2: if p.in_round(1).score_diff_p3 == Constants.belief_error: p.random3_belief = 1 else: p.random3_belief = 0 elif p.in_round(1).id_in_group > 2: if p.in_round(1).score_diff_p2 == Constants.belief_error: p.random3_belief = 1 else: p.random3_belief = 0 elif self.random3 == 3: if p.in_round(1).id_in_group <= 3: if p.in_round(1).score_diff_p4 == Constants.belief_error: p.random3_belief = 1 else: p.random3_belief = 0 elif p.in_round(1).id_in_group > 3: if p.in_round(1).score_diff_p3 == Constants.belief_error: p.random3_belief = 1 else: p.random3_belief = 0 if self.random4 == 1: if p.in_round(11).id_in_group <= 1: if p.in_round(11).score_diff_p2 == Constants.belief_error: p.random4_belief = 1 else: p.random4_belief = 0 elif p.in_round(11).id_in_group > 1: if p.in_round(11).score_diff_p1 == Constants.belief_error: p.random4_belief = 1 else: p.random4_belief = 0 elif self.random4 == 2: if p.in_round(11).id_in_group <= 2: if p.in_round(11).score_diff_p3 == Constants.belief_error: p.random4_belief = 1 else: p.random4_belief = 0 elif p.in_round(11).id_in_group > 2: if p.in_round(11).score_diff_p2 == Constants.belief_error: p.random4_belief = 1 else: p.random4_belief = 0 elif self.random4 == 3: if p.in_round(11).id_in_group <= 3: if p.in_round(11).score_diff_p4 == Constants.belief_error: p.random4_belief = 1 else: p.random4_belief = 0 elif p.in_round(11).id_in_group > 3: if p.in_round(11).score_diff_p3 == Constants.belief_error: p.random4_belief = 1 else: p.random4_belief = 0 if self.random5 == 1: if p.in_round(20).id_in_group <= 1: if p.in_round(20).score_diff_p2 == Constants.belief_error: p.random5_belief = 1 else: p.random5_belief = 0 elif p.in_round(20).id_in_group > 1: if p.in_round(20).score_diff_p1 == Constants.belief_error: p.random5_belief = 1 else: p.random5_belief = 0 elif self.random5 == 2: if p.in_round(20).id_in_group <= 2: if p.in_round(20).score_diff_p3 == Constants.belief_error: p.random5_belief = 1 else: p.random5_belief = 0 elif p.in_round(20).id_in_group > 2: if p.in_round(20).score_diff_p2 == Constants.belief_error: p.random5_belief = 1 else: p.random5_belief = 0 elif self.random5 == 3: if p.in_round(20).id_in_group <= 3: if p.in_round(20).score_diff_p4 == Constants.belief_error: p.random5_belief = 1 else: p.random5_belief = 0 elif p.in_round(20).id_in_group > 3: if p.in_round(20).score_diff_p3 == Constants.belief_error: p.random5_belief = 1 else: p.random5_belief = 0 # count number of random payoffs equal to 2 p.belief_num = p.random3_belief + p.random4_belief + p.random5_belief # calculate belief payoff as twice the belief number p.belief_payoff = Constants.belief_payment * p.belief_num elif self.subsession.treatment == 5 or self.subsession.treatment == 6: self.random3 = random.randint(1, 4) print("@@@ random3", self.random3) self.random4 = random.randint(1, 4) print("@@@ random4", self.random4) self.random5 = random.randint(1, 4) print("@@@ random5", self.random5) for p in self.get_players(): if self.random3 == 1: if p.in_round(1).score_diff_p1 == Constants.belief_error: p.random3_belief = 1 else: p.random3_belief = 0 elif self.random3 == 2: if p.in_round(1).score_diff_p2 == Constants.belief_error: p.random3_belief = 1 else: p.random3_belief = 0 elif self.random3 == 3: if p.in_round(1).score_diff_p3 == Constants.belief_error: p.random3_belief = 1 else: p.random3_belief = 0 elif self.random3 == 4: if p.in_round(1).score_diff_p4 == Constants.belief_error: p.random3_belief = 1 else: p.random3_belief = 0 if self.random4 == 1: if p.in_round(11).score_diff_p1 == Constants.belief_error: p.random4_belief = 1 else: p.random4_belief = 0 elif self.random4 == 2: if p.in_round(11).score_diff_p2 == Constants.belief_error: p.random4_belief = 1 else: p.random4_belief = 0 elif self.random4 == 3: if p.in_round(11).score_diff_p3 == Constants.belief_error: p.random4_belief = 1 else: p.random4_belief = 0 elif self.random4 == 4: if p.in_round(11).score_diff_p4 == Constants.belief_error: p.random4_belief = 1 else: p.random4_belief = 0 if self.random5 == 1: if p.in_round(20).score_diff_p1 == Constants.belief_error: p.random5_belief = 1 else: p.random5_belief = 0 elif self.random5 == 2: if p.in_round(20).score_diff_p2 == Constants.belief_error: p.random5_belief = 1 else: p.random5_belief = 0 elif self.random5 == 3: if p.in_round(20).score_diff_p3 == Constants.belief_error: p.random5_belief = 1 else: p.random5_belief = 0 elif self.random5 == 4: if p.in_round(20).score_diff_p4 == Constants.belief_error: p.random5_belief = 1 else: p.random5_belief = 0 # count number of random payoffs equal to 2 p.belief_num = p.random3_belief + p.random4_belief + p.random5_belief # calculate belief payoff as twice the belief number p.belief_payoff = Constants.belief_payment * p.belief_num def get_fields(self, prefix=''): fields = [] for p in self.player.get_others_in_group(): if self.subsession.treatment == 2 or self.subsession.treatment == 3 or self.subsession.treatment == 4: fields.append('{}score_p{}'.format(prefix, p.id_in_group)) if self.round_number in [1, 11, 20]: fields.append('{}belief_p{}_score'.format(prefix, p.id_in_group)) print('@@@get_fields', fields) elif self.subsession.treatment == 5 or self.subsession.treatment == 6: if self.round_number in [1, 11, 20]: fields.append('belief_AIscore_you') fields.append('{}belief_AIscore_p{}'.format(prefix, p.id_in_group)) print('@@@get_ai_fields', fields) return fields class Player(BasePlayer): # for public good game endowment = models.CurrencyField() contribution = models.CurrencyField(min=0, label="请决定你要从初始资产中拿出多少代币投资给小组账户(可保留两位小数)") contribution_prop = models.StringField() def contribution_max(self): return self.endowment def calculate_proportion(self): self.contribution_prop = "{:.1f}".format(100 * (float(self.contribution) / float(self.endowment))) # for score def make_score_field(id_in_group): return models.IntegerField(min=0, max=Constants.MAX_SCORE, label="请给 玩家 {} 评分:".format(id_in_group)) score_p1 = make_score_field(1) score_p2 = make_score_field(2) score_p3 = make_score_field(3) score_p4 = make_score_field(4) # for belief_score def make_belief_score_field(id_in_group): return models.IntegerField(min=0, max=Constants.MAX_SCORE, label="你认为 玩家 {} 给你的评分是:".format(id_in_group)) belief_p1_score = make_belief_score_field(1) belief_p2_score = make_belief_score_field(2) belief_p3_score = make_belief_score_field(3) belief_p4_score = make_belief_score_field(4) # for belief_AIscore def make_belief_AIscore_field(id_in_group): return models.IntegerField(min=0, max=Constants.MAX_SCORE, label="你认为 AI 给 玩家 {} 的评分是(请输入0-100之间的整数):".format(id_in_group)) belief_AIscore_p1 = make_belief_AIscore_field(1) belief_AIscore_p2 = make_belief_AIscore_field(2) belief_AIscore_p3 = make_belief_AIscore_field(3) belief_AIscore_p4 = make_belief_AIscore_field(4) belief_AIscore_you = models.IntegerField(min=0, max=Constants.MAX_SCORE, label="你认为 AI 给 你 的评分是(请输入0-100之间的整数):") # for score received def make_score_received_field(id_in_group): return models.IntegerField(min=0, max=Constants.MAX_SCORE) score_received_from_p1 = make_score_received_field(1) score_received_from_p2 = make_score_received_field(2) score_received_from_p3 = make_score_received_field(3) score_received_from_p4 = make_score_received_field(4) average_scores_received = models.FloatField() score_rate = models.IntegerField() # for score received from AI def make_AIscore_received_field(id_in_group): return models.IntegerField(min=0, max=Constants.MAX_SCORE) p1_score_received_from_AI = make_AIscore_received_field(1) p2_score_received_from_AI = make_AIscore_received_field(2) p3_score_received_from_AI = make_AIscore_received_field(3) p4_score_received_from_AI = make_AIscore_received_field(4) average_AIscores_received = models.IntegerField() def make_score_diff_field(id_in_group): return models.IntegerField(min=0, max=Constants.MAX_SCORE) score_diff_p1 = make_score_diff_field(1) score_diff_p2 = make_score_diff_field(2) score_diff_p3 = make_score_diff_field(3) score_diff_p4 = make_score_diff_field(4) # for punish punish = models.CurrencyField() pgg_payoff = models.CurrencyField() # for pgg payoff random1_payoff = models.CurrencyField() random2_payoff = models.CurrencyField() total_payoff = models.CurrencyField() # for belief payoff random3_belief = models.IntegerField() random4_belief = models.IntegerField() random5_belief = models.IntegerField() belief_payoff = models.FloatField() belief_num = models.IntegerField() # for AI Score endowment_other1 = models.CurrencyField() endowment_other2 = models.CurrencyField() endowment_other3 = models.CurrencyField() contribution_other1 = models.CurrencyField() contribution_other2 = models.CurrencyField() contribution_other3 = models.CurrencyField() contribution_prop_other1 = models.StringField() contribution_prop_other2 = models.StringField() contribution_prop_other3 = models.StringField() group_contribution_sum = models.CurrencyField() public_account_payoff = models.CurrencyField() result1 = models.FloatField() result2 = models.FloatField() def set_AIScore(self): if self.subsession.treatment == 5 or self.subsession.treatment == 6: data_sample = pd.DataFrame( {"endowment": pd.Series([int(self.endowment)], dtype="int64"), "contribution": pd.Series([float(self.contribution)], dtype="float64"), "contribution_prop": pd.Series([float(self.contribution_prop)], dtype="float64"), "contribution_sum": pd.Series([float(self.group_contribution_sum)], dtype="float64"), "round_number": pd.Series([int(self.round_number)], dtype="int64"), "public_account_payoff": pd.Series([float(self.public_account_payoff)], dtype="float64"), "endowment_other1": pd.Series([int(self.endowment_other1)], dtype="int64"), "endowment_other2": pd.Series([int(self.endowment_other2)], dtype="int64"), "endowment_other3": pd.Series([int(self.endowment_other3)], dtype="int64"), "contribution_other1": pd.Series([float(self.contribution_other1)], dtype="float64"), "contribution_other2": pd.Series([float(self.contribution_other2)], dtype="float64"), "contribution_other3": pd.Series([float(self.contribution_other3)], dtype="float64"), "contribution_prop_other1": pd.Series([float(self.contribution_prop_other1)], dtype="float64"), "contribution_prop_other2": pd.Series([float(self.contribution_prop_other2)], dtype="float64"), "contribution_prop_other3": pd.Series([float(self.contribution_prop_other3)], dtype="float64"), }) input_sample = {'data': data_sample} print("@@@input_sample", input_sample) result_sample = NumpyParameterType(np.array([0.0])) output_sample = StandardPythonParameterType({'Results': result_sample}) sample_global_parameters = StandardPythonParameterType(1.0) # 注释掉日志 # try: # log_server.enable_telemetry(INSTRUMENTATION_KEY) # log_server.set_verbosity('INFO') # logger = logging.getLogger('azureml.automl.core.scoring_script_v2') # except: # pass def init(): global model # 以下为按treatment调用不同路径的模型 if self.subsession.treatment == 5: model_path = 'pgg/model5.pkl' print("@@@位置", model_path) model = joblib.load(model_path) elif self.subsession.treatment == 6: model_path = 'pgg/model6.pkl' print("@@@位置", model_path) model = joblib.load(model_path) print("Model loaded successfully") # @input_schema('Inputs', input_sample) # @input_schema('GlobalParameters', sample_global_parameters, convert_to_provided_type=False) # @output_schema(output_sample) def run(Inputs, GlobalParameters=1.0): print("@@@run function begin") data = Inputs['data'] print("Input data received successfully") print("@@@Input datas", data) result = model.predict(data) print("@@@result是否有结果", result) if result is None: print("@@@set_AIScore result NONE") elif result is not None: print("@@@set_AIScore result NOT NONE") return {'Results': result.tolist()} def final(): init() result = run(input_sample) if result is None: print("@@@final result NONE") return None, None elif result is not None: print("@@@final result", result) result1 = result['Results'] result2 = result1[0] self.average_scores_received = abs(round(float(result2), 2)) self.average_AIscores_received = int(self.average_scores_received) print("@@@result1", result1) print("@@@result2", result2) print("@@@average_AIscores_received", self.average_AIscores_received) final() def trans(self): self.participant.vars['random1_trans'] = self.group.random1 self.participant.vars['random2_trans'] = self.group.random2 self.participant.vars['random1_payoff_trans'] = self.random1_payoff self.participant.vars['random2_payoff_trans'] = self.random2_payoff self.participant.vars['total_payoff_trans'] = self.total_payoff self.participant.vars['belief_payoff_trans'] = self.belief_payoff self.participant.vars['belief_num_trans'] = self.belief_num