163 lines
5.2 KiB
Python
163 lines
5.2 KiB
Python
"""Unit tests for the KPICalculator and mathematical aggregation functions."""
|
|
|
|
import pytest
|
|
|
|
from warehouse_twin.analytics.kpi_calculator import (
|
|
ComprehensiveKPIReport,
|
|
DistributionStats,
|
|
KPICalculator,
|
|
compute_percentile,
|
|
)
|
|
from warehouse_twin.domain.robot import Robot
|
|
from warehouse_twin.domain.task import Task, TaskPriority, TaskStatus, TaskType
|
|
from warehouse_twin.domain.warehouse import Position3D
|
|
from warehouse_twin.simulation.sim_engine import SimulationMetrics
|
|
|
|
|
|
def test_compute_percentile_empty_and_single() -> None:
|
|
"""Empty and single-element sequences should return expected percentiles without errors."""
|
|
assert compute_percentile([], 50.0) == 0.0
|
|
assert compute_percentile([42.0], 50.0) == 42.0
|
|
assert compute_percentile([42.0], 99.0) == 42.0
|
|
|
|
|
|
def test_compute_percentile_interpolation() -> None:
|
|
"""Percentile function should correctly interpolate across sorted ranks."""
|
|
data = [10.0, 20.0, 30.0, 40.0, 50.0]
|
|
assert compute_percentile(data, 0.0) == 10.0
|
|
assert compute_percentile(data, 50.0) == 30.0
|
|
assert compute_percentile(data, 100.0) == 50.0
|
|
# Interpolated rank between 20 and 30
|
|
p25 = compute_percentile(data, 25.0)
|
|
assert 15.0 <= p25 <= 25.0
|
|
|
|
|
|
def test_distribution_stats_from_values() -> None:
|
|
"""DistributionStats should compute mean, std_dev, min, max, and percentiles."""
|
|
data = [100.0, 200.0, 300.0, 400.0, 500.0]
|
|
stats = DistributionStats.from_values(data)
|
|
assert stats.count == 5
|
|
assert stats.mean == 300.0
|
|
assert stats.min == 100.0
|
|
assert stats.max == 500.0
|
|
assert stats.p50 == 300.0
|
|
assert stats.p90 > 400.0
|
|
assert stats.p99 <= 500.0
|
|
assert stats.std_dev > 0.0
|
|
|
|
|
|
def test_kpi_calculator_isolates_incomplete_tasks() -> None:
|
|
"""Incomplete tasks (pending, in-progress, failed) must be excluded from completion distributions."""
|
|
t_comp1 = Task(
|
|
task_id="t1",
|
|
task_type=TaskType.OUTBOUND_PICKING,
|
|
source="s1",
|
|
destination="d1",
|
|
release_time=10.0,
|
|
start_time=20.0,
|
|
completion_time=60.0,
|
|
status=TaskStatus.COMPLETED,
|
|
)
|
|
t_comp2 = Task(
|
|
task_id="t2",
|
|
task_type=TaskType.INBOUND_TRANSPORT,
|
|
source="s1",
|
|
destination="d1",
|
|
release_time=20.0,
|
|
start_time=35.0,
|
|
completion_time=80.0,
|
|
status=TaskStatus.COMPLETED,
|
|
)
|
|
t_pending = Task(
|
|
task_id="t3",
|
|
task_type=TaskType.OUTBOUND_PICKING,
|
|
source="s1",
|
|
destination="d1",
|
|
release_time=40.0,
|
|
status=TaskStatus.PENDING,
|
|
)
|
|
t_in_progress = Task(
|
|
task_id="t4",
|
|
task_type=TaskType.PRODUCTION_FEED,
|
|
source="s1",
|
|
destination="d1",
|
|
release_time=50.0,
|
|
start_time=60.0,
|
|
status=TaskStatus.IN_PROGRESS,
|
|
)
|
|
|
|
all_tasks = [t_comp1, t_comp2, t_pending, t_in_progress]
|
|
|
|
r1 = Robot(
|
|
robot_id="r1",
|
|
robot_type="light_amr",
|
|
position=Position3D(0.0, 0.0, 0.0),
|
|
maximum_speed=1.5,
|
|
payload_capacity=150.0,
|
|
battery_level=2.5,
|
|
battery_capacity=3.0,
|
|
energy_consumption={"idle": 0.05, "moving_empty": 0.35, "moving_loaded": 0.65, "handling": 0.15},
|
|
allowed_zones={"zone_a"},
|
|
accumulated_distance=200.0,
|
|
loaded_distance=120.0,
|
|
empty_distance=80.0,
|
|
busy_time=60.0,
|
|
total_energy_consumed_kwh=0.5,
|
|
)
|
|
|
|
sim_metrics = SimulationMetrics(
|
|
duration_seconds=100.0,
|
|
tasks_created=4,
|
|
tasks_completed=2,
|
|
)
|
|
|
|
report = KPICalculator.evaluate(
|
|
sim_metrics=sim_metrics,
|
|
all_tasks=all_tasks,
|
|
robots={"r1": r1},
|
|
wall_clock_seconds=0.05,
|
|
random_seed=42,
|
|
)
|
|
|
|
assert report.tasks_created == 4
|
|
assert report.tasks_completed == 2
|
|
assert report.tasks_pending == 1
|
|
assert report.tasks_in_progress == 1
|
|
assert report.completion_rate_percent == 50.0
|
|
|
|
# Turnaround distribution should only consider t1 (50s) and t2 (60s)
|
|
assert report.turnaround_time.count == 2
|
|
assert report.turnaround_time.mean == 55.0
|
|
assert report.turnaround_time.min == 50.0
|
|
assert report.turnaround_time.max == 60.0
|
|
|
|
# Spatial distance check
|
|
assert report.total_travel_distance_meters == 200.0
|
|
assert report.loaded_travel_distance_meters == 120.0
|
|
assert report.empty_travel_distance_meters == 80.0
|
|
assert report.loaded_distance_ratio_percent == 60.0
|
|
|
|
|
|
def test_kpi_task_type_breakdowns() -> None:
|
|
"""KPI calculator should generate operational metrics grouped by task type."""
|
|
t1 = Task(
|
|
task_id="t1",
|
|
task_type=TaskType.PRODUCTION_FEED,
|
|
source="s1",
|
|
destination="d1",
|
|
release_time=0.0,
|
|
start_time=10.0,
|
|
completion_time=50.0,
|
|
deadline=100.0,
|
|
status=TaskStatus.COMPLETED,
|
|
)
|
|
sim_metrics = SimulationMetrics(duration_seconds=3600.0)
|
|
report = KPICalculator.evaluate(sim_metrics, [t1], {})
|
|
|
|
assert TaskType.PRODUCTION_FEED.value in report.task_type_breakdowns
|
|
feed_bd = report.task_type_breakdowns[TaskType.PRODUCTION_FEED.value]
|
|
assert feed_bd.created_count == 1
|
|
assert feed_bd.completed_count == 1
|
|
assert feed_bd.avg_turnaround_seconds == 50.0
|
|
assert feed_bd.on_time_percent == 100.0
|