feat(phase3): implement SimPy discrete-event simulation engine, multi-zone order generation, fleet dispatcher, and wider Zone B aisles

This commit is contained in:
2026-10-19 18:02:02 +05:30
parent 291b02cf0c
commit 3d9fb6be66
21 changed files with 1728 additions and 66 deletions
+7 -1
View File
@@ -5,12 +5,13 @@ from __future__ import annotations
import json
from dataclasses import asdict, dataclass, field
from enum import Enum
from typing import Any, Dict, Optional
from typing import Any, Dict, Optional, Tuple
class SimulationEventType(str, Enum):
"""Categorization of discrete events emitted by the simulation engine."""
TASK_RELEASED = "TASK_RELEASED"
TASK_CREATED = "TASK_RELEASED"
TASK_ASSIGNED = "TASK_ASSIGNED"
TASK_STARTED = "TASK_STARTED"
TASK_COMPLETED = "TASK_COMPLETED"
@@ -21,11 +22,14 @@ class SimulationEventType(str, Enum):
ROBOT_STATE_CHANGED = "ROBOT_STATE_CHANGED"
ROBOT_ARRIVED = "ROBOT_ARRIVED"
ROBOT_CHARGING_STARTED = "ROBOT_CHARGING_STARTED"
ROBOT_CHARGING_START = "ROBOT_CHARGING_STARTED"
ROBOT_CHARGING_COMPLETED = "ROBOT_CHARGING_COMPLETED"
ROBOT_CHARGING_COMPLETE = "ROBOT_CHARGING_COMPLETED"
INVENTORY_RESERVED = "INVENTORY_RESERVED"
INVENTORY_CONSUMED = "INVENTORY_CONSUMED"
INVENTORY_REPLENISHED = "INVENTORY_REPLENISHED"
INVENTORY_REPLENISHMENT_TRIGGERED = "INVENTORY_REPLENISHED"
STOCKOUT_OCCURRED = "STOCKOUT_OCCURRED"
@@ -38,6 +42,8 @@ class SimulationEvent:
entity_id: str
task_id: Optional[str] = None
metadata: Dict[str, Any] = field(default_factory=dict)
position: Optional[Tuple[float, float, float]] = None
zone: Optional[str] = None
def to_dict(self) -> Dict[str, Any]:
"""Convert event to serializable dictionary format."""
+14
View File
@@ -31,6 +31,11 @@ class InventoryItem:
"""On-hand quantity unencumbered by pending reservations."""
return max(0, self.current_stock - self.reserved_stock)
@property
def available_quantity(self) -> int:
"""Alias for available_stock."""
return self.available_stock
def reserve(self, qty: int) -> None:
"""Reserve units for an assigned picking task.
@@ -69,6 +74,10 @@ class InventoryItem:
if qty > 0:
self.current_stock += qty
def restock(self, qty: int) -> None:
"""Alias for replenish."""
self.replenish(qty)
def needs_replenishment(self) -> bool:
"""Check if on-hand stock has dropped to or below minimum threshold."""
return self.current_stock <= self.minimum_stock
@@ -109,6 +118,11 @@ class InventoryCatalog:
return cls(catalog_items)
@property
def items(self) -> Dict[str, InventoryItem]:
"""Dictionary of all items keyed by SKU item_id."""
return self._items
def get_item(self, item_id: str) -> Optional[InventoryItem]:
"""Retrieve an inventory item by ID."""
return self._items.get(item_id)
+15 -4
View File
@@ -166,6 +166,11 @@ class Robot:
return 0.0
return max(0.0, min(100.0, (self.battery_level / self.battery_capacity) * 100.0))
@property
def is_available(self) -> bool:
"""Check if robot is idle and ready to be assigned a new task."""
return self.current_state == RobotState.IDLE and self.current_task_id is None
def transition_to(self, new_state: RobotState, sim_time: float) -> None:
"""Execute a state transition verifying legality against the transition matrix.
@@ -249,15 +254,17 @@ class Robot:
return True, None
def assign_task(self, task: Any) -> None:
def assign_task(self, task: Any, sim_time: Optional[float] = None) -> None:
"""Assign active task after verifying compatibility rules.
Raises:
IncompatibleRobotTaskError: If robot is incompatible with task payload or zone.
"""
compatible, reason = self.validate_task_compatibility(task)
if not compatible:
raise IncompatibleRobotTaskError(reason or "Robot incompatible with task")
# If task is already a Task object, validate compatibility
if hasattr(task, "required_payload") or hasattr(task, "target_zone"):
compatible, reason = self.validate_task_compatibility(task)
if not compatible:
raise IncompatibleRobotTaskError(reason or "Robot incompatible with task")
task_id = getattr(task, "task_id", str(task))
self.current_task_id = task_id
@@ -269,6 +276,10 @@ class Robot:
self.current_task_id = None
self.current_route.clear()
def complete_task(self, sim_time: Optional[float] = None) -> None:
"""Complete active task and clear binding."""
self.release_task()
def consume_energy(self, duration_seconds: float, mode: str = "moving_empty") -> float:
"""Deduct energy consumed over duration according to active power mode.
+56
View File
@@ -12,11 +12,22 @@ from warehouse_twin.domain.robot import InvalidStateTransitionError
class TaskType(str, Enum):
"""Classification of tasks executed by warehouse AMR fleet."""
ORDER_PICK = "ORDER_PICK"
OUTBOUND_PICKING = "ORDER_PICK"
INBOUND_TRANSPORT = "INBOUND_TRANSPORT"
REPLENISHMENT = "REPLENISHMENT"
PRODUCTION_LINE_FEED = "PRODUCTION_LINE_FEED"
PRODUCTION_FEED = "PRODUCTION_LINE_FEED"
CHARGING = "CHARGING"
class TaskPriority(int, Enum):
"""Priority ranking for warehouse tasks (1 is highest, 4 is lowest)."""
CRITICAL = 1
HIGH = 2
MEDIUM = 3
LOW = 4
class TaskStatus(str, Enum):
"""Discrete operational lifecycle states for simulation tasks."""
PENDING = "PENDING"
@@ -68,6 +79,51 @@ class Task:
completion_time: Optional[float] = None
target_zone: Optional[str] = None # Operational zone hint ('zone_a', 'zone_b', 'zone_c')
@property
def source_station(self) -> str:
return self.source
@source_station.setter
def source_station(self, val: str) -> None:
self.source = val
@property
def destination_station(self) -> str:
return self.destination
@destination_station.setter
def destination_station(self, val: str) -> None:
self.destination = val
@property
def payload_weight(self) -> float:
return self.required_payload
@payload_weight.setter
def payload_weight(self, val: float) -> None:
self.required_payload = val
@property
def created_at(self) -> float:
return self.release_time
@created_at.setter
def created_at(self, val: float) -> None:
self.release_time = val
def assign_to_robot(self, robot_id: str, sim_time: float) -> None:
"""Assign task to an AMR and transition to ASSIGNED status."""
self.assigned_robot_id = robot_id
self.transition_to(TaskStatus.ASSIGNED, sim_time)
def start_execution(self, sim_time: float) -> None:
"""Transition task to IN_PROGRESS status."""
self.transition_to(TaskStatus.IN_PROGRESS, sim_time)
def complete(self, sim_time: float) -> None:
"""Transition task to COMPLETED status."""
self.transition_to(TaskStatus.COMPLETED, sim_time)
def transition_to(self, new_status: TaskStatus, sim_time: float) -> None:
"""Advance task state machine, validating legal transitions and updating timestamps.