Ondřej Bouchala
Kontakt
- email: ondrej.bouchala@fit.cvut.cz
- místnost: TH:A-1427 (14. patro staré budovy Fakulty architektury)
- osobní rozvrh
Prisoners Dilemma
| Loyal | Betray | |
|---|---|---|
| Loyal | 1 year / 1 year | 5 years / 0 years |
| Betray | 0 years / 5 years | 3 years / 3 years |
Příklad strategie: Gandhi
"""
A simple strategy that never betrays.
"""
def move(history):
"""
Determine the next move for the Prisoner's Dilemma strategy given the game history.
Parameters
----------
history : list of str
Game history as a list of two-character strings: 'LL', 'LB', 'BL', or 'BB'.
Each pair represents one round where the first character is this player's
move and the second character is the opponent's move. 'L' denotes "Loyal"
(cooperate) and 'B' denotes "Betray" (defect). The last element in the list
is the most recent round. Example: ['LL', 'LB', 'BB'].
Returns
-------
str
A single-character string indicating the chosen move for the next round:
- 'L' — Loyal (cooperate)
- 'B' — Betray (defect)
Notes
-----
- The function must always return either 'L' or 'B'.
- If history is empty, the strategy should still return a valid move.
"""
return "L"Příklad vyhodnocení zápasu:
from strategy_A import move as move_A
from strategy_B import move as move_B
def get_scores(rounds=100):
game = [] # ['LB', 'LL', ...]
score_A = 0
score_B = 0
for i in range(rounds):
game_history_A = game
game_history_B = [b + a for a, b in game] # swap moves for opponent
next_move_A = move_A(game_history_A)
next_move_B = move_B(game_history_B)
if next_move_A == "L" and next_move_B == "L":
score_A += 1
score_B += 1
elif next_move_A == "L" and next_move_B == "B":
score_A += 5
score_B += 0
elif next_move_A == "B" and next_move_B == "L":
score_A += 0
score_B += 5
elif next_move_A == "B" and next_move_B == "B":
score_A += 3
score_B += 3
game.append(next_move_A + next_move_B)
return score_A, score_B, game
if __name__ == "__main__":
score_A, score_B, game = get_scores(rounds=10)
print(f"Final years: A={score_A}, B={score_B} (lower is better)")
print("Game history:", game)