feat(phase-6): implement analytics, kpi calculator, event store, heatmaps, and experiment runner
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
"""Unit tests for ExperimentRunner, repeatability, fleet scaling, and Monte Carlo execution."""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from warehouse_twin.analytics.experiment_runner import ExperimentConfig, ExperimentRunner
|
||||
|
||||
|
||||
def test_experiment_runner_single_execution(tmp_path: Path) -> None:
|
||||
"""ExperimentRunner should execute a short simulation and write all artifacts."""
|
||||
runner = ExperimentRunner()
|
||||
cfg = ExperimentConfig(
|
||||
experiment_id="test_exp_01",
|
||||
name="Short Test Experiment",
|
||||
seed=42,
|
||||
duration_seconds=300.0,
|
||||
output_dir=str(tmp_path / "test_exp_01"),
|
||||
)
|
||||
|
||||
result = runner.run_single(cfg)
|
||||
|
||||
assert result.output_dir.exists()
|
||||
assert (result.output_dir / "events.jsonl").exists()
|
||||
assert (result.output_dir / "tasks.csv").exists()
|
||||
assert (result.output_dir / "robots.csv").exists()
|
||||
assert (result.output_dir / "aisle_congestion.csv").exists()
|
||||
assert (result.output_dir / "kpi_summary.json").exists()
|
||||
assert (result.output_dir / "report.md").exists()
|
||||
assert (result.output_dir / "congestion_heatmap.svg").exists()
|
||||
assert (result.output_dir / "config.json").exists()
|
||||
|
||||
rep = result.kpi_report
|
||||
assert rep.tasks_created > 0
|
||||
assert rep.random_seed == 42
|
||||
|
||||
|
||||
def test_experiment_reproducibility(tmp_path: Path) -> None:
|
||||
"""Two independent simulation runs with the same random seed must produce identical results."""
|
||||
runner = ExperimentRunner()
|
||||
|
||||
cfg1 = ExperimentConfig(
|
||||
experiment_id="rep_1",
|
||||
seed=999,
|
||||
duration_seconds=300.0,
|
||||
output_dir=str(tmp_path / "rep_1"),
|
||||
)
|
||||
cfg2 = ExperimentConfig(
|
||||
experiment_id="rep_2",
|
||||
seed=999,
|
||||
duration_seconds=300.0,
|
||||
output_dir=str(tmp_path / "rep_2"),
|
||||
)
|
||||
|
||||
res1 = runner.run_single(cfg1)
|
||||
res2 = runner.run_single(cfg2)
|
||||
|
||||
assert res1.kpi_report.tasks_created == res2.kpi_report.tasks_created
|
||||
assert res1.kpi_report.tasks_completed == res2.kpi_report.tasks_completed
|
||||
assert res1.kpi_report.throughput_tasks_per_hour == res2.kpi_report.throughput_tasks_per_hour
|
||||
assert res1.kpi_report.total_travel_distance_meters == res2.kpi_report.total_travel_distance_meters
|
||||
|
||||
|
||||
def test_experiment_fleet_size_scaling(tmp_path: Path) -> None:
|
||||
"""ExperimentRunner should scale AMR fleet to specified fleet size."""
|
||||
runner = ExperimentRunner()
|
||||
|
||||
cfg_small = ExperimentConfig(
|
||||
experiment_id="fleet_2",
|
||||
fleet_size=2,
|
||||
duration_seconds=120.0,
|
||||
output_dir=str(tmp_path / "fleet_2"),
|
||||
)
|
||||
res_small = runner.run_single(cfg_small)
|
||||
assert res_small.kpi_report.fleet_size == 2
|
||||
|
||||
cfg_large = ExperimentConfig(
|
||||
experiment_id="fleet_6",
|
||||
fleet_size=6,
|
||||
duration_seconds=120.0,
|
||||
output_dir=str(tmp_path / "fleet_6"),
|
||||
)
|
||||
res_large = runner.run_single(cfg_large)
|
||||
assert res_large.kpi_report.fleet_size == 6
|
||||
|
||||
|
||||
def test_monte_carlo_execution(tmp_path: Path) -> None:
|
||||
"""Monte Carlo execution should run across multiple seeds and compute confidence intervals."""
|
||||
runner = ExperimentRunner()
|
||||
cfg = ExperimentConfig(
|
||||
experiment_id="mc_test",
|
||||
duration_seconds=120.0,
|
||||
output_dir=str(tmp_path / "mc_test"),
|
||||
)
|
||||
|
||||
mc_summary = runner.run_monte_carlo(cfg, seeds=[101, 102])
|
||||
|
||||
assert mc_summary["replications"] == 2
|
||||
assert "mean" in mc_summary["throughput_tasks_per_hour"]
|
||||
assert "ci95" in mc_summary["throughput_tasks_per_hour"]
|
||||
assert (tmp_path / "mc_test" / "monte_carlo_summary.json").exists()
|
||||
Reference in New Issue
Block a user