diff --git a/sim/planning/sweep.py b/sim/planning/sweep.py index af91324..c540941 100644 --- a/sim/planning/sweep.py +++ b/sim/planning/sweep.py @@ -1,7 +1,5 @@ from dataclasses import replace -import numpy as np - from sim.planning.comparison import build_comparison_row from sim.simulator import LapsRaceSimulator from sim.units import mph_to_mps @@ -41,8 +39,8 @@ def run_fixed_speed_sweep( car=car, race=race, use_api_weather=use_api_weather, + synthetic_weather_seed=synthetic_weather_seed, ) - np.random.seed(synthetic_weather_seed) results = simulator.run() row = build_comparison_row( @@ -85,8 +83,8 @@ def run_strategy_sweep( car=car, race=race, use_api_weather=use_api_weather, + synthetic_weather_seed=synthetic_weather_seed, ) - np.random.seed(synthetic_weather_seed) results = simulator.run() rows.append(build_comparison_row(strategy, race, car, results, study="strategy")) diff --git a/sim/simulator.py b/sim/simulator.py index 0eb438b..c594564 100644 --- a/sim/simulator.py +++ b/sim/simulator.py @@ -1,17 +1,18 @@ from sim.config import SimulationResults from sim.physics import PhysicsEngine -from sim.weather import WeatherService +from sim.weather import DEFAULT_SYNTHETIC_SEED, WeatherService from sim.controllers import build_strategy from sim.units import minutes_to_hours, minutes_to_seconds, mps_to_mph import numpy as np class LapsRaceSimulator: - def __init__(self, track, car, race, use_api_weather=True): + def __init__(self, track, car, race, use_api_weather=True, synthetic_weather_seed=DEFAULT_SYNTHETIC_SEED): self.track = track self.car = car self.race = race self.use_api_weather = use_api_weather + self.synthetic_weather_seed = synthetic_weather_seed self.physics = PhysicsEngine(self.car) self.weather = WeatherService(self.track) @@ -23,9 +24,9 @@ def run(self) -> SimulationResults: time_minutes, ghi_data, cloud_data = self.weather.fetch_weather_data(self.race) except Exception as e: print(f"Warning: API fetch failed ({e}), using synthetic data") - time_minutes, ghi_data, cloud_data = self.weather.generate_synthetic_data(self.race) + time_minutes, ghi_data, cloud_data = self.weather.generate_synthetic_data(self.race, self.synthetic_weather_seed) else: - time_minutes, ghi_data, cloud_data = self.weather.generate_synthetic_data(self.race) + time_minutes, ghi_data, cloud_data = self.weather.generate_synthetic_data(self.race, self.synthetic_weather_seed) dt_minutes = self.race.time_step_minutes dt_hours = minutes_to_hours(dt_minutes) diff --git a/sim/weather.py b/sim/weather.py index ba8db5c..d14389a 100644 --- a/sim/weather.py +++ b/sim/weather.py @@ -5,6 +5,8 @@ import openmeteo_requests import requests_cache +DEFAULT_SYNTHETIC_SEED = 7 + def resample_to_time_step(time_minutes, ghi_data, cloud_data, step_minutes: float): """Resample 1-minute weather arrays onto the simulator's time step. @@ -119,23 +121,44 @@ def fetch_weather_data(self, race_config) -> Tuple[np.ndarray, np.ndarray, np.nd time_minutes, ghi_data, cloud_data, race_config.time_step_minutes ) - def generate_synthetic_data(self, race_config) -> Tuple[np.ndarray, np.ndarray, np.ndarray]: + def generate_synthetic_data( + self, race_config, seed: int = DEFAULT_SYNTHETIC_SEED + ) -> Tuple[np.ndarray, np.ndarray, np.ndarray]: + """Offline weather profile. Deterministic: the same race window and seed give the same arrays.""" start_min = int(race_config.start_time_hour * 60) end_min = int(race_config.end_time_hour * 60) + if end_min <= start_min: + raise ValueError( + f"Race window must contain at least one minute (start={start_min} min, end={end_min} min)" + ) time_minutes = np.arange(start_min, end_min) + # Local generator so the global np.random state is neither read nor modified. + rng = np.random.RandomState(seed) + t_peak = 13 * 60 peak_ghi = 900 - a = peak_ghi / ((t_peak - start_min) ** 2) + half_width = abs(t_peak - start_min) or (end_min - start_min) + a = peak_ghi / (half_width**2) ghi_data = np.maximum(0, -a * (time_minutes - t_peak) ** 2 + peak_ghi) - noise = np.clip(np.random.normal(1.0, 0.1, len(ghi_data)), 0.7, 1.2) - smooth_noise = np.convolve(noise, np.ones(15) / 15, mode="same") - ghi_data = ghi_data * smooth_noise + noise = np.clip(rng.normal(1.0, 0.1, len(ghi_data)), 0.7, 1.2) + ghi_data = ghi_data * _moving_average(noise, 15) - cloud_data = np.clip(0.3 + 0.3 * np.random.randn(len(time_minutes)), 0, 1) - cloud_data = np.convolve(cloud_data, np.ones(30) / 30, mode="same") + cloud_data = np.clip(0.3 + 0.3 * rng.randn(len(time_minutes)), 0, 1) + cloud_data = _moving_average(cloud_data, 30) return resample_to_time_step( time_minutes, ghi_data, cloud_data, race_config.time_step_minutes ) + + +def _moving_average(values: np.ndarray, window: int) -> np.ndarray: + """Zero-padded moving average that always returns len(values) samples. + + Equals np.convolve(values, ones(window)/window, mode="same") when len(values) >= window; + np.convolve returns max(len(values), window) samples, which breaks short race windows. + """ + full = np.convolve(values, np.ones(window) / window, mode="full") + offset = (window - 1) // 2 + return full[offset : offset + len(values)] diff --git a/tests/planning/test_csv.py b/tests/planning/test_csv.py index 0f76101..b252bb5 100644 --- a/tests/planning/test_csv.py +++ b/tests/planning/test_csv.py @@ -24,7 +24,7 @@ def test_fixed_speed_sweep(self): simulator_args = [] class StubSimulator: - def __init__(self, *, track, car, race, use_api_weather): + def __init__(self, *, track, car, race, use_api_weather, synthetic_weather_seed=7): simulator_args.append((track, car, use_api_weather)) def run(self): @@ -84,7 +84,7 @@ def test_planning_report(self): ) class StubSimulator: - def __init__(self, *, track, car, race, use_api_weather): + def __init__(self, *, track, car, race, use_api_weather, synthetic_weather_seed=7): pass def run(self): diff --git a/tests/test_simulator.py b/tests/test_simulator.py index fe5a619..c8ef314 100644 --- a/tests/test_simulator.py +++ b/tests/test_simulator.py @@ -20,7 +20,7 @@ def regen_energy(self, prev_speed, speed): class FakeWeather: - def generate_synthetic_data(self, race): + def generate_synthetic_data(self, race, seed=None): return ( np.array([0.0, 1.0]), np.array([0.0, 0.0]), @@ -54,6 +54,7 @@ def make_simulator(lap_distance_m=1000.0, time_step_minutes=1.0, strategy="test", ) simulator.use_api_weather = False + simulator.synthetic_weather_seed = 7 simulator.physics = FakePhysics() simulator.weather = FakeWeather() simulator.strategy = FakeStrategy(optimal_speed) diff --git a/tests/test_units.py b/tests/test_units.py index c0a8452..e72311a 100644 --- a/tests/test_units.py +++ b/tests/test_units.py @@ -125,7 +125,7 @@ def test_sweep_converts_mph_to_commanded_mps(self, monkeypatch): seen = [] class FakeSim: - def __init__(self, track, car, race, use_api_weather=True): + def __init__(self, track, car, race, use_api_weather=True, synthetic_weather_seed=None): seen.append(race.fixed_speed_mps) def run(self): diff --git a/tests/test_weather.py b/tests/test_weather.py new file mode 100644 index 0000000..2cc876c --- /dev/null +++ b/tests/test_weather.py @@ -0,0 +1,242 @@ +"""Tests for deterministic synthetic weather (no network access).""" +from types import SimpleNamespace + +import numpy as np +import pytest + +from sim.weather import DEFAULT_SYNTHETIC_SEED, WeatherService, _moving_average + + +def race(start=10.0, end=18.0, time_step_minutes=1.0): + return SimpleNamespace( + start_time_hour=start, end_time_hour=end, time_step_minutes=time_step_minutes + ) + + +@pytest.fixture +def service(monkeypatch): + def no_network(*args, **kwargs): + raise AssertionError("synthetic weather must not touch the network") + + monkeypatch.setattr(WeatherService, "fetch_weather_data", no_network) + monkeypatch.setattr(WeatherService, "client", property(no_network)) + return WeatherService(track=None) + + +def assert_same(a, b): + for x, y in zip(a, b): + np.testing.assert_array_equal(x, y) + + +class TestDeterminism: + def test_repeated_calls_return_same_arrays(self, service): + assert_same(service.generate_synthetic_data(race()), service.generate_synthetic_data(race())) + + def test_separate_service_instances_agree(self, service): + other = WeatherService(track=None) + assert_same(service.generate_synthetic_data(race()), other.generate_synthetic_data(race())) + + def test_default_seed_matches_explicit_default(self, service): + assert_same( + service.generate_synthetic_data(race()), + service.generate_synthetic_data(race(), seed=DEFAULT_SYNTHETIC_SEED), + ) + + def test_different_seeds_differ(self, service): + _, ghi_a, cloud_a = service.generate_synthetic_data(race(), seed=1) + _, ghi_b, cloud_b = service.generate_synthetic_data(race(), seed=2) + assert not np.array_equal(ghi_a, ghi_b) + assert not np.array_equal(cloud_a, cloud_b) + + def test_same_seed_is_reproducible_across_many_calls(self, service): + first = service.generate_synthetic_data(race(), seed=123) + for _ in range(5): + assert_same(first, service.generate_synthetic_data(race(), seed=123)) + + def test_ignores_global_numpy_random_state(self, service): + np.random.seed(0) + a = service.generate_synthetic_data(race()) + np.random.seed(999) + np.random.rand(50) + b = service.generate_synthetic_data(race()) + assert_same(a, b) + + def test_does_not_consume_global_numpy_random_state(self, service): + np.random.seed(42) + expected = np.random.rand(3) + np.random.seed(42) + service.generate_synthetic_data(race()) + np.testing.assert_array_equal(np.random.rand(3), expected) + + def test_input_race_config_not_mutated(self, service): + r = race() + service.generate_synthetic_data(r) + assert (r.start_time_hour, r.end_time_hour) == (10.0, 18.0) + + def test_matches_legacy_global_seed_stream(self, service): + """Seed 7 reproduces the output the sweeps got from np.random.seed(7) before this change.""" + np.random.seed(7) + start, end = 600, 1080 + t = np.arange(start, end) + a = 900 / ((780 - start) ** 2) + ghi = np.maximum(0, -a * (t - 780) ** 2 + 900) + noise = np.clip(np.random.normal(1.0, 0.1, len(ghi)), 0.7, 1.2) + ghi = ghi * np.convolve(noise, np.ones(15) / 15, mode="same") + cloud = np.clip(0.3 + 0.3 * np.random.randn(len(t)), 0, 1) + cloud = np.convolve(cloud, np.ones(30) / 30, mode="same") + + _, got_ghi, got_cloud = service.generate_synthetic_data(race(), seed=7) + np.testing.assert_allclose(got_ghi, ghi) + np.testing.assert_allclose(got_cloud, cloud) + + +class TestShapeAndRange: + def test_array_lengths_match_race_window(self, service): + t, ghi, cloud = service.generate_synthetic_data(race(10.0, 18.0)) + assert len(t) == len(ghi) == len(cloud) == 8 * 60 + + def test_time_axis_is_minutes_of_day(self, service): + t, _, _ = service.generate_synthetic_data(race(9.0, 17.0)) + assert t[0] == 9 * 60 + assert t[-1] == 17 * 60 - 1 + assert np.all(np.diff(t) == 1) + + def test_ghi_nonnegative_and_finite(self, service): + _, ghi, cloud = service.generate_synthetic_data(race()) + assert np.all(np.isfinite(ghi)) and np.all(np.isfinite(cloud)) + assert ghi.min() >= 0 + + def test_cloud_cover_within_unit_interval(self, service): + for seed in range(20): + _, _, cloud = service.generate_synthetic_data(race(), seed=seed) + assert cloud.min() >= 0 and cloud.max() <= 1 + + def test_ghi_peaks_near_solar_noon(self, service): + t, ghi, _ = service.generate_synthetic_data(race()) + assert abs(t[np.argmax(ghi)] - 13 * 60) < 60 + assert ghi.max() <= 900 * 1.2 + 1e-9 + + def test_returns_numpy_arrays(self, service): + for arr in service.generate_synthetic_data(race()): + assert isinstance(arr, np.ndarray) + + +class TestEdgeCases: + def test_start_at_solar_noon_does_not_divide_by_zero(self, service): + t, ghi, cloud = service.generate_synthetic_data(race(13.0, 18.0)) + assert len(t) == len(ghi) == len(cloud) == 5 * 60 + assert np.all(np.isfinite(ghi)) + + def test_start_after_solar_noon(self, service): + t, ghi, cloud = service.generate_synthetic_data(race(14.0, 18.0)) + assert len(ghi) == len(cloud) == len(t) == 4 * 60 + assert np.all(np.isfinite(ghi)) and ghi.min() >= 0 + + @pytest.mark.parametrize("minutes", [1, 2, 5, 14, 15, 16, 29, 30, 31]) + def test_short_windows_keep_matching_lengths(self, service, minutes): + r = race(10.0, 10.0 + minutes / 60) + t, ghi, cloud = service.generate_synthetic_data(r) + assert len(t) == len(ghi) == len(cloud) == minutes + + def test_empty_window_raises_clear_error(self, service): + with pytest.raises(ValueError, match="at least one minute"): + service.generate_synthetic_data(race(10.0, 10.0)) + + def test_reversed_window_raises_clear_error(self, service): + with pytest.raises(ValueError, match="at least one minute"): + service.generate_synthetic_data(race(18.0, 10.0)) + + def test_sub_minute_window_raises(self, service): + with pytest.raises(ValueError): + service.generate_synthetic_data(race(10.0, 10.0 + 0.5 / 60)) + + def test_fractional_hours(self, service): + t, ghi, _ = service.generate_synthetic_data(race(9.5, 17.25)) + assert t[0] == 570 and len(ghi) == 17 * 60 + 15 - 9 * 60 - 30 + + def test_full_day_window(self, service): + t, ghi, cloud = service.generate_synthetic_data(race(0.0, 24.0)) + assert len(t) == len(ghi) == len(cloud) == 1440 + assert np.all(np.isfinite(ghi)) + + def test_seed_zero_is_valid_and_not_treated_as_missing(self, service): + a = service.generate_synthetic_data(race(), seed=0) + b = service.generate_synthetic_data(race(), seed=DEFAULT_SYNTHETIC_SEED) + assert not np.array_equal(a[1], b[1]) + + +class TestMovingAverage: + @pytest.mark.parametrize("n,w", [(100, 15), (30, 30), (31, 30), (50, 30), (200, 15)]) + def test_matches_numpy_same_when_long_enough(self, n, w): + x = np.random.RandomState(0).rand(n) + np.testing.assert_allclose( + _moving_average(x, w), np.convolve(x, np.ones(w) / w, mode="same") + ) + + @pytest.mark.parametrize("n", [1, 2, 7, 14]) + def test_output_length_for_short_input(self, n): + assert len(_moving_average(np.ones(n), 15)) == n + + def test_constant_signal_interior_unchanged(self): + out = _moving_average(np.full(100, 2.0), 15) + np.testing.assert_allclose(out[10:-10], 2.0) + + +class TestSimulatorIntegration: + """End to end: an offline run is reproducible, and the seed reaches the weather.""" + + def _run(self, capsys, seed=None): + from sim.config import CarConfig, RaceConfig, TrackConfig + from sim.simulator import LapsRaceSimulator + + track = TrackConfig.from_json("courses/shenandoah_speedway.json") + car = CarConfig.from_json("car_params.json") + kwargs = {} if seed is None else {"synthetic_weather_seed": seed} + sim = LapsRaceSimulator( + track=track, car=car, race=RaceConfig(strategy="fixed"), use_api_weather=False, **kwargs + ) + results = sim.run() + capsys.readouterr() + return results + + def test_offline_run_is_reproducible(self, capsys): + a, b = self._run(capsys), self._run(capsys) + np.testing.assert_array_equal(a.soc, b.soc) + assert a.total_laps == b.total_laps + + def test_explicit_seed_is_reproducible(self, capsys): + a, b = self._run(capsys, seed=3), self._run(capsys, seed=3) + np.testing.assert_array_equal(a.soc, b.soc) + + +class TestTimeStep: + def test_default_step_matches_one_minute_output(self, service): + t, g, c = service.generate_synthetic_data(race(10.0, 12.0, 1.0)) + assert np.array_equal(t, np.arange(600, 720)) + assert len(t) == len(g) == len(c) == 120 + + def test_five_minute_aggregation(self, service): + t1, g1, c1 = service.generate_synthetic_data(race(10.0, 12.0, 1.0)) + t5, g5, c5 = service.generate_synthetic_data(race(10.0, 12.0, 5.0)) + assert len(t5) == len(g5) == len(c5) == 24 + assert np.allclose(np.diff(t5), 5.0) + assert t5[0] == t1[0] + # Coarser steps average the minutes inside each step. + assert np.allclose(g5, g1.reshape(24, 5).mean(axis=1)) + assert np.allclose(c5, c1.reshape(24, 5).mean(axis=1)) + + def test_half_minute_interpolation(self, service): + t1, g1, c1 = service.generate_synthetic_data(race(10.0, 12.0, 1.0)) + th, gh, ch = service.generate_synthetic_data(race(10.0, 12.0, 0.5)) + assert len(th) == len(gh) == len(ch) == 240 + assert np.allclose(np.diff(th), 0.5) + # Whole-minute samples are unchanged by interpolation. + assert np.allclose(gh[::2], g1) + assert np.allclose(ch[::2], c1) + + @pytest.mark.parametrize("step", [0.5, 1.0, 2.0, 5.0, 15.0]) + def test_step_is_deterministic(self, service, step): + a = service.generate_synthetic_data(race(9.0, 15.0, step)) + b = service.generate_synthetic_data(race(9.0, 15.0, step)) + for x, y in zip(a, b): + assert np.array_equal(x, y)