Download scripts/eval_dispatch.py from Gaurav711/HyperFlow: direct link, hf CLI and curl.
- Browser
- Download file 3.32 kB
-
https://huggingface.co/spaces/Gaurav711/HyperFlow/resolve/main/scripts/eval_dispatch.py
- Command line
-
hf download hf://spaces/Gaurav711/HyperFlow/scripts/eval_dispatch.py
-
curl -L -o eval_dispatch.py https://huggingface.co/spaces/Gaurav711/HyperFlow/resolve/main/scripts/eval_dispatch.py
3.32 kB
| #!/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() | |