Import source: Shayan1/Prod/nash/visualize
Import the existing local project snapshot. This commit organizes existing files; it does not represent newly developed functionality.
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import matplotlib
|
||||
|
||||
matplotlib.use("Agg")
|
||||
import matplotlib.pyplot as plt
|
||||
|
||||
|
||||
def print_round(round_num, game, profile, payoffs):
|
||||
parts = []
|
||||
for player_idx in range(game.num_players):
|
||||
name = game.players[player_idx]
|
||||
strategy = game.strategy_name(player_idx, profile[player_idx])
|
||||
parts.append(f"{name}: {strategy} (payoff {payoffs[player_idx]:g})")
|
||||
print(f"Round {round_num}: " + " | ".join(parts))
|
||||
|
||||
|
||||
def print_history(game, simulation):
|
||||
for round_num, (profile, payoffs) in enumerate(
|
||||
zip(simulation.history, simulation.payoff_history), start=1
|
||||
):
|
||||
print_round(round_num, game, profile, payoffs)
|
||||
|
||||
|
||||
def plot_strategy_frequencies(game, simulation, save_path):
|
||||
frequencies = simulation.strategy_frequencies()
|
||||
num_players = game.num_players
|
||||
rounds = range(1, len(simulation.history) + 1)
|
||||
|
||||
fig, axes = plt.subplots(1, num_players, figsize=(6 * num_players, 4), squeeze=False)
|
||||
for player_idx in range(num_players):
|
||||
ax = axes[0][player_idx]
|
||||
for strategy_idx, name in enumerate(game.strategies[player_idx]):
|
||||
ax.plot(rounds, frequencies[player_idx][strategy_idx], label=name)
|
||||
ax.set_title(game.players[player_idx])
|
||||
ax.set_xlabel("Round")
|
||||
ax.set_ylabel("Strategy usage frequency")
|
||||
ax.set_ylim(-0.05, 1.05)
|
||||
ax.legend()
|
||||
|
||||
fig.tight_layout()
|
||||
fig.savefig(save_path)
|
||||
plt.close(fig)
|
||||
return save_path
|
||||
|
||||
def plot_evolutionary_trajectories(game, evo_sim, save_path):
|
||||
num_players = game.num_players
|
||||
rounds = list(range(1, len(evo_sim.history) + 1))
|
||||
|
||||
fig, axes = plt.subplots(1, num_players, figsize=(6 * num_players, 4), squeeze=False)
|
||||
for player_idx in range(num_players):
|
||||
ax = axes[0][player_idx]
|
||||
for strategy_idx, name in enumerate(game.strategies[player_idx]):
|
||||
probs = [h[player_idx][strategy_idx] for h in evo_sim.history]
|
||||
ax.plot(rounds, probs, label=name)
|
||||
ax.set_title(game.players[player_idx])
|
||||
ax.set_xlabel("Round")
|
||||
ax.set_ylabel("Strategy Probability")
|
||||
ax.set_ylim(-0.05, 1.05)
|
||||
ax.legend()
|
||||
|
||||
fig.tight_layout()
|
||||
fig.savefig(save_path)
|
||||
plt.close(fig)
|
||||
return save_path
|
||||
Reference in New Issue
Block a user