2025-01-02 15:05:06 +00:00
|
|
|
# © Crown-owned copyright 2025, Defence Science and Technology Laboratory UK
|
2024-08-02 12:49:17 +01:00
|
|
|
from pprint import pprint
|
2024-08-05 10:34:06 +01:00
|
|
|
|
2024-08-02 12:49:17 +01:00
|
|
|
import pytest
|
2024-08-05 10:34:06 +01:00
|
|
|
import yaml
|
|
|
|
|
|
|
|
|
|
from primaite.config.load import data_manipulation_config_path
|
|
|
|
|
from primaite.game.agent.interface import AgentHistoryItem
|
|
|
|
|
from primaite.session.environment import PrimaiteGymEnv
|
2025-02-10 14:39:28 +00:00
|
|
|
from primaite.simulator import SIM_OUTPUT
|
2024-08-05 10:34:06 +01:00
|
|
|
|
2024-08-02 12:49:17 +01:00
|
|
|
|
|
|
|
|
@pytest.fixture()
|
|
|
|
|
def create_env():
|
2024-08-05 10:34:06 +01:00
|
|
|
with open(data_manipulation_config_path(), "r") as f:
|
2024-08-02 12:49:17 +01:00
|
|
|
cfg = yaml.safe_load(f)
|
|
|
|
|
|
2024-08-05 10:34:06 +01:00
|
|
|
env = PrimaiteGymEnv(env_config=cfg)
|
2024-08-02 12:49:17 +01:00
|
|
|
return env
|
|
|
|
|
|
2024-08-05 10:34:06 +01:00
|
|
|
|
2024-08-02 12:49:17 +01:00
|
|
|
def test_rng_seed_set(create_env):
|
2024-08-05 10:34:06 +01:00
|
|
|
"""Test with RNG seed set."""
|
2024-08-02 12:49:17 +01:00
|
|
|
env = create_env
|
|
|
|
|
env.reset(seed=3)
|
|
|
|
|
for i in range(100):
|
|
|
|
|
env.step(0)
|
2025-02-03 16:24:03 +00:00
|
|
|
a = [item.timestep for item in env.game.agents["client_2_green_user"].history if item.action != "do-nothing"]
|
2024-08-02 12:49:17 +01:00
|
|
|
|
|
|
|
|
env.reset(seed=3)
|
|
|
|
|
for i in range(100):
|
|
|
|
|
env.step(0)
|
2025-02-03 16:24:03 +00:00
|
|
|
b = [item.timestep for item in env.game.agents["client_2_green_user"].history if item.action != "do-nothing"]
|
2024-08-05 10:34:06 +01:00
|
|
|
|
|
|
|
|
assert a == b
|
2024-08-02 12:49:17 +01:00
|
|
|
|
2025-02-10 14:39:28 +00:00
|
|
|
# Check that seed log file was created.
|
|
|
|
|
path = SIM_OUTPUT.path / "seed.log"
|
|
|
|
|
with open(path, "r") as file:
|
|
|
|
|
assert file
|
|
|
|
|
|
2024-08-02 12:49:17 +01:00
|
|
|
|
|
|
|
|
def test_rng_seed_unset(create_env):
|
2024-08-05 10:34:06 +01:00
|
|
|
"""Test with no RNG seed."""
|
2024-08-02 12:49:17 +01:00
|
|
|
env = create_env
|
|
|
|
|
env.reset()
|
|
|
|
|
for i in range(100):
|
|
|
|
|
env.step(0)
|
2025-02-03 16:24:03 +00:00
|
|
|
a = [item.timestep for item in env.game.agents["client_2_green_user"].history if item.action != "do-nothing"]
|
2024-08-02 12:49:17 +01:00
|
|
|
|
|
|
|
|
env.reset()
|
|
|
|
|
for i in range(100):
|
|
|
|
|
env.step(0)
|
2025-02-03 16:24:03 +00:00
|
|
|
b = [item.timestep for item in env.game.agents["client_2_green_user"].history if item.action != "do-nothing"]
|
2024-08-02 12:49:17 +01:00
|
|
|
|
2024-08-05 10:34:06 +01:00
|
|
|
assert a != b
|
2025-02-10 14:39:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_for_generated_seed():
|
|
|
|
|
"""
|
|
|
|
|
Show that setting generate_seed_value to true producess a valid seed.
|
|
|
|
|
"""
|
|
|
|
|
with open(data_manipulation_config_path(), "r") as f:
|
|
|
|
|
cfg = yaml.safe_load(f)
|
|
|
|
|
|
|
|
|
|
cfg["game"]["generate_seed_value"] = True
|
|
|
|
|
PrimaiteGymEnv(env_config=cfg)
|
|
|
|
|
path = SIM_OUTPUT.path / "seed.log"
|
|
|
|
|
with open(path, "r") as file:
|
|
|
|
|
data = file.read()
|
|
|
|
|
|
|
|
|
|
assert data.split(" ")[3] != None
|