def move(self, game, player, history): if game == "prison": # first round action if not history: # calculate probabilities p_a = float(self.reflvl_a) / (self.reflvl_a + self.reflvl_b) p_b = 1 - p_a # p_b = float(self.reflvl_b) / (self.reflvl_a + self.reflvl_b) random.seed() rdm = random.random() #print "rdm: " + str(rdm) if rdm < p_a: #print "a" return "a" else: #print "b" return "b" # adjust reinforcfement levels if history[-1][0] == "a": self.reflvl_a = self.reflvl_a + tournament.determine_payoff("prison", "a", history[-1][1])[0] if history[-1][0] == "b": self.reflvl_b = self.reflvl_b + tournament.determine_payoff("prison", "b", history[-1][1])[0] #print "reflvl_(a,b): (" + str(self.reflvl_a) + ", " + str(self.reflvl_b) + ")" # calculate probabilities p_a = float(self.reflvl_a) / (self.reflvl_a + self.reflvl_b) p_b = 1 - p_a # p_b = float(self.reflvl_b) / (self.reflvl_a + self.reflvl_b) #print "probabilities (a,b): (" + str(p_a) + ", " + str(p_b) + ")" # choose action random.seed() rdm = random.random() #print "rdm: " + str(rdm) if rdm < p_a: #print "a" return "a" else: #print "b" return "b" else: return "b"
def move(self, game, player, history): if not history: # first round - randomize if random.randint(0,1) == 0: # play "a" or "b" with prob. 1/2 return "a" else: return "b" else: # determine payoffs of last round last_payoffs = tournament.determine_payoff(game, history[-1][0], history[-1][1]) best_result = max(last_payoffs) for i in range(len(last_payoffs)): # look up which player got the best payoff this round if last_payoffs[i] == best_result: best_key = i return history[-1][best_key] # play the best strategy of last round
def move(self, game, player, history): if not history: # first round - randomize if random.randint(0, 1) == 0: # play "a" or "b" with prob. 1/2 return "a" else: return "b" else: # determine payoffs of last round last_payoffs = tournament.determine_payoff(game, history[-1][0], history[-1][1]) if game == "prison": # for prisoners dilemma, lower values are better! best_result = max(last_payoffs) # min for inversed payoffs else: best_result = min(last_payoffs) # max for inversed payoffs for i in range(len(last_payoffs)): # look up which player got the best payoff this round if last_payoffs[i] == best_result: best_key = i return history[-1][best_key] # play the best strategy of last round
def make_payoff_history(game, history): pay_history = [] for i in range(len(history)): pay_history.append(tournament.determine_payoff(game,history[i][0],history[i][1])) return pay_history
def move(self, game, player, history): # play zero determinant strategy in prisoner's dilemma and otherwise all B if game == "prison": # set ZD strategy factors extortion_factor = 2 phi = float(1)/8 # range of phi: 0 < phi < (P-S)/((P-S) + extortion_factor*(T-P)) = 1/(1+2*extortion_factor) # set payoffs R = tournament.determine_payoff("prison", "a", "a")[0] S = tournament.determine_payoff("prison", "a", "b")[0] T = tournament.determine_payoff("prison", "b", "a")[0] P = tournament.determine_payoff("prison", "b", "b")[0] #print "R: " + str(R) #print "S: " + str(S) #print "T: " + str(T) #print "P: " + str(P) # set probabilities p1 = 1 - float(R-P)/float(P-S)*phi*(extortion_factor - 1) p2 = 1- phi*(float(T-P)/float(P-S)*extortion_factor + 1) p3 = phi*(extortion_factor + float(T-P)/float(P-S)) p4 = 0 #print "p1: " + str(p1) #print "p2: " + str(p2) #print "p3: " + str(p3) #print "p4: " + str(p4) # get random value to determine behaviour random.seed() rdm = random.random() # first round cooperate if not history: return "a" # print history #print "move: (" + str(history[-1][0]) + "," + str(history[-1][1]) + ")" # set behaviour for rounds if history[-1][0] == "a" and history[-1][1] == "a": if rdm < p1: return "a" else: return "b" if history[-1][0] == "a" and history[-1][1] == "b": if rdm < p2: return "a" else: return "b" if history[-1][0] == "b" and history[-1][1] == "a": if rdm < p3: return "a" else: return "b" if history[-1][0] == "b" and history[-1][1] == "b": if rdm < p4: return "a" else: return "b" else: return "b"
def move(self, game, player, history): # if history is empty, we are in the very first round. Let's cooperate! if game == "prison": # in Prisoners' Dilemma I play imitation. if not history: # first round - randomize if random.randint(0,1) == 0: # play "a" or "b" with prob. 1/2 return "a" else: return "b" else: # determine payoffs of last round last_payoffs = tournament.determine_payoff(game, history[-1][0], history[-1][1]) if game == "prison": # for prisoners dilemma, lower values are better! best_result = min(last_payoffs) else: best_result = max(last_payoffs) for i in range(len(last_payoffs)): # look up which player got the best payoff this round if last_payoffs[i] == best_result: best_key = i return history[-1][best_key] # play the best strategy of last round elif game == "staghunt": # in staghunt I play grimtrigger if not history: # first round cooperate return "a" else: sumB = 0 for run in history: # let's count how often opponent defected so far if run[1] == "b": sumB =+ 1 if sumB == 0: # if no defect, play a return "a" else: # if opponent ever defected, play b return "b" elif game == "chicken": # in chicken i play cournot adjustment best_response = {"chicken":{"a":"b","b":"a"}} if not history: # first round if random.randint(0,1) == 0: # play "a" or "b" with prob. 1/2 return "a" else: return "b" else: # subsequent rounds return best_response[game][history[-1][1]] elif game == "pennies": if not history: return "a"; else: if player == 1: # when playing matching pennies, player number (1 = row, 2 = column) matters: if history[-1][1] == "a": # if I am player one, I want matching choices return "a" # let's mirror what the other has done in his last round else: return "b" else: # if I am not player 1, I must be player 2, so i don't want matching choices: if history[-1][1] == "a": return "b" # let's do the opposite of what the other did last round. else: return "a"