Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,7 +3,6 @@
|
|
| 3 |
#
|
| 4 |
# Guided scenarios (ACTIVE):
|
| 5 |
# A) Straight (single heading)
|
| 6 |
-
# A2) Straight + drift (per-leg ±deg, penalized)
|
| 7 |
#
|
| 8 |
# Key behavior:
|
| 9 |
# - Last radar spot FIXED: 18:22:00Z @ 6.578N, 96.341E
|
|
@@ -22,13 +21,11 @@
|
|
| 22 |
# - Turn speed is no longer fixed to the southern cruise speed.
|
| 23 |
# - The solver searches a plausible TURN speed range and chooses the best-scoring solution.
|
| 24 |
# - After 18:40, the southern trajectory continues “as per usual” using the selected speed profile.
|
| 25 |
-
# - Applies to BOTH scenarios: Straight and Straight + drift.
|
| 26 |
#
|
| 27 |
-
# NEW (
|
| 28 |
# 4) Two-stage guided search (COARSE → REFINE) to make generation much faster
|
| 29 |
# - Stage 1: coarse grid search to find the region of the best solution
|
| 30 |
# - Stage 2: refined grid around the best candidate only
|
| 31 |
-
# - Applies to BOTH scenarios: Straight and Straight + drift
|
| 32 |
|
| 33 |
import os
|
| 34 |
import re
|
|
@@ -133,23 +130,14 @@ STRAIGHT_STEP_DEG = 0.5 # refined step
|
|
| 133 |
# Southern schedule (kept)
|
| 134 |
GUIDED_LEG_SCHEDULE_MIN = [12.0, 60.0, 60.0, 60.0, 60.0, 90.0, 10.0]
|
| 135 |
|
| 136 |
-
#
|
| 137 |
-
DEFAULT_DRIFT_MAX_DEG = 2.0
|
| 138 |
-
DRIFT_STEP_DEG = 0.5 # refined step
|
| 139 |
-
DEFAULT_WEIGHT_DRIFT = 0.30
|
| 140 |
-
DEFAULT_DRIFT_ITERS = 2
|
| 141 |
-
DRIFT_SCALE_DEG = 1.0
|
| 142 |
-
|
| 143 |
-
# ===== NEW: Two-stage search knobs =====
|
| 144 |
# Stage 1 (coarse)
|
| 145 |
COARSE_HEADING_STEP_DEG = 2.0
|
| 146 |
COARSE_TURN_SPEED_STEP_KT = 10.0
|
| 147 |
-
COARSE_DRIFT_STEP_DEG = 1.0
|
| 148 |
|
| 149 |
# Stage 2 (refine around best)
|
| 150 |
REFINE_HEADING_HALF_WINDOW_DEG = 2.0 # +/- around best
|
| 151 |
REFINE_TURN_SPEED_HALF_WINDOW_KT = 10.0
|
| 152 |
-
REFINE_DRIFT_HALF_WINDOW_DEG = 1.0
|
| 153 |
|
| 154 |
# Search area colors (optional)
|
| 155 |
SEARCH_AREA_COLORS = {
|
|
@@ -1397,136 +1385,6 @@ def solve_guided_straight(
|
|
| 1397 |
return best_full_legs, report, start_dt
|
| 1398 |
|
| 1399 |
|
| 1400 |
-
def solve_guided_straight_with_drift(
|
| 1401 |
-
method, south_speed_knots, alt_ft, vs_fpm,
|
| 1402 |
-
descent_start_choice, weight_bfo, weight_bto,
|
| 1403 |
-
drift_max_deg, drift_weight
|
| 1404 |
-
):
|
| 1405 |
-
start_dt = _guided_start_dt()
|
| 1406 |
-
end_dt = _dt_from_label(GUIDED_FINAL_LABEL)
|
| 1407 |
-
|
| 1408 |
-
# descent start
|
| 1409 |
-
descent_start_dt = None
|
| 1410 |
-
if (descent_start_choice or "").startswith("After Ping Ring 6"):
|
| 1411 |
-
descent_start_dt = dt_ping6_utc()
|
| 1412 |
-
elif (descent_start_choice or "").startswith("After Ping Ring 7"):
|
| 1413 |
-
descent_start_dt = dt_ping7_utc()
|
| 1414 |
-
|
| 1415 |
-
# Precompute ping points once (windowed)
|
| 1416 |
-
pts_all = _ping_points_after(start_dt)
|
| 1417 |
-
pts_window = [(lbl, dt, bfo, bto) for (lbl, dt, bfo, bto) in pts_all if start_dt <= dt <= end_dt]
|
| 1418 |
-
if len(pts_window) < 2:
|
| 1419 |
-
return None, "Guided drift solve: not enough ping points to score.", None
|
| 1420 |
-
|
| 1421 |
-
drift_max_deg = float(drift_max_deg)
|
| 1422 |
-
|
| 1423 |
-
def try_candidate(h0_nominal: float, drift_deg: float, spd_turn: float):
|
| 1424 |
-
h_eff = _wrap_heading_deg(float(h0_nominal) + float(drift_deg))
|
| 1425 |
-
|
| 1426 |
-
southern_legs = make_legs_from_schedule(h_eff, float(south_speed_knots), GUIDED_LEG_SCHEDULE_MIN)
|
| 1427 |
-
preturn_legs, _turn_info = build_turn_legs(
|
| 1428 |
-
speed_knots=float(spd_turn),
|
| 1429 |
-
method=method,
|
| 1430 |
-
heading_final_deg=float(h_eff),
|
| 1431 |
-
)
|
| 1432 |
-
full_legs = preturn_legs + southern_legs
|
| 1433 |
-
full_legs = clip_legs_to_end_dt(full_legs, start_dt, end_dt)
|
| 1434 |
-
|
| 1435 |
-
s = score_legs_bfo_bto(
|
| 1436 |
-
RADAR_FIX_LAT, RADAR_FIX_LON, method, start_dt, end_dt, full_legs,
|
| 1437 |
-
alt_ft, vs_fpm, descent_start_dt,
|
| 1438 |
-
weight_bfo, weight_bto,
|
| 1439 |
-
pts_window=pts_window
|
| 1440 |
-
)
|
| 1441 |
-
if s is None:
|
| 1442 |
-
return None, None, None
|
| 1443 |
-
|
| 1444 |
-
combined_plus = float(s["combined"]) + float(drift_weight) * (abs(float(drift_deg)) / DRIFT_SCALE_DEG)
|
| 1445 |
-
return s, combined_plus, full_legs
|
| 1446 |
-
|
| 1447 |
-
# -------- Stage 1: COARSE search --------
|
| 1448 |
-
best_h0 = None
|
| 1449 |
-
best_d = None
|
| 1450 |
-
best_turn_spd = None
|
| 1451 |
-
best_full_legs = None
|
| 1452 |
-
best_score = None
|
| 1453 |
-
best_combined_plus = None
|
| 1454 |
-
|
| 1455 |
-
coarse_headings = _frange(GUIDED_HEADING_MIN, GUIDED_HEADING_MAX, COARSE_HEADING_STEP_DEG)
|
| 1456 |
-
|
| 1457 |
-
# Drift candidates (coarse)
|
| 1458 |
-
if drift_max_deg <= 1e-9:
|
| 1459 |
-
coarse_drifts = [0.0]
|
| 1460 |
-
else:
|
| 1461 |
-
coarse_drifts = list(np.arange(-drift_max_deg, drift_max_deg + 1e-9, COARSE_DRIFT_STEP_DEG))
|
| 1462 |
-
if 0.0 not in coarse_drifts:
|
| 1463 |
-
coarse_drifts.append(0.0)
|
| 1464 |
-
coarse_drifts = sorted({round(float(x), 6) for x in coarse_drifts})
|
| 1465 |
-
|
| 1466 |
-
coarse_turn_speeds = _turn_speed_candidates(float(south_speed_knots), COARSE_TURN_SPEED_STEP_KT)
|
| 1467 |
-
|
| 1468 |
-
for h0 in coarse_headings:
|
| 1469 |
-
for d in coarse_drifts:
|
| 1470 |
-
for spd_turn in coarse_turn_speeds:
|
| 1471 |
-
s, combined_plus, legs = try_candidate(h0, d, spd_turn)
|
| 1472 |
-
if s is None:
|
| 1473 |
-
continue
|
| 1474 |
-
if best_combined_plus is None or float(combined_plus) < float(best_combined_plus):
|
| 1475 |
-
best_score = dict(s)
|
| 1476 |
-
best_combined_plus = float(combined_plus)
|
| 1477 |
-
best_h0 = float(h0)
|
| 1478 |
-
best_d = float(d)
|
| 1479 |
-
best_turn_spd = float(spd_turn)
|
| 1480 |
-
best_full_legs = legs
|
| 1481 |
-
|
| 1482 |
-
if best_full_legs is None:
|
| 1483 |
-
return None, "Guided drift solve: not enough ping points to score.", None
|
| 1484 |
-
|
| 1485 |
-
# -------- Stage 2: REFINE around best --------
|
| 1486 |
-
refine_headings = _refine_range(best_h0, REFINE_HEADING_HALF_WINDOW_DEG, 1.0, GUIDED_HEADING_MIN, GUIDED_HEADING_MAX)
|
| 1487 |
-
|
| 1488 |
-
# Refine drift around best_d with DRIFT_STEP_DEG (0.5)
|
| 1489 |
-
d_lo = max(-drift_max_deg, best_d - REFINE_DRIFT_HALF_WINDOW_DEG)
|
| 1490 |
-
d_hi = min(+drift_max_deg, best_d + REFINE_DRIFT_HALF_WINDOW_DEG)
|
| 1491 |
-
refine_drifts = _frange(d_lo, d_hi, DRIFT_STEP_DEG)
|
| 1492 |
-
if not refine_drifts:
|
| 1493 |
-
refine_drifts = [best_d]
|
| 1494 |
-
|
| 1495 |
-
refine_turn_speeds = _refine_range(
|
| 1496 |
-
best_turn_spd, REFINE_TURN_SPEED_HALF_WINDOW_KT, TURN_SPEED_STEP_KT,
|
| 1497 |
-
max(TURN_SPEED_MIN_ABS_KT, float(south_speed_knots) - TURN_SPEED_SEARCH_DELTA_KT),
|
| 1498 |
-
min(TURN_SPEED_MAX_ABS_KT, float(south_speed_knots) + TURN_SPEED_SEARCH_DELTA_KT),
|
| 1499 |
-
)
|
| 1500 |
-
|
| 1501 |
-
for h0 in refine_headings:
|
| 1502 |
-
for d in refine_drifts:
|
| 1503 |
-
for spd_turn in refine_turn_speeds:
|
| 1504 |
-
s, combined_plus, legs = try_candidate(h0, d, spd_turn)
|
| 1505 |
-
if s is None:
|
| 1506 |
-
continue
|
| 1507 |
-
if best_combined_plus is None or float(combined_plus) < float(best_combined_plus):
|
| 1508 |
-
best_score = dict(s)
|
| 1509 |
-
best_combined_plus = float(combined_plus)
|
| 1510 |
-
best_h0 = float(h0)
|
| 1511 |
-
best_d = float(d)
|
| 1512 |
-
best_turn_spd = float(spd_turn)
|
| 1513 |
-
best_full_legs = legs
|
| 1514 |
-
|
| 1515 |
-
report = "\n".join([
|
| 1516 |
-
"**Guided mode: Straight + drift + computed turn (18:22→18:40), clipped to 00:20**",
|
| 1517 |
-
f"- Search mode: **two-stage (coarse → refine)**",
|
| 1518 |
-
f"- Southern speed (speed profile): **{float(south_speed_knots):.1f} kt**",
|
| 1519 |
-
f"- Best turn speed (18:22→18:40): **{float(best_turn_spd):.1f} kt**",
|
| 1520 |
-
f"- Best nominal heading: **{best_h0:.1f}°**",
|
| 1521 |
-
f"- Drift used: **{best_d:+.1f}°** | Drift penalty weight: **{float(drift_weight):.2f}**",
|
| 1522 |
-
f"- BFO points: **{best_score['n_bfo']}** | RMSE_BFO: **{best_score['rmse_bfo']:.2f} Hz**",
|
| 1523 |
-
f"- BTO points: **{best_score['n_bto']}** | RMSE_BTO: **{best_score['rmse_bto']:.1f} µs**" if best_score.get("rmse_bto") is not None else
|
| 1524 |
-
f"- BTO points: **{best_score['n_bto']}** | RMSE_BTO: n/a",
|
| 1525 |
-
])
|
| 1526 |
-
|
| 1527 |
-
return best_full_legs, report, start_dt
|
| 1528 |
-
|
| 1529 |
-
|
| 1530 |
# ================= RUNNERS =================
|
| 1531 |
def _run_common(start_lat, start_lon, legs, method, start_dt, alt_ft, vs_fpm, descent_start_choice):
|
| 1532 |
# In guided mode, ensure the polyline ends at 00:20 by clipping legs to end_dt first.
|
|
@@ -1570,26 +1428,17 @@ def run_sim_manual(start_lat, start_lon, legs_df, method, start_time_str, alt_ft
|
|
| 1570 |
|
| 1571 |
def run_guided(
|
| 1572 |
start_lat, start_lon, method, alt_ft, vs_fpm, descent_start_choice,
|
| 1573 |
-
speed_profile, custom_mach, custom_knots,
|
| 1574 |
-
weight_bfo, weight_bto
|
| 1575 |
-
drift_max_deg, drift_weight
|
| 1576 |
):
|
| 1577 |
# This is the SOUTHERN cruise speed (post-18:40). Turn speed is searched independently.
|
| 1578 |
south_spd_knots = speed_from_profile(speed_profile, alt_ft, custom_mach, custom_knots)
|
| 1579 |
|
| 1580 |
-
|
| 1581 |
-
|
| 1582 |
-
|
| 1583 |
-
|
| 1584 |
-
|
| 1585 |
-
drift_max_deg, drift_weight
|
| 1586 |
-
)
|
| 1587 |
-
else:
|
| 1588 |
-
legs, report_md, start_dt = solve_guided_straight(
|
| 1589 |
-
method, south_spd_knots,
|
| 1590 |
-
alt_ft, vs_fpm,
|
| 1591 |
-
descent_start_choice, weight_bfo, weight_bto
|
| 1592 |
-
)
|
| 1593 |
|
| 1594 |
if legs is None:
|
| 1595 |
fig = go.Figure()
|
|
@@ -1657,14 +1506,6 @@ with gr.Blocks() as demo:
|
|
| 1657 |
|
| 1658 |
gr.Markdown("### Guided mode (Recommended)")
|
| 1659 |
with gr.Row():
|
| 1660 |
-
guided_scenario = gr.Dropdown(
|
| 1661 |
-
[
|
| 1662 |
-
"Straight (single heading)",
|
| 1663 |
-
"Straight + drift (per-leg ±deg, penalized)",
|
| 1664 |
-
],
|
| 1665 |
-
value="Straight (single heading)",
|
| 1666 |
-
label="Scenario",
|
| 1667 |
-
)
|
| 1668 |
speed_profile = gr.Dropdown(
|
| 1669 |
["MRC Mach 0.82", "LRC Mach 0.84", "ECON Mach 0.80", "Custom Mach", "Custom knots"],
|
| 1670 |
value="MRC Mach 0.82",
|
|
@@ -1673,11 +1514,6 @@ with gr.Blocks() as demo:
|
|
| 1673 |
custom_mach = gr.Number(value=0.82, label="Custom Mach (if selected)")
|
| 1674 |
custom_knots = gr.Number(value=470, label="Custom knots (if selected)")
|
| 1675 |
|
| 1676 |
-
gr.Markdown("### Heading drift settings (for “Straight + drift” scenario)")
|
| 1677 |
-
with gr.Row():
|
| 1678 |
-
drift_max_deg = gr.Slider(0.0, 5.0, value=DEFAULT_DRIFT_MAX_DEG, step=0.1, label="Max drift per leg (±deg)")
|
| 1679 |
-
drift_weight = gr.Slider(0.0, 2.0, value=DEFAULT_WEIGHT_DRIFT, step=0.05, label="Drift penalty weight (higher = less drift)")
|
| 1680 |
-
|
| 1681 |
gr.Markdown("### Guided scoring weights")
|
| 1682 |
with gr.Row():
|
| 1683 |
weight_bfo = gr.Slider(0.0, 5.0, value=DEFAULT_WEIGHT_BFO, step=0.1, label="Weight: BFO")
|
|
@@ -1731,9 +1567,8 @@ with gr.Blocks() as demo:
|
|
| 1731 |
run_guided,
|
| 1732 |
inputs=[
|
| 1733 |
start_lat, start_lon, method, alt_ft, vs_fpm, descent_start_choice,
|
| 1734 |
-
speed_profile, custom_mach, custom_knots,
|
| 1735 |
-
weight_bfo, weight_bto
|
| 1736 |
-
drift_max_deg, drift_weight
|
| 1737 |
],
|
| 1738 |
outputs=[legs_df, status_md, out_map, stats, bfo_plot, bfo_score, alt_plot, validation_df],
|
| 1739 |
)
|
|
|
|
| 3 |
#
|
| 4 |
# Guided scenarios (ACTIVE):
|
| 5 |
# A) Straight (single heading)
|
|
|
|
| 6 |
#
|
| 7 |
# Key behavior:
|
| 8 |
# - Last radar spot FIXED: 18:22:00Z @ 6.578N, 96.341E
|
|
|
|
| 21 |
# - Turn speed is no longer fixed to the southern cruise speed.
|
| 22 |
# - The solver searches a plausible TURN speed range and chooses the best-scoring solution.
|
| 23 |
# - After 18:40, the southern trajectory continues “as per usual” using the selected speed profile.
|
|
|
|
| 24 |
#
|
| 25 |
+
# NEW (PREVIOUS UPDATE):
|
| 26 |
# 4) Two-stage guided search (COARSE → REFINE) to make generation much faster
|
| 27 |
# - Stage 1: coarse grid search to find the region of the best solution
|
| 28 |
# - Stage 2: refined grid around the best candidate only
|
|
|
|
| 29 |
|
| 30 |
import os
|
| 31 |
import re
|
|
|
|
| 130 |
# Southern schedule (kept)
|
| 131 |
GUIDED_LEG_SCHEDULE_MIN = [12.0, 60.0, 60.0, 60.0, 60.0, 90.0, 10.0]
|
| 132 |
|
| 133 |
+
# ===== Two-stage search knobs =====
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
# Stage 1 (coarse)
|
| 135 |
COARSE_HEADING_STEP_DEG = 2.0
|
| 136 |
COARSE_TURN_SPEED_STEP_KT = 10.0
|
|
|
|
| 137 |
|
| 138 |
# Stage 2 (refine around best)
|
| 139 |
REFINE_HEADING_HALF_WINDOW_DEG = 2.0 # +/- around best
|
| 140 |
REFINE_TURN_SPEED_HALF_WINDOW_KT = 10.0
|
|
|
|
| 141 |
|
| 142 |
# Search area colors (optional)
|
| 143 |
SEARCH_AREA_COLORS = {
|
|
|
|
| 1385 |
return best_full_legs, report, start_dt
|
| 1386 |
|
| 1387 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1388 |
# ================= RUNNERS =================
|
| 1389 |
def _run_common(start_lat, start_lon, legs, method, start_dt, alt_ft, vs_fpm, descent_start_choice):
|
| 1390 |
# In guided mode, ensure the polyline ends at 00:20 by clipping legs to end_dt first.
|
|
|
|
| 1428 |
|
| 1429 |
def run_guided(
|
| 1430 |
start_lat, start_lon, method, alt_ft, vs_fpm, descent_start_choice,
|
| 1431 |
+
speed_profile, custom_mach, custom_knots,
|
| 1432 |
+
weight_bfo, weight_bto
|
|
|
|
| 1433 |
):
|
| 1434 |
# This is the SOUTHERN cruise speed (post-18:40). Turn speed is searched independently.
|
| 1435 |
south_spd_knots = speed_from_profile(speed_profile, alt_ft, custom_mach, custom_knots)
|
| 1436 |
|
| 1437 |
+
legs, report_md, start_dt = solve_guided_straight(
|
| 1438 |
+
method, south_spd_knots,
|
| 1439 |
+
alt_ft, vs_fpm,
|
| 1440 |
+
descent_start_choice, weight_bfo, weight_bto
|
| 1441 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1442 |
|
| 1443 |
if legs is None:
|
| 1444 |
fig = go.Figure()
|
|
|
|
| 1506 |
|
| 1507 |
gr.Markdown("### Guided mode (Recommended)")
|
| 1508 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1509 |
speed_profile = gr.Dropdown(
|
| 1510 |
["MRC Mach 0.82", "LRC Mach 0.84", "ECON Mach 0.80", "Custom Mach", "Custom knots"],
|
| 1511 |
value="MRC Mach 0.82",
|
|
|
|
| 1514 |
custom_mach = gr.Number(value=0.82, label="Custom Mach (if selected)")
|
| 1515 |
custom_knots = gr.Number(value=470, label="Custom knots (if selected)")
|
| 1516 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1517 |
gr.Markdown("### Guided scoring weights")
|
| 1518 |
with gr.Row():
|
| 1519 |
weight_bfo = gr.Slider(0.0, 5.0, value=DEFAULT_WEIGHT_BFO, step=0.1, label="Weight: BFO")
|
|
|
|
| 1567 |
run_guided,
|
| 1568 |
inputs=[
|
| 1569 |
start_lat, start_lon, method, alt_ft, vs_fpm, descent_start_choice,
|
| 1570 |
+
speed_profile, custom_mach, custom_knots,
|
| 1571 |
+
weight_bfo, weight_bto
|
|
|
|
| 1572 |
],
|
| 1573 |
outputs=[legs_df, status_md, out_map, stats, bfo_plot, bfo_score, alt_plot, validation_df],
|
| 1574 |
)
|