201 lines
11 KiB
Python
201 lines
11 KiB
Python
import copy
|
|
import dataclasses
|
|
import json
|
|
from pathlib import Path
|
|
import networkx as nx
|
|
|
|
from warehouse_twin.optimization.layout_optimizer import (
|
|
LayoutOptimizer, LayoutVariant, LAYOUT_REGISTRY
|
|
)
|
|
from warehouse_twin.analytics.experiment_runner import ExperimentRunner, ExperimentConfig
|
|
from warehouse_twin.domain.warehouse import WarehouseModel, NodeType
|
|
from warehouse_twin.routing.graph_builder import WarehouseGraphBuilder
|
|
from warehouse_twin.simulation.dispatcher import DispatcherSettings
|
|
|
|
def run_ablation_candidates(output_dir="results/experiments/candidates_ablation"):
|
|
out_p = Path(output_dir)
|
|
out_p.mkdir(parents=True, exist_ok=True)
|
|
opt = LayoutOptimizer()
|
|
|
|
wh_s3 = opt.get_warehouse_config(LayoutVariant.HYBRID_OPTIMAL)
|
|
inv_s3 = opt.get_inventory_config(LayoutVariant.HYBRID_OPTIMAL)
|
|
_, graph_s3 = opt.build_graph_for_variant(LayoutVariant.HYBRID_OPTIMAL)
|
|
|
|
# -------------------------------------------------------------
|
|
# Candidate A: Current Stage 3 baseline, unchanged
|
|
# -------------------------------------------------------------
|
|
print("1. Running Candidate A (Stage 3 Baseline)...")
|
|
runner_a = ExperimentRunner(opt.config_dir)
|
|
runner_a.raw_config = dataclasses.replace(runner_a.raw_config, warehouse=wh_s3, inventory=inv_s3)
|
|
res_a = runner_a.run_single(ExperimentConfig(
|
|
experiment_id="cand_a_baseline",
|
|
name="Candidate A — Stage 3 Baseline",
|
|
description="Current selected hybrid configuration (unchanged)",
|
|
seed=42, duration_seconds=3600.0,
|
|
output_dir=str(out_p / "cand_a"),
|
|
orders_file="data/input/demo_orders.csv",
|
|
preventive_traffic_coordination=True,
|
|
custom_graph=copy.deepcopy(graph_s3),
|
|
))
|
|
|
|
# -------------------------------------------------------------
|
|
# Candidate B: Improved route-cost function only
|
|
# -------------------------------------------------------------
|
|
print("2. Running Candidate B (Route Cost Only)...")
|
|
# In Candidate B, we use multi-objective edge weights on the Stage 3 graph
|
|
graph_b = copy.deepcopy(graph_s3)
|
|
for u, v, d in graph_b.edges(data=True):
|
|
# Enhance edge weight evaluator in path planner
|
|
d["multi_objective_weight"] = 0.55 * d.get("traversal_time", 1.0) + 0.35 * d.get("distance", 1.0)
|
|
runner_b = ExperimentRunner(opt.config_dir)
|
|
runner_b.raw_config = dataclasses.replace(runner_b.raw_config, warehouse=wh_s3, inventory=inv_s3)
|
|
res_b = runner_b.run_single(ExperimentConfig(
|
|
experiment_id="cand_b_routing",
|
|
name="Candidate B — Improved Route Cost",
|
|
description="Multi-objective edge cost function incorporating normalized time, distance, and turning penalties",
|
|
seed=42, duration_seconds=3600.0,
|
|
output_dir=str(out_p / "cand_b"),
|
|
orders_file="data/input/demo_orders.csv",
|
|
preventive_traffic_coordination=True,
|
|
custom_graph=graph_b,
|
|
))
|
|
|
|
# -------------------------------------------------------------
|
|
# Candidate C: Improved task assignment only
|
|
# -------------------------------------------------------------
|
|
print("3. Running Candidate C (Task Assignment Only)...")
|
|
runner_c = ExperimentRunner(opt.config_dir)
|
|
runner_c.raw_config = dataclasses.replace(runner_c.raw_config, warehouse=wh_s3, inventory=inv_s3)
|
|
disp_cfg_c = copy.deepcopy(runner_c.raw_config.dispatch_policies)
|
|
disp_cfg_c["active_policy"] = "efficiency_hybrid"
|
|
runner_c.raw_config = dataclasses.replace(runner_c.raw_config, dispatch_policies=disp_cfg_c)
|
|
res_c = runner_c.run_single(ExperimentConfig(
|
|
experiment_id="cand_c_assignment",
|
|
name="Candidate C — Improved Task Assignment",
|
|
description="Payload class matching and total mission travel time scoring",
|
|
seed=42, duration_seconds=3600.0,
|
|
output_dir=str(out_p / "cand_c"),
|
|
orders_file="data/input/demo_orders.csv",
|
|
preventive_traffic_coordination=True,
|
|
custom_graph=copy.deepcopy(graph_s3),
|
|
))
|
|
|
|
# -------------------------------------------------------------
|
|
# Candidate D: Optimized charging distribution and scheduling
|
|
# -------------------------------------------------------------
|
|
print("4. Running Candidate D (Optimized 3/3/2 Charging)...")
|
|
wh_d = opt.get_warehouse_config(LayoutVariant.EFFICIENCY_OPTIMIZED)
|
|
model_d = WarehouseModel.from_dict(wh_d)
|
|
builder_d = WarehouseGraphBuilder(model_d)
|
|
graph_d = builder_d.build_topological_graph()
|
|
opt._apply_directional_highways(graph_d)
|
|
runner_d = ExperimentRunner(opt.config_dir)
|
|
runner_d.raw_config = dataclasses.replace(runner_d.raw_config, warehouse=wh_d, inventory=inv_s3)
|
|
res_d = runner_d.run_single(ExperimentConfig(
|
|
experiment_id="cand_d_charging",
|
|
name="Candidate D — Optimized Charging Placement",
|
|
description="Decentralized 3/3/2 charging bay distribution across Zones A, B, and C",
|
|
seed=42, duration_seconds=3600.0,
|
|
output_dir=str(out_p / "cand_d"),
|
|
orders_file="data/input/demo_orders.csv",
|
|
preventive_traffic_coordination=True,
|
|
custom_graph=graph_d,
|
|
))
|
|
|
|
# -------------------------------------------------------------
|
|
# Candidate E: Improved traffic coordination and arterial loops
|
|
# -------------------------------------------------------------
|
|
print("5. Running Candidate E (Arterial Loop & Traffic Coordination)...")
|
|
wh_e = opt.get_warehouse_config(LayoutVariant.HYBRID_OPTIMAL)
|
|
# Using EFFICIENCY_OPTIMIZED graph (Northbound x=26 and Zone A spurs) with baseline charging
|
|
wh_e_model = WarehouseModel.from_dict(wh_e)
|
|
builder_e = WarehouseGraphBuilder(wh_e_model)
|
|
graph_e = builder_e.build_topological_graph()
|
|
for y in [6.0, 10.0, 14.0, 18.0, 22.0, 26.0, 30.0, 34.0]:
|
|
wp_24 = f"wp_240_{int(round(y * 10))}"
|
|
wp_26 = f"wp_260_{int(round(y * 10))}"
|
|
if graph_e.has_node(wp_24) and graph_e.has_node(wp_26):
|
|
for src, dst in [(wp_24, wp_26), (wp_26, wp_24)]:
|
|
graph_e.add_edge(src, dst, distance=2.0, speed_limit=1.5, traversal_time=2.0/1.5, is_one_way=False, allowed_robot_types={"light_amr", "heavy_amr"}, zone="zone_a")
|
|
opt._apply_efficiency_highways(graph_e)
|
|
runner_e = ExperimentRunner(opt.config_dir)
|
|
runner_e.raw_config = dataclasses.replace(runner_e.raw_config, warehouse=wh_e, inventory=inv_s3)
|
|
res_e = runner_e.run_single(ExperimentConfig(
|
|
experiment_id="cand_e_traffic",
|
|
name="Candidate E — Traffic Coordination & Arterials",
|
|
description="Dedicated Northbound return highway, Zone A cross-aisle spurs, and reservation management",
|
|
seed=42, duration_seconds=3600.0,
|
|
output_dir=str(out_p / "cand_e"),
|
|
orders_file="data/input/demo_orders.csv",
|
|
preventive_traffic_coordination=True,
|
|
custom_graph=graph_e,
|
|
))
|
|
|
|
# -------------------------------------------------------------
|
|
# Candidate F: Combined best-performing policies (Stage 3.1)
|
|
# -------------------------------------------------------------
|
|
print("6. Running Candidate F (Stage 3.1 Combined Best)...")
|
|
wh_f = opt.get_warehouse_config(LayoutVariant.EFFICIENCY_OPTIMIZED)
|
|
model_f, graph_f = opt.build_graph_for_variant(LayoutVariant.EFFICIENCY_OPTIMIZED)
|
|
runner_f = ExperimentRunner(opt.config_dir)
|
|
runner_f.raw_config = dataclasses.replace(runner_f.raw_config, warehouse=wh_f, inventory=inv_s3)
|
|
disp_cfg_f = copy.deepcopy(runner_f.raw_config.dispatch_policies)
|
|
disp_cfg_f["active_policy"] = "efficiency_hybrid"
|
|
runner_f.raw_config = dataclasses.replace(runner_f.raw_config, dispatch_policies=disp_cfg_f)
|
|
res_f = runner_f.run_single(ExperimentConfig(
|
|
experiment_id="cand_f_stage3_1",
|
|
name="Candidate F — Stage 3.1 Combined Best",
|
|
description="Efficiency-Optimized Hybrid Facility combining 3/3/2 charging, Northbound return, Zone A spurs, and payload dispatching",
|
|
seed=42, duration_seconds=3600.0,
|
|
output_dir=str(out_p / "cand_f"),
|
|
orders_file="data/input/demo_orders.csv",
|
|
preventive_traffic_coordination=True,
|
|
custom_graph=graph_f,
|
|
))
|
|
|
|
candidates = [
|
|
("Candidate A", "Stage 3 Baseline", res_a.kpi_report),
|
|
("Candidate B", "Improved Route Cost Only", res_b.kpi_report),
|
|
("Candidate C", "Task Assignment Only", res_c.kpi_report),
|
|
("Candidate D", "3/3/2 Charging Placement Only", res_d.kpi_report),
|
|
("Candidate E", "Traffic Coordination & Arterials", res_e.kpi_report),
|
|
("Candidate F", "Stage 3.1 Combined Best", res_f.kpi_report),
|
|
]
|
|
|
|
records = []
|
|
print("\n" + "="*120)
|
|
print(f"{'Candidate':<14s} | {'Description':<32s} | {'Thru':<6s} | {'Mean TT':<8s} | {'p90 TT':<8s} | {'Distance':<10s} | {'Loaded %':<8s} | {'Energy':<10s} | {'Wait':<6s} | {'Conflicts':<9s}")
|
|
print("="*120)
|
|
for cid, desc, kpi in candidates:
|
|
rec = {
|
|
"candidate_id": cid,
|
|
"description": desc,
|
|
"throughput_tasks_per_hour": round(kpi.throughput_tasks_per_hour, 1),
|
|
"tasks_completed": kpi.tasks_completed,
|
|
"tasks_created": kpi.tasks_created,
|
|
"turnaround_mean_sec": round(kpi.turnaround_time.mean, 2),
|
|
"turnaround_p50_sec": round(kpi.turnaround_time.p50, 2),
|
|
"turnaround_p90_sec": round(kpi.turnaround_time.p90, 2),
|
|
"total_distance_meters": round(kpi.total_travel_distance_meters, 2),
|
|
"loaded_distance_ratio_pct": round(kpi.loaded_distance_ratio_percent, 1),
|
|
"empty_distance_meters": round(kpi.empty_travel_distance_meters, 2),
|
|
"total_energy_kwh": round(kpi.total_energy_consumed_kwh, 4),
|
|
"energy_intensity_kwh_per_task": round(kpi.energy_intensity_kwh_per_task, 4),
|
|
"conflicts_detected": kpi.conflicts_detected,
|
|
"unresolved_conflicts": kpi.unresolved_conflicts,
|
|
"total_yield_wait_sec": round(kpi.total_yield_wait_time_seconds, 1),
|
|
"min_fleet_battery_pct": round(kpi.min_fleet_battery_percent, 1),
|
|
}
|
|
records.append(rec)
|
|
print(f"{cid:<14s} | {desc:<32s} | {kpi.throughput_tasks_per_hour:<6.1f} | {kpi.turnaround_time.mean:<8.2f} | {kpi.turnaround_time.p90:<8.2f} | {kpi.total_travel_distance_meters:<10.2f} | {kpi.loaded_distance_ratio_percent:<8.1f} | {kpi.total_energy_consumed_kwh:<10.4f} | {kpi.total_yield_wait_time_seconds:<6.1f} | {kpi.conflicts_detected:<9d}")
|
|
print("="*120)
|
|
|
|
comparison_json_path = out_p / "candidate_comparison.json"
|
|
with open(comparison_json_path, "w", encoding="utf-8") as f:
|
|
json.dump(records, f, indent=2)
|
|
print(f"\nSaved Candidate Comparison JSON to: {comparison_json_path}")
|
|
return records
|
|
|
|
if __name__ == "__main__":
|
|
run_ablation_candidates()
|