Renommage Fuel Advisor (titre, header, favicon SVG/PNG, apple-touch-icon, logo SVG servis localement). Oracle: contrainte de penalty corrigee (portee inversee), quantites non entieres ecartees avant rejeu, double comptage de penalite supprime. Audit economique regenerer: plafonds omniscients coherents et positifs; deux lignes non resolues conservees telles quelles.
111 lines
4.2 KiB
Python
111 lines
4.2 KiB
Python
"""Valeur du lieu d'achat : vrais prix et routes OSRM existantes, aucun prix futur."""
|
||
|
||
import json
|
||
from collections import Counter
|
||
from pathlib import Path
|
||
|
||
from economic_engine import compare, demand
|
||
|
||
ROOT = Path(__file__).parent
|
||
|
||
|
||
def station_run(points, vehicle, routes, best=False):
|
||
days = [p["day"] for p in points]
|
||
use = demand(vehicle, days)
|
||
cap = vehicle["tank_liters"]
|
||
reserve = vehicle["min_range_km"] * vehicle["consumption"] / 100
|
||
nearby = {s: r for s, r in routes.items() if r["roundtrip_km"] <= 20}
|
||
maxdetour = max(r["roundtrip_km"] for r in nearby.values()) * vehicle["consumption"] / 100
|
||
target = cap - maxdetour / 2
|
||
stock = cap
|
||
cash = 0.0
|
||
consumed = 0.0
|
||
detours = 0.0
|
||
visits = []
|
||
purchases = []
|
||
levels = []
|
||
for i, p in enumerate(points):
|
||
remaining = float(sum(use[i:]))
|
||
need = stock < reserve + use[i] + maxdetour / 2 + 1e-9 and stock < reserve + remaining - 1e-7
|
||
if need:
|
||
if visits and i - visits[-1] < 3:
|
||
raise ValueError("Confort intervalle")
|
||
if sum(days[j][:7] == days[i][:7] for j in visits) >= 8:
|
||
raise ValueError("Confort mensuel")
|
||
candidates = []
|
||
for sid, price in p["prices"].items():
|
||
if sid not in nearby:
|
||
continue
|
||
km = nearby[sid]["roundtrip_km"]
|
||
dl = km * vehicle["consumption"] / 100
|
||
q = max(10, min(target, remaining + reserve) - stock + dl)
|
||
if stock - dl / 2 < reserve - 1e-7 or stock + q - dl / 2 > cap + 1e-7:
|
||
continue
|
||
candidates.append((q * price, km, sid, q, price, dl))
|
||
if not candidates:
|
||
raise ValueError("Aucune station routée accessible à " + days[i])
|
||
cost, km, sid, q, price, dl = min(candidates, key=(lambda x: x[0]) if best else (lambda x: x[1]))
|
||
if q > 1e-6:
|
||
stock += q - dl
|
||
consumed += dl
|
||
cash += cost
|
||
detours += km
|
||
purchases.append(dict(day=days[i], litres=q, price=price, station_id=sid, route_km=km))
|
||
visits.append(i)
|
||
stock -= use[i]
|
||
consumed += use[i]
|
||
levels.append(stock)
|
||
if stock < reserve - 1e-7:
|
||
raise ValueError("Réserve")
|
||
bought = sum(p["litres"] for p in purchases)
|
||
error = bought - consumed - stock + cap
|
||
assert abs(error) < 1e-7
|
||
# Même valorisation terminale pour les deux bras : prix de la plus proche au début.
|
||
first = points[0]["prices"]
|
||
closest = min((s for s in nearby if s in first), key=lambda s: nearby[s]["roundtrip_km"])
|
||
return dict(
|
||
cash=cash,
|
||
stock_adjusted_cost=cash + (cap - stock) * first[closest],
|
||
stock_initial=cap,
|
||
stock_final=stock,
|
||
litres=bought,
|
||
consumed=consumed,
|
||
conservation_error=error,
|
||
price_paid=cash / bought,
|
||
passages=len(visits),
|
||
visits=visits,
|
||
purchases=purchases,
|
||
levels=levels,
|
||
detour_km=detours,
|
||
reserve_ok=bool(min(levels) >= reserve - 1e-7),
|
||
comfort_ok=all(b - a >= 3 for a, b in zip(visits, visits[1:], strict=False))
|
||
and max(Counter(days[i][:7] for i in visits).values(), default=0) <= 8,
|
||
)
|
||
|
||
|
||
def run():
|
||
data = json.loads((ROOT / "economic-inputs.json").read_text())
|
||
result = []
|
||
for key, v in data["profiles"].items():
|
||
points = [p for p in data["fuels"][v["fuel"]] if "2025-01-01" <= p["day"] <= "2026-08-30"]
|
||
base = station_run(points, v, data["routes"])
|
||
best = station_run(points, v, data["routes"], True)
|
||
summary = compare(base, best, [p["day"] for p in points])
|
||
result.append(dict(profile=key, baseline=base, best=best, summary=summary))
|
||
print(key, summary, flush=True)
|
||
(ROOT / "station-audit-results.json").write_text(
|
||
json.dumps(
|
||
dict(
|
||
origin="fav:1",
|
||
max_roundtrip_km=20,
|
||
routes="OSRM actuelles appliquées rétrospectivement, hypothèse réseau routier stable ; aller-retour = 2 × aller, pas trafic",
|
||
results=result,
|
||
),
|
||
indent=1,
|
||
)
|
||
)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
run()
|