Conseil simule aligne sur le moteur reel et validation par regimes

Le conseiller du backtest raisonne desormais comme le moteur de decision : besoin d abord, anticipation seulement si le gain depasse le seuil du vehicule, et plancher d appoint qui interdit de repasser a la pompe pour quelques litres. La campagne precedente le montrait : sans ce plancher, la strategie multipliait les passages quotidiens et le verdict la refusait a juste titre. Ajout de la validation par regimes : chaque periode atypique est evaluee avec un apprentissage arrete la veille de son debut, plus l experience ou la crise actuelle reste entierement hors entrainement.
This commit is contained in:
root
2026-09-13 00:50:59 +02:00
parent ad46a08d64
commit b82efb054a
24 changed files with 2657 additions and 1517 deletions

View File

@@ -1,4 +1,5 @@
"""Cache de routes : origine normalisée, rattachement au favori, séparation statique / trafic."""
from datetime import datetime, timedelta, timezone
import pytest
@@ -14,11 +15,11 @@ def meters_between(origin, other):
def test_gps_jitter_of_a_few_dozens_of_meters_shares_one_cache_key(store):
target = (44.795280, -0.604509)
first = (44.78060, -0.57060)
second = (44.78063, -0.57064) # ~5 m plus loin : bruit normal d'une mesure téléphone
third = (44.78110, -0.57110) # ~70 m plus loin
second = (44.78063, -0.57064) # ~5 m plus loin : bruit normal d'une mesure téléphone
third = (44.78110, -0.57110) # ~70 m plus loin
assert meters_between(first, second) < 10
keys = {storage.route_key(point, target) for point in (first, second, third)}
assert len(keys) == 1, f'positions quasi identiques, clés distinctes : {keys}'
assert len(keys) == 1, f"positions quasi identiques, clés distinctes : {keys}"
def test_two_positions_hundreds_of_meters_apart_use_distinct_keys(store):
@@ -37,77 +38,89 @@ def test_distinct_stations_never_share_a_key(store):
def test_favourite_tag_replaces_raw_coordinates(store):
target = (44.795280, -0.604509)
key = storage.route_key((44.78055, -0.57051), target, origin_tag='fav:3')
assert key.startswith('fav:3|')
assert storage.route_key((44.79999, -0.57051), target, origin_tag='fav:3') == key
key = storage.route_key((44.78055, -0.57051), target, origin_tag="fav:3")
assert key.startswith("fav:3|")
assert storage.route_key((44.79999, -0.57051), target, origin_tag="fav:3") == key
def test_match_favorite_absorbs_gps_jitter(store):
storage.save_entity('favorites', dict(name='Villenave', lat=44.78060, lon=-0.57060, radius_km=10))
storage.save_entity("favorites", dict(name="Villenave", lat=44.78060, lon=-0.57060, radius_km=10))
favorite = storage.match_favorite(44.78068, -0.57070)
assert favorite and favorite['name'] == 'Villenave'
assert favorite['distance_km'] <= 0.15
assert favorite and favorite["name"] == "Villenave"
assert favorite["distance_km"] <= 0.15
assert storage.match_favorite(44.90000, -0.57070) is None
def test_osrm_duration_is_static_and_never_an_eta(store, monkeypatch):
monkeypatch.setitem(routing.PROVIDERS, 'osrm',
lambda origin, targets: {t[0]: dict(distance_m=5000.0, duration_s=600.0,
duration_kind=routing.STATIC)
for t in targets})
monkeypatch.setenv('FUEL_ROUTING_PROVIDERS', 'osrm')
routes, info = routing.routes((44.78, -0.57), [('A', 44.79, -0.60)])
assert routes['A']['duration_kind'] == routing.STATIC
assert routes['A']['duration_label'] == 'hors trafic'
assert routes['A']['eta_min'] is None
assert info['eta_count'] == 0
assert 'hors trafic' in info['eta_note']
monkeypatch.setitem(
routing.PROVIDERS,
"osrm",
lambda origin, targets: {
t[0]: dict(distance_m=5000.0, duration_s=600.0, duration_kind=routing.STATIC) for t in targets
},
)
monkeypatch.setenv("FUEL_ROUTING_PROVIDERS", "osrm")
routes, info = routing.routes((44.78, -0.57), [("A", 44.79, -0.60)])
assert routes["A"]["duration_kind"] == routing.STATIC
assert routes["A"]["duration_label"] == "hors trafic"
assert routes["A"]["eta_min"] is None
assert info["eta_count"] == 0
assert "hors trafic" in info["eta_note"]
assert store.eta_lookup([storage.route_key((44.78, -0.57), (44.79, -0.60))]) == {}
def test_google_traffic_duration_goes_to_short_cache_only(store, monkeypatch):
monkeypatch.setattr(routing, 'GOOGLE_KEY', 'clé-de-test')
monkeypatch.setitem(routing.PROVIDERS, 'google',
lambda origin, targets: {t[0]: dict(distance_m=8000.0, duration_s=900.0,
duration_kind=routing.TRAFFIC)
for t in targets})
monkeypatch.setenv('FUEL_ROUTING_PROVIDERS', 'google')
routes, info = routing.routes((44.78, -0.57), [('A', 44.79, -0.60)])
assert routes['A']['distance_km'] == pytest.approx(8.0)
assert routes['A']['duration_min'] is None, 'une durée de trafic n’est pas une durée statique'
assert routes['A']['eta_min'] == pytest.approx(15.0)
assert routes['A']['eta_age_seconds'] is not None
assert info['eta_count'] == 1 and info['eta_provider'] == 'google'
monkeypatch.setattr(routing, "GOOGLE_KEY", "clé-de-test")
monkeypatch.setitem(
routing.PROVIDERS,
"google",
lambda origin, targets: {
t[0]: dict(distance_m=8000.0, duration_s=900.0, duration_kind=routing.TRAFFIC) for t in targets
},
)
monkeypatch.setenv("FUEL_ROUTING_PROVIDERS", "google")
routes, info = routing.routes((44.78, -0.57), [("A", 44.79, -0.60)])
assert routes["A"]["distance_km"] == pytest.approx(8.0)
assert routes["A"]["duration_min"] is None, "une durée de trafic n’est pas une durée statique"
assert routes["A"]["eta_min"] == pytest.approx(15.0)
assert routes["A"]["eta_age_seconds"] is not None
assert info["eta_count"] == 1 and info["eta_provider"] == "google"
cached = store.route_lookup([storage.route_key((44.78, -0.57), (44.79, -0.60))])
assert list(cached.values())[0]['duration_s'] is None, 'pas de durée trafic dans le cache long'
assert list(cached.values())[0]["duration_s"] is None, "pas de durée trafic dans le cache long"
def test_stale_eta_is_ignored_and_static_duration_is_used(store, monkeypatch):
monkeypatch.setattr(routing, 'GOOGLE_KEY', 'clé-de-test')
monkeypatch.setitem(routing.PROVIDERS, 'google',
lambda origin, targets: {t[0]: dict(distance_m=8000.0, duration_s=900.0,
duration_kind=routing.TRAFFIC)
for t in targets})
monkeypatch.setenv('FUEL_ROUTING_PROVIDERS', 'google')
routing.routes((44.78, -0.57), [('A', 44.79, -0.60)])
monkeypatch.setattr(routing, "GOOGLE_KEY", "clé-de-test")
monkeypatch.setitem(
routing.PROVIDERS,
"google",
lambda origin, targets: {
t[0]: dict(distance_m=8000.0, duration_s=900.0, duration_kind=routing.TRAFFIC) for t in targets
},
)
monkeypatch.setenv("FUEL_ROUTING_PROVIDERS", "google")
routing.routes((44.78, -0.57), [("A", 44.79, -0.60)])
key = storage.route_key((44.78, -0.57), (44.79, -0.60))
stale = (datetime.now(timezone.utc) - timedelta(hours=3)).isoformat()
store.eta_save([dict(key=key, duration_s=1800.0, provider='google')])
store.eta_save([dict(key=key, duration_s=1800.0, provider="google")])
with store.connect() as db:
db.execute('UPDATE route_eta SET computed_at=? WHERE key=?', (stale, key))
routes, info = routing.routes((44.78, -0.57), [('A', 44.79, -0.60)], eta_ttl=900)
assert routes['A']['eta_min'] is None, 'une ETA de 3 heures n’est pas une ETA actuelle'
assert info['eta_count'] == 0
assert 'Aucune ETA de trafic' in info['eta_note']
db.execute("UPDATE route_eta SET computed_at=? WHERE key=?", (stale, key))
routes, info = routing.routes((44.78, -0.57), [("A", 44.79, -0.60)], eta_ttl=900)
assert routes["A"]["eta_min"] is None, "une ETA de 3 heures n’est pas une ETA actuelle"
assert info["eta_count"] == 0
assert "Aucune ETA de trafic" in info["eta_note"]
def test_eta_key_is_separate_for_tagged_origin(store, monkeypatch):
monkeypatch.setattr(routing, 'GOOGLE_KEY', 'clé-de-test')
monkeypatch.setitem(routing.PROVIDERS, 'google',
lambda origin, targets: {t[0]: dict(distance_m=4000.0, duration_s=600.0,
duration_kind=routing.TRAFFIC)
for t in targets})
monkeypatch.setenv('FUEL_ROUTING_PROVIDERS', 'google')
_, info = routing.routes((44.78, -0.57), [('A', 44.79, -0.60)], origin_tag='fav:7')
assert info['eta_count'] == 1
assert store.eta_lookup(['fav:7|44.7900|-0.6000']) != {}
monkeypatch.setattr(routing, "GOOGLE_KEY", "clé-de-test")
monkeypatch.setitem(
routing.PROVIDERS,
"google",
lambda origin, targets: {
t[0]: dict(distance_m=4000.0, duration_s=600.0, duration_kind=routing.TRAFFIC) for t in targets
},
)
monkeypatch.setenv("FUEL_ROUTING_PROVIDERS", "google")
_, info = routing.routes((44.78, -0.57), [("A", 44.79, -0.60)], origin_tag="fav:7")
assert info["eta_count"] == 1
assert store.eta_lookup(["fav:7|44.7900|-0.6000"]) != {}