From 6c5a95f3b4e121f39bbe37fc38d1e3f9b8ec4786 Mon Sep 17 00:00:00 2001 From: arsam Date: Fri, 18 Sep 2026 23:14:53 +0330 Subject: [PATCH] Import tests: Shayan1/Prod/tests/test_simulation Import the existing local project snapshot. This commit organizes existing files; it does not represent newly developed functionality. --- Shayan1/Prod/tests/test_simulation.py | 59 +++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 Shayan1/Prod/tests/test_simulation.py diff --git a/Shayan1/Prod/tests/test_simulation.py b/Shayan1/Prod/tests/test_simulation.py new file mode 100644 index 0000000..d018f76 --- /dev/null +++ b/Shayan1/Prod/tests/test_simulation.py @@ -0,0 +1,59 @@ +import numpy as np +import random + +from nash.game import Game +from nash.simulation import Simulation, EvolutionarySimulation + +PRICING = Game.load_from_json("games/pricing.json") +MATCHING_PENNIES = Game.load_from_json("games/matching_pennies.json") + + +def test_pricing_simulation_converges_to_low_low_with_memory_on(): + rng = random.Random(1) + simulation = Simulation(PRICING, num_rounds=10, memory=True, rng=rng) + simulation.run() + + converged_round = simulation.converged_round() + assert converged_round is not None + assert simulation.converged_profile() == (1, 1) + + +def test_matching_pennies_frequencies_trend_toward_fifty_fifty_with_memory_on(): + rng = random.Random(7) + simulation = Simulation(MATCHING_PENNIES, num_rounds=300, memory=True, rng=rng) + simulation.run() + + frequencies = simulation.strategy_frequencies() + for player_idx in range(2): + final_freqs = [frequencies[player_idx][s][-1] for s in range(2)] + assert abs(final_freqs[0] - 0.5) < 0.15 + assert abs(final_freqs[1] - 0.5) < 0.15 + +def test_evolutionary_simulation_initialization(): + # 2 players, pricing game has 2 strategies each + sim = EvolutionarySimulation(PRICING, num_rounds=5) + assert len(sim.populations) == 2 + assert np.allclose(sim.populations[0], [0.5, 0.5]) + assert np.allclose(sim.populations[1], [0.5, 0.5]) + assert len(sim.history) == 0 + +def test_evolutionary_simulation_run_pricing(): + sim = EvolutionarySimulation(PRICING, num_rounds=5) + history = sim.run() + assert len(history) == 5 + assert len(sim.history) == 5 + for pop_profile in history: + assert len(pop_profile) == 2 + # check each player's population is normalized + assert np.allclose(np.sum(pop_profile[0]), 1.0) + assert np.allclose(np.sum(pop_profile[1]), 1.0) + +def test_evolutionary_simulation_matching_pennies(): + # Matching pennies has no pure Nash equilibrium, populations should cycle/fluctuate + sim = EvolutionarySimulation(MATCHING_PENNIES, num_rounds=100) + sim.run() + assert len(sim.history) == 100 + # Final populations should be valid probability distributions + for pop in sim.populations: + assert np.allclose(np.sum(pop), 1.0) + assert np.all(pop >= 0)