|
| 1 | +"""Evaluate a completed experiment's plan against actual demand. |
| 2 | +
|
| 3 | +The optimizer only sees forecasted demand, so its plan may over- or under-ship |
| 4 | +relative to what actually happens. This module simulates the planned flows |
| 5 | +against ground-truth demand to produce realized (as-executed) metrics, |
| 6 | +both in aggregate and broken down per destination. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + from actuals_evaluator import evaluate, save_realized_metrics |
| 10 | + metrics = evaluate(output_path) |
| 11 | + save_realized_metrics(metrics, output_path) |
| 12 | +""" |
| 13 | + |
| 14 | +import json |
| 15 | +from dataclasses import asdict, dataclass |
| 16 | +from pathlib import Path |
| 17 | + |
| 18 | +import polars as pl |
| 19 | + |
| 20 | +from data.ingestion import Reader |
| 21 | +from experiment_config import load_experiment_config |
| 22 | +from utils.system_paths import get_project_root |
| 23 | + |
| 24 | + |
| 25 | +@dataclass |
| 26 | +class DestinationActuals: |
| 27 | + transport_cost: float |
| 28 | + realized_holding_cost: float |
| 29 | + realized_total_cost: float |
| 30 | + total_actual_demand: float |
| 31 | + total_fulfilled: float |
| 32 | + total_shortage: float |
| 33 | + fill_rate: float |
| 34 | + cost_per_unit_demanded: float |
| 35 | + cost_per_unit_fulfilled: float |
| 36 | + |
| 37 | + |
| 38 | +@dataclass |
| 39 | +class RealizedMetrics: |
| 40 | + transport_cost: float |
| 41 | + realized_holding_cost: float |
| 42 | + realized_total_cost: float |
| 43 | + total_actual_demand: float |
| 44 | + total_fulfilled: float |
| 45 | + total_shortage: float |
| 46 | + fill_rate: float |
| 47 | + cost_per_unit_demanded: float |
| 48 | + cost_per_unit_fulfilled: float |
| 49 | + per_destination: dict[str, DestinationActuals] |
| 50 | + |
| 51 | + |
| 52 | +def evaluate(experiment_output_path: Path) -> RealizedMetrics: |
| 53 | + """Simulate the experiment's planned flows against actual demand. |
| 54 | +
|
| 55 | + Reads flows.parquet and metrics.json from experiment_output_path, plus the |
| 56 | + original dataset referenced in config.yaml, and returns realized metrics. |
| 57 | + """ |
| 58 | + project_root = get_project_root() |
| 59 | + config = load_experiment_config( |
| 60 | + project_root, experiment_output_path / "config.yaml" |
| 61 | + ) |
| 62 | + |
| 63 | + flows = pl.read_parquet(experiment_output_path / "flows.parquet") |
| 64 | + |
| 65 | + raw = Reader(config.dataset_path).read() |
| 66 | + |
| 67 | + holding_cost_map: dict[str, float] = {} |
| 68 | + if "holding_cost" in raw.destinations.columns: |
| 69 | + holding_cost_map = dict( |
| 70 | + zip( |
| 71 | + raw.destinations["destination_id"].to_list(), |
| 72 | + raw.destinations["holding_cost"].to_list(), |
| 73 | + ) |
| 74 | + ) |
| 75 | + |
| 76 | + # Per-destination transport cost from planned flows × lane unit costs |
| 77 | + lane_cost_map: dict[tuple, float] = { |
| 78 | + (row["origin_id"], row["destination_id"]): row["unit_cost"] |
| 79 | + for row in raw.lanes.to_dicts() |
| 80 | + } |
| 81 | + dest_transport_cost: dict[str, float] = {} |
| 82 | + for row in flows.to_dicts(): |
| 83 | + d_id = row["destination_id"] |
| 84 | + cost = lane_cost_map.get((row["origin_id"], d_id), 0.0) * row["flow"] |
| 85 | + dest_transport_cost[d_id] = dest_transport_cost.get(d_id, 0.0) + cost |
| 86 | + |
| 87 | + forecast_dates = sorted(flows["period"].unique().to_list()) |
| 88 | + |
| 89 | + actual_demand = raw.demand_history.filter(pl.col("date").is_in(forecast_dates)) |
| 90 | + |
| 91 | + # Aggregate planned inflows per (destination, period) |
| 92 | + inflows = ( |
| 93 | + flows.group_by(["destination_id", "period"]) |
| 94 | + .agg(pl.col("flow").sum().alias("inflow")) |
| 95 | + .rename({"period": "date"}) |
| 96 | + ) |
| 97 | + |
| 98 | + destinations = sorted(raw.destinations["destination_id"].to_list()) |
| 99 | + |
| 100 | + total_actual_demand = 0.0 |
| 101 | + total_fulfilled = 0.0 |
| 102 | + total_shortage = 0.0 |
| 103 | + realized_holding_cost = 0.0 |
| 104 | + per_destination: dict[str, DestinationActuals] = {} |
| 105 | + |
| 106 | + for d_id in destinations: |
| 107 | + h_cost = holding_cost_map.get(d_id, 0.0) |
| 108 | + |
| 109 | + inflow_map: dict = { |
| 110 | + row["date"]: row["inflow"] |
| 111 | + for row in inflows.filter(pl.col("destination_id") == d_id).to_dicts() |
| 112 | + } |
| 113 | + demand_map: dict = { |
| 114 | + row["date"]: row["demand"] |
| 115 | + for row in actual_demand.filter(pl.col("destination_id") == d_id).to_dicts() |
| 116 | + } |
| 117 | + |
| 118 | + d_fulfilled = 0.0 |
| 119 | + d_shortage = 0.0 |
| 120 | + d_holding_cost = 0.0 |
| 121 | + d_demand = 0.0 |
| 122 | + inventory = 0.0 |
| 123 | + |
| 124 | + for t in forecast_dates: |
| 125 | + inflow = inflow_map.get(t, 0.0) |
| 126 | + actual_d = demand_map.get(t, 0.0) |
| 127 | + |
| 128 | + available = inventory + inflow |
| 129 | + fulfilled = min(actual_d, available) |
| 130 | + shortage = max(0.0, actual_d - available) |
| 131 | + inventory = available - fulfilled |
| 132 | + |
| 133 | + d_demand += actual_d |
| 134 | + d_fulfilled += fulfilled |
| 135 | + d_shortage += shortage |
| 136 | + d_holding_cost += h_cost * inventory |
| 137 | + |
| 138 | + d_transport = dest_transport_cost.get(d_id, 0.0) |
| 139 | + d_realized_total = d_transport + d_holding_cost |
| 140 | + d_fill_rate = d_fulfilled / d_demand if d_demand > 0 else 1.0 |
| 141 | + d_cpu_demanded = d_realized_total / d_demand if d_demand > 0 else 0.0 |
| 142 | + d_cpu_fulfilled = d_realized_total / d_fulfilled if d_fulfilled > 0 else 0.0 |
| 143 | + |
| 144 | + per_destination[d_id] = DestinationActuals( |
| 145 | + transport_cost=d_transport, |
| 146 | + realized_holding_cost=d_holding_cost, |
| 147 | + realized_total_cost=d_realized_total, |
| 148 | + total_actual_demand=d_demand, |
| 149 | + total_fulfilled=d_fulfilled, |
| 150 | + total_shortage=d_shortage, |
| 151 | + fill_rate=d_fill_rate, |
| 152 | + cost_per_unit_demanded=d_cpu_demanded, |
| 153 | + cost_per_unit_fulfilled=d_cpu_fulfilled, |
| 154 | + ) |
| 155 | + |
| 156 | + total_actual_demand += d_demand |
| 157 | + total_fulfilled += d_fulfilled |
| 158 | + total_shortage += d_shortage |
| 159 | + realized_holding_cost += d_holding_cost |
| 160 | + |
| 161 | + transport_cost = sum(dest_transport_cost.values()) |
| 162 | + realized_total_cost = transport_cost + realized_holding_cost |
| 163 | + fill_rate = total_fulfilled / total_actual_demand if total_actual_demand > 0 else 1.0 |
| 164 | + cpu_demanded = realized_total_cost / total_actual_demand if total_actual_demand > 0 else 0.0 |
| 165 | + cpu_fulfilled = realized_total_cost / total_fulfilled if total_fulfilled > 0 else 0.0 |
| 166 | + |
| 167 | + return RealizedMetrics( |
| 168 | + transport_cost=transport_cost, |
| 169 | + realized_holding_cost=realized_holding_cost, |
| 170 | + realized_total_cost=realized_total_cost, |
| 171 | + total_actual_demand=total_actual_demand, |
| 172 | + total_fulfilled=total_fulfilled, |
| 173 | + total_shortage=total_shortage, |
| 174 | + fill_rate=fill_rate, |
| 175 | + cost_per_unit_demanded=cpu_demanded, |
| 176 | + cost_per_unit_fulfilled=cpu_fulfilled, |
| 177 | + per_destination=per_destination, |
| 178 | + ) |
| 179 | + |
| 180 | + |
| 181 | +def save_realized_metrics(metrics: RealizedMetrics, output_path: Path) -> None: |
| 182 | + with open(output_path / "realized_metrics.json", "w") as f: |
| 183 | + json.dump(asdict(metrics), f, indent=2) |
0 commit comments