119 lines
4.6 KiB
Python
119 lines
4.6 KiB
Python
"""Unit tests for benchmark comparator and optimization report generator."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
from pathlib import Path
|
|
import pytest
|
|
|
|
from warehouse_twin.analytics.kpi_calculator import (
|
|
ComprehensiveKPIReport,
|
|
DistributionStats,
|
|
)
|
|
from warehouse_twin.optimization.benchmark_comparator import (
|
|
BenchmarkComparator,
|
|
OptimizationDelta,
|
|
OptimizationEvaluation,
|
|
)
|
|
from warehouse_twin.optimization.report import OptimizationReportGenerator
|
|
|
|
|
|
def make_mock_kpi_report(
|
|
throughput: float = 85.0,
|
|
p90: float = 120.0,
|
|
p50: float = 75.0,
|
|
conflicts: int = 30,
|
|
energy_intensity: float = 0.09,
|
|
loaded_pct: float = 58.0,
|
|
) -> ComprehensiveKPIReport:
|
|
"""Construct mock KPI report for unit tests."""
|
|
return ComprehensiveKPIReport(
|
|
duration_seconds=3600.0,
|
|
tasks_created=90,
|
|
tasks_completed=85,
|
|
completion_rate_percent=94.4,
|
|
throughput_tasks_per_hour=throughput,
|
|
turnaround_time=DistributionStats(mean=p50 + 10, std_dev=15.0, min=30.0, p50=p50, p90=p90, p95=p90 + 20, p99=p90 + 40, max=p90 + 50),
|
|
waiting_time=DistributionStats(mean=20.0, std_dev=10.0, min=0.0, p50=15.0, p90=40.0, p95=50.0, p99=60.0, max=70.0),
|
|
execution_time=DistributionStats(mean=55.0, std_dev=10.0, min=30.0, p50=50.0, p90=70.0, p95=75.0, p99=80.0, max=85.0),
|
|
on_time_delivery_percent=100.0,
|
|
fleet_size=4,
|
|
fleet_utilization_percent=50.0,
|
|
total_travel_distance_meters=4000.0,
|
|
loaded_travel_distance_meters=2320.0,
|
|
empty_travel_distance_meters=1680.0,
|
|
loaded_distance_ratio_percent=loaded_pct,
|
|
total_energy_consumed_kwh=7.65,
|
|
energy_intensity_kwh_per_task=energy_intensity,
|
|
conflicts_detected=conflicts,
|
|
yield_waits_count=conflicts,
|
|
total_yield_wait_time_seconds=conflicts * 1.5,
|
|
)
|
|
|
|
|
|
def test_benchmark_comparator_deltas_and_scoring():
|
|
"""Verify quantitative delta calculations and operational score outputs."""
|
|
base_kpi = make_mock_kpi_report(throughput=80.0, p90=140.0, conflicts=35, energy_intensity=0.095, loaded_pct=52.0)
|
|
opt_kpi = make_mock_kpi_report(throughput=88.0, p90=95.0, conflicts=12, energy_intensity=0.082, loaded_pct=61.0)
|
|
|
|
eval_result = BenchmarkComparator.compare_kpis(
|
|
baseline=base_kpi,
|
|
optimized=opt_kpi,
|
|
baseline_name="Centralized Baseline",
|
|
optimized_name="Hybrid Optimal",
|
|
)
|
|
|
|
assert eval_result.overall_score >= 80.0
|
|
assert eval_result.throughput_score > 75.0
|
|
assert eval_result.latency_score > 75.0
|
|
assert eval_result.traffic_score > 75.0
|
|
assert "RECOMMENDED" in eval_result.recommendation
|
|
|
|
# Check deltas
|
|
deltas = {d.metric_name: d for d in eval_result.deltas}
|
|
assert deltas["Throughput"].absolute_delta == 8.0
|
|
assert deltas["Throughput"].percent_delta == 10.0
|
|
assert deltas["Throughput"].is_improvement is True
|
|
|
|
assert deltas["Turnaround Latency (p90)"].absolute_delta == -45.0
|
|
assert deltas["Turnaround Latency (p90)"].is_improvement is True
|
|
|
|
assert deltas["Corridor Conflicts"].absolute_delta == -23.0
|
|
assert deltas["Corridor Conflicts"].is_improvement is True
|
|
|
|
assert deltas["Loaded Travel Ratio"].absolute_delta == 9.0
|
|
assert deltas["Loaded Travel Ratio"].is_improvement is True
|
|
|
|
|
|
def test_optimization_report_generator(tmp_path: Path):
|
|
"""Verify Markdown report, summary JSON, and SVG layout comparison export."""
|
|
base_kpi = make_mock_kpi_report()
|
|
opt_kpi = make_mock_kpi_report(throughput=88.0, p90=100.0, conflicts=15)
|
|
evaluation = BenchmarkComparator.compare_kpis(base_kpi, opt_kpi)
|
|
|
|
# 1. Export Markdown report
|
|
md_file = tmp_path / "opt_report.md"
|
|
OptimizationReportGenerator.export_report_file(evaluation, md_file)
|
|
assert md_file.exists()
|
|
md_text = md_file.read_text(encoding="utf-8")
|
|
assert "# Warehouse Fleet Optimization" in md_text
|
|
assert "Executive Summary" in md_text
|
|
assert "Quantitative Performance Deltas" in md_text
|
|
|
|
# 2. Export JSON summary
|
|
json_file = tmp_path / "opt_summary.json"
|
|
OptimizationReportGenerator.export_summary_json(evaluation, json_file)
|
|
assert json_file.exists()
|
|
data = json.loads(json_file.read_text(encoding="utf-8"))
|
|
assert "evaluation" in data
|
|
assert data["evaluation"]["overall_score"] > 0
|
|
|
|
# 3. Export SVG diagram
|
|
svg_file = tmp_path / "layout_comparison.svg"
|
|
OptimizationReportGenerator.export_layout_comparison_svg(svg_file)
|
|
assert svg_file.exists()
|
|
svg_text = svg_file.read_text(encoding="utf-8")
|
|
assert "<svg" in svg_text
|
|
assert "BASELINE LAYOUT" in svg_text
|
|
assert "HYBRID OPTIMIZED FACILITY" in svg_text
|