feat(phase-5): implement battery model, charging queues, production deadlines, and safe reassignment
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
"""Unit tests for ChargingManager: capacity-constrained queues, station allocation, and charging dynamics."""
|
||||
|
||||
import pytest
|
||||
import simpy
|
||||
|
||||
from warehouse_twin.domain.robot import Robot, RobotTypeSpec
|
||||
from warehouse_twin.domain.warehouse import ChargingStation, Position3D
|
||||
from warehouse_twin.fleet.charging_manager import ChargingBay, ChargingManager
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def test_charging_stations():
|
||||
return [
|
||||
ChargingStation(station_id="charge_bay_1", position=Position3D(2.0, 36.0, 0.0), charging_rate_kw=25.0, capacity=1),
|
||||
ChargingStation(station_id="charge_bay_2", position=Position3D(4.0, 36.0, 0.0), charging_rate_kw=25.0, capacity=1),
|
||||
ChargingStation(station_id="charge_bay_3", position=Position3D(6.0, 36.0, 0.0), charging_rate_kw=50.0, capacity=1),
|
||||
ChargingStation(station_id="charge_bay_4", position=Position3D(8.0, 36.0, 0.0), charging_rate_kw=50.0, capacity=1),
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def light_robot():
|
||||
return Robot(
|
||||
robot_id="amr_light_01",
|
||||
robot_type="light_amr",
|
||||
position=Position3D(2.0, 30.0, 0.0),
|
||||
maximum_speed=1.8,
|
||||
payload_capacity=150.0,
|
||||
battery_level=0.5, # Needs charge
|
||||
battery_capacity=3.0,
|
||||
energy_consumption={"idle": 0.05, "moving_empty": 0.35, "moving_loaded": 0.65, "handling": 0.15},
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def heavy_robot():
|
||||
return Robot(
|
||||
robot_id="amr_heavy_01",
|
||||
robot_type="heavy_amr",
|
||||
position=Position3D(6.0, 30.0, 0.0),
|
||||
maximum_speed=1.1,
|
||||
payload_capacity=1500.0,
|
||||
battery_level=2.0, # Needs charge
|
||||
battery_capacity=15.0,
|
||||
energy_consumption={"idle": 0.12, "moving_empty": 0.90, "moving_loaded": 2.20, "handling": 0.50},
|
||||
)
|
||||
|
||||
|
||||
def test_charging_manager_initialization(test_charging_stations):
|
||||
env = simpy.Environment()
|
||||
manager = ChargingManager(env, test_charging_stations)
|
||||
assert len(manager.bays) == 4
|
||||
assert manager.bays["charge_bay_1"].charging_rate_kw == 25.0
|
||||
assert manager.bays["charge_bay_3"].charging_rate_kw == 50.0
|
||||
assert manager.bays["charge_bay_1"].capacity == 1
|
||||
|
||||
|
||||
def test_charging_bay_selection_preference(test_charging_stations, light_robot, heavy_robot):
|
||||
env = simpy.Environment()
|
||||
manager = ChargingManager(env, test_charging_stations)
|
||||
|
||||
# Light robot should prefer 25kW bays near x=2.0
|
||||
bay_light = manager.select_best_charging_bay(light_robot)
|
||||
assert bay_light.station_id in ("charge_bay_1", "charge_bay_2")
|
||||
assert bay_light.charging_rate_kw == 25.0
|
||||
|
||||
# Heavy robot should prefer 50kW bays near x=6.0
|
||||
bay_heavy = manager.select_best_charging_bay(heavy_robot)
|
||||
assert bay_heavy.station_id in ("charge_bay_3", "charge_bay_4")
|
||||
assert bay_heavy.charging_rate_kw == 50.0
|
||||
|
||||
|
||||
def test_capacity_constrained_queueing(test_charging_stations, light_robot):
|
||||
env = simpy.Environment()
|
||||
manager = ChargingManager(env, test_charging_stations)
|
||||
bay = manager.bays["charge_bay_1"]
|
||||
|
||||
second_robot = Robot(
|
||||
robot_id="amr_light_02",
|
||||
robot_type="light_amr",
|
||||
position=Position3D(2.0, 32.0, 0.0),
|
||||
maximum_speed=1.8,
|
||||
payload_capacity=150.0,
|
||||
battery_level=0.6,
|
||||
battery_capacity=3.0,
|
||||
energy_consumption={"idle": 0.05, "moving_empty": 0.35, "moving_loaded": 0.65, "handling": 0.15},
|
||||
)
|
||||
|
||||
queue_wait_recorded = []
|
||||
|
||||
def robot_charge_sim(robot, target_charge_time):
|
||||
q_start = env.now
|
||||
with bay.resource.request() as req:
|
||||
yield req
|
||||
q_wait = env.now - q_start
|
||||
queue_wait_recorded.append((robot.robot_id, q_wait))
|
||||
bay.total_queue_wait_time_seconds += q_wait
|
||||
yield env.timeout(target_charge_time)
|
||||
robot.charge_energy(target_charge_time, bay.charging_rate_kw)
|
||||
bay.total_sessions += 1
|
||||
bay.total_charging_time_seconds += target_charge_time
|
||||
|
||||
# Robot 1 arrives at t=0, charges for 100s
|
||||
env.process(robot_charge_sim(light_robot, 100.0))
|
||||
# Robot 2 arrives at t=10s, must wait until t=100s (wait = 90s)
|
||||
def delayed_arrival():
|
||||
yield env.timeout(10.0)
|
||||
yield env.process(robot_charge_sim(second_robot, 50.0))
|
||||
|
||||
env.process(delayed_arrival())
|
||||
|
||||
env.run(until=200.0)
|
||||
|
||||
# First robot waited 0s; second robot waited 90s
|
||||
assert len(queue_wait_recorded) == 2
|
||||
assert queue_wait_recorded[0] == ("amr_light_01", 0.0)
|
||||
assert queue_wait_recorded[1] == ("amr_light_02", 90.0)
|
||||
assert bay.total_sessions == 2
|
||||
assert bay.total_charging_time_seconds == 150.0
|
||||
|
||||
metrics = manager.get_metrics(200.0)
|
||||
assert metrics["total_charging_sessions"] == 2
|
||||
assert metrics["total_queue_wait_time_seconds"] == 90.0
|
||||
assert metrics["average_queue_wait_time_seconds"] == 45.0
|
||||
Reference in New Issue
Block a user