import random alpha = 0.5 alpha_step = 0.05 beta = 0.1 def incr_alpha(): global alpha if (alpha < 1.0 - alpha_step): alpha += alpha_step def decr_alpha(): global alpha if (alpha > alpha_step): alpha -= alpha_step def play(strategy, my_moves, opponent_moves): if strategy == 'A': return play_strategyA(my_moves, opponent_moves) elif strategy == 'B': return play_strategyB(my_moves, opponent_moves) elif strategy == 'C': return play_strategyC(my_moves, opponent_moves) elif strategy == 'D': return play_strategyD(my_moves, opponent_moves) elif strategy == 'E': return play_strategyE(my_moves, opponent_moves) else: print('ERROR: unknown strategy') def update(strategy, my_moves, opponent_moves, my_score): if strategy == 'A': update_strategyA(my_moves, opponent_moves, my_score) elif strategy == 'B': update_strategyB(my_moves, opponent_moves, my_score) elif strategy == 'C': update_strategyC(my_moves, opponent_moves, my_score) elif strategy == 'D': return update_strategyD(my_moves, opponent_moves, my_score) elif strategy == 'E': return update_strategyE(my_moves, opponent_moves, my_score) else: print('ERROR: unknown strategy') def play_strategyA(my_moves, opponent_moves): # Random-Strategy: Choose cooperate with prob 0.5 if random.uniform(0,1) < 0.5: return 'cooperate' else: return 'defect' def update_strategyA(my_moves, opponent_moves, my_score): # Random-Strategy: nothing is updated pass def play_strategyB(my_moves, opponent_moves): # Strategy-TFT: # If it's the first move, cooperate # Else play the most recent opponent's move. if len(my_moves) == 0: return 'cooperate' # WRITE YOUR CODE HERE to return opponent's most recent move def update_strategyB(myMoves, opponentMoves, my_score): # Do nothing pass def play_strategyC(my_moves, opponent_moves): # Strategy-TFT-Random: # If it's the first move, pick randomly between cooperate and defect. # Else with probability beta pick randomly between cooperate and defect. # Else apply TFT (opponent's most recent move) if len(my_moves) == 0: return random.choice(['cooperate', 'defect']) # Occasionally try a random choice if random.uniform(0,1) < beta: return random.choice(['cooperate', 'defect']) # WRITE YOUR CODE HERE to return opponent's most recent move def update_strategyC(myMoves, opponentMoves, my_score): # Do nothing pass def play_strategyD(my_moves, opponent_moves): # If we haven't played 'cooperate' so far, cooperate if not ('cooperate' in my_moves): return 'cooperate' # If we haven't played 'defect' so far, defect if not ('defect' in my_moves): return 'defect' # With small prob (beta), choose randomly between the two if random.uniform(0,1) < beta: return random.choice(['cooperate', 'defect']) # Otherwise, choose according to alpha: Pr[C] = alpha if random.uniform(0,1) < alpha: return 'cooperate' else: return 'defect' def update_strategyD(my_moves, opponent_moves, my_score): # Increment alpha if my last move was 'cooperate' and the # resulting score was positive. # Increment alpha if my last move was 'defect' and the # resulting score was negative or zero. # Otherwise decrement alpha # REPLACE pass WITH YOUR CODE HERE: pass def play_strategyE(my_moves, opponent_moves): # REPLACE pass with your strategy. pass def update_strategyE(my_moves, opponent_moves, my_score): # REPLACE pass with your strategy. pass