"""Unit tests for multi-objective fleet optimization and Pareto frontier extraction.""" from __future__ import annotations from pathlib import Path import pytest from warehouse_twin.optimization.fleet_optimizer import ( FleetCompositionSpec, FleetOptimizer, ParetoPoint, ) def test_fleet_composition_spec_attributes(): """Verify fleet composition specification and label generation.""" spec = FleetCompositionSpec(num_light=3, num_heavy=1, dispatch_policy="nearest_idle") assert spec.total_fleet_size == 4 assert spec.label == "3L+1H (nearest_idle)" d = spec.to_dict() assert d["num_light"] == 3 assert d["num_heavy"] == 1 assert d["total_fleet_size"] == 4 def test_fleet_optimizer_single_evaluation(project_config_dir: Path): """Verify simulation evaluation of a specific fleet composition.""" optimizer = FleetOptimizer(project_config_dir) spec = FleetCompositionSpec(num_light=2, num_heavy=1, dispatch_policy="priority_nearest_idle") point = optimizer.evaluate_configuration(spec, duration_seconds=120.0, seed=42) assert point.spec == spec assert point.throughput >= 0.0 assert point.turnaround_p90 >= 0.0 assert point.fleet_utilization >= 0.0 assert point.energy_intensity_kwh_per_task >= 0.0 assert point.conflict_count >= 0 def test_pareto_frontier_extraction(project_config_dir: Path): """Verify Pareto frontier identification from candidate search space.""" optimizer = FleetOptimizer(project_config_dir) # Synthetic design points to test non-dominated logic pt1 = ParetoPoint( spec=FleetCompositionSpec(num_light=2, num_heavy=1), throughput=80.0, turnaround_p90=85.0, turnaround_mean=60.0, fleet_utilization=70.0, energy_intensity_kwh_per_task=0.08, conflict_count=10, wait_time_seconds=5.0, sla_compliance_percent=100.0, ) # pt2 dominates pt3 (higher throughput, lower latency, lower energy) pt2 = ParetoPoint( spec=FleetCompositionSpec(num_light=3, num_heavy=2), throughput=95.0, turnaround_p90=60.0, turnaround_mean=45.0, fleet_utilization=55.0, energy_intensity_kwh_per_task=0.07, conflict_count=15, wait_time_seconds=8.0, sla_compliance_percent=100.0, ) pt3 = ParetoPoint( spec=FleetCompositionSpec(num_light=1, num_heavy=2), throughput=70.0, turnaround_p90=95.0, turnaround_mean=75.0, fleet_utilization=90.0, energy_intensity_kwh_per_task=0.10, conflict_count=20, wait_time_seconds=18.0, sla_compliance_percent=90.0, ) frontier = optimizer._compute_pareto_frontier([pt1, pt2, pt3]) assert pt2 in frontier assert pt3 not in frontier assert pt2.is_pareto_optimal is True assert pt3.is_pareto_optimal is False def test_fleet_optimizer_sweep_and_archetypes(project_config_dir: Path): """Verify sweep across candidate fleet specs and selection of best archetypes.""" optimizer = FleetOptimizer(project_config_dir) candidates = [ FleetCompositionSpec(num_light=1, num_heavy=1, dispatch_policy="priority_nearest_idle", name="Mini 1L+1H"), FleetCompositionSpec(num_light=2, num_heavy=1, dispatch_policy="priority_nearest_idle", name="Fast 2L+1H"), ] result = optimizer.optimize_fleet(candidate_specs=candidates, duration_seconds=100.0, seed=42) assert len(result.all_points) == 2 assert len(result.frontier_points) >= 1 assert result.best_throughput_point is not None assert result.best_energy_point is not None assert result.best_balanced_point is not None d = result.to_dict() assert d["total_evaluated"] == 2 assert "best_balanced" in d