feat(phase-1): implement warehouse domain models, topological graph builder, connectivity validation, and export
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
"""Domain models for Warehouse Robot Fleet Digital Twin."""
|
||||
|
||||
from warehouse_twin.domain.warehouse import (
|
||||
ChargingStation,
|
||||
NodeType,
|
||||
Position3D,
|
||||
Station,
|
||||
StationType,
|
||||
StorageLocation,
|
||||
WarehouseDimensions,
|
||||
WarehouseEdge,
|
||||
WarehouseModel,
|
||||
WarehouseNode,
|
||||
ZoneArea,
|
||||
ZoneDefinition,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"ChargingStation",
|
||||
"NodeType",
|
||||
"Position3D",
|
||||
"Station",
|
||||
"StationType",
|
||||
"StorageLocation",
|
||||
"WarehouseDimensions",
|
||||
"WarehouseEdge",
|
||||
"WarehouseModel",
|
||||
"WarehouseNode",
|
||||
"ZoneArea",
|
||||
"ZoneDefinition",
|
||||
]
|
||||
@@ -0,0 +1,231 @@
|
||||
"""Warehouse domain models: spatial dimensions, zones, stations, and nodes."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import math
|
||||
from dataclasses import dataclass, field
|
||||
from enum import Enum
|
||||
from typing import Any, Dict, List, Optional, Set, Tuple
|
||||
|
||||
|
||||
class NodeType(str, Enum):
|
||||
"""Categorization of warehouse graph vertices."""
|
||||
WAYPOINT = "waypoint"
|
||||
STORAGE_RACK = "storage_rack"
|
||||
INBOUND_DOCK = "inbound_dock"
|
||||
OUTBOUND_DOCK = "outbound_dock"
|
||||
PICKING_STATION = "picking_station"
|
||||
PRODUCTION_FEED = "production_feed"
|
||||
CHARGING_BAY = "charging_bay"
|
||||
|
||||
|
||||
class StationType(str, Enum):
|
||||
"""Types of fixed operational stations."""
|
||||
INBOUND_DOCK = "inbound_dock"
|
||||
OUTBOUND_DOCK = "outbound_dock"
|
||||
PICKING_STATION = "picking_station"
|
||||
PRODUCTION_FEED = "production_feed"
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Position3D:
|
||||
"""3D Cartesian position in meters (X=East/Width, Y=North/Depth, Z=Up/Height)."""
|
||||
x: float
|
||||
y: float
|
||||
z: float = 0.0
|
||||
|
||||
def distance_to(self, other: Position3D) -> float:
|
||||
"""Calculate Euclidean distance to another 3D position."""
|
||||
return math.sqrt(
|
||||
(self.x - other.x) ** 2 +
|
||||
(self.y - other.y) ** 2 +
|
||||
(self.z - other.z) ** 2
|
||||
)
|
||||
|
||||
def to_tuple(self) -> Tuple[float, float, float]:
|
||||
"""Convert to a 3-element tuple."""
|
||||
return (self.x, self.y, self.z)
|
||||
|
||||
def to_list(self) -> List[float]:
|
||||
"""Convert to a 3-element list."""
|
||||
return [self.x, self.y, self.z]
|
||||
|
||||
@classmethod
|
||||
def from_iterable(cls, coords: Any) -> Position3D:
|
||||
"""Create Position3D from list, tuple, or dict."""
|
||||
if isinstance(coords, (list, tuple)):
|
||||
z_val = coords[2] if len(coords) > 2 else 0.0
|
||||
return cls(float(coords[0]), float(coords[1]), float(z_val))
|
||||
elif isinstance(coords, dict):
|
||||
return cls(float(coords["x"]), float(coords["y"]), float(coords.get("z", 0.0)))
|
||||
raise ValueError(f"Cannot construct Position3D from {coords!r}")
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class WarehouseDimensions:
|
||||
"""Bounding dimensions of warehouse interior in meters."""
|
||||
width: float
|
||||
depth: float
|
||||
height: float
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ZoneArea:
|
||||
"""Rectangular 2D bounding box defining an inventory zone."""
|
||||
x_min: float
|
||||
x_max: float
|
||||
y_min: float
|
||||
y_max: float
|
||||
|
||||
def contains(self, pos: Position3D) -> bool:
|
||||
"""Check if position is inside the zone boundaries."""
|
||||
return (
|
||||
self.x_min <= pos.x <= self.x_max and
|
||||
self.y_min <= pos.y <= self.y_max
|
||||
)
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class ZoneDefinition:
|
||||
"""Inventory zone configuration and physical constraints."""
|
||||
zone_id: str
|
||||
name: str
|
||||
description: str
|
||||
area: ZoneArea
|
||||
aisle_width: float
|
||||
max_robot_speed: float
|
||||
|
||||
|
||||
@dataclass
|
||||
class Station:
|
||||
"""Physical interaction station (dock, picking point, or assembly drop-off)."""
|
||||
station_id: str
|
||||
station_type: StationType
|
||||
position: Position3D
|
||||
capacity: int = 1
|
||||
nearest_node_id: Optional[str] = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class ChargingStation:
|
||||
"""AMR charging station with power output rating and queue capacity."""
|
||||
station_id: str
|
||||
position: Position3D
|
||||
charging_rate_kw: float
|
||||
capacity: int = 1
|
||||
nearest_node_id: Optional[str] = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class StorageLocation:
|
||||
"""Rack storage location for an inventory SKU."""
|
||||
location_id: str
|
||||
zone: str
|
||||
position: Position3D
|
||||
aisle_id: str = ""
|
||||
nearest_node_id: Optional[str] = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class WarehouseNode:
|
||||
"""Topological graph vertex representing a traversable point or station access point."""
|
||||
node_id: str
|
||||
position: Position3D
|
||||
node_type: NodeType
|
||||
zone: str
|
||||
allowed_robot_types: Set[str] = field(default_factory=set)
|
||||
|
||||
|
||||
@dataclass
|
||||
class WarehouseEdge:
|
||||
"""Topological graph directed edge between two traversable vertices."""
|
||||
source: str
|
||||
target: str
|
||||
distance: float
|
||||
speed_limit: float
|
||||
traversal_time: float
|
||||
is_one_way: bool = False
|
||||
allowed_robot_types: Set[str] = field(default_factory=set)
|
||||
|
||||
|
||||
@dataclass
|
||||
class WarehouseModel:
|
||||
"""Complete in-memory model of warehouse floor physical layout."""
|
||||
dimensions: WarehouseDimensions
|
||||
zones: Dict[str, ZoneDefinition]
|
||||
stations: Dict[str, Station]
|
||||
charging_stations: Dict[str, ChargingStation]
|
||||
storage_locations: Dict[str, StorageLocation]
|
||||
coordinate_system: Dict[str, Any] = field(default_factory=dict)
|
||||
grid_spacing: float = 2.0
|
||||
corridor_speed_limit: float = 1.8
|
||||
|
||||
@classmethod
|
||||
def from_config_dict(cls, data: Dict[str, Any]) -> WarehouseModel:
|
||||
"""Factory constructor building WarehouseModel from validated dictionary."""
|
||||
dims = WarehouseDimensions(
|
||||
width=float(data["dimensions"]["width"]),
|
||||
depth=float(data["dimensions"]["depth"]),
|
||||
height=float(data["dimensions"]["height"]),
|
||||
)
|
||||
|
||||
zones: Dict[str, ZoneDefinition] = {}
|
||||
for z_id, z_data in data.get("zones", {}).items():
|
||||
area_data = z_data["area"]
|
||||
area = ZoneArea(
|
||||
x_min=float(area_data["x_min"]),
|
||||
x_max=float(area_data["x_max"]),
|
||||
y_min=float(area_data["y_min"]),
|
||||
y_max=float(area_data["y_max"]),
|
||||
)
|
||||
zones[z_id] = ZoneDefinition(
|
||||
zone_id=z_id,
|
||||
name=z_data["name"],
|
||||
description=z_data.get("description", ""),
|
||||
area=area,
|
||||
aisle_width=float(z_data.get("aisle_width", 2.5)),
|
||||
max_robot_speed=float(z_data.get("max_robot_speed", 1.5)),
|
||||
)
|
||||
|
||||
stations: Dict[str, Station] = {}
|
||||
for st_data in data.get("stations", []):
|
||||
st_type = StationType(st_data["type"])
|
||||
st = Station(
|
||||
station_id=st_data["station_id"],
|
||||
station_type=st_type,
|
||||
position=Position3D.from_iterable(st_data["position"]),
|
||||
capacity=int(st_data.get("capacity", 1)),
|
||||
)
|
||||
stations[st.station_id] = st
|
||||
|
||||
chargers: Dict[str, ChargingStation] = {}
|
||||
for ch_data in data.get("charging_stations", []):
|
||||
ch = ChargingStation(
|
||||
station_id=ch_data["station_id"],
|
||||
position=Position3D.from_iterable(ch_data["position"]),
|
||||
charging_rate_kw=float(ch_data.get("charging_rate_kw", 25.0)),
|
||||
capacity=int(ch_data.get("capacity", 1)),
|
||||
)
|
||||
chargers[ch.station_id] = ch
|
||||
|
||||
storage: Dict[str, StorageLocation] = {}
|
||||
for loc_data in data.get("storage_locations", []):
|
||||
loc = StorageLocation(
|
||||
location_id=loc_data["location_id"],
|
||||
zone=loc_data["zone"],
|
||||
position=Position3D.from_iterable(loc_data["position"]),
|
||||
aisle_id=loc_data.get("aisle_id", ""),
|
||||
)
|
||||
storage[loc.location_id] = loc
|
||||
|
||||
grid_cfg = data.get("grid", {})
|
||||
return cls(
|
||||
dimensions=dims,
|
||||
zones=zones,
|
||||
stations=stations,
|
||||
charging_stations=chargers,
|
||||
storage_locations=storage,
|
||||
coordinate_system=data.get("coordinate_system", {}),
|
||||
grid_spacing=float(grid_cfg.get("node_spacing", 2.0)),
|
||||
corridor_speed_limit=float(grid_cfg.get("corridor_speed_limit", 1.8)),
|
||||
)
|
||||
Reference in New Issue
Block a user