feat(phase3): implement SimPy discrete-event simulation engine, multi-zone order generation, fleet dispatcher, and wider Zone B aisles
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
"""Unit tests for FleetDispatcher task prioritization and robot allocation."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
import pytest
|
||||
|
||||
from warehouse_twin.config.loader import ConfigLoader
|
||||
from warehouse_twin.domain.robot import Robot, RobotState, RobotTypeSpec
|
||||
from warehouse_twin.domain.task import Task, TaskPriority, TaskStatus, TaskType
|
||||
from warehouse_twin.domain.warehouse import Position3D, WarehouseModel
|
||||
from warehouse_twin.routing.graph_builder import WarehouseGraphBuilder
|
||||
from warehouse_twin.simulation.dispatcher import DispatcherSettings, FleetDispatcher
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def dispatcher_fixture(project_config_dir: Path):
|
||||
loader = ConfigLoader(project_config_dir)
|
||||
bundle = loader.load_all()
|
||||
warehouse = WarehouseModel.from_config_dict(bundle.warehouse)
|
||||
builder = WarehouseGraphBuilder.from_config_dict(bundle.warehouse)
|
||||
graph = builder.build_graph()
|
||||
|
||||
type_specs = {
|
||||
t_id: RobotTypeSpec.from_dict(t_id, t_data)
|
||||
for t_id, t_data in bundle.robots["robot_types"].items()
|
||||
}
|
||||
fleet = [
|
||||
Robot.from_fleet_dict(r_data, type_specs)
|
||||
for r_data in bundle.robots["fleet"]
|
||||
]
|
||||
|
||||
settings = DispatcherSettings.from_dict(bundle.dispatch_policies)
|
||||
dispatcher = FleetDispatcher(settings, graph)
|
||||
return dispatcher, fleet, graph
|
||||
|
||||
|
||||
def test_dispatcher_priority_queue_sorting(dispatcher_fixture):
|
||||
dispatcher, _, _ = dispatcher_fixture
|
||||
|
||||
# Enqueue tasks in random priority order
|
||||
t_low = Task("t_low", TaskType.ORDER_PICK, "rack_a_01", "picking_station_a1", priority=4, release_time=10.0)
|
||||
t_high = Task("t_high", TaskType.REPLENISHMENT, "dock_inbound_1", "rack_a_01", priority=2, release_time=20.0)
|
||||
t_crit = Task("t_crit", TaskType.PRODUCTION_FEED, "rack_c_01", "production_dropoff_c1", priority=1, release_time=30.0)
|
||||
|
||||
dispatcher.enqueue_task(t_low)
|
||||
dispatcher.enqueue_task(t_high)
|
||||
dispatcher.enqueue_task(t_crit)
|
||||
|
||||
assert dispatcher.queue_length == 3
|
||||
# Critical should be first, then High, then Low
|
||||
assert dispatcher.queue[0].task_id == "t_crit"
|
||||
assert dispatcher.queue[1].task_id == "t_high"
|
||||
assert dispatcher.queue[2].task_id == "t_low"
|
||||
|
||||
|
||||
def test_dispatcher_payload_compatibility_matching(dispatcher_fixture):
|
||||
dispatcher, fleet, _ = dispatcher_fixture
|
||||
# Task with heavy transmission (650kg)
|
||||
heavy_task = Task(
|
||||
"t_heavy",
|
||||
TaskType.ORDER_PICK,
|
||||
"rack_b_01",
|
||||
"picking_station_b1",
|
||||
required_payload=650.0,
|
||||
target_zone="zone_b",
|
||||
)
|
||||
|
||||
available_robots = [r for r in fleet if r.is_available]
|
||||
chosen = dispatcher.select_best_robot(heavy_task, available_robots, sim_time=0.0)
|
||||
|
||||
# Must choose heavy_amr, cannot choose light_amr (max 150kg)
|
||||
assert chosen is not None
|
||||
assert "heavy" in chosen.robot_type
|
||||
assert chosen.payload_capacity >= 650.0
|
||||
|
||||
|
||||
def test_dispatcher_zone_b_restriction(dispatcher_fixture):
|
||||
dispatcher, fleet, _ = dispatcher_fixture
|
||||
# Light payload in Zone B (45kg engine piston set)
|
||||
piston_task = Task(
|
||||
"t_piston",
|
||||
TaskType.ORDER_PICK,
|
||||
"rack_b_03",
|
||||
"picking_station_b1",
|
||||
required_payload=45.0,
|
||||
target_zone="zone_b",
|
||||
)
|
||||
|
||||
available_robots = [r for r in fleet if r.is_available]
|
||||
chosen = dispatcher.select_best_robot(piston_task, available_robots, sim_time=0.0)
|
||||
|
||||
# Must be heavy AMR because Zone B restricted to heavy AMRs
|
||||
assert chosen is not None
|
||||
assert "heavy" in chosen.robot_type
|
||||
|
||||
|
||||
def test_dispatcher_battery_headroom_filter(dispatcher_fixture):
|
||||
dispatcher, fleet, _ = dispatcher_fixture
|
||||
task = Task("t_light", TaskType.ORDER_PICK, "rack_a_01", "picking_station_a1", required_payload=5.0)
|
||||
|
||||
# Discharge all light AMRs below minimum headroom (20%)
|
||||
for r in fleet:
|
||||
if "light" in r.robot_type:
|
||||
r.battery_level = 0.10 * r.battery_capacity
|
||||
|
||||
available_robots = [r for r in fleet if r.is_available]
|
||||
chosen = dispatcher.select_best_robot(task, available_robots, sim_time=0.0)
|
||||
|
||||
# Light AMRs excluded due to low battery; heavy AMR selected
|
||||
assert chosen is not None
|
||||
assert chosen.battery_percent >= 20.0
|
||||
Reference in New Issue
Block a user