-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2_Optimisation.py
More file actions
446 lines (375 loc) · 16.8 KB
/
Copy path2_Optimisation.py
File metadata and controls
446 lines (375 loc) · 16.8 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import datetime
import os
import threading
import folium
import numpy as np
import streamlit as st
import main as main_module
import fonction_calcul_trajet as fct
from data import get_interventions
from optimisation.interface_helper import optimize_allocation_from_interface, get_optimization_profiles
from ui.common import (
ensure_session_state_initialized,
load_secteurs_and_gdf,
ensure_secteur_df_loaded,
render_simulation_date_range_sidebar,
convert_session_engins_to_objects,
set_gdal_env_if_needed,
)
from ui.utilisation import summarise_utilisation_for_window
st.set_page_config(layout="wide", page_title="Optimisation")
set_gdal_env_if_needed()
# --- Pre-chargement GPU data en arriere-plan ---
def _preload_gpu_data():
"""Charge les donnees GPU et cree le cache pickle si necessaire."""
try:
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent / "Reflets_gpu"))
from data_loader import load_data
load_data()
except Exception:
pass
if "gpu_preload_started" not in st.session_state:
st.session_state.gpu_preload_started = True
threading.Thread(target=_preload_gpu_data, daemon=True).start()
st.title("Optimisation")
st.caption("Page dédiée aux paramètres d’optimisation (métrique, algorithme, profil, etc.).")
ensure_session_state_initialized()
secteurs_initiaux, _gdf = load_secteurs_and_gdf()
ensure_secteur_df_loaded()
render_simulation_date_range_sidebar()
st.sidebar.markdown("---")
st.sidebar.caption("Les réglages d’optimisation sont sur la page (centre).")
profiles = get_optimization_profiles()
profile_names = {k: v["name"] for k, v in profiles.items()}
default_algo = st.session_state.get("optimization_algorithm", "simulated_annealing")
default_profile = st.session_state.get("optimization_profile", "rapide")
default_metric = st.session_state.get("optimization_metric", "temps_moyen")
st.subheader("Configuration")
with st.form("optim_config", clear_on_submit=False, border=True):
c1, c2, c3 = st.columns(3)
with c1:
algo_key = st.selectbox(
"Algorithme",
options=["simulated_annealing", "genetic"],
index=0 if default_algo == "simulated_annealing" else 1,
format_func=lambda x: "Recuit simulé"
if x == "simulated_annealing"
else "Algorithme génétique",
)
with c2:
profile_options = list(profile_names.keys())
profile_index = (
profile_options.index(default_profile)
if default_profile in profile_options
else 1
)
selected_profile_key = st.selectbox(
"Profil",
options=profile_options,
index=profile_index,
format_func=lambda x: profile_names[x],
help="Compromis vitesse/qualité",
)
with c3:
metric_key = st.selectbox(
"Métrique (objectif)",
options=["temps_moyen", "taux_couverture", "manque_local_ratio", "mixte"],
index=["temps_moyen", "taux_couverture", "manque_local_ratio", "mixte"].index(
default_metric if default_metric in {"temps_moyen", "taux_couverture", "manque_local_ratio", "mixte"} else "temps_moyen"
),
help="UI prête pour brancher le calcul de score sur une métrique différente.",
)
selected_profile = profiles[selected_profile_key]
st.caption(f"{selected_profile['description']} - {selected_profile['use_case']}")
if metric_key != "temps_moyen":
st.info(
"Le choix de métrique est enregistré, mais le score actuel de l’optimiseur "
"reste basé sur le temps moyen (à brancher côté `eval_window`)."
)
run = st.form_submit_button("Lancer l’optimisation (séquentiel)", type="primary")
st.session_state["optimization_algorithm"] = algo_key
st.session_state["optimization_profile"] = selected_profile_key
st.session_state["optimization_metric"] = metric_key
# --- Optimisation GPU ---
st.markdown("---")
st.subheader("Optimisation GPU")
with st.form("gpu_config", clear_on_submit=False, border=True):
gc1, gc2, gc3 = st.columns(3)
with gc1:
gpu_metric = st.selectbox(
"Métrique GPU",
options=["temps_moyen", "p95_vsav", "p99_vsav", "std_q95_pression", "taux_couverture", "manque_local"],
index=0,
)
with gc2:
gpu_algo = st.selectbox("Algorithme GPU", options=["sa", "ga"], format_func=lambda x: "Recuit simulé" if x == "sa" else "Génétique")
with gc3:
gpu_iter = st.number_input("Itérations", min_value=1, max_value=2000, value=5)
gc4, gc5 = st.columns(2)
with gc4:
gpu_window = st.number_input("Fenêtre (nb inter)", min_value=100, max_value=478898, value=1000)
with gc5:
gpu_batch = st.number_input("Batch size", min_value=32, max_value=2048, value=512)
run_gpu = st.form_submit_button("Lancer l’optimisation GPU", type="primary")
if run_gpu:
import subprocess, sys
from pathlib import Path
gpu_script = Path(__file__).parent.parent / "Reflets_gpu" / "run_gpu_optim.py"
if not gpu_script.exists():
st.error(f"Script GPU introuvable: {gpu_script}")
else:
# Utiliser le meme Python que celui qui fait tourner Streamlit
python_exe = sys.executable
cmd = [
python_exe, str(gpu_script),
"--algo", gpu_algo,
"--metric", gpu_metric,
"--max_iter", str(gpu_iter),
"--window_size", str(gpu_window),
"--n_batch", str(gpu_batch),
]
import re
import plotly.graph_objects as pgo
st.markdown("---")
st.subheader("Optimisation GPU en cours...")
st.caption(" ".join(cmd))
progress_bar = st.progress(0)
progress_text = st.empty()
# Metriques header
col_m1, col_m2, col_m3, col_m4 = st.columns(4)
ph_init = col_m1.empty()
ph_current = col_m2.empty()
ph_best = col_m3.empty()
ph_temp = col_m4.empty()
# Graphe
chart_ph = st.empty()
# Logs
log_expander = st.expander("Logs", expanded=False)
log_area = log_expander.empty()
try:
env = {**os.environ, "KMP_DUPLICATE_LIB_OK": "TRUE", "PYTHONUNBUFFERED": "1"}
proc = subprocess.Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
text=True, bufsize=1, env=env,
)
lines = []
iterations = []
current_scores = []
best_scores = []
initial_score = None
# Pattern pour parser les logs SA:
# [SA] it= 10 T= 80.00 score=540.1 best=538.2 n_moves=3 accept=Y 12s
sa_pattern = re.compile(
r"\[SA\]\s+it=\s*(\d+)\s+T=\s*([\d.]+)\s+score=([\d.]+)\s+best=([\d.]+)"
)
for line in proc.stdout:
line = line.rstrip()
lines.append(line)
# Parser le score initial
if "Score allocation initiale:" in line:
try:
initial_score = float(line.split(":")[-1].strip())
ph_init.metric("Score initial", f"{initial_score:.1f}")
except ValueError:
pass
# Parser les iterations SA
m = sa_pattern.search(line)
if m:
it = int(m.group(1))
temp = float(m.group(2))
score = float(m.group(3))
best = float(m.group(4))
iterations.append(it)
current_scores.append(score)
best_scores.append(best)
# Progression
progress_bar.progress(min(it / gpu_iter, 1.0))
progress_text.text(f"Itération {it}/{gpu_iter}")
# Metriques
ph_current.metric("Score actuel", f"{score:.1f}")
if initial_score:
delta = best - initial_score
pct = 100 * delta / initial_score if initial_score else 0
ph_best.metric("Meilleur score", f"{best:.1f}",
delta=f"{delta:.1f} ({pct:.1f}%)", delta_color="inverse")
else:
ph_best.metric("Meilleur score", f"{best:.1f}")
ph_temp.metric("Température", f"{temp:.2f}")
# Graphe (pas trop souvent pour ne pas ralentir)
if len(iterations) % 5 == 0 or it <= 10:
fig = pgo.Figure()
fig.add_trace(pgo.Scatter(
x=iterations, y=current_scores,
mode="lines", name="Score actuel",
line=dict(color="lightblue", width=1), opacity=0.6,
))
fig.add_trace(pgo.Scatter(
x=iterations, y=best_scores,
mode="lines", name="Meilleur score",
line=dict(color="green", width=3),
))
if initial_score:
fig.add_hline(y=initial_score, line_dash="dash",
line_color="red", annotation_text="initial")
fig.update_layout(
xaxis_title="Itération", yaxis_title="Score",
height=300, margin=dict(l=50, r=20, t=30, b=40),
showlegend=True, legend=dict(x=0.02, y=0.98),
)
chart_ph.plotly_chart(fig, use_container_width=True)
# Logs
log_area.code("\n".join(lines[-20:]), language="text")
proc.wait()
progress_bar.progress(1.0)
progress_text.text("Terminé !")
if proc.returncode == 0:
st.success("Optimisation GPU terminée ! Résultats dans Reflets_gpu/results/")
else:
st.error(f"Erreur GPU (code {proc.returncode})")
st.code("\n".join(lines[-50:]), language="text")
except Exception as e:
st.error(f"Erreur lancement GPU: {e}")
def _filter_interventions_by_date(interventions):
sim_start = st.session_state.get("simulation_start")
sim_end = st.session_state.get("simulation_end")
if not sim_start and not sim_end:
return interventions
return [
i
for i in interventions
if (sim_start is None or i.date >= sim_start)
and (sim_end is None or i.date <= sim_end)
]
if run:
try:
interventions = get_interventions()
except Exception as e:
st.error(f"Impossible de charger les interventions: {e}")
interventions = []
if interventions:
interventions_valides = [
i for i in interventions if hasattr(i, "cstc") and i.cstc != "NR"
]
nb_filtrees = len(interventions) - len(interventions_valides)
if nb_filtrees > 0:
st.info(f"{nb_filtrees} interventions ignorées (CSTC non renseigné)")
interventions = interventions_valides
st.markdown("---")
st.subheader("Résultats optimisation")
try:
updated_engins, metrics = optimize_allocation_from_interface(
interventions=interventions,
secteurs=secteurs_initiaux,
current_engins_state=st.session_state.engins,
profile=selected_profile_key,
algorithm=algo_key,
)
st.session_state.engins = updated_engins
st.session_state.allocation_dirty = False
st.success(
f"Optimisation terminée ! Amélioration: {metrics['improvement']:.2f}% "
f"({metrics['total_iterations']} itérations)"
)
with st.expander("Détails", expanded=False):
st.json(
{
"profil": selected_profile_key,
"algorithme": algo_key,
"metrique_choisie": metric_key,
"metrics": metrics,
}
)
st.markdown("### Simulation avec la nouvelle allocation")
try:
main_module.interventions_simulees.clear()
except Exception:
pass
fct.reset_stats()
interventions_filtered = _filter_interventions_by_date(interventions)
progress_bar = st.progress(0)
progress_text = st.empty()
def update_progress(current, total):
progress = current / total if total else 1.0
progress_bar.progress(progress)
progress_text.text(
f"Simulation : {current}/{total} interventions ({progress*100:.1f}%)"
)
engins_dict = convert_session_engins_to_objects(secteurs_initiaux)
main_module.main(
progress_callback=update_progress,
interventions=interventions_filtered,
indisponibilites=st.session_state.indisponibilites_data,
engins=engins_dict,
)
progress_bar.progress(1.0)
progress_text.text("Simulation terminée !")
stats = fct.get_stats()
st.session_state.simulation_time = main_module.simulation_processing_time
st.session_state.cache_stats = stats
st.session_state.progress = 1
st.session_state.map_needs_update = True
reponses_s = [i.temps_reponse_seconds for i in main_module.interventions_simulees]
if reponses_s:
mean_reponse = float(sum(reponses_s) / len(reponses_s))
st.metric("Temps de réponse moyen (départ + trajet)", f"{mean_reponse:.1f} s")
st.caption(f"{len(reponses_s)} interventions simulées")
# Carte utilisation (pression 08h->08h) — provisoire après simulation
engaged = getattr(main_module, "engaged_seconds_by_cs_by_window", None) or {}
vehicle_counts = getattr(main_module, "vehicle_counts_by_cs", None) or {}
all_windows = sorted({w for cs_map in engaged.values() for w in cs_map.keys()})
if all_windows:
selected_w = all_windows[0]
ratio_by_cs, _hours_by_cs = summarise_utilisation_for_window(
engaged_seconds_by_cs_by_window=engaged,
window_start=selected_w,
vehicle_counts_by_cs=vehicle_counts,
)
st.markdown("---")
st.subheader("Carte d'utilisation des véhicules (pression 08h → 08h)")
st.caption(
f"Fenêtre affichée: {selected_w:%Y-%m-%d} 08:00 → {(selected_w + datetime.timedelta(days=1)):%Y-%m-%d} 08:00"
)
util_map = folium.Map(location=[48.8566, 2.3522], zoom_start=9)
ratios = [
v
for v in ratio_by_cs.values()
if v is not None and isinstance(v, (int, float)) and not np.isnan(v)
]
rmin = float(min(ratios)) if ratios else 0.0
rmax = float(max(ratios)) if ratios else 1.0
if rmax == rmin:
rmax = rmin + 1e-9
def style_util(f):
cs = f["properties"]["nom"]
v = ratio_by_cs.get(cs, float("nan"))
if v is None or (isinstance(v, float) and np.isnan(v)):
return {
"fillColor": "#999999",
"color": "black",
"weight": 1,
"fillOpacity": 0.25,
}
t = max(0.0, min(1.0, (float(v) - rmin) / (rmax - rmin)))
r = int(255 * t)
g = int(255 * (1 - t))
return {
"fillColor": f"#{r:02X}{g:02X}00",
"color": "black",
"weight": 1,
"fillOpacity": 0.6,
}
folium.GeoJson(
_gdf,
style_function=style_util,
tooltip=folium.GeoJsonTooltip(fields=["nom"], aliases=["Secteur"]),
).add_to(util_map)
st.components.v1.html(util_map._repr_html_(), width=700, height=500)
except Exception as e:
st.error(f"Erreur lors de l'optimisation: {e}")
import traceback
with st.expander("Détails de l'erreur"):
st.code(traceback.format_exc())
else:
st.warning("Aucune intervention à optimiser")