Spaces:
Sleeping
Sleeping
File size: 7,159 Bytes
43bbf9a 149cb7a 43bbf9a d6b41a8 149cb7a 43bbf9a 149cb7a 43bbf9a d6b41a8 149cb7a 43bbf9a 149cb7a 43bbf9a 149cb7a 43bbf9a 149cb7a 43bbf9a 149cb7a | 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 | """Deterministic baseline planner used by inference and tests."""
from __future__ import annotations
from collections import defaultdict
from typing import Dict, List, Tuple
try:
from .engine import SimulationOutcome, simulate_plan
from .models import (
ATCOptimizationObservation,
FlightRecord,
PriorityClass,
RunwaySpec,
SlotAssignment,
)
from .constants import SEPARATION_BY_WAKE
from .tasks import task_catalog
except ImportError:
from engine import SimulationOutcome, simulate_plan
from models import (
ATCOptimizationObservation,
FlightRecord,
PriorityClass,
RunwaySpec,
SlotAssignment,
)
from constants import SEPARATION_BY_WAKE
from tasks import task_catalog
PRIORITY_RANK = {
PriorityClass.EMERGENCY: 0,
PriorityClass.MEDICAL: 1,
PriorityClass.CONNECTION: 2,
PriorityClass.NORMAL: 3,
}
def _capacity_spacing(runway: RunwaySpec) -> int:
base_gap = max(2, round(60 / runway.hourly_capacity))
return max(2, round(base_gap * runway.weather_penalty))
def _flight_sort_key(flight: FlightRecord) -> Tuple[int, int, int, float, int]:
return (
PRIORITY_RANK[flight.priority],
flight.scheduled_minute,
0 if flight.operation.value == "arrival" else 1,
-flight.connection_risk,
-flight.passengers,
)
def _outcome_key(outcome: SimulationOutcome) -> Tuple[float, float, float, float, float, float]:
metrics = outcome.metrics
return (
metrics.overall_score,
metrics.priority_handling,
metrics.conflict_free_ratio,
metrics.fairness,
metrics.delay_efficiency,
-float(metrics.total_delay_minutes),
)
def build_heuristic_plan(observation: ATCOptimizationObservation) -> List[SlotAssignment]:
"""Create a safe, deterministic seed schedule."""
runway_lookup = {runway.runway_id: runway for runway in observation.runways}
runway_state: Dict[str, Tuple[int, str]] = {
runway.runway_id: (-999, "M") for runway in observation.runways
}
airline_delay_totals: Dict[str, List[int]] = defaultdict(list)
assignments: List[SlotAssignment] = []
for flight in sorted(observation.flights, key=_flight_sort_key):
best_choice: Tuple[float, str, int] | None = None
for runway_id in flight.allowed_runways:
runway = runway_lookup[runway_id]
last_minute, last_wake = runway_state[runway_id]
gap = max(
_capacity_spacing(runway),
SEPARATION_BY_WAKE[(last_wake, flight.wake_class.value)],
)
earliest_safe = max(flight.earliest_minute, last_minute + gap, flight.scheduled_minute)
candidate_time = min(max(earliest_safe, flight.earliest_minute), flight.latest_minute)
delay = abs(candidate_time - flight.scheduled_minute)
airline_avg_delay = (
sum(airline_delay_totals[flight.airline]) / len(airline_delay_totals[flight.airline])
if airline_delay_totals[flight.airline]
else 0.0
)
objective = (
delay
+ 6 * PRIORITY_RANK[flight.priority]
+ 12 * flight.connection_risk
+ 0.25 * airline_avg_delay
+ (3 if runway_id.endswith("L") and flight.priority == PriorityClass.NORMAL else 0)
)
candidate = (objective, runway_id, candidate_time)
if best_choice is None or candidate < best_choice:
best_choice = candidate
assert best_choice is not None
_, chosen_runway, chosen_time = best_choice
assignments.append(
SlotAssignment(
flight_id=flight.flight_id,
runway=chosen_runway,
assigned_minute=chosen_time,
hold_minutes=max(0, chosen_time - flight.scheduled_minute),
)
)
airline_delay_totals[flight.airline].append(abs(chosen_time - flight.scheduled_minute))
runway_state[chosen_runway] = (chosen_time, flight.wake_class.value)
assignments.sort(key=lambda item: item.assigned_minute)
return assignments
def build_refined_plan(
observation: ATCOptimizationObservation,
seed_plan: List[SlotAssignment] | None = None,
max_passes: int = 2,
) -> List[SlotAssignment]:
"""Greedily improve the seed plan using the deterministic simulator."""
task = task_catalog().get(observation.task_id)
if task is None:
return seed_plan or build_heuristic_plan(observation)
ordered_flights = sorted(observation.flights, key=_flight_sort_key)
ordered_ids = [flight.flight_id for flight in ordered_flights]
current_plan = list(seed_plan) if seed_plan is not None else build_heuristic_plan(observation)
assignment_map = {assignment.flight_id: assignment for assignment in current_plan}
for flight in ordered_flights:
assignment_map.setdefault(
flight.flight_id,
SlotAssignment(
flight_id=flight.flight_id,
runway=flight.allowed_runways[0],
assigned_minute=flight.scheduled_minute,
hold_minutes=0,
),
)
best_plan = [assignment_map[flight_id] for flight_id in ordered_ids]
best_outcome = simulate_plan(task, best_plan)
for _ in range(max_passes):
improved = False
for flight in ordered_flights:
current_assignment = assignment_map[flight.flight_id]
best_local_assignment = current_assignment
for runway_id in flight.allowed_runways:
for minute in range(flight.earliest_minute, flight.latest_minute + 1):
if (
runway_id == current_assignment.runway
and minute == current_assignment.assigned_minute
):
continue
candidate_assignment = SlotAssignment(
flight_id=flight.flight_id,
runway=runway_id,
assigned_minute=minute,
hold_minutes=max(0, minute - flight.scheduled_minute),
)
assignment_map[flight.flight_id] = candidate_assignment
candidate_plan = [assignment_map[item_id] for item_id in ordered_ids]
candidate_outcome = simulate_plan(task, candidate_plan)
if _outcome_key(candidate_outcome) > _outcome_key(best_outcome):
best_outcome = candidate_outcome
best_local_assignment = candidate_assignment
improved = True
assignment_map[flight.flight_id] = best_local_assignment
assignment_map[flight.flight_id] = best_local_assignment
best_plan = [assignment_map[item_id] for item_id in ordered_ids]
if not improved:
break
best_plan.sort(key=lambda item: (item.assigned_minute, item.flight_id))
return best_plan
|