Import the existing local project snapshot. This commit organizes existing files; it does not represent newly developed functionality.
117 lines
2.7 KiB
Python
117 lines
2.7 KiB
Python
import random
|
|
from nash.game import Game
|
|
from nash.simulation import Simulation
|
|
|
|
MATCHING_PENNIES = Game.load_from_json("games/matching_pennies.json")
|
|
|
|
|
|
def test_best_response_logging():
|
|
logs = []
|
|
|
|
def debug_logger(player_index, message):
|
|
logs.append((player_index, message))
|
|
|
|
rng = random.Random(42)
|
|
sim = Simulation(
|
|
MATCHING_PENNIES,
|
|
num_rounds=5,
|
|
player_kind="best_response",
|
|
debug_logger=debug_logger,
|
|
rng=rng,
|
|
)
|
|
sim.run()
|
|
|
|
# Ensure logs were captured
|
|
assert len(logs) > 0
|
|
|
|
# Ensure that each player logs (since num_rounds=5, rounds 1-4 should have history)
|
|
players_logged = {player_idx for player_idx, _ in logs}
|
|
assert players_logged == {0, 1}
|
|
|
|
# Verify message contents
|
|
for _, message in logs:
|
|
assert "expected payoffs:" in message
|
|
|
|
|
|
def test_softmax_logging():
|
|
logs = []
|
|
|
|
def debug_logger(player_index, message):
|
|
logs.append((player_index, message))
|
|
|
|
rng = random.Random(42)
|
|
sim = Simulation(
|
|
MATCHING_PENNIES,
|
|
num_rounds=5,
|
|
player_kind="softmax",
|
|
debug_logger=debug_logger,
|
|
rng=rng,
|
|
)
|
|
sim.run()
|
|
|
|
assert len(logs) > 0
|
|
|
|
players_logged = {player_idx for player_idx, _ in logs}
|
|
assert players_logged == {0, 1}
|
|
|
|
for _, message in logs:
|
|
assert "expected payoffs:" in message
|
|
|
|
|
|
def test_regret_matching_logging():
|
|
logs = []
|
|
|
|
def debug_logger(player_index, message):
|
|
logs.append((player_index, message))
|
|
|
|
rng = random.Random(42)
|
|
sim = Simulation(
|
|
MATCHING_PENNIES,
|
|
num_rounds=5,
|
|
player_kind="regret_matching",
|
|
debug_logger=debug_logger,
|
|
rng=rng,
|
|
)
|
|
sim.run()
|
|
|
|
assert len(logs) > 0
|
|
|
|
players_logged = {player_idx for player_idx, _ in logs}
|
|
assert players_logged == {0, 1}
|
|
|
|
# Regret matching logs cumulative regret and probabilities on every round
|
|
# 5 rounds * 2 players = 10 logs
|
|
assert len(logs) == 10
|
|
|
|
for _, message in logs:
|
|
assert "cumulative regret:" in message
|
|
assert "probabilities:" in message
|
|
|
|
|
|
def test_q_learning_logging():
|
|
logs = []
|
|
|
|
def debug_logger(player_index, message):
|
|
logs.append((player_index, message))
|
|
|
|
rng = random.Random(42)
|
|
sim = Simulation(
|
|
MATCHING_PENNIES,
|
|
num_rounds=5,
|
|
player_kind="q_learning",
|
|
debug_logger=debug_logger,
|
|
rng=rng,
|
|
)
|
|
sim.run()
|
|
|
|
assert len(logs) > 0
|
|
|
|
players_logged = {player_idx for player_idx, _ in logs}
|
|
assert players_logged == {0, 1}
|
|
|
|
# Q-learning logs Q-values for state on every round
|
|
assert len(logs) == 10
|
|
|
|
for _, message in logs:
|
|
assert "Q-values for state" in message
|