File size: 3,323 Bytes
2b86c60 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | #!/usr/bin/env python3
"""Compare dispatch methods on identical deterministic, labelled scenarios.
No files are written unless --output is supplied. Distances represent independent
straight-line trips, not measured delivery outcomes or optimized multi-stop routes.
"""
from __future__ import annotations
import argparse
import hashlib
import json
import os
from pathlib import Path
import platform
import random
import sys
import time
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
from backend.flagship.operations import dispatch_plan
def evaluate(seed: int = 42, cases: int = 20) -> dict:
if cases < 1 or cases > 1000:
raise ValueError("cases must be between 1 and 1000")
import scipy
import scipy.optimize # Exclude one-time dependency import from solver timings.
rng = random.Random(seed)
results = []
for case in range(cases):
def position():
return {"latitude": 12.95 + rng.random() * .06, "longitude": 77.58 + rng.random() * .06}
jobs = [{"id": f"job-{i}", "pickup": position(), "dropoff": position(), "load_units": rng.randint(1, 3)} for i in range(8)]
couriers = [{"id": f"courier-{i}", "position": position(), "capacity_units": 5, "speed_kmh": 20, "handling_minutes": 2} for i in range(3)]
encoded = json.dumps({"jobs": jobs, "couriers": couriers}, sort_keys=True, separators=(",", ":")).encode()
row = {"case": case, "input_sha256": hashlib.sha256(encoded).hexdigest()}
for strategy in ["nearest_first", "cost"]:
started = time.perf_counter()
result = dispatch_plan(jobs, couriers, strategy)
row[strategy] = {"assigned_count": result["assigned_count"], "total_distance_km": result["total_distance_km"],
"runtime_ms": round((time.perf_counter() - started) * 1000, 3)}
results.append(row)
return {"schema_version": 1, "source": "simulation", "seed": seed, "cases": cases,
"runtime": {"python": platform.python_version(), "scipy": scipy.__version__,
"system": platform.system(), "release": platform.release(),
"machine": platform.machine(), "cpu_count": os.cpu_count()},
"model": "independent_straight_line_trips", "results": results,
"summary": {strategy: {"assigned_count": sum(r[strategy]["assigned_count"] for r in results),
"total_distance_km": round(sum(r[strategy]["total_distance_km"] for r in results), 6)}
for strategy in ["nearest_first", "cost"]},
"interpretation": "Compare distance only alongside assigned count; these are controlled scenario results, not operational savings."}
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--seed", type=int, default=42)
parser.add_argument("--cases", type=int, default=20)
parser.add_argument("--output", type=Path)
args = parser.parse_args()
result = evaluate(args.seed, args.cases)
encoded = json.dumps(result, indent=2, allow_nan=False) + "\n"
if args.output:
args.output.parent.mkdir(parents=True, exist_ok=True)
args.output.write_text(encoded)
else:
print(encoded, end="")
if __name__ == "__main__":
main()
|