diff --git a/Dockerfile.occ3 b/Dockerfile.occ3 index 698b41c..3c50e04 100644 --- a/Dockerfile.occ3 +++ b/Dockerfile.occ3 @@ -1,15 +1,18 @@ -# The AirOps ops control center with passengers env: the airops-occ3 MCP server with the b6-v3 pack, on port 18765. +# The AirOps ops control center with passengers env: the airops-occ3 MCP server with the b6-v3 pack and its realistic +# days (b6-v3r), on port 18765. # Build: agent-env airops-occ3 setup (docker build -f Dockerfile.occ3 -t agentenv-airops-occ3-env .) FROM python:3.12-slim LABEL org.opencontainers.image.source="https://github.com/earakely-scale/agentenv-airops-plugin" \ org.opencontainers.image.licenses="Apache-2.0" \ org.opencontainers.image.description="AirOpsEnv's ops control center with passengers as an AgentEnv environment. Built from public U.S. records: BTS on-time and T-100 data, the DB1B and DB1C passenger surveys, the FAA registry and FAA Command Center advisories." -ENV PYTHONUNBUFFERED=1 PIP_NO_CACHE_DIR=1 PIP_DISABLE_PIP_VERSION_CHECK=1 AIROPS_OCC3_PACK=/app/data/b6-v3 +ENV PYTHONUNBUFFERED=1 PIP_NO_CACHE_DIR=1 PIP_DISABLE_PIP_VERSION_CHECK=1 AIROPS_OCC3_PACK=/app/data/b6-v3 \ + AIROPS_OCC3R_PACK=/app/data/b6-v3r RUN pip install "agentenv-framework-protocol==0.1.338" "fastmcp==3.4.8" "mcp==1.30.0" "pydantic==2.13.5" "tzdata" \ "httpx==0.28.1" "numpy==2.5.4" "pandas==3.0.6" "highspy==1.15.1" "airportsdata==20260905" WORKDIR /app COPY LICENSE NOTICE /app/ COPY data/b6-v3 /app/data/b6-v3 +COPY data/b6-v3r /app/data/b6-v3r COPY src/agentenv_airops /app/agentenv_airops RUN useradd --create-home --uid 1000 airops USER airops diff --git a/Dockerfile.occ3.dockerignore b/Dockerfile.occ3.dockerignore index b7a02ba..7b47d84 100644 --- a/Dockerfile.occ3.dockerignore +++ b/Dockerfile.occ3.dockerignore @@ -2,6 +2,7 @@ !LICENSE !NOTICE !data/b6-v3 +!data/b6-v3r !src/agentenv_airops src/agentenv_airops/data src/agentenv_airops/web diff --git a/README.md b/README.md index 4e599ca..898078b 100644 --- a/README.md +++ b/README.md @@ -332,6 +332,15 @@ agent-env hf run earakely-scale/AirOpsEnv-AgentEnv@v0.5.0 --bundle airops-occ3 - --model openai/gpt-6.1-sol ``` +Realistic mode plays the same days in a world closer to the real one (v4's caps and closures, gates, aircraft that +break, swaps only at the hubs, tomorrow's first departures), graded on JetBlue's own cost with JetBlue's own decisions +replayed as 0.5; [REALISTIC.md](REALISTIC.md) has the world, the cost and the references: + +```bash +agent-env airops-occ3 tasks --realistic --split all # results/bundles/airops-occ3-real: b6-v3r's 22 days +agent-env run results/bundles/airops-occ3-real --task b6-20230627-0400-joint-real --model openai/gpt-6.1-sol +``` + A pilot plays runs in a fixed order under a budget, retries a run that failed for an infrastructure reason at most twice, and reports every scored run replayed and audited: diff --git a/REALISTIC.md b/REALISTIC.md new file mode 100644 index 0000000..672c51e --- /dev/null +++ b/REALISTIC.md @@ -0,0 +1,146 @@ +# Realistic mode (airops-occ3, `realistic`) + +A second way to play an occ3 day, beside the current mode, which it leaves unchanged. The day runs in a world closer to +the real one. It is graded on JetBlue's own cost, with JetBlue's own decisions that day as the 0.5 mark. + +## The world (`realistic.py`, `sim.World`) + +- **Every station's hourly room** (`realistic.capacity`), from every carrier's BTS pushbacks and landings: + - **Short hours.** In an hour when all carriers together managed under 90% of their schedule (v4's rule), JetBlue + gets its share of what they managed. This applies to departures everywhere and to arrivals at outstations. + - **Congested airports.** At JFK, LGA, DCA (slot-controlled), EWR (schedule-facilitated) and BOS (JetBlue's hub), + every other hour has a capacity too: the most the airport handled in that hour on its fair days. JetBlue may use + whatever the other carriers left of it. So a flight pushed out of a short hour finds the next hour as full as it + was, not empty. + - **Elsewhere,** fair days measure demand rather than capacity, so only short hours are capped. + - **Floors.** A cap is never below what JetBlue's flights did in the hour, nor, outside short hours, below its own + schedule. + - **Hub arrivals** answer to the FAA programs and the hub's nominal rate, as in v3. +- **Program slots JetBlue actually used** (`realistic.recorded_slots`). Each ground delay program's JetBlue slots also + hold the arrival time of each controlled flight JetBlue pushed back during the program (its pushback plus scheduled + block), so a replay of JetBlue's day can take the slots JetBlue got. Truth only; the desks see the programs as issued. +- **Ramp closures** at the hubs come from their METARs and SPECIs (v4). +- **Gates.** A station holds at most as many JetBlue aircraft at once as the pack's 70 recorded days ever had there. A + flight into a full station waits at its origin. +- **Breakdowns.** Each leg's aircraft breaks with JetBlue's fair-weather code-A cancellation rate (BTS), seeded by the + date, an hour before its departure, and is out for the rest of the day. +- **Swaps only at JFK and BOS.** At an outstation a flight takes the aircraft that flew in on JetBlue's own day, while + it is there. +- **Tomorrow.** The end-of-day aircraft must cover the next day's first departures (BTS, d + 1). + +## The cost J (`realistic.airline`, `realistic.jetblue`) + +J is in passenger-minutes; the desks see it in dollars, at $1.07 per passenger-minute. + +- **Aircraft:** FAA's rates (crew and fixed costs per minute of departure delay, the whole cost per minute over block), + less SAVE of the fuel and maintenance a cancelled flight saves. +- **Crews:** deadheads, hotels away from base and the end of day. +- **Tomorrow:** the ferries plus the aircraft's cost for each minute a first departure leaves late. +- **Revenue lost:** each refunded passenger's fare, and TAKE of each stranded passenger's fare. A passenger rebooked + today costs nothing. +- **Fares:** each itinerary's fare by distance, from a line through the DOT Consumer Airfare Report (Table 1, B6), + CPI-adjusted. +- **TAKE and SAVE** are fitted on the 13 stream days to JetBlue's Q1 2014 8-K (4,100 cancellations, $50M of revenue, + $35M of operating income): + - on JetBlue's replay, it loses 0.676 of the fares booked on its cancelled flights (TAKE = 0.728); + - each cancellation saves 30% of the revenue it loses (SAVE = 0.445). + +## The reward (`realistic.reward`) + +`0.2 + 0.8 / (1 + 5/3 x^2)`, with `x = (J - top) / (anchor - top)`: + +- **anchor:** J of JetBlue's replay (`jbfull@360`: its cancellations decided 6 h ahead, its tails, its GDP slot + claims). It scores 0.5. +- **top:** the cheapest J of any plan priced, in practice the best plan found under J (`scripts/realistic_best_j.py`, + 15 min a day). It scores 1.0. + +Cost P's figures stay in the grade under `P`. + +## What the desks know at watch t (`realistic.known`) + +- **From sim.known** (DESIGN §4): + - a flight's own delay and block 2 h ahead; + - FAA programs as issued; + - closures once begun. +- **Each station's hourly room as it stands in t's hour,** carried over the next 4 hours: a persistence forecast, + `realworld.persisted`. The desks never see the room those hours turn out to have. +- **The tails broken by t.** A breakdown is unknown until it happens. +- **Gates and the swaps rule**, which are rules. + +An audit found no page or tool that runs the simulation on the truth after t. Left as they are, all small and shared +with the current mode: + +- the standard crew desk reads flights' true early-departure lb (at most 15 min) before the switch to the day as + known; +- crew alerts include minute t's truth messages (`<=` t); +- whether the day ends looks one watch ahead on the truth; +- the customer desk's free seats use each flight's recorded tail; +- GDP exemptions use the ground stops as they ran. + +## Validation + +Run with `scripts/realistic_report.py` (fidelity, J) and `scripts/realistic_peers.py` (other carriers). + +1. **Same actions, same outcomes.** JetBlue's own actions are replayed: its cancellations decided 6 h ahead, its + tails and its GDP slot claims. The replay is compared with JetBlue's recorded day, flight by flight, on the flights + JetBlue flew, and by hour and by day. +2. **Is J JetBlue's money?** On the days of JetBlue's replay, the operating income it loses per cancellation (revenue + lost less the saving) is set against JetBlue's disclosed storms: + - Hurricane Irene, Q3 2011: about 1,400 cancellations, about $8M of operating income; + - the early January 2014 storms: about 1,800 cancellations, about $30M; + - Q1 2014: 4,100 cancellations, about $35M. + + The Q1 2014 figure is the one TAKE and SAVE are calibrated to. 21 of the 22 days fall within the three storms' + range ($6.0k to $16.7k per cancellation, 2014 dollars); 2025-02-06 is below it. +3. **Where JetBlue cancelled, did the airport have trouble?** For each JetBlue cancellation the standard desks fly + within an hour, we take the other carriers' cancellation share at that airport in that hour, from BTS. Where they + kept running, flying it is plausible: JetBlue's own hedge, crew or aircraft. Where they cancelled 20% or more, the + world is likely missing something, though they may have hedged too. + +## Files + +| What | Where | +|---|---| +| Pack: b6-v3's 22 priced days, layered, with breakdowns, tomorrow's firsts, fares and J's references | `data/b6-v3r`, built by `scripts/build_realistic_pack.py REALREP BESTJ` | +| The day | `occ/day3r.py` (`OccDay3R`), loaded with `realistic: true` (`occ/live3.py`) | +| The bundle | `tasks3.generate(..., realistic=True)`: tasks `--real`, rules `tasks3.RULES_R` | +| The replays and the best plans under J | `scripts/realistic_compare.py`, `scripts/realistic_best_j.py` | +| Validation | `scripts/realistic_report.py`, `scripts/realistic_peers.py` | +| Tests | `tests/occ/test_occ3r.py` | + +## Results on the 22 days (stream plans replayed, no new model play) + +| Plan | Not flown, stream / held-out | Reward, stream / held-out | +|---|---|---| +| JetBlue recorded | 888 / 528 | | +| JetBlue replay | 952 / 565 | 0.5 / 0.5 | +| Standard desks | 214 / 128 | 0.714 / 0.771 | +| Model (2026-10-23 stream plans) | 210 / - | 0.772 / - | +| Best plan under P | 382 / 198 | 0.794 / 0.850 | +| Best plan under J | 533 / 283 | 1.0 / 1.0 | + +JetBlue's replay flies what JetBlue flew in a world built from the record: on the flights both flew, per-flight delay +correlates 0.75 with the record on the stream days (0.67 held out), each hour's total 0.85 (0.80) and each day's total +0.97 (0.98), with the days' total delay within 1% (4%). Its count is JetBlue's cancellations plus the flights the world +could not fly by day's end. + +The best plan under J cancels a bit over half of what JetBlue did, and a mostly different set. Read it as a hindsight +bound, not a verdict on JetBlue. Its count depends heavily on SAVE: at SAVE = 1 it cancelled 1,426 on the stream days +of an earlier world. + +## Model days (GPT-6 Astra, 2026-10-29) + +Three desks, each capped at $23.50 as on the stream, through the real `agent-env run` on a gr6.4xlarge +(`live_runner.py --config scripts/live_runner_real.toml`, pilot `occ3-real-astra-final`). All three audits pass. + +| Day | Reward | Astra J | Cancelled | 60+ late | JetBlue replay J | JetBlue recorded: cancelled, 60+ late | Desks J | Best J | Model cost | +|---|---|---|---|---|---|---|---|---|---| +| 2024-08-03 | 0.810 | $2.48M | 40 | 175 | $3.25M | 99, 150 | $2.81M | $1.89M, 61 cancelled | $27.24 | +| 2023-06-27 | 0.599 | $5.04M | 72 | 319 | $5.62M | 179, 242 | $5.36M | $2.99M, 104 cancelled | $40.10 | +| 2025-01-19 | 0.940 | $1.71M | 21 | 102 | $3.28M | 157, 59 | $1.88M | $1.27M, 35 cancelled | $20.78 | + +Late flights are counted over the flights each plan flew. Astra cancels well under half of what JetBlue did and fewer +than the best plans found, so it lands more flights late. On JetBlue's cost it still beats JetBlue's decisions on every +day (by 10%, 24% and 48%, 24% over the three) and the standard desks. On 2025-01-19 JetBlue's decisions cost at least +two-thirds more than any other plan priced, so every plan that flies scores high. 2025-01-19 was replayed on an +m7i.4xlarge after the first box shut down before its run was copied off. diff --git a/data/b6-v3r/dates/2023-06-27.json.gz b/data/b6-v3r/dates/2023-06-27.json.gz new file mode 100644 index 0000000..5ef1b4d Binary files /dev/null and b/data/b6-v3r/dates/2023-06-27.json.gz differ diff --git a/data/b6-v3r/dates/2023-07-01.json.gz b/data/b6-v3r/dates/2023-07-01.json.gz new file mode 100644 index 0000000..fb615fa Binary files /dev/null and b/data/b6-v3r/dates/2023-07-01.json.gz differ diff --git a/data/b6-v3r/dates/2023-07-08.json.gz b/data/b6-v3r/dates/2023-07-08.json.gz new file mode 100644 index 0000000..d8f4311 Binary files /dev/null and b/data/b6-v3r/dates/2023-07-08.json.gz differ diff --git a/data/b6-v3r/dates/2023-07-09.json.gz b/data/b6-v3r/dates/2023-07-09.json.gz new file mode 100644 index 0000000..c347f14 Binary files /dev/null and b/data/b6-v3r/dates/2023-07-09.json.gz differ diff --git a/data/b6-v3r/dates/2023-07-13.json.gz b/data/b6-v3r/dates/2023-07-13.json.gz new file mode 100644 index 0000000..9919c29 Binary files /dev/null and b/data/b6-v3r/dates/2023-07-13.json.gz differ diff --git a/data/b6-v3r/dates/2023-07-29.json.gz b/data/b6-v3r/dates/2023-07-29.json.gz new file mode 100644 index 0000000..78ad4c7 Binary files /dev/null and b/data/b6-v3r/dates/2023-07-29.json.gz differ diff --git a/data/b6-v3r/dates/2023-08-17.json.gz b/data/b6-v3r/dates/2023-08-17.json.gz new file mode 100644 index 0000000..104c16d Binary files /dev/null and b/data/b6-v3r/dates/2023-08-17.json.gz differ diff --git a/data/b6-v3r/dates/2024-01-06.json.gz b/data/b6-v3r/dates/2024-01-06.json.gz new file mode 100644 index 0000000..a8f549e Binary files /dev/null and b/data/b6-v3r/dates/2024-01-06.json.gz differ diff --git a/data/b6-v3r/dates/2024-06-14.json.gz b/data/b6-v3r/dates/2024-06-14.json.gz new file mode 100644 index 0000000..11743c0 Binary files /dev/null and b/data/b6-v3r/dates/2024-06-14.json.gz differ diff --git a/data/b6-v3r/dates/2024-06-23.json.gz b/data/b6-v3r/dates/2024-06-23.json.gz new file mode 100644 index 0000000..a656d37 Binary files /dev/null and b/data/b6-v3r/dates/2024-06-23.json.gz differ diff --git a/data/b6-v3r/dates/2024-06-30.json.gz b/data/b6-v3r/dates/2024-06-30.json.gz new file mode 100644 index 0000000..171631a Binary files /dev/null and b/data/b6-v3r/dates/2024-06-30.json.gz differ diff --git a/data/b6-v3r/dates/2024-08-03.json.gz b/data/b6-v3r/dates/2024-08-03.json.gz new file mode 100644 index 0000000..1ee9e8a Binary files /dev/null and b/data/b6-v3r/dates/2024-08-03.json.gz differ diff --git a/data/b6-v3r/dates/2024-08-18.json.gz b/data/b6-v3r/dates/2024-08-18.json.gz new file mode 100644 index 0000000..ae57999 Binary files /dev/null and b/data/b6-v3r/dates/2024-08-18.json.gz differ diff --git a/data/b6-v3r/dates/2025-01-11.json.gz b/data/b6-v3r/dates/2025-01-11.json.gz new file mode 100644 index 0000000..2dae49e Binary files /dev/null and b/data/b6-v3r/dates/2025-01-11.json.gz differ diff --git a/data/b6-v3r/dates/2025-01-19.json.gz b/data/b6-v3r/dates/2025-01-19.json.gz new file mode 100644 index 0000000..2cee9fa Binary files /dev/null and b/data/b6-v3r/dates/2025-01-19.json.gz differ diff --git a/data/b6-v3r/dates/2025-02-06.json.gz b/data/b6-v3r/dates/2025-02-06.json.gz new file mode 100644 index 0000000..e3667f9 Binary files /dev/null and b/data/b6-v3r/dates/2025-02-06.json.gz differ diff --git a/data/b6-v3r/dates/2025-08-13.json.gz b/data/b6-v3r/dates/2025-08-13.json.gz new file mode 100644 index 0000000..e525394 Binary files /dev/null and b/data/b6-v3r/dates/2025-08-13.json.gz differ diff --git a/data/b6-v3r/dates/2026-07-01.json.gz b/data/b6-v3r/dates/2026-07-01.json.gz new file mode 100644 index 0000000..e3d3060 Binary files /dev/null and b/data/b6-v3r/dates/2026-07-01.json.gz differ diff --git a/data/b6-v3r/dates/2026-07-09.json.gz b/data/b6-v3r/dates/2026-07-09.json.gz new file mode 100644 index 0000000..b46c2ba Binary files /dev/null and b/data/b6-v3r/dates/2026-07-09.json.gz differ diff --git a/data/b6-v3r/dates/2026-07-15.json.gz b/data/b6-v3r/dates/2026-07-15.json.gz new file mode 100644 index 0000000..52e596d Binary files /dev/null and b/data/b6-v3r/dates/2026-07-15.json.gz differ diff --git a/data/b6-v3r/dates/2026-07-27.json.gz b/data/b6-v3r/dates/2026-07-27.json.gz new file mode 100644 index 0000000..e217537 Binary files /dev/null and b/data/b6-v3r/dates/2026-07-27.json.gz differ diff --git a/data/b6-v3r/dates/2026-07-29.json.gz b/data/b6-v3r/dates/2026-07-29.json.gz new file mode 100644 index 0000000..1cdd24e Binary files /dev/null and b/data/b6-v3r/dates/2026-07-29.json.gz differ diff --git a/data/b6-v3r/manifest.json b/data/b6-v3r/manifest.json new file mode 100644 index 0000000..45263d9 --- /dev/null +++ b/data/b6-v3r/manifest.json @@ -0,0 +1,261 @@ +{ + "pack": "b6-v3r", + "base": "b6-v3", + "base_manifest_sha256": "06cceb32582de8ee41759a862529eb85e27a25af92a32258b380e0ffd6516f44", + "assembled_at_commit": "3f7b91630055ae00dfda8deea92a549ceb88f308", + "realistic": { + "gates": { + "ABQ": 1, + "ACK": 5, + "ALB": 3, + "ATL": 7, + "AUS": 3, + "AVL": 1, + "BDL": 7, + "BNA": 4, + "BOS": 55, + "BQN": 5, + "BTV": 1, + "BUF": 6, + "BUR": 1, + "BWI": 2, + "BZN": 2, + "CHS": 4, + "CLE": 2, + "CLT": 2, + "DAB": 2, + "DCA": 11, + "DEN": 5, + "DFW": 2, + "DTW": 3, + "EWR": 13, + "EYW": 1, + "FLL": 51, + "HDN": 1, + "HPN": 9, + "HYA": 2, + "IAD": 2, + "IAH": 5, + "ILM": 1, + "ISP": 3, + "JAX": 4, + "JFK": 62, + "LAS": 7, + "LAX": 20, + "LGA": 17, + "MCI": 1, + "MCO": 27, + "MHT": 1, + "MIA": 4, + "MKE": 2, + "MSP": 2, + "MSY": 3, + "MVY": 4, + "ONT": 1, + "ORD": 5, + "ORF": 2, + "ORH": 3, + "PBI": 14, + "PDX": 2, + "PHL": 3, + "PHX": 4, + "PIT": 4, + "PQI": 1, + "PSE": 3, + "PSP": 1, + "PVD": 6, + "PWM": 2, + "RDU": 6, + "RIC": 4, + "RNO": 2, + "ROC": 2, + "RSW": 8, + "SAN": 4, + "SAT": 2, + "SAV": 2, + "SEA": 5, + "SFO": 10, + "SJC": 2, + "SJU": 20, + "SLC": 5, + "SMF": 2, + "SRQ": 3, + "STT": 2, + "STX": 1, + "SYR": 3, + "TLH": 1, + "TPA": 10, + "TVC": 1, + "VPS": 1, + "VRB": 2 + }, + "rate": 0.0042652724270776, + "swaps": [ + "BOS", + "JFK" + ], + "take": 0.7291231212543318, + "save": 0.4443964331182013, + "lost": 0.6758898166047423, + "kept": 0.7, + "usd_per_pax_min": 1.07 + }, + "tasks": { + "b6-20230627-0400": { + "date": "2023-06-27", + "split": "train", + "best_from": "rerun", + "best_from_p": "repair" + }, + "b6-20230701-0400": { + "date": "2023-07-01", + "split": "train", + "best_from": "rerun", + "best_from_p": "v2_best" + }, + "b6-20230708-0400": { + "date": "2023-07-08", + "split": "eval", + "best_from": "run", + "best_from_p": "repair" + }, + "b6-20230709-0400": { + "date": "2023-07-09", + "split": "eval", + "best_from": "run", + "best_from_p": "repair" + }, + "b6-20230713-0400": { + "date": "2023-07-13", + "split": "eval", + "best_from": "run", + "best_from_p": "v2_best" + }, + "b6-20230729-0400": { + "date": "2023-07-29", + "split": "eval", + "best_from": "run", + "best_from_p": "repair" + }, + "b6-20230817-0400": { + "date": "2023-08-17", + "split": "train", + "best_from": "run", + "best_from_p": "repair" + }, + "b6-20240106-0400": { + "date": "2024-01-06", + "split": "train", + "best_from": "run", + "best_from_p": "repair" + }, + "b6-20240614-0400": { + "date": "2024-06-14", + "split": "train", + "best_from": "run", + "best_from_p": "repair" + }, + "b6-20240623-0400": { + "date": "2024-06-23", + "split": "eval", + "best_from": "run", + "best_from_p": "B0" + }, + "b6-20240630-0400": { + "date": "2024-06-30", + "split": "train", + "best_from": "run", + "best_from_p": "v2_best" + }, + "b6-20240803-0400": { + "date": "2024-08-03", + "split": "train", + "best_from": "run", + "best_from_p": "repair" + }, + "b6-20240818-0400": { + "date": "2024-08-18", + "split": "train", + "best_from": "run", + "best_from_p": "repair" + }, + "b6-20250111-0400": { + "date": "2025-01-11", + "split": "eval", + "best_from": "run", + "best_from_p": "v2_best" + }, + "b6-20250119-0400": { + "date": "2025-01-19", + "split": "train", + "best_from": "run", + "best_from_p": "v2_best" + }, + "b6-20250206-0400": { + "date": "2025-02-06", + "split": "train", + "best_from": "run", + "best_from_p": "v2_best" + }, + "b6-20250813-0400": { + "date": "2025-08-13", + "split": "train", + "best_from": "run", + "best_from_p": "v2_best" + }, + "b6-20260701-0400": { + "date": "2026-07-01", + "split": "eval", + "best_from": "run", + "best_from_p": "v2_best" + }, + "b6-20260709-0400": { + "date": "2026-07-09", + "split": "eval", + "best_from": "run", + "best_from_p": "repair" + }, + "b6-20260715-0400": { + "date": "2026-07-15", + "split": "eval", + "best_from": "run", + "best_from_p": "v2_best" + }, + "b6-20260727-0400": { + "date": "2026-07-27", + "split": "train", + "best_from": "run", + "best_from_p": "repair" + }, + "b6-20260729-0400": { + "date": "2026-07-29", + "split": "train", + "best_from": "run", + "best_from_p": "repair" + } + }, + "files": { + "dates/2023-06-27.json.gz": "44b407975e12d77667e9a59dc81a820c0b6c976025f747099df9e152683ca6a4", + "dates/2023-07-01.json.gz": "57fbacc277ceb802824cbdcc0a81a5c9752cbb1e3856806f162c055f2b03d2cd", + "dates/2023-07-08.json.gz": "5617624e7bbde2e01c5b6dd50f97600b8a60f4651eaff6fb05d5b56885933078", + "dates/2023-07-09.json.gz": "5606f72114a9165c8c6a21cb5b3aaa2e5283ab8da260925a22dc2ad658d3ece1", + "dates/2023-07-13.json.gz": "7ebe6ff39accac6d6fd8cd39e88aa66967f0bac5b1c048cb4038b67e99eaac96", + "dates/2023-07-29.json.gz": "972b24b22f6768bc023190cbdcdedc481cf3b918372865b3c12ef085a07752c7", + "dates/2023-08-17.json.gz": "37b6eab5d0382373ed39a019a3f1162531147bd460f0fc0cbf11e564a46a6c90", + "dates/2024-01-06.json.gz": "33cb30bd1e49589e30877997c151f114259fccb51c84a08fe46cc438cc7c1b40", + "dates/2024-06-14.json.gz": "ad91c58a7341ba117235771e0313f5be35689b5096250b1444a8ebd2f5c628ce", + "dates/2024-06-23.json.gz": "e848b8a4763cd00a462308ebba5e2dedd41b6b0c1d94ebda4a85bbb7b63915fb", + "dates/2024-06-30.json.gz": "4c222520c40045402efe206577b41a2729122319fd4451291722e689b80da77f", + "dates/2024-08-03.json.gz": "9e9b74becbedb542e0e3c72ed815ade0c2ce1b37953e8335a7b4c75a3eb2e8f0", + "dates/2024-08-18.json.gz": "f2b91835da9ee04a5d05eff4fbda3f241aff700cf4ad953ed439c31f4e587270", + "dates/2025-01-11.json.gz": "e976d90c318d806293a09085b846b20cb4ff5e18e319564640e14e5dd7c9fbbd", + "dates/2025-01-19.json.gz": "db73391c0a7d3c2320f6780be9542ccd66ad77e0a7e6dc6c0ef8fafc0ad070e3", + "dates/2025-02-06.json.gz": "d31696b6715c2d38ea5ad9bfa73e4842b4adf61fe6b6d5639d90f8f227a90423", + "dates/2025-08-13.json.gz": "27b819ab2bc3d0897cfba7a54ac818efa847b32f546f90ec5eb1fd7a153481da", + "dates/2026-07-01.json.gz": "45510a472b4daac42e78ef98c8c84f8a6d6ba800a3b90b58bbe32a922899f424", + "dates/2026-07-09.json.gz": "ab4a34b81b753083f5f7d6c8b9d7a48470fe349297e9a10cc8c6d98a6ec1d1ed", + "dates/2026-07-15.json.gz": "363d4a1582dd6a66c1fcc739190270cd81262aaf60260c450229811005a2995e", + "dates/2026-07-27.json.gz": "e213cae38f882f014885d5a29b09c2c06977b9b69eccc599ad4bc162a957fe2f", + "dates/2026-07-29.json.gz": "87f6663863356db716958427d254124da7bd3195762b09a95d3d8a575f0c8e83" + } +} diff --git a/hub/dataset/README.md b/hub/dataset/README.md index b63732e..ba38d42 100644 --- a/hub/dataset/README.md +++ b/hub/dataset/README.md @@ -162,6 +162,50 @@ passengers have no later seat that day) did not beat two scripted desks: better 12 needed. Its requests not to cancel mostly protected flights that couldn't fly, which then cancelled at their deadline at late notice. The customer desk ships anyway, labelled. +## Realistic mode + +A second way to play a v3 day, on 22 of the pack's dates (`b6-v3r`): the 13 the model first streamed, as `train`, and +9 held out, as `eval`. The day runs in a world rebuilt from JetBlue's public record and is graded on JetBlue's own +money, with JetBlue's actual decisions that day as the 0.5 mark. + +- **The world.** Every station's hourly room from what all airlines actually pushed back and landed (BTS); at JFK, + LaGuardia, Newark, Washington National and Boston every hour is as full as the other airlines left it. Under each + ground delay program, the slots JetBlue actually used. Aircraft swap only at JFK and Boston, a station holds only as + many JetBlue aircraft as it ever has, aircraft break at JetBlue's fair-weather mechanical rate, and the day's last + aircraft must cover tomorrow's first departures. +- **What the desks know.** As in v3, plus each station's room as it stands now, assumed to hold for four hours, and a + breakdown when it happens. +- **The cost J** (the desks see dollars, $1.07 a passenger-minute): aircraft and crew time at FAA rates less the fuel + and maintenance a cancellation saves, crews away from base, tomorrow's late or ferried first departures, and the + fares lost to refunds and stranded passengers. Two shares are fitted to JetBlue's Q1 2014 8-K (4,100 storm + cancellations, $50M of revenue, $35M of operating income). +- **The reward** is 0.2 + 0.8 / (1 + 5/3 x²), x = (J − top) / (anchor − top): JetBlue's own decisions replayed (its + cancellations decided 6 hours ahead, its aircraft and its program slots) score 0.5, and the cheapest plan found with + hindsight scores 1.0. P's figures stay in the grade. + +**Is the world JetBlue's?** JetBlue's decisions replayed against its recorded day, on the flights it flew (the two cost +shares were fitted on the `train` dates): + +| | `train` (13 dates) | `eval` (9) | +|---|---|---| +| Each day's total delay, correlation | 0.97 | 0.98 | +| Total delay vs the record | +1% | +4% | +| Hour-by-hour average delay, correlation | 0.85 | 0.80 | +| Each flight's arrival delay, correlation (R²) | 0.75 (0.56) | 0.67 (0.45) | +| Flights 60+ min late vs the record | −6% | −7% | +| Not flown, the replay vs JetBlue's cancellations | 952 vs 888 | 565 vs 528 | + +The operating income JetBlue's replay loses per cancellation (median $8.5k in 2014 dollars) falls inside the range of +the storms JetBlue has disclosed ($6.0k to $16.7k) on 21 of the 22 dates. The world, the cost and the checks are in +[REALISTIC.md](https://github.com/earakely-scale/agentenv-airops-plugin/blob/v0.5.0/REALISTIC.md). + + + + + +Late flights are counted over the flights each plan flew. Every run above passes the audit and replays exactly on the +realistic world. + ## Watch a run Every run on the v3 board replays in the [replay Space](https://huggingface.co/spaces/earakely-scale/AirOpsEnv-AgentEnv), @@ -187,12 +231,15 @@ All parquet, one config per table, a split per split of the pack (`eval`, `contr | `v3_references` | one row per date: every candidate the best plan was chosen from with its P and reward, where the best plan came from, the bound, the gap, v2's best plan over the best, and the best plan itself | | `v3_episodes` | one row per recorded attempt: reward, reward_L and the costs against the best plan, the bound and B0, the day's cost by kind (lodging and refunded fares among them), cancellations, holds, crew moves and requests by type and outcome, spend, tokens, turns, each desk's end, the audit, its decisions and the request log; `model` names the model, `pilot` the run's folder, and `on_board` and `superseded_by` mark the board's runs | | `v3_desks` | one row per desk of each attempt, the customer desk's included, with its usage and its transcript as chat `messages` | +| `v3r_tasks` | realistic mode, one row per date: its flights, the flights JetBlue's recorded day didn't fly and those it landed 60+ min late (`recorded_*`), J of JetBlue's decisions replayed (the anchor), of the standard desks and of the best plans under P and J with their rewards and how many flights each left unflown, every plan priced, the breakdowns, the best plan under J, and the date's world (`world`) | +| `v3r_episodes`, `v3r_desks` | realistic mode's recorded attempts, as `v3_episodes` and `v3_desks`, with J and its parts in dollars, the anchor and the top, the flights left unflown and those 60+ min late, and P beside them | | `v2_tasks`, `v2_board`, `v2_runs`, `v2_desks` | AirOpsEnv v2, as published in v0.3.0 (below) | | `v1_tasks`, `results`, `v1_references`, `v1_episodes` | AirOpsEnv v1, as published in v0.2.0 (below) | And agentenv-hf's for v1's bundle `airops`, exactly as `agent-env hf publish` writes them: `airops_tasks` and `airops_episodes`; `raw/airops.jsonl` holds each v1 run's record and native trajectory. `bundles/airops/`, -`bundles/airops-occ/` and `bundles/airops-occ3/` are the three bundles `agent-env hf run` plays, v1's the default. +`bundles/airops-occ/`, `bundles/airops-occ3/` and `bundles/airops-occ3-real/` are the four bundles `agent-env hf run` +plays, v1's the default. `results/eval-v3.json`, `results/eval-v2.json` and `results/eval-v1.json` are the eval cards, and `runs/` holds the run folders as recorded: each control-center attempt's day record (`days/`) and each desk's transcript (`transcripts/`), @@ -230,6 +277,13 @@ agent-env hf run earakely-scale/AirOpsEnv-AgentEnv@v0.5.0 --bundle airops-occ3 - --model openai/gpt-6.1-sol ``` +Realistic mode is the bundle `airops-occ3-real`, every b6-v3r date in both modes as `--real`: + +```bash +agent-env hf run earakely-scale/AirOpsEnv-AgentEnv@v0.5.0 --bundle airops-occ3-real --task b6-20230627-0400-joint-real \ + --model openai/gpt-6-astra +``` + The bundle `airops-occ3` (`bundles/airops-occ3/`) holds every date of the pack in both modes, `-joint` and `-one`, the very task files Sol's board ran (each desk capped at $3 in a three-desk run, $6 for one agent; the other models played the same dates at the caps the board above gives), and `wiring-noplay`, which runs the first eval diff --git a/scripts/build_realistic_pack.py b/scripts/build_realistic_pack.py new file mode 100644 index 0000000..6871060 --- /dev/null +++ b/scripts/build_realistic_pack.py @@ -0,0 +1,93 @@ +"""The realistic pack (b6-v3r) from b6-v3, the realistic inputs cache and the realism report's runs: the days the +report priced, each with its layered task (v4's caps and closures), the tails that break and when, tomorrow's first +departures, each passenger group's fare and the references of JetBlue's cost J; the manifest carries the gates, the +breakdown rate and J's fitted shares, so the env reads no cache. + +J's references per day: JetBlue's replay (jbfull@360, the anchor, 0.5), the best plan found under J (bestj, its plan +kept) and the cheapest of every plan the report priced (the top, 1.0). TAKE and SAVE are fitted on the stream days, as +the report fits them. + +Usage: .venv/bin/python scripts/build_realistic_pack.py REALREP_DIR BESTJ_DIR [--out data/b6-v3r] +""" + +import gzip +import hashlib +import json +import subprocess +import sys +from pathlib import Path + +from agentenv_airops import realistic, realworld +from agentenv_airops.occ.pack import Pack3 + +ROOT = Path(__file__).resolve().parents[1] +BASE = ROOT / "data" / "b6-v3" +JB = "jbfull@360" +LOST = 50e6 / (4100 * 7_333_000 / 68_152 * 167.69) +KEPT = 35 / 50 + + +def fits(rows: list[dict]) -> tuple[float, float]: + """TAKE and SAVE over the stream days' JetBlue replays: the stranded's share of the fare such that JetBlue loses + LOST of the fares booked on its cancelled flights, and the share of FAA's fuel and maintenance rate such that a + cancellation saves 1 - KEPT of the revenue it loses (JetBlue's Q1 2014 storms).""" + a = [r["real"][JB]["airline"] for r in rows] + take = sum(LOST * x["cancelled_rev"] - x["refunded_rev"] for x in a) / sum(x["stranded_rev"] for x in a) + save = (1 - KEPT) * sum(LOST * x["cancelled_rev"] for x in a) / sum(x["saved"] for x in a) + return take, save + + +def main(): + rep, bestj = Path(sys.argv[1]), Path(sys.argv[2]) + out = Path(sys.argv[sys.argv.index("--out") + 1]) if "--out" in sys.argv else ROOT / "data" / "b6-v3r" + base = json.loads((BASE / "manifest.json").read_text()) + pack = Pack3(BASE) + ids = {v["date"]: t for t, v in pack.tasks.items()} + rows = {p.stem: json.load(open(p)) for p in sorted(rep.glob("*.json"))} + take, save = fits([r for r in rows.values() if "model" in r["now"]]) + gate_caps = realistic.gates(BASE / "dates") + rate = next(iter(rows.values()))["realistic"]["rate"] + (out / "dates").mkdir(parents=True, exist_ok=True) + tasks, files = {}, {} + for date, row in rows.items(): + task_id = ids[date] + w = pack.world(task_id) + raw = json.loads(gzip.decompress((BASE / "dates" / f"{date}.json.gz").read_bytes())) + task_r = realistic.layered(w.task) + down = realistic.broken(task_r, rate) + if down != row["realistic"]["broken"]: + raise ValueError(f"{date}: breakdowns {down} differ from the report's {row['realistic']['broken']}") + firsts = realistic.first_departures(w.task) + fares = {g.id: realistic.od_fare(task_r, g.legs) for g in w.passengers.groups} + found = json.load(open(bestj / f"{date}.json")) + priced = {k: v["airline"] for k, v in row["real"].items() if "airline" in v} | {"best J": found["airline"]} + cost = {k: round(realworld.jetblue(a, take, save), 1) for k, a in priced.items()} + raw["task"] = task_r.to_dict() + raw["realistic"] = { + "broken": down, "firsts": [[fam, s, d] for (fam, s), d in sorted(firsts.items())], + "fares": {str(k): round(v, 3) for k, v in fares.items()}, + "refs": {"jetblue": cost[JB], "best": cost["best J"], "top": min(cost.values()), + "top_from": min(cost, key=cost.get), "priced": cost, + "cancelled": {k: sum(x["status"] != "flown" for x in v["flights"].values()) + for k, v in row["real"].items() if k in priced} | + {"best J": sum(x["status"] != "flown" for x in found["flights"].values())}}, + "best_plan": found["plan"]} + body = gzip.compress(json.dumps(raw, separators=(",", ":")).encode(), mtime=0) + (out / "dates" / f"{date}.json.gz").write_bytes(body) + files[f"dates/{date}.json.gz"] = hashlib.sha256(body).hexdigest() + tasks[task_id] = base["tasks"][task_id] + print(f"{date} JB {cost[JB] * 1.07 / 1e6:.2f}M top {min(cost.values()) * 1.07 / 1e6:.2f}M " + f"({min(cost, key=cost.get)}) broken {len(down)}", flush=True) + commit = subprocess.run(["git", "-C", str(ROOT), "rev-parse", "HEAD"], capture_output=True, + text=True).stdout.strip() + manifest = {"pack": "b6-v3r", "base": "b6-v3", "base_manifest_sha256": hashlib.sha256( + (BASE / "manifest.json").read_bytes()).hexdigest(), "assembled_at_commit": commit, + "realistic": {"gates": gate_caps, "rate": rate, "swaps": sorted(realworld.SWAPS), "take": take, + "save": save, "lost": LOST, "kept": KEPT, "usd_per_pax_min": 1.07}, + "tasks": dict(sorted(tasks.items(), key=lambda kv: kv[1]["date"])), "files": files} + (out / "manifest.json").write_text(json.dumps(manifest, indent=1) + "\n") + print(f"{len(tasks)} days, take {take:.3f} save {save:.3f} -> {out}") + + +if __name__ == "__main__": + main() diff --git a/scripts/hub_dataset.py b/scripts/hub_dataset.py index 03c3b95..e3c0a4d 100644 --- a/scripts/hub_dataset.py +++ b/scripts/hub_dataset.py @@ -1,5 +1,5 @@ """Build the Hugging Face dataset earakely-scale/AirOpsEnv-AgentEnv from this checkout and the recorded sweeps, and the -static replay Space that reads it; push either. Three kinds of tables share the dataset: +static replay Space that reads it; push either. These kinds of tables share the dataset: - v3's, passengers and the customer desk (the default): v3_tasks, the b6-v3 pack's dates per split with their passengers and their references under cost P; v3_references, every candidate the reference was chosen from, the @@ -8,6 +8,9 @@ played, ranked by the three-desk mean, and results/eval-v3.json; and the bundle airops-occ3, every date in both modes, whose task files also give the card the desks' rules, tools and turns. A folder occ3-board- is a board, occ3-board-sol the one whose two modes are paired. +- realistic mode's (--occ3r): v3r_tasks, the b6-v3r pack's dates with JetBlue's recorded day and J of every plan + priced; v3r_episodes and v3r_desks, every attempt of the realistic pilots (folders occ3-real-*), replayed on the + realistic world; and the bundle airops-occ3-real. - v2's, the ops control center: v2_tasks, the b6-v2 pack's dates per split with their references, the FAA's advisories as the desks get them, the desks' rules and each date's world as the env loads it; v2_runs, every attempt the control-center pilots recorded under --runs (the board, its labelled retry and the pilot), each replayed @@ -59,7 +62,7 @@ from agentenv_hf.scan import known_values, scan from huggingface_hub import CommitOperationAdd, CommitOperationDelete, DatasetCard, HfApi -from agentenv_airops import export, tasks +from agentenv_airops import export, realworld, tasks from agentenv_airops.core.model import Task from agentenv_airops.core.reward import reward from agentenv_airops.day import Day @@ -70,9 +73,11 @@ from agentenv_airops.occ.day import GRADED, OccDay from agentenv_airops.occ.day import reward as occ_reward from agentenv_airops.occ.day3 import OccDay3 +from agentenv_airops.occ.day3r import OccDay3R from agentenv_airops.occ.pack import PACK as OCC_PACK from agentenv_airops.occ.pack import PACK3 as OCC3_PACK -from agentenv_airops.occ.pack import Pack, Pack3, World +from agentenv_airops.occ.pack import PACK3R as OCC3R_PACK +from agentenv_airops.occ.pack import Pack, Pack3, Pack3R, World from agentenv_airops.occ.replay import replay as occ_replay from agentenv_airops.programs import occ from agentenv_airops.schedule import ENV, TOOLS, max_turns, timetable @@ -169,6 +174,12 @@ "naive": "doing nothing: the planned crews by the trip rule, no desk", "v2_best": "v2's best plan, blind to passengers, played with them", "airline": "the airline: JetBlue's own decisions, replayed with the generated crews and passengers"} +OCC3R = "v3r" +OCC3R_BUNDLE = "airops-occ3-real" +OCC3R_PREFIX = "occ3-real-" +OCC3R_COSTS = ("aircraft", "crew", "tomorrow", "refunds", "stranded") +OCC3R_PLANS = {"jetblue": "jbfull@360", "desks": "desks", "best_p": "best", "best_j": "best J"} +OCC3R_BOARD_MD = re.compile(r"(\n).*?(\n)", re.S) def task_row(task: Task, raw: str) -> dict: @@ -352,6 +363,7 @@ def configs(files: dict[str, bytes]) -> list[dict]: out = [] for version, names in ((OCC3, (("tasks", "tasks"), ("references", "references"), ("episodes", "episodes"), ("desks", "desks"))), + (OCC3R, (("tasks", "tasks"), ("episodes", "episodes"), ("desks", "desks"))), (OCC, (("tasks", "tasks"), ("episodes", "runs"), ("desks", "desks")))): first = len(out) for table, name in names: @@ -379,12 +391,14 @@ def split_order(path: str) -> int: return ("eval", "control", "train").index(split) -def card_text(plugin: str, files: dict[str, bytes], board_md: str = "", occ_md: str = "", occ3_md: str = "") -> str: +def card_text(plugin: str, files: dict[str, bytes], board_md: str = "", occ_md: str = "", occ3_md: str = "", + occ3r_md: str = "") -> str: """hub/dataset/README.md with the dataset's configs, each version's board between its markers, the bundle airops's - two configs and needs as agentenv-hf's publish writes them, and airops-occ's and airops-occ3's needs when the build - has them.""" + two configs and needs as agentenv-hf's publish writes them, and airops-occ's, airops-occ3's and airops-occ3-real's + needs when the build has them.""" text = CARD.read_text() - for marker, md in ((BOARD, board_md), (OCC_BOARD_MD, occ_md), (OCC3_BOARD_MD, occ3_md)): + for marker, md in ((BOARD, board_md), (OCC_BOARD_MD, occ_md), (OCC3_BOARD_MD, occ3_md), + (OCC3R_BOARD_MD, occ3r_md)): text = marker.sub(lambda m, md=md: m[1] + (f"\n{md}\n" if md else "") + m[2], text) ours = DatasetCard(text) ours.data.configs = configs(files) @@ -392,7 +406,8 @@ def card_text(plugin: str, files: dict[str, bytes], board_md: str = "", occ_md: ours.data.configs[0]["default"] = True text = card(str(ours), name=BUNDLE, split=SPLIT, repo=None, description=None, license=None, needs={"plugins": [plugin], "setup": SETUP}) - more = {name: setup for name, setup in ((OCC3_BUNDLE, OCC3_SETUP), (OCC_BUNDLE, OCC_SETUP)) + more = {name: setup for name, setup in ((OCC3_BUNDLE, OCC3_SETUP), (OCC3R_BUNDLE, OCC3_SETUP), + (OCC_BUNDLE, OCC_SETUP)) if any(p.startswith(f"bundles/{name}/") for p in files)} if not more: return text @@ -405,7 +420,8 @@ def card_text(plugin: str, files: dict[str, bytes], board_md: str = "", occ_md: def build(sweeps: list[str], runs_dir: Path, out: Path, pack: Path, plugin: str, fixtures: list[Path], cache: Path, date: str, occ_pilots: list[str] = (), occ_pack: Path | None = None, occ_viewer: list[Path] = (), replays: Path | None = None, workers: int = 1, occ3_pilots: list[str] = (), - occ3_pack: Path | None = None, occ3_running: list[str] = ()) -> dict[str, bytes]: + occ3_pack: Path | None = None, occ3_running: list[str] = (), occ3r_pilots: list[str] = (), + occ3r_pack: Path | None = None) -> dict[str, bytes]: days = tasks.pack_tasks(pack) raw = {json.loads(line)["task_id"]: line for line in tasks_file(pack).read_text().splitlines() if line.strip()} bundle = out / "bundles" / BUNDLE @@ -441,12 +457,14 @@ def build(sweeps: list[str], runs_dir: Path, out: Path, pack: Path, plugin: str, occ3_tables, occ3_md = occ3_files(runs_dir, list(occ3_pilots), Pack3(occ3_pack), bundle3, replays, workers, date, list(occ3_running)) files |= occ3_tables | bundle3 - for d in [d for d, _ in played] + [runs_dir / name for name in [*occ_pilots, *occ3_pilots]]: + occ3r_tables, occ3r_md = occ3r_files(runs_dir, list(occ3r_pilots), Pack3R(occ3r_pack), replays, workers) + files |= occ3r_tables | occ3r_bundle(out, occ3r_pack) + for d in [d for d, _ in played] + [runs_dir / name for name in [*occ_pilots, *occ3_pilots, *occ3r_pilots]]: for path in sorted(d.rglob("*")): rel = path.relative_to(d) if path.is_file() and rel.parts[0] not in ("logs", "bundle"): files[f"runs/{d.name}/{rel.as_posix()}"] = path.read_bytes() - return files | {"README.md": card_text(plugin, files, board_md, occ_md, occ3_md).encode()} + return files | {"README.md": card_text(plugin, files, board_md, occ_md, occ3_md, occ3r_md).encode()} def occ_bundle(out: Path, pack: Path | None) -> dict[str, bytes]: @@ -472,6 +490,16 @@ def occ3_bundle(out: Path, pack: Path | None, caps: dict | None = None) -> dict[ for p in sorted(folder.rglob("*")) if p.is_file()} +def occ3r_bundle(out: Path, pack: Path | None) -> dict[str, bytes]: + """The bundle airops-occ3-real as `agent-env airops-occ3 tasks --realistic` writes it, every date of the b6-v3r pack + in both modes at the env's caps, with wiring-noplay.""" + folder = out / "bundles" / OCC3R_BUNDLE + shutil.rmtree(folder, ignore_errors=True) + occ3_tasks.generate(folder, split="all", pack=pack, noplay=True, realistic=True) + return {f"bundles/{OCC3R_BUNDLE}/{p.relative_to(folder).as_posix()}": p.read_bytes() + for p in sorted(folder.rglob("*")) if p.is_file()} + + def board_caps(runs_dir: Path, pilots: list[str]) -> dict | None: """The caps per desk the v3 board ran at, from its pilot.json, when the build publishes the board.""" path = runs_dir / OCC3_BOARD / "pilot.json" @@ -1091,6 +1119,87 @@ def occ3_eval_card(rows: list[dict], days: list[dict], harness_: dict, date: str } +def occ3r_files(runs_dir: Path, pilots: list[str], pack: Pack3R, replays: Path, workers: int + ) -> tuple[dict[str, bytes], str]: + """Realistic mode's tables and its runs as the card shows them: the b6-v3r pack's dates with JetBlue's recorded + day and J of every plan priced, and every attempt of ``pilots`` replayed on the realistic world, with each desk's + transcript.""" + days = sorted((occ3r_date(pack, t) for t in pack.tasks), key=lambda d: (OCC_SPLITS.index(d["split"]), d["date"])) + files = by_split(f"tasks/{OCC3R}", days) + attempts = occ_attempts(runs_dir, pilots, pack, (), {}) + if not attempts: + return files, "" + checked = occ_replays(attempts, runs_dir, pack, replays, workers, OccDay3R) + rows = [occ3r_run_row(a, checked[a["run_id"]], runs_dir, pack) for a in attempts] + files |= by_split(f"episodes/{OCC3R}", rows) + files |= by_split(f"desks/{OCC3R}", [row for a in attempts for row in occ_desk_rows(a, runs_dir)]) + return files, occ3r_markdown(rows, {d["task_id"]: d for d in days}) + + +def occ3r_date(pack: Pack3R, task_id: str) -> dict: + """A b6-v3r date: its flights and what JetBlue's recorded day did with them, J (passenger-minutes, the desks see + dollars) of JetBlue's decisions replayed (the anchor, 0.5), the cheapest plan priced (the top, 1.0) and the plans + between with their rewards and how many flights each left unflown, the breakdowns, the best plan under J and the + date's world as the env loads it.""" + entry = pack.tasks[task_id] + raw = gzip.decompress((pack.path / "dates" / f"{entry['date']}.json.gz").read_bytes()).decode() + data = json.loads(raw) + real, flights, usd = data["realistic"], data["task"]["flights"], pack.manifest["realistic"]["usd_per_pax_min"] + refs, flew = real["refs"], [f for f in data["task"]["flights"] if not (f["actual"]["cancelled"] + or f["actual"]["diverted"])] + plans = {k: refs["priced"].get(plan) for k, plan in OCC3R_PLANS.items()} + return {"task_id": task_id, "date": entry["date"], "split": entry["split"], "flights": len(flights), + "recorded_not_flown": len(flights) - len(flew), + "recorded_late_60": sum(f["actual"]["arr"] - f["sched_arr"] >= 60 for f in flew + if f["actual"]["arr"] is not None), + "anchor": refs["jetblue"], "top": refs["top"], "top_from": refs["top_from"], "usd_per_pax_min": usd, + **{f"{k}_J": v for k, v in plans.items()}, + **{f"{k}_usd": None if v is None else round(v * usd) for k, v in plans.items()}, + **{f"{k}_reward": None if v is None else round(realworld.reward(v, refs["top"], refs["jetblue"]), 6) + for k, v in plans.items()}, + **{f"{k}_not_flown": refs["cancelled"].get(plan) for k, plan in OCC3R_PLANS.items()}, + "priced": json.dumps(refs["priced"]), "not_flown_by_plan": json.dumps(refs["cancelled"]), + "broken": json.dumps(real["broken"]), "best_plan_j": json.dumps(real["best_plan"]), "world": raw} + + +def occ3r_run_row(a: dict, checked: dict, runs_dir: Path, pack: Pack3R) -> dict: + """An attempt as in v3_episodes, with realistic mode's grade: J and its parts in dollars, the anchor and the top, + the flights it left unflown and those it landed 60 minutes or more late, and P's figures beside them.""" + r = a["row"] + day = json.loads((runs_dir / a["folder"] / r["day"]).read_text()) if r.get("day") else {} + grade = day.get("grade") or {} + real, p = grade.get("realistic") or {}, grade.get("P") or {} + task = pack.world(a["task_id"]).task + executed = {x["id"]: x for x in grade.get("executed") or []} + flown = [(f, executed[f.id]) for f in task.flights if executed.get(f.id, {}).get("status") == "flown"] + return occ_run_row(a, checked, runs_dir, OCC3_MODES, ()) | { + "anchor": grade.get("anchor"), "top": grade.get("best"), "cost_usd": real.get("usd"), + **{f"usd_{k}": (grade.get("costs") or {}).get(k) for k in OCC3R_COSTS}, + "not_flown": len(executed) - len(flown) if executed else None, + "late_60": sum(x["arr"] - f.sched_arr >= 60 for f, x in flown) if executed else None, + "P": p.get("cost"), "P_reward": p.get("reward"), "criterion": grade.get("criterion")} + + +def occ3r_markdown(rows: list[dict], days: dict[str, dict]) -> str: + """Each scored realistic run beside JetBlue's day: what JetBlue recorded, J of its decisions replayed, of the run + and of the best plan found, in dollars.""" + def millions(x: float | None) -> str: + return "–" if x is None else f"${x / 1e6:.2f}M" + + scored = sorted((r for r in rows if r["outcome"] == "scored"), key=lambda r: (r["model"], r["date"])) + if not scored: + return "" + lines = ["| Day | Model | Reward | J, JetBlue's decisions | J, the model | J, best found | Not flown: JetBlue / " + "the model | 60+ min late: JetBlue / the model | Spend |", "|---|---|---|---|---|---|---|---|---|"] + for r in scored: + d = days[r["task_id"]] + lines.append(cells(r["date"], r["model_name"], f"{r['reward']:.3f}", millions(d["jetblue_usd"]), + millions(r["cost_usd"]), millions(d["best_j_usd"]), + f"{d['recorded_not_flown']} / {r['not_flown']}", f"{d['recorded_late_60']} / {r['late_60']}", + dollars(r["spend_usd"]))) + return "\n".join(lines) + + def tasks_file(pack: Path) -> Path: return pack / "tasks.jsonl" if pack.is_dir() else pack @@ -1158,6 +1267,10 @@ def main(): help=f"A three-desk pilot folder under --runs for v3's tables (repeat); default: every folder " f"named {OCC3_PREFIX}* with a pilot.json.") ap.add_argument("--occ3-pack", type=Path, default=Path(str(OCC3_PACK)), help="Default: the packaged b6-v3 pack.") + ap.add_argument("--occ3r", action="append", metavar="PILOT", + help=f"A realistic-mode pilot folder under --runs for v3r's tables (repeat); default: every folder " + f"named {OCC3R_PREFIX}* with a pilot.json.") + ap.add_argument("--occ3r-pack", type=Path, default=Path(str(OCC3R_PACK)), help="Default: the packaged b6-v3r pack.") ap.add_argument("--occ3-running", action="append", default=[], metavar="MODEL", help="A model whose v3 board is still running (repeat), named under the card's board until its " "occ3-board-* folder is given.") @@ -1165,15 +1278,17 @@ def main(): plugin = f"agentenv-airops @ git+{CODE_URL}@{args.plugin_ref}" sweeps = args.sweeps or sorted(p.parent.name for p in args.runs.glob("*/sweep.json")) found = sorted(p.parent.name for p in args.runs.glob("*/pilot.json")) - occ3 = args.occ3 if args.occ3 is not None else [f for f in found if f.startswith(OCC3_PREFIX)] + occ3 = args.occ3 if args.occ3 is not None else [f for f in found if f.startswith(OCC3_PREFIX) + and not f.startswith(OCC3R_PREFIX)] + occ3r = args.occ3r if args.occ3r is not None else [f for f in found if f.startswith(OCC3R_PREFIX)] pilots = args.occ if args.occ is not None else [f for f in found if not f.startswith(OCC3_PREFIX)] print("sweeps:", " ".join(sweeps) or "none", "\ncontrol-center pilots:", " ".join(pilots) or "none", - "\nthree-desk pilots:", " ".join(occ3) or "none") + "\nthree-desk pilots:", " ".join(occ3) or "none", "\nrealistic pilots:", " ".join(occ3r) or "none") with namespace_routing(): files = build(sweeps, args.runs, args.out, args.pack, plugin, args.fixture, args.cache, args.date, occ_pilots=pilots, occ_pack=args.occ_pack, occ_viewer=args.occ_viewer, replays=args.occ_replays, workers=args.workers, occ3_pilots=occ3, occ3_pack=args.occ3_pack, - occ3_running=args.occ3_running) + occ3_running=args.occ3_running, occ3r_pilots=occ3r, occ3r_pack=args.occ3r_pack) space = space_files(args.tag or "main") if args.space_out else {} scan(files | {f"space/{p}": c for p, c in space.items()}, known_values()) write(files, args.out) diff --git a/scripts/live_runner.py b/scripts/live_runner.py index 9caf3dd..68352ed 100644 --- a/scripts/live_runner.py +++ b/scripts/live_runner.py @@ -31,7 +31,7 @@ from agent_env.task.store import get_task_instance_store from agentenv_airops.occ import sweep -from agentenv_airops.occ.pack import Pack3 +from agentenv_airops.occ.pack import Pack3, Pack3R ROOT = Path(__file__).resolve().parents[1] CONFIG = Path(__file__).with_name("live_runner.toml") @@ -53,6 +53,7 @@ class Settings: model: str = "openai/gpt-6-astra" policy: str | None = None pack: str | None = None + realistic: bool = False cap_joint_usd: float = 23.5 request_margin_usd: float = 4.0 budget_usd: float = 430.0 @@ -75,7 +76,7 @@ def read(cls, path: Path, **overrides) -> "Settings": s = replace(cls(**raw), days=tuple(raw.get("days", ())), **given) if s.policy == "llm": s = replace(s, policy=None) - known = Pack3(s.pack).tasks + known = s.packed().tasks if bad := [t for t in s.days if t not in known or known[t]["split"] == "control"]: raise ValueError(f"{path}: not a train or eval day of the pack: {', '.join(bad)}") if len(set(s.days)) != len(s.days): @@ -84,9 +85,13 @@ def read(cls, path: Path, **overrides) -> "Settings": stamp(s.start_utc) return s + def packed(self) -> Pack3: + return (Pack3R if self.realistic else Pack3)(self.pack) + def pilot(self, runs: list[str]) -> sweep.Pilot3: - return sweep.Pilot3(self.name, self.model, runs, {"joint": self.cap_joint_usd, "one": self.cap_joint_usd}, - self.budget_usd, self.pack, self.policy, self.request_margin_usd) + kind = sweep.Pilot3R if self.realistic else sweep.Pilot3 + return kind(self.name, self.model, runs, {"joint": self.cap_joint_usd, "one": self.cap_joint_usd}, + self.budget_usd, self.pack, self.policy, self.request_margin_usd) def utc(t: float | None = None) -> str: @@ -178,6 +183,7 @@ def describe(d: dict) -> str: name = "occ3-live-astra" # results/runs/: pilot.json, results.jsonl, days/, transcripts/, logs/ model = "openai/gpt-6-astra" # policy = "script" # the env's scripted desks, $0 +# realistic = true # b6-v3r's days in the realistic world under JetBlue's own cost (REALISTIC.md) cap_joint_usd = 23.5 # each desk's cap, as registered for the board request_margin_usd = 4.0 # one worst request a desk may make past its cap budget_usd = 430.0 # model spend may not pass it @@ -416,11 +422,11 @@ def instances(self, task: str) -> list[str]: return [] def dates(self) -> dict[str, str]: - return {t: v["date"] for t, v in Pack3(self.s.pack).tasks.items()} + return {t: v["date"] for t, v in self.s.packed().tasks.items()} def hub(self, task_id: str) -> str: if task_id not in self.hubs: - pack = Pack3(self.s.pack) + pack = self.s.packed() raw = gzip.decompress((pack.path / "dates" / f"{pack.tasks[task_id]['date']}.json.gz").read_bytes()) self.hubs[task_id] = json.loads(raw)["task"]["hub"] return self.hubs[task_id] @@ -499,8 +505,8 @@ def attempt(self, task_id: str, number: int, day: str, hub: str) -> dict: pilot = s.pilot(list(dict.fromkeys([r["run"] for r in rows] + [run]))) (self.out / "pilot.json").write_text(json.dumps(asdict(pilot), indent=2) + "\n") bundle = self.out / "bundle" - sweep.KIT3.generate(bundle, task_ids=[task_id], modes=("joint",), - pack=Path(s.pack).expanduser() if s.pack else None, cap=s.cap_joint_usd, policy=s.policy) + pilot.kit.generate(bundle, task_ids=[task_id], modes=("joint",), + pack=Path(s.pack).expanduser() if s.pack else None, cap=s.cap_joint_usd, policy=s.policy) task = bundle_task(bundle, run) log = self.out / "logs" / f"{run}-a{number}.log" log.parent.mkdir(parents=True, exist_ok=True) diff --git a/scripts/live_runner.toml b/scripts/live_runner.toml index f8fd91c..e9889fb 100644 --- a/scripts/live_runner.toml +++ b/scripts/live_runner.toml @@ -2,6 +2,7 @@ name = "occ3-live-astra" # results/runs/: pilot.json, results.jsonl, days/, transcripts/, logs/ model = "openai/gpt-6-astra" # policy = "script" # the env's scripted desks, $0 +# realistic = true # b6-v3r's days in the realistic world under JetBlue's own cost (REALISTIC.md) cap_joint_usd = 23.5 # each desk's cap, as registered for the board request_margin_usd = 4.0 # one worst request a desk may make past its cap budget_usd = 430.0 # model spend may not pass it diff --git a/scripts/live_runner_real.toml b/scripts/live_runner_real.toml new file mode 100644 index 0000000..a5c7e39 --- /dev/null +++ b/scripts/live_runner_real.toml @@ -0,0 +1,46 @@ +# The live runner's settings (scripts/live_runner.py), read again before every day: edits apply from the next day on. +name = "occ3-real-astra" # results/runs/: pilot.json, results.jsonl, days/, transcripts/, logs/ +model = "openai/gpt-6-astra" +# policy = "script" # the env's scripted desks, $0 +realistic = true # b6-v3r's days in the realistic world under JetBlue's own cost (REALISTIC.md) +cap_joint_usd = 23.5 # each desk's cap, as registered for the board +request_margin_usd = 4.0 # one worst request a desk may make past its cap +budget_usd = 130.0 # model spend may not pass it +worst_day_usd = 42.0 # a day starts only if the spend so far plus this fits (the board's worst: $41.46) +typical_day_usd = 22.68 # the board's mean Astra day: --dry-run's projection only +slot_minutes = 80.0 # a day that ends early is followed by an intermission until its slot ends +min_intermission_minutes = 5.0 # after a day past its slot, or a failed attempt +card_seconds = 60.0 # status "done" for this long after a day, then "intermission" +window_hours = 24.0 # a day starts only if its slot ends within the window +# start_utc = "2026-10-23T16:00:00Z" # the first day's start; default: when the runner first runs +longest_day_minutes = 150.0 # a day still running then is stopped (the board's longest: 84) + +# The realistic pack's days (b6-v3r) in the stream's play order (`live_runner.py order`): the training days by storm +# at JFK and BOS, plus one per ten flights cancelled), each season by score with a storm's second day after the rest, +# the seasons interleaved; then the eval days the board played, the same way. Reorder or delete freely. +days = [ + # training days + "b6-20240803-0400", # 1 2024-08-03 score 81.6: summer; JFK GDP 11.2 h, 15 ground stops 15.6 h; BOS GDP 16.6 h, 9 ground stops 6.9 h; 88 cancelled + "b6-20230627-0400", # 2 2023-06-27 score 79.1: summer; JFK GDP 7.2 h, 10 ground stops 10.2 h; BOS GDP 19.1 h, 7 ground stops 7.5 h; 174 cancelled + "b6-20250119-0400", # 3 2025-01-19 score 33.9: winter; JFK GDP 9.8 h, 4 ground stops 4.5 h; BOS no program; 151 cancelled + "b6-20240818-0400", # 4 2024-08-18 score 68.3: summer; JFK GDP 19.4 h, 7 ground stops 8.6 h; BOS GDP 21.1 h, 2 ground stops 2.3 h; 60 cancelled + "b6-20260727-0400", # 5 2026-07-27 score 66.5: summer; JFK GDP 11 h, 18 ground stops 16.7 h; BOS GDP 13.9 h, 2 ground stops 1.6 h; 50 cancelled + "b6-20240630-0400", # 6 2024-06-30 score 54: summer; JFK GDP 14 h, 8 ground stops 9.6 h; BOS 6 ground stops 5.1 h; 106 cancelled + "b6-20240106-0400", # 7 2024-01-06 score 24.2: winter; JFK GDP 5.2 h; BOS GDP 10.8 h, 4 ground stops 3.1 h; 20 cancelled + "b6-20260729-0400", # 8 2026-07-29 score 51.7: summer; JFK 8 ground stops 7.6 h; BOS GDP 27.7 h, 2 ground stops 1.6 h; 56 cancelled + "b6-20250813-0400", # 9 2025-08-13 score 51.3: summer; JFK GDP 15.4 h, 9 ground stops 9.5 h; BOS GDP 8.8 h, 2 ground stops 1.6 h; 49 cancelled + "b6-20250206-0400", # 11 2025-02-06 score 18.9: winter; JFK GDP 6.7 h; BOS GDP 10.7 h; 15 cancelled + "b6-20230701-0400", # 12 2023-07-01 score 33.9: summer; JFK GDP 16.9 h; BOS GDP 9.8 h, 4 ground stops 3.5 h; 2 cancelled + "b6-20240614-0400", # 13 2024-06-14 score 32.9: summer; JFK GDP 11.3 h, 3 ground stops 3.2 h; BOS GDP 11.4 h, 2 ground stops 0.7 h; 24 cancelled + "b6-20230817-0400", # 14 2023-08-17 score 32.4: summer; JFK GDP 4.3 h; BOS GDP 19.4 h, 5 ground stops 3.5 h; 17 cancelled + # eval days: Astra played these on the board + "b6-20230709-0400", # 45 2023-07-09 score 79.9: summer; JFK GDP 16.7 h, 8 ground stops 7.8 h; BOS GDP 15.5 h, 7 ground stops 5.5 h; 211 cancelled + "b6-20230713-0400", # 48 2023-07-13 score 58.1: summer; JFK GDP 10.1 h, 12 ground stops 11.3 h; BOS GDP 8.5 h, 9 ground stops 7.7 h; 15 cancelled + "b6-20260709-0400", # 50 2026-07-09 score 48.8: summer; JFK GDP 21.5 h, 10 ground stops 7.7 h; BOS GDP 6.2 h; 57 cancelled + "b6-20230729-0400", # 53 2023-07-29 score 40.2: summer; JFK GDP 10 h, 6 ground stops 3.6 h; BOS GDP 8.8 h, 5 ground stops 3.6 h; 70 cancelled + "b6-20260701-0400", # 58 2026-07-01 score 18.5: summer; JFK GDP 4.9 h, 1 ground stop 1 h; BOS GDP 7.2 h, 2 ground stops 1.8 h; 8 cancelled + "b6-20260715-0400", # 62 2026-07-15 score 15.3: summer; JFK GDP 3.7 h, 3 ground stops 3.6 h; BOS 2 ground stops 1.7 h; 10 cancelled + "b6-20250111-0400", # 63 2025-01-11 score 10.2: winter; JFK no program; BOS GDP 5.6 h, 2 ground stops 2 h; 6 cancelled + "b6-20230708-0400", # 67 2023-07-08 score 78.3: summer; JFK GDP 20.7 h, 13 ground stops 12.4 h; BOS GDP 11.8 h, 8 ground stops 9.2 h; 26 cancelled + "b6-20240623-0400", # 68 2024-06-23 score 58.9: summer; JFK GDP 19.5 h, 5 ground stops 4 h; BOS GDP 17.5 h, 5 ground stops 4.1 h; 57 cancelled +] diff --git a/scripts/realistic_best_j.py b/scripts/realistic_best_j.py new file mode 100644 index 0000000..b6e617a --- /dev/null +++ b/scripts/realistic_best_j.py @@ -0,0 +1,125 @@ +"""The best plan found under JetBlue's cost J in realistic mode, per day. + +The pack's search (crewplan.search: the aircraft MIP's optimum, crewed, then its local search over departures and +cancellations, every plan replayed in the crew sim with passengers) with J in place of cost P: +- the aircraft MIP built without passengers and repriced: a flight's copy costs its aircraft's minutes alone + (cost.flown_price with no passengers), a cancellation the lost revenue of its booked passengers (each itinerary's + fare by realistic.od_fare, times LOST, JetBlue's Q1 2014 rate, for one start, times 1 for a second: the linear MIP + can't see refunds rise as a day's seats run out) less SAVE (the report's fit, --save) of what it saves; +- a replay's worth is J as the realism report prices it: aircraft with SAVE of its cancellations' saving, crew, + tomorrow at the aircraft's cost, the refunded at their fares and the stranded at TAKE (the report's fit, --take) of + theirs. +Seeds (crewed by the crew sim's default rule): the desks' day, JetBlue's replay, the pack's best plan under cost P and +the cut@Nh probes. The plan found is replayed in the env's truth (occ.run.truth) and priced there. + +Usage: .venv/bin/python scripts/realistic_best_j.py REALREP_DIR OUT_DIR --take T --save S [--budget 1200] [--workers 4] +""" + +import json +import sys +import time +from concurrent.futures import ProcessPoolExecutor, as_completed +from dataclasses import replace +from pathlib import Path + +from agentenv_airops import realistic +from agentenv_airops.occ import run as occrun +from agentenv_airops.occ.pack import Pack3 +from agentenv_airops.programs import crewplan, sim +from agentenv_airops.programs.cost import RATES, flown_price + +PACK = Path.home() / "Code" / "agentenv-airops-plugin" / "data" / "b6-v3" +LOST = 50e6 / (4100 * 7_333_000 / 68_152 * 167.69) +CUTS = (2, 4, 6) + + +def j_of(a: dict, take: float, save: float) -> float: + return (a["aircraft"] + (1 - save) * a["saved"] + a["crew"] + a["tomorrow"] + a["refunded_rev"] + + take * a["stranded_rev"]) + + +def repriced(air: crewplan.Aircraft, task, world, booked: dict, share: float, save: float): + """The MIP's cost vector under J, a cancelled passenger losing `share` of its fare.""" + net = air.net + vec = air.notice.copy() + for (i, d), c in net.xs.items(): + vec[c] = flown_price(task.flight(i), d, d + world.block[i], pax=0) + for i, c in (*net.cancel.items(), *net.lapsed.items(), *net.on_reserve.items()): + f = task.flight(i) + vec[c] = round(share * booked.get(i, 0.0) - save * RATES[f.family].saved * f.sched_block) + return vec + + +def one(task_id: str, rep_dir: str, out_dir: str, budget: float, gate_caps: dict, rate: float, take: float, + save: float) -> str: + t0 = time.monotonic() + w = Pack3(PACK).world(task_id) + task_r = realistic.layered(w.task) + w_r = replace(w, task=task_r) + world_r = realistic.world(task_r, gate_caps, rate) + firsts = realistic.first_departures(w.task) + row = json.load(open(Path(rep_dir) / f"{task_r.date}.json")) + crewplan.worth = lambda day, flat: j_of(realistic.airline(task_r, day, firsts, w.passengers), take, save) + booked: dict = {} + for g in w.passengers.groups: + v = g.pax * realistic.od_fare(task_r, g.legs) + for i in g.legs: + booked[i] = booked.get(i, 0.0) + v + air = crewplan.Aircraft(task_r, world_r) + desks0 = sim.execute(task_r, {}, world_r) + starts = [] + for label, share in (("J optimum, full fare", 1.0), ("J optimum", LOST)): + air.notice = repriced(air, task_r, world_r, booked, share, save) + air.flat = air.notice.copy() + starts.append((label, air.optimum(air.cost(), air.ub, desks0, 300.0)[0])) + gone = [f for f in w.task.flights if f.actual.cancelled or f.actual.diverted] + jb = {f.id: {"cancel": True, "decided": max(f.sched_dep - 360, task_r.start)} for f in gone} + desks = row["real"]["desks"]["flights"] + seeds = [("desks", {}), ("jetblue", jb), ("best P", w.best_plan)] + for h in CUTS: + seeds.append((f"cut@{h}h", {f.id: {"cancel": True, "decided": max(f.sched_dep - 120, task_r.start)} + for f in task_r.flights if desks[str(f.id)]["status"] == "flown" + and desks[str(f.id)]["arr"] - f.sched_arr >= 60 * h})) + found = crewplan.search(task_r, world_r, w.units, w.local, w.airports, air, starts, + time.monotonic() + budget, crew_limit=300.0, air_limit=300.0, days=seeds, + pax=w.passengers) + day = occrun.truth(w_r, found.plan, world_r) + out = {"date": task_r.date, "optima": {k: v.objective for k, v in starts}, "search_value": found.value, + "attempts": found.attempts, "plan": {str(k): v for k, v in found.plan.items()}, + "airline": realistic.airline(task_r, day, firsts, w.passengers), + "flights": {str(i): {"status": r.status, "dep": r.dep, "arr": r.arr} for i, r in day.run.flights.items()}, + "seconds": round(time.monotonic() - t0, 1)} + Path(out_dir, f"{task_r.date}.json").write_text(json.dumps(out, default=str)) + canc = sum(r.status != "flown" for r in day.run.flights.values()) + return f"{task_r.date} {out['seconds']}s J {j_of(out['airline'], take, save) * 1.07 / 1e6:.2f}M cancelled {canc}" + + +def main(): + rep_dir, out_dir = Path(sys.argv[1]), Path(sys.argv[2]) + budget = float(sys.argv[sys.argv.index("--budget") + 1]) if "--budget" in sys.argv else 1200.0 + workers = int(sys.argv[sys.argv.index("--workers") + 1]) if "--workers" in sys.argv else 4 + take = float(sys.argv[sys.argv.index("--take") + 1]) + save = float(sys.argv[sys.argv.index("--save") + 1]) + out_dir.mkdir(parents=True, exist_ok=True) + pack = Pack3(PACK) + ids = {v["date"]: t for t, v in pack.tasks.items()} + dates = sorted(p.stem for p in rep_dir.glob("*.json")) + if "--dates" in sys.argv: + dates = sys.argv[sys.argv.index("--dates") + 1].split(",") + gate_caps = realistic.gates(PACK / "dates") + rate = json.load(open(rep_dir / f"{dates[0]}.json"))["realistic"]["rate"] + todo = [d for d in dates if not (out_dir / f"{d}.json").exists()] + t0 = time.monotonic() + with ProcessPoolExecutor(workers, max_tasks_per_child=1) as pool: + futures = {pool.submit(one, ids[d], str(rep_dir), str(out_dir), budget, gate_caps, rate, take, save): d + for d in todo} + for n, f in enumerate(as_completed(futures), 1): + try: + msg = f.result() + except Exception as e: + msg = f"{futures[f]} FAILED {type(e).__name__}: {e}" + print(f"[{time.monotonic() - t0:6.0f}s] {n}/{len(todo)} {msg}", flush=True) + + +if __name__ == "__main__": + main() diff --git a/scripts/realistic_compare.py b/scripts/realistic_compare.py new file mode 100644 index 0000000..500760e --- /dev/null +++ b/scripts/realistic_compare.py @@ -0,0 +1,145 @@ +"""Replay sample b6-v3 days in the current mode and in realistic mode (agentenv_airops.realistic), per flight, for the +realism report. + +Scenarios, each played by occ.run.truth (the standard crew desk crews what the plan leaves alone, passengers on board): + desks the empty plan (the pack's B0) + jbsim@N JetBlue's cancellations and diversions only, each decided N min before its scheduled departure + (120, 360, or "start": the day's start); everything else as the desks fly it + jbfull@360 jbtails@360 with JetBlue's slot use in the programs too: each controlled or ground-stopped flight + claims the slot at its recorded departure plus its scheduled block (its CDM substitutions) + jbtails@360 jbsim@360 with JetBlue's own aircraft assignments too: every flight takes the aircraft JetBlue flew + it with when that aircraft is at its origin (no other swap anywhere) + model the plan GPT-6 Astra made on the 2026-10-23 stream (its day record), on the 13 stream days + best the pack's best plan found under cost P + cut@Nh a probe, in realistic mode only: the desks' day with every flight the desks fly N h or more late + cancelled 2 h before its scheduled departure (hindsight, one pass) +In realistic mode each scenario is also priced without the passenger model ("tdi": v2's cost, a cancelled passenger +457 min of delay plus the 66 refund exposure, the Total Delay Impact study's disrupted-passenger average). + +Usage: .venv/bin/python scripts/realistic_compare.py OUT_DIR --dates D1,D2,... [--run RUN_DIR] [--workers 6] +""" + +import json +import sys +import time +from concurrent.futures import ProcessPoolExecutor, as_completed +from dataclasses import replace +from pathlib import Path + +from agentenv_airops import realistic +from agentenv_airops.occ import run as occrun +from agentenv_airops.occ.pack import Pack3 +from agentenv_airops.programs import crewplan, sim + +PACK = Path.home() / "Code" / "agentenv-airops-plugin" / "data" / "b6-v3" +LEADS = {"jbsim@120": 120, "jbsim@360": 360, "jbsim@start": None} +CUTS = (2, 4, 6) + + +def plans(w, model_plan: dict | None) -> dict[str, dict]: + task = w.task + out = {"desks": {}} + gone = [f for f in task.flights if f.actual.cancelled or f.actual.diverted] + for name, lead in LEADS.items(): + out[name] = {f.id: {"cancel": True, "decided": task.start if lead is None else max(f.sched_dep - lead, + task.start)} + for f in gone} + if model_plan is not None: + out["model"] = model_plan + out["best"] = w.best_plan + return out + + +def played(task, day) -> dict: + return {i: {"status": r.status, "dep": r.dep, "arr": r.arr} for i, r in day.run.flights.items()} + + +def one(task_id: str, out_dir: str, model_path: str | None, gate_caps: dict, rate: float) -> str: + t0 = time.monotonic() + w = Pack3(PACK).world(task_id) + model_plan = None + if model_path: + raw = json.load(open(model_path))["plan"] + model_plan = {(int(k) if k.isdigit() else k): v for k, v in raw.items()} + task_r = realistic.layered(w.task) + w_r = replace(w, task=task_r) + world_now, world_r = sim.truth(w.task), realistic.world(task_r, gate_caps, rate) + firsts = realistic.first_departures(w.task) + lay = task_r.layers + row = {"task_id": task_id, "date": w.task.date, "split": w.task.split, "refs": {"best": w.refs["best"], + "B0": w.refs["B0"]["notice"]}, + "realistic": {"dep_caps": sum(len(v) for v in lay.dep_caps.values()), + "arr_caps": sum(len(v) for v in lay.arr_caps.values()), + "closures": sum(len(v) for v in lay.closures.values()), + "broken": dict(world_r.broken), "rate": rate}, + "now": {}, "real": {}} + for name, plan in plans(w, model_plan).items(): + day = occrun.truth(w, plan, world_now) + row["now"][name] = {"cost": realistic.rated(w.task, day.run, crewplan.worth(day, False), firsts), + "flights": played(w.task, day)} + day = occrun.truth(w_r, plan, world_r) + row["real"][name] = {"cost": realistic.rated(task_r, day.run, crewplan.worth(day, False), firsts), + "flights": played(task_r, day), + "airline": realistic.airline(task_r, day, firsts, w.passengers)} + jb = plans(w, None)["jbsim@360"] + full = jb | {f.id: {"claim": f.actual.dep + f.sched_block} for f in w.task.flights + if f.id not in jb and f.actual.dep is not None and (f.controlled or f.gs_held)} + row["claims"] = len(full) - len(jb) + for mode, ww, base in (("now", w, world_now), ("real", w_r, world_r)): + for name, plan in (("jbtails@360", jb), ("jbfull@360", full)): + day = occrun.truth(ww, plan, replace(base, swaps=frozenset())) + row[mode][name] = {"cost": realistic.rated(ww.task, day.run, crewplan.worth(day, False), firsts), + "flights": played(ww.task, day)} + if mode == "real": + row[mode][name]["airline"] = realistic.airline(ww.task, day, firsts, w.passengers) + desks = row["real"]["desks"]["flights"] + for hours in CUTS: + cut = {f.id: {"cancel": True, "decided": max(f.sched_dep - 120, task_r.start)} for f in task_r.flights + if desks[f.id]["status"] == "flown" and desks[f.id]["arr"] - f.sched_arr >= 60 * hours} + day = occrun.truth(w_r, cut, world_r) + row["real"][f"cut@{hours}h"] = {"cost": realistic.rated(task_r, day.run, crewplan.worth(day, False), firsts), + "flights": played(task_r, day), + "airline": realistic.airline(task_r, day, firsts, w.passengers)} + w_tdi = replace(w_r, passengers=None) + for name, plan in (*plans(w, model_plan).items(), ("jbfull@360", full)): + base = replace(world_r, swaps=frozenset()) if name == "jbfull@360" else world_r + day = occrun.truth(w_tdi, plan, base) + row["real"][name]["cost"]["tdi"] = realistic.rated(task_r, day.run, crewplan.worth(day, False), + firsts)["realistic"] + row["seconds"] = round(time.monotonic() - t0, 1) + Path(out_dir, f"{w.task.date}.json").write_text(json.dumps(row)) + return f"{w.task.date} {row['seconds']}s layers {row['realistic']} " + + +def main(): + out_dir = Path(sys.argv[1]) + dates = sys.argv[sys.argv.index("--dates") + 1].split(",") + run = Path(sys.argv[sys.argv.index("--run") + 1]) if "--run" in sys.argv else None + workers = int(sys.argv[sys.argv.index("--workers") + 1]) if "--workers" in sys.argv else 4 + out_dir.mkdir(parents=True, exist_ok=True) + pack = Pack3(PACK) + ids = {v["date"]: t for t, v in pack.tasks.items()} + models = {} + if run: + for line in (run / "results.jsonl").open(): + r = json.loads(line) + if r.get("reward") is not None and r.get("day"): + models[r["date"]] = str(run / r["day"]) + gate_caps = realistic.gates(PACK / "dates") + months = sorted({m for d in dates for m in realistic.candidates.months_of(d)}) + rate = realistic.breakdown_rate(tuple(months)) + print(f"gates at {len(gate_caps)} stations; breakdown rate {rate:.5f} per leg over {months}", flush=True) + todo = [d for d in dates if not (out_dir / f"{d}.json").exists()] + t0 = time.monotonic() + with ProcessPoolExecutor(workers, max_tasks_per_child=1) as pool: + futures = {pool.submit(one, ids[d], str(out_dir), models.get(d), gate_caps, rate): d for d in todo} + for n, f in enumerate(as_completed(futures), 1): + try: + msg = f.result() + except Exception as e: + msg = f"{futures[f]} FAILED {type(e).__name__}: {e}" + print(f"[{time.monotonic() - t0:6.0f}s] {n}/{len(todo)} {msg}", flush=True) + + +if __name__ == "__main__": + main() diff --git a/scripts/realistic_peers.py b/scripts/realistic_peers.py new file mode 100644 index 0000000..6417e3e --- /dev/null +++ b/scripts/realistic_peers.py @@ -0,0 +1,93 @@ +"""The peer-airline check of the realistic world (REALISTIC.md): where JetBlue cancelled a flight, what every other +carrier did at the same airport in the same hour, set against what the world does with the flight. + +For each of JetBlue's flights on the days realistic_compare.py replayed, the other carriers' cancellation share at its +worse end (the departures from its origin within an hour of its scheduled departure, or the arrivals at its +destination within an hour of its scheduled arrival, each counted with at least 3 flights; BTS, every carrier). Rows: +the flights JetBlue flew; JetBlue's cancellations (and diversions) the world flies within an hour of schedule in a +scenario (the standard desks by default; `--current`, the current mode's world); and those the world flies an hour or +more late or cancels. A world that reads the day right flies, within an hour, the cancellations whose airport the other +carriers kept running, and holds up those where they cancelled too. + +Usage: .venv/bin/python scripts/realistic_peers.py REALREP_DIR [--scenario desks] [--current] +""" + +import gzip +import json +import sys +from collections import Counter, defaultdict +from pathlib import Path + +import numpy as np + +from agentenv_airops import realistic +from agentenv_airops.programs import candidates + +ROOT = Path(__file__).resolve().parents[1] +DATES = ROOT / "data" / "b6-v3" / "dates" +HUBS = ("JFK", "BOS") +FLEW = "JetBlue flew" +EASY = "JetBlue cancelled, world flies within 1 h" +HARD = "JetBlue cancelled, world 1 h+ or cancels" + + +def peers(rep: Path, scenario: str = "desks", mode: str = "real") -> tuple[dict[str, np.ndarray], Counter]: + """Per row the other carriers' cancellation share at each flight's worse end, and the worse ends of the world's + easy flights where the other carriers cancelled 20% or more.""" + rows, ends = defaultdict(list), Counter() + for p in sorted(rep.glob("*.json")): + day = json.load(open(p)) + task = json.load(gzip.open(DATES / f"{day['date']}.json.gz"))["task"] + x = candidates.frame(realistic.INPUTS, realistic.DERIVED, day["date"]) + x = x[x.Reporting_Airline != "B6"] + dep, arr = defaultdict(list), defaultdict(list) + for o, d, sd, sa, c in zip(x.Origin, x.Dest, x.sched_dep - task["base"], x.sched_arr - task["base"], + x.Cancelled, strict=True): + dep[o].append((sd, c)) + arr[d].append((sa, c)) + dep = {k: np.array(v) for k, v in dep.items()} + arr = {k: np.array(v) for k, v in arr.items()} + world = day[mode][scenario]["flights"] + for f in task["flights"]: + rd, ra = share(dep, f["orig"], f["sched_dep"]), share(arr, f["dest"], f["sched_arr"]) + if rd is None and ra is None: + continue + worse = max(v for v in (rd, ra) if v is not None) + a, w = f["actual"], world[str(f["id"])] + if not (a["cancelled"] or a["diverted"]): + rows[FLEW].append(worse) + continue + easy = w["status"] == "flown" and w["dep"] - f["sched_dep"] < 60 + rows[EASY if easy else HARD].append(worse) + if easy and worse >= 0.2: + at_dep = (rd or 0) >= (ra or 0) + ends[("departure from " if at_dep else "arrival at ") + + ("a hub" if (f["orig"] if at_dep else f["dest"]) in HUBS else "an outstation")] += 1 + return {k: np.array(v) for k, v in rows.items()}, ends + + +def share(table: dict, station: str, t: int) -> float | None: + a = table.get(station) + if a is None: + return None + near = np.abs(a[:, 0] - t) <= 60 + return float(a[near, 1].mean()) if near.sum() >= 3 else None + + +def main(): + rep = Path(sys.argv[1]).expanduser() + scenario = sys.argv[sys.argv.index("--scenario") + 1] if "--scenario" in sys.argv else "desks" + mode = "now" if "--current" in sys.argv else "real" + rows, ends = peers(rep, scenario, mode) + print(f"other carriers' cancellation share at the flight's worse end, within an hour ({rep.name}, {scenario}, " + f"{'current' if mode == 'now' else 'realistic'} mode)") + print(f" {'':42} {'flights':>7} {'median':>7} {'mean':>6} {'20%+':>5} {'<5%':>5}") + for k in (FLEW, EASY, HARD): + v = rows[k] + print(f" {k:42} {len(v):7} {np.median(v):7.1%} {v.mean():6.1%} {(v >= 0.2).mean():5.0%} " + f"{(v < 0.05).mean():5.0%}") + print(f" the world's easy flights where the other carriers cancelled 20%+, by worse end: {dict(ends)}") + + +if __name__ == "__main__": + main() diff --git a/scripts/realistic_report.py b/scripts/realistic_report.py new file mode 100644 index 0000000..0fe5b42 --- /dev/null +++ b/scripts/realistic_report.py @@ -0,0 +1,281 @@ +"""The realism report over realistic_compare.py's output: per mode (current, realistic) the fidelity of JetBlue's +replay against JetBlue's recorded day, the outcome counts of every scenario, the diagnostics, and the realistic reward. + +Usage: python3 -I scripts/realistic_report.py REALREP_DIR [--late 15] [--bestj BESTJ_DIR] +""" + +import gzip +import json +import math +import sys +from pathlib import Path + +rep = Path(sys.argv[1]) +LATE = int(sys.argv[sys.argv.index("--late") + 1]) if "--late" in sys.argv else 15 +BESTJ = Path(sys.argv[sys.argv.index("--bestj") + 1]) if "--bestj" in sys.argv else None +DATES = Path(__file__).resolve().parents[1] / "data" / "b6-v3" / "dates" +LEADS = ("jbsim@120", "jbsim@360", "jbsim@start", "jbtails@360", "jbfull@360") +MODES = (("now", "current"), ("real", "realistic")) +ROWS = ("recorded", "jbsim", "desks", "model", "best") +JB = "jbfull@360" +J_ROWS = ("desks", "model", "best", "cut@2h", "cut@4h", "cut@6h", "best J") +USD = 1.07 +LOST = 50e6 / (4100 * 7_333_000 / 68_152 * 167.69) +KEPT = 35 / 50 + + +def score(cost, anchor, k=2.0): + return 0.2 + 0.8 / (1 + 5 / 3 * (max(cost, 0) / anchor) ** k) + + +def take_of(days): + """The share of the fare JetBlue loses on a passenger stranded to a later day, such that on JetBlue's replay over + `days` (the refunded at the full fare) it loses LOST of the fares booked on its cancelled flights: JetBlue's Q1 + 2014 storms, 4,100 cancellations, $50M revenue, 107.6 passengers a departure at a $167.69 average fare; each + itinerary's fare by its distance (realistic.od_fare).""" + num = den = 0.0 + for _, row in days: + a = row["real"][JB]["airline"] + num += LOST * a["cancelled_rev"] - a["refunded_rev"] + den += a["stranded_rev"] + return num / den + + +def save_of(days): + """The share of FAA's fuel and maintenance rate a cancellation saves, such that on JetBlue's replay over `days` it + saves 1 - KEPT of the revenue it loses: JetBlue's Q1 2014 storms cost $50M of revenue and $35M of operating + income.""" + rev = sum(LOST * row["real"][JB]["airline"]["cancelled_rev"] for _, row in days) + return (1 - KEPT) * rev / sum(row["real"][JB]["airline"]["saved"] for _, row in days) + + +def jetblue(a, take, save): + return a["aircraft"] + (1 - save) * a["saved"] + a["crew"] + a["tomorrow"] + lost(a, take) + + +def lost(a, take): + return a["refunded_rev"] + take * a["stranded_rev"] + + +def pearson(xs, ys): + n = len(xs) + mx, my = sum(xs) / n, sum(ys) / n + sxy = sum((x - mx) * (y - my) for x, y in zip(xs, ys, strict=True)) + sx, sy = math.sqrt(sum((x - mx) ** 2 for x in xs)), math.sqrt(sum((y - my) ** 2 for y in ys)) + return sxy / (sx * sy) if sx and sy else float("nan") + + +def recorded(f): + a = f["actual"] + return None if a["cancelled"] or a["diverted"] or a["arr"] is None else a["arr"] + + +def simmed(x): + return x["arr"] if x["status"] == "flown" else None + + +def outcome(sched, out): + c = {"cancelled": 0, "late15": 0, "late60": 0, "late180": 0, "minutes": 0, "flown": 0} + for i, f in sched.items(): + if i not in out: + continue + a = out[i] + if a is None: + c["cancelled"] += 1 + continue + d = max(0, a - f["sched_arr"]) + c["flown"] += 1 + c["minutes"] += d + c["late15"] += d >= LATE + c["late60"] += d >= 60 + c["late180"] += d >= 180 + return c + + +def fidelity(days, mode, lead): + pairs, auto, flew = [], 0, 0 + rec15 = sim15 = rec60 = sim60 = 0 + for sched, row in days: + js = row[mode][lead]["flights"] + for i, f in sched.items(): + r = recorded(f) + if r is None: + continue + flew += 1 + s = simmed(js[str(i)]) + if s is None: + auto += 1 + continue + dr, ds = max(0, r - f["sched_arr"]), max(0, s - f["sched_arr"]) + pairs.append((dr, ds)) + rec15 += dr >= LATE + sim15 += ds >= LATE + rec60 += dr >= 60 + sim60 += ds >= 60 + xs, ys = zip(*pairs, strict=True) + return {"corr": pearson(xs, ys), "mae": sum(abs(a - b) for a, b in pairs) / len(pairs), + "late15": sim15 / rec15 - 1, "late60": sim60 / rec60 - 1, "unflown": auto / flew, "flew": flew} + + +def report_j(days, take, save): + print(f"\nJetBlue cost J (0.5 = JetBlue's replay, {JB}): aircraft and crew minutes (a cancellation saving " + f"{save:.2f} of FAA's fuel and maintenance), tomorrow at the aircraft's cost, lost revenue (refunded at the " + f"fare, stranded at {take:.2f} of it); no passenger time") + print(f" {'date':10} {'JB $M':>7} " + " ".join(f"{r:>8}" for r in J_ROWS)) + agg = {r: [] for r in J_ROWS} + for _, row in days: + real = row["real"] + anchor = jetblue(real[JB]["airline"], take, save) + cells = [] + for r in J_ROWS: + if r in real: + v = score(jetblue(real[r]["airline"], take, save), anchor) + agg[r].append(v) + cells.append(f"{v:8.3f}") + else: + cells.append(f"{'-':>8}") + print(f" {row['date']:10} {anchor * USD / 1e6:7.2f} " + " ".join(cells)) + print(f" {'mean':10} {'0.500':>7} " + " ".join(f"{sum(v) / len(v):8.3f}" if v else f"{'-':>8}" + for v in agg.values())) + print("\nrescaled JetBlue reward: 0.5 at JetBlue's replay, 1.0 at the cheapest plan known under J (any row)") + print(f" {'date':10} {'gap $M':>7} " + " ".join(f"{r:>8}" for r in J_ROWS) + f" {'JB':>8}") + agg = {r: [] for r in J_ROWS + (JB,)} + for _, row in days: + real = row["real"] + js = {r: jetblue(real[r]["airline"], take, save) for r in J_ROWS + (JB,) if r in real} + top, anchor = min(js.values()), js[JB] + cells = [] + for r in J_ROWS + (JB,): + if r in js: + v = score(js[r] - top, max(anchor - top, 1.0)) + agg[r].append(v) + cells.append(f"{v:8.3f}") + else: + cells.append(f"{'-':>8}") + print(f" {row['date']:10} {(anchor - top) * USD / 1e6:7.2f} " + " ".join(cells)) + print(f" {'mean':10} {'':>7} " + " ".join(f"{sum(v) / len(v):8.3f}" if v else f"{'-':>8}" for v in agg.values())) + print(f"\n per day, mean $k {'cancelled':>9} {'aircraft':>9} {'crew':>7} {'tomorrow':>9} {'lost rev':>9} " + f"{'total':>8}") + rec = sum(sum(1 for f in sched.values() if recorded(f) is None) for sched, _ in days) / len(days) + print(f" {'recorded':20} {rec:9.1f}") + for r in (JB,) + J_ROWS: + xs = [row["real"][r]["airline"] for _, row in days if r in row["real"]] + if not xs: + continue + n = len(xs) + canc = sum(sum(1 for x in row["real"][r]["flights"].values() if x["status"] != "flown") + for _, row in days if r in row["real"]) / n + parts = ([sum(a["aircraft"] + (1 - save) * a["saved"] for a in xs) / n] + + [sum(a[k] for a in xs) / n for k in ("crew", "tomorrow")] + [sum(lost(a, take) for a in xs) / n]) + print(f" {r:20} {canc:9.1f} " + " ".join(f"{v * USD / 1e3:9.0f}" for v in parts) + + f" {sum(parts) * USD / 1e3:8.0f}") + + +def report(name, days, take=None, save=None): + if not days: + return + print(f"\n{'=' * 100}\n{name}: {len(days)} days ({', '.join(r['date'] for _, r in days)})") + lead = {} + print("\nfidelity: JetBlue's cancellations replayed vs JetBlue's recorded day (flights JetBlue flew)") + print(f" {'mode':10} {'lead':12} {'corr':>6} {'MAE min':>8} {f'{LATE}+ late':>9} {'60+ late':>9} {'unflown':>8}") + for mode, label in MODES: + fits = {k: fidelity(days, mode, k) for k in LEADS} + lead[mode] = max(fits, key=lambda k: fits[k]["corr"]) + for k, x in fits.items(): + mark = " <- used" if k == lead[mode] else "" + print(f" {label:10} {k:12} {x['corr']:6.3f} {x['mae']:8.1f} {100 * x['late15']:+8.1f}% " + f"{100 * x['late60']:+8.1f}% {100 * x['unflown']:7.2f}%{mark}") + for metric, label in (("cancelled", "cancelled"), ("late15", f"{LATE}+ min late"), ("late60", "60+ min late"), + ("late180", "3 h+ late"), ("avg", "avg delay (min)")): + print(f"\n{label}") + print(f" {'date':10} " + " ".join(f"{m[:4] + ':' + r:>13}" for m, _ in MODES for r in ROWS)) + tot = {} + for sched, row in days: + cells = [] + for mode, _ in MODES: + outs = {"recorded": {i: recorded(f) for i, f in sched.items()}} + for r in ROWS[1:]: + key = lead[mode] if r == "jbsim" else r + if key in row[mode]: + outs[r] = {int(i): simmed(x) for i, x in row[mode][key]["flights"].items()} + for r in ROWS: + if r not in outs: + cells.append("-") + continue + c = outcome(sched, outs[r]) + t = tot.setdefault((mode, r), {k: 0 for k in c}) + for k, v in c.items(): + t[k] += v + cells.append(f"{c['minutes'] / c['flown']:.0f}" if metric == "avg" else str(c[metric])) + print(f" {row['date']:10} " + " ".join(f"{c:>13}" for c in cells)) + cells = [] + for mode, _ in MODES: + for r in ROWS: + t = tot.get((mode, r)) + cells.append("-" if t is None else f"{t['minutes'] / t['flown']:.0f}" if metric == "avg" + else str(t[metric])) + print(f" {'total':10} " + " ".join(f"{c:>13}" for c in cells)) + print("\ndiagnostic: of the flights JetBlue cancelled, the desks fly them ... (never a target)") + for mode, label in MODES: + n = under = cant = 0 + for sched, row in days: + for i, f in sched.items(): + if recorded(f) is not None: + continue + n += 1 + x = row[mode]["desks"]["flights"][str(i)] + if x["status"] != "flown": + cant += 1 + elif x["dep"] - f["sched_dep"] < 60: + under += 1 + print(f" {label:10} under 1 h late {under}/{n} ({100 * under / n:.0f}%), not at all {cant}") + for price, label in (("realistic", "cost P: the passenger model"), ("tdi", "TDI: 457 min per cancelled passenger")): + print(f"\nrealistic reward, {label} (0.5 = JetBlue's replay at {lead['real']}) | current reward") + print(f" {'date':10} {'JB replay':>11} {'desks':>11} {'model':>11} {'best':>11} | {'cur model':>9} " + f"{'cur desks':>9}") + agg = {"desks": [], "model": [], "best": []} + for _, row in days: + real = row["real"] + anchor = real[lead["real"]]["cost"][price] + best, b0 = row["refs"]["best"], row["refs"]["B0"] + + def current(c, best=best, b0=b0): + return 0.2 + 0.8 * math.exp(-max(0, c - best) / max(b0 - best, 0.1 * best) / 0.5) + + cells = [] + for r in ("desks", "model", "best"): + if r in real: + s = score(real[r]["cost"][price], anchor) + agg[r].append(s) + cells.append(f"{s:11.3f}") + else: + cells.append(f"{'-':>11}") + cm = f"{current(row['now']['model']['cost']['v3']):9.3f}" if "model" in row["now"] else f"{'-':>9}" + print(f" {row['date']:10} {anchor / 1e6:9.2f}M " + " ".join(cells) + + f" | {cm} {current(row['now']['desks']['cost']['v3']):9.3f}") + print(f" {'mean':10} {'0.500':>11} " + " ".join(f"{sum(v) / len(v):11.3f}" if v else f"{'-':>11}" + for v in agg.values())) + if take is not None and all("airline" in row["real"].get(JB, {}) for _, row in days): + report_j(days, take, save) + print("\nrealistic layers per day: dep-cap hours, outstation arr-cap hours, closures, aircraft broken") + for _, row in days: + x = row["realistic"] + print(f" {row['date']:10} {x['dep_caps']:>4} {x['arr_caps']:>5} {x['closures']:>3} {len(x['broken']):>3}") + + +days = [] +for p in sorted(rep.glob("*.json")): + row = json.load(open(p)) + sched = {f["id"]: f for f in json.load(gzip.open(DATES / f"{row['date']}.json.gz"))["task"]["flights"]} + if BESTJ and (b := BESTJ / p.name).exists(): + x = json.load(open(b)) + row["real"]["best J"] = {"airline": x["airline"], "flights": x["flights"]} + days.append((sched, row)) +stream = [d for d in days if "model" in d[1]["now"]] +take = take_of(stream) if all("saved" in r["real"].get(JB, {}).get("airline", {}) for _, r in stream) else None +save = save_of(stream) if take is not None else None +if take is not None: + print(f"lost revenue: LOST = {LOST:.3f} of the fare per passenger booked on a cancelled flight (JetBlue Q1 2014); " + f"stranded take {take:.3f}, saving {save:.3f} of FAA's rate, fitted on the stream days") +report("stream days (train)", stream, take, save) +report("held-out days (eval)", [d for d in days if d[1]["split"] == "eval"], take, save) diff --git a/src/agentenv_airops/bundles/airops-occ3/artifacts/airops-occ3-verifier/verify.py b/src/agentenv_airops/bundles/airops-occ3/artifacts/airops-occ3-verifier/verify.py index b4232bb..8c38dd9 100644 --- a/src/agentenv_airops/bundles/airops-occ3/artifacts/airops-occ3-verifier/verify.py +++ b/src/agentenv_airops/bundles/airops-occ3/artifacts/airops-occ3-verifier/verify.py @@ -1,6 +1,7 @@ """Score an ops control center day with passengers with the reward of its plan on the truth under cost P; a day that never finished, failed its audit (news, turns, causality, roles, crew and passenger legality, the bound, each desk's -presence) or has no reward raises, so the run is not scored.""" +presence) or has no reward raises, so the run is not scored. A realistic day's grade names its own criterion (JetBlue's +own cost) and has no lower bound.""" import json @@ -13,7 +14,8 @@ async def verify(mcp_url: str) -> list[dict]: raise RuntimeError("the day can't be scored: " + json.dumps( {"done": day["done"], "problems": day["audit"]["problems"], "reward": day["reward"]})) grade = day["grade"] - out = [{"criterion": "the day's reward on the truth under cost P, 1.0 at the best plan found in hindsight", + out = [{"criterion": grade.get("criterion", "the day's reward on the truth under cost P, 1.0 at the best plan " + "found in hindsight"), "result": grade["reward"] >= 1.0, "score": grade["reward"], "episode": day}] if grade["reward_L"] is not None: out.append({"criterion": "the same reward against the capacity-aware lower bound", diff --git a/src/agentenv_airops/occ/cli3.py b/src/agentenv_airops/occ/cli3.py index bb66863..dedcea9 100644 --- a/src/agentenv_airops/occ/cli3.py +++ b/src/agentenv_airops/occ/cli3.py @@ -61,11 +61,13 @@ def setup(build_platform: str | None, source: Path | None): @click.option("--cap-one", type=float, default=CAPS["one"], show_default=True, help="USD for one agent.") @click.option("--noplay", is_flag=True, help=f"Also write {NOPLAY}: the first day with no desk.") @click.option("--policy", type=click.Choice(("llm", "script")), help="The desks' policy (default: llm).") -def tasks(split, task_ids, modes, pack, out, cap_joint, cap_one, noplay, policy): +@click.option("--realistic", is_flag=True, help="The days of b6-v3r in the realistic world under JetBlue's own cost " + "(REALISTIC.md), each task --real.") +def tasks(split, task_ids, modes, pack, out, cap_joint, cap_one, noplay, policy, realistic): """Write a bundle: per day, its three-desk and its one-agent task.""" - out = out or Path("results/bundles") / "airops-occ3" + out = out or Path("results/bundles") / ("airops-occ3-real" if realistic else "airops-occ3") names = generate(out, task_ids=list(task_ids) or None, split=split, modes=modes or ("joint", "one"), pack=pack, - caps={"joint": cap_joint, "one": cap_one}, noplay=noplay, policy=policy) + caps={"joint": cap_joint, "one": cap_one}, noplay=noplay, policy=policy, realistic=realistic) if not names: raise click.UsageError(f"no {split} days in the pack") click.echo(f"Wrote {len(names)} tasks into {out}; play one with: agent-env run {out} --task {names[0]} " diff --git a/src/agentenv_airops/occ/day.py b/src/agentenv_airops/occ/day.py index e75698f..53d4289 100644 --- a/src/agentenv_airops/occ/day.py +++ b/src/agentenv_airops/occ/day.py @@ -109,7 +109,7 @@ def __init__(self, world: World, mode: str = "joint"): if mode not in self.MODES: raise ValueError(f"mode must be one of {', '.join(self.MODES)}") self.w, self.task, self.mode = world, world.task, mode - self.world = sim.truth(self.task) + self.world = self.truth_world() self.known: dict[int, sim.World] = {} self.units = {u.id: u for u in world.units} self.times = occ.watches(self.task) @@ -161,11 +161,21 @@ def line(self) -> int | None: def max_turns(self) -> int: return self.TURNS[self.mode] * len(self.times) + EXTRA_TURNS + def truth_world(self) -> sim.World: + return sim.truth(self.task) + + def known_world(self, t: int) -> sim.World: + return sim.known(self.task, t) + def known_at(self, t: int) -> sim.World: if t not in self.known: - self.known[t] = sim.known(self.task, t) + self.known[t] = self.known_world(t) return self.known[t] + def costs(self, day: crewsim.CrewDay) -> dict[str, float]: + """The day's cost by part, as the desks read it (estimates, reviews) and the grade reports it.""" + return costs(day) + def open_flights(self, t: int) -> list[int]: return [i for i in self.task.by_id if run.opened(self.truth, i, t) and not self.plan.get(i, {}).get("cancel")] @@ -197,7 +207,7 @@ def _advance(self, via: str) -> int | None: def _close(self) -> None: if self.desks: - self.estimates.append({"watch": self.k, "t": self.t, **costs(self.project(self.plan).day)}) + self.estimates.append({"watch": self.k, "t": self.t, **self.costs(self.project(self.plan).day)}) self.committed |= run.commits(self.task, self.truth, self.plan, self.t, self.known_at(self.t)) def _open(self, by: str) -> None: @@ -597,7 +607,7 @@ def review(self, plan: dict) -> dict: cancellations, every flight it moves by more than WARN minutes or cancels (warnings), the flights it moves or crews otherwise (by impact) and the crew flags it changes.""" now, then = self.project(self.plan), self.project(plan) - a, b = costs(now.day), costs(then.day) + a, b = self.costs(now.day), self.costs(then.day) moved, warnings = [], [] for i in sorted(then.day.run.flights): x, y = now.day.run.flights[i], then.day.run.flights[i] @@ -849,7 +859,7 @@ def _finish(self) -> None: "avoidable_share": None if b0 <= best else round((b0 - c) / (b0 - best), 4), "above_script": None if script is None or script <= best else round((script - c) / (script - best), 4), - "script": script, "below_best": c < best - 0.5, "costs": costs(day), "counts": self.counts(), + "script": script, "below_best": c < best - 0.5, "costs": self.costs(day), "counts": self.counts(), "executed": day.run.rows()} def counts(self) -> dict: diff --git a/src/agentenv_airops/occ/day3.py b/src/agentenv_airops/occ/day3.py index 13a863a..75a239b 100644 --- a/src/agentenv_airops/occ/day3.py +++ b/src/agentenv_airops/occ/day3.py @@ -39,14 +39,7 @@ def __init__(self, world: World, mode: str = "joint"): super().__init__(world, mode) def compose(self, desk: str, proj, flags) -> str: - parts = [pages.header(self, desk)] - if desk in (OPS, ONE): - parts.append(pages.cost_line(proj, "ops")) - if desk in (CREW, ONE): - parts.append(pages.cost_line(proj, "crew")) - if desk in (CUSTOMER, ONE): - parts.append(customer.cost_line(proj)) - parts.append(pages.messages(self, desk)) + parts = [pages.header(self, desk), *self.cost_lines(desk, proj), pages.messages(self, desk)] if desk in (OPS, ONE): parts += [pages.attention(self, proj, flags), pages.programs(self, proj), pages.spares(self, proj)] if desk in (CREW, ONE): @@ -55,6 +48,16 @@ def compose(self, desk: str, proj, flags) -> str: parts += customer.page(self, proj, flags) return "\n\n".join(p for p in parts if p) + def cost_lines(self, desk: str, proj) -> list[str]: + out = [] + if desk in (OPS, ONE): + out.append(pages.cost_line(proj, "ops")) + if desk in (CREW, ONE): + out.append(pages.cost_line(proj, "crew")) + if desk in (CUSTOMER, ONE): + out.append(customer.cost_line(proj)) + return out + # ------------------------------------------------------------------ requests def supersedes(self, desk: str, r: dict) -> bool: diff --git a/src/agentenv_airops/occ/day3r.py b/src/agentenv_airops/occ/day3r.py new file mode 100644 index 0000000..c3015fd --- /dev/null +++ b/src/agentenv_airops/occ/day3r.py @@ -0,0 +1,82 @@ +"""One day of the ops control center with passengers in the realistic world (airops-occ3, realistic): v3's day (day3.py) +on a b6-v3r world, graded on JetBlue's own cost J. + +The truth is the realistic one (realworld.truth: v4's caps and closures in the task, at most so many JetBlue aircraft +on the ground at a station, the day's tails that break, aircraft swaps only at the hubs), and the day as known at t is +realworld.known (the tails broken by t, the gates and the swaps rule as they are). The desks' cost lines, estimates +and reviews price J on the projection in dollars: the aircraft's minutes with SAVE of what cancellations save at FAA's +rates, the crews' deadheads, hotels and end of day, tomorrow's first departures at the aircraft's own cost, and the +revenue lost (the refunded's fares, TAKE of the stranded's). The grade's reward is J's, 0.5 at JetBlue's own replay +and 1.0 at the cheapest plan the realism report found (realworld.reward); cost P's figures stay in the grade under +`P`, and the passengers still fly, rebook and are stranded or refunded as in v3.""" + +from collections import Counter + +from .. import realworld +from ..core.sim import AUTO_CANCELLED, CANCELLED +from ..programs import crewsim, pax +from .day import CREW, ONE, OPS +from .day3 import CUSTOMER, OccDay3 +from .pack import World + +CRITERION = ("the day's reward on the realistic truth under JetBlue's own cost, 0.5 at JetBlue's replay and 1.0 at " + "the cheapest plan found in hindsight") + + +class OccDay3R(OccDay3): + + def __init__(self, world: World, mode: str = "joint"): + if world.real is None: + raise ValueError("a realistic day needs a b6-v3r world") + super().__init__(world, mode) + + def truth_world(self): + return realworld.truth(self.task, self.w.real["gates"], self.w.real["broken"]) + + def known_world(self, t: int): + return realworld.known(self.task, t, self.w.real["gates"], self.w.real["broken"]) + + def airline(self, day: crewsim.CrewDay) -> dict: + r = self.w.real + return realworld.airline(self.task, day, r["firsts"], self.w.passengers, r["fares"]) + + def costs(self, day: crewsim.CrewDay) -> dict[str, float]: + """J by part in dollars.""" + r, a = self.w.real, self.airline(day) + usd = r["usd_per_pax_min"] + parts = {"aircraft": a["aircraft"] + (1 - r["save"]) * a["saved"], "crew": a["crew"], + "tomorrow": a["tomorrow"], "refunds": a["refunded_rev"], "stranded": r["take"] * a["stranded_rev"]} + return {"total": round(usd * sum(parts.values())), **{k: round(usd * v) for k, v in parts.items()}} + + def cost_lines(self, desk: str, proj) -> list[str]: + c = self.costs(proj.day) + statuses = Counter(x.status for x in proj.day.run.flights.values()) + tail = f"; {statuses[CANCELLED]} cancelled, {statuses[AUTO_CANCELLED]} auto-cancel" + out = [] + if desk in (OPS, ONE): + out.append(f"JetBlue's cost of the plan as projected: ${c['total']:,}: aircraft ${c['aircraft']:,}, crew " + f"${c['crew']:,}, tomorrow ${c['tomorrow']:,}, revenue lost ${c['refunds'] + c['stranded']:,}" + f"{tail}.") + if desk in (CREW, ONE): + crew, usd = proj.day.cost, self.w.real["usd_per_pax_min"] + out.append(f"Crew cost of the day as projected: deadhead ${usd * crew['deadhead']:,.0f}, hotel " + f"${usd * crew['hotel']:,.0f}, end of day ${usd * crew['end']:,.0f}{tail}.") + if desk in (CUSTOMER, ONE): + d = proj.day.run.pax + o = d.outcomes() + out.append(f"Passengers as projected: of {sum(o.values()):,} booked today {d.misconnected():,} misconnect, " + f"{o[pax.REBOOKED]:,} rebooked the same day, {o[pax.STRANDED]:,} on a later day (revenue lost " + f"${c['stranded']:,}), {o[pax.REFUNDED]:,} refunded (${c['refunds']:,}).") + return out + + def _finish(self) -> None: + super()._finish() + r, g = self.w.real, self.grade + refs = r["refs"] + a = self.airline(self.truth) + c = realworld.jetblue(a, r["take"], r["save"]) + g["P"] = {"cost": g["cost"], "reward": g["reward"], "reward_L": g["reward_L"], "best": g["best"]} + g.update(cost=round(c, 1), reward=round(realworld.reward(c, refs["top"], refs["jetblue"]), 6), reward_L=None, + best=refs["top"], anchor=refs["jetblue"], below_best=c < refs["top"] - 0.5, criterion=CRITERION, + realistic={"airline": a, "take": r["take"], "save": r["save"], "refs": refs, + "usd": round(c * r["usd_per_pax_min"])}) diff --git a/src/agentenv_airops/occ/live3.py b/src/agentenv_airops/occ/live3.py index 260ea6c..ab68411 100644 --- a/src/agentenv_airops/occ/live3.py +++ b/src/agentenv_airops/occ/live3.py @@ -1,7 +1,8 @@ """The ops control center with passengers live (airops-occ3): v2's env (live.py) on a b6-v3 day, every day costing P, with a third desk in the role customer. The customer desk reads its page with wait_turn, looks passengers up with cust_flights and cust_flight, and asks ops with cust_request; ops answers with ops_reply and texts the customer desk -with ops_request. Rebooking stays automatic: no tool rebooks.""" +with ops_request. Rebooking stays automatic: no tool rebooks. Loaded `realistic`, the day is b6-v3r's in the realistic +world, graded on JetBlue's own cost (day3r.py).""" from typing import Annotated, Literal @@ -10,8 +11,9 @@ from pydantic import Field from .day3 import OccDay3 +from .day3r import OccDay3R from .live import LOAD_URI, Flight, OccEnv, Status, Text, Time -from .pack import Pack3 +from .pack import Pack3, Pack3R ENV3 = "airops-occ3" Note = Annotated[str | None, Field(description="at most 400 characters, or null")] @@ -67,6 +69,9 @@ async def cust_request(self, ctx: ToolContext, type: Literal["HOLD", "PROTECT", # ------------------------------------------------------------------ the env's own @extension(LOAD_URI, description="Start a day on the task with this id, played by three desks (joint) or one agent " - "at all three (one).") - def occ_load(self, task_id: str, mode: str = "joint", turn_timeout_s: float = 1800.0) -> dict: - return super().occ_load(task_id, mode, turn_timeout_s) + "at all three (one); realistic, in the realistic world under JetBlue's own cost.") + def occ_load(self, task_id: str, mode: str = "joint", turn_timeout_s: float = 1800.0, + realistic: bool = False) -> dict: + if realistic and not self.loaded: + self.pack, self.DAY = Pack3R(), OccDay3R + return super().occ_load(task_id, mode, turn_timeout_s) | {"realistic": realistic} diff --git a/src/agentenv_airops/occ/pack.py b/src/agentenv_airops/occ/pack.py index c55ff04..cc2de3e 100644 --- a/src/agentenv_airops/occ/pack.py +++ b/src/agentenv_airops/occ/pack.py @@ -3,7 +3,11 @@ the grade reads. The env never rebuilds units, so a day starts alike on any machine. b6-v3 (`Pack3`) adds each date's passengers and its references under cost P; its world carries the passengers and reads -those references and the best plan under P in v2's places.""" +those references and the best plan under P in v2's places. + +b6-v3r (`Pack3R`, scripts/build_realistic_pack.py) is b6-v3's days the realism report priced, in the realistic world: +each task layered, its world's `real` the day's breakdowns, tomorrow's first departures, its groups' fares and the +references of JetBlue's cost J, with the manifest's gates, breakdown rate and J's shares.""" import functools import gzip @@ -23,6 +27,7 @@ PACK = files("agentenv_airops") / "data" / "b6-v2" PACK3 = files("agentenv_airops") / "data" / "b6-v3" +PACK3R = files("agentenv_airops") / "data" / "b6-v3r" @dataclass(frozen=True) @@ -35,6 +40,7 @@ class World: best_plan: dict local: Callable[[str, int], int] = field(compare=False, repr=False) passengers: Passengers | None = field(default=None, compare=False, repr=False) + real: dict | None = field(default=None, compare=False, repr=False) @classmethod def from_dict(cls, d: dict) -> "World": @@ -104,3 +110,21 @@ def world(self, task_id: str) -> World: raise ValueError(f"unknown task id {task_id!r}") raw = gzip.decompress((self.path / "dates" / f"{self.tasks[task_id]['date']}.json.gz").read_bytes()) return World.from_v3(json.loads(raw)) + + +class Pack3R(Pack3): + """A b6-v3r pack folder: b6-v3's priced days in the realistic world.""" + + def __init__(self, path: Path | str | None = None): + Pack.__init__(self, path or os.environ.get("AIROPS_OCC3R_PACK") or PACK3R) + + def world(self, task_id: str) -> World: + if task_id not in self.tasks: + raise ValueError(f"unknown task id {task_id!r}") + d = json.loads(gzip.decompress((self.path / "dates" / f"{self.tasks[task_id]['date']}.json.gz").read_bytes())) + r = d["realistic"] + real = {**self.manifest["realistic"], "broken": r["broken"], "refs": r["refs"], + "firsts": {(fam, s): list(m) for fam, s, m in r["firsts"]}, + "fares": {int(k): v for k, v in r["fares"].items()}, + "best_plan": {int(k) if k.isdigit() else k: v for k, v in r["best_plan"].items()}} + return replace(World.from_v3(d), real=real) diff --git a/src/agentenv_airops/occ/sweep.py b/src/agentenv_airops/occ/sweep.py index 67a46d4..d4d44db 100644 --- a/src/agentenv_airops/occ/sweep.py +++ b/src/agentenv_airops/occ/sweep.py @@ -25,7 +25,8 @@ from . import tasks3 from .day import OccDay from .day3 import OccDay3 -from .pack import Pack, Pack3 +from .day3r import OccDay3R +from .pack import Pack, Pack3, Pack3R from .replay import replay from .tasks import DESKS, generate @@ -57,7 +58,12 @@ def generate3(out: Path, cap: float | None = None, **kw) -> list[str]: KIT = Kit(DESKS, generate, VERIFIER, Pack, OccDay) +def generate3r(out: Path, cap: float | None = None, **kw) -> list[str]: + return generate3(out, cap, realistic=True, suffix="", **kw) + + KIT3 = Kit(tasks3.DESKS, generate3, "airops-occ3", Pack3, OccDay3) +KIT3R = Kit(tasks3.DESKS, generate3r, "airops-occ3", Pack3R, OccDay3R) @dataclass(frozen=True) @@ -95,6 +101,15 @@ def kit(self) -> Kit: return KIT3 +@dataclass(frozen=True) +class Pilot3R(Pilot): + """A pilot of airops-occ3's realistic days (b6-v3r), each run - loading the realistic world.""" + + @property + def kit(self) -> Kit: + return KIT3R + + def results(out: Path) -> list[dict]: path = out / "results.jsonl" return [json.loads(line) for line in path.read_text().splitlines() if line.strip()] if path.is_file() else [] diff --git a/src/agentenv_airops/occ/tasks3.py b/src/agentenv_airops/occ/tasks3.py index 5f4628a..c96a58e 100644 --- a/src/agentenv_airops/occ/tasks3.py +++ b/src/agentenv_airops/occ/tasks3.py @@ -10,7 +10,7 @@ from .day3 import TURNS from .live import END_DAY_URI, LOAD_URI, NOTICE_TOOL from .live3 import ENV3 -from .pack import Pack3 +from .pack import Pack3, Pack3R from .tasks import ( AGENT, BARRIER_SECONDS, @@ -160,6 +160,42 @@ {COST}""", } +DAY_R = """The day: JetBlue's whole network on a real storm day, every flight and aircraft, in a world as close to the \ +real one as public records allow: the FAA's ground stops and ground delay programs at JFK and Boston as issued; each \ +airport's hourly departure room and each outstation's hourly arrival room, cut by weather and congestion as they \ +were, and at JFK, LaGuardia, Newark, Washington National and Boston no more room in an hour than the other airlines \ +left; \ +ramp closures at the hubs in lightning, snow or freezing rain; at most so many JetBlue aircraft on the ground at a \ +station at once (a flight into a full station waits at its origin); and aircraft that break during the day, out for \ +the rest of it. Watches every hour from 04:00; your turn opens with your page, the day as projected now on what is \ +known; act through your tools, then end_turn, whose result is your next page. A flight's own delay becomes known 2 \ +hours before its scheduled departure ("?" until then); FAA advisories arrive as issued; an airport's hourly room as \ +it stands now, which the projection assumes holds for the next 4 hours; a closure once it begins, a breakdown when it \ +happens. The env flies the plan: a flight leaves at the \ +earliest its delay, its hold, an aircraft at its origin (at JFK and Boston any aircraft of its type; elsewhere the \ +aircraft that flew in on JetBlue's own day, while it is there), its FAA slot, the hourly room, a gate at its \ +destination and its crews allow; one that can't leave by its deadline is cancelled automatically, late. Pilots fly \ +under FAR 117 (duty limits by report time and legs, a 2 h extension with the captain's agreement, 10 h rest), flight \ +attendants under 14 CFR 121.467 (14 h scheduled duty, 10 h rest). The crew system crews every flight the crew desk \ +leaves alone by standard practice (its own crew, else a reserve, a swap, a deadhead, the extension) and can't cancel: \ +where it is stuck the flight waits for a crew or its deadline. Crew flags, pilot/cabin: legal; "times out HH:MM" (its \ +crew's last legal departure is near); "crew HH:MM" (it waits for its crew until then); "no crew" (no crew can reach \ +it: it will cancel automatically unless something changes). Times are local, "+1" after midnight; flights are \ +f.""" + +COST_R = """Cost: JetBlue's own, in dollars, from its own and the FAA's figures: the aircraft's time (crew and fixed \ +costs for each minute a flight leaves late, its whole cost for each minute over its scheduled block, less part of the \ +fuel and maintenance a cancelled flight saves); crew deadheads, hotels away from base and crews out of place for \ +tomorrow; tomorrow's first departures (the ferries to cover them and the aircraft's cost for each minute one leaves \ +late); and the revenue lost: each refunded passenger's fare and most of the fare of each passenger stranded to a later \ +day (a passenger rebooked today costs only the seat). Passengers' own lost time is not JetBlue's cost. The day is \ +graded once, on everything as it really happened: 0.5 is JetBlue's own decisions that day replayed, 1.0 the cheapest \ +plan found in hindsight. Decisions are taken now: none reaches back before this watch.""" + +RULES_R = {role: text.replace(DAY, DAY_R).replace(COST, COST_R).replace( + "Keep the day's cost as low as you can.", "Keep JetBlue's cost of the day as low as you can.") + for role, text in RULES.items()} + OPENING = {"ops": "You are the ops desk on {date}. Your first page follows.", "crew": "You are the crew desk on {date}. Your first page follows.", "customer": "You are the customer desk on {date}. Your first page follows.", @@ -174,12 +210,14 @@ def triggers(task) -> list[dict]: def steps(task, mode: str, caps: dict | None = None, env_vars: dict | None = None, turn_timeout: float | None = None, - policy: str | None = None) -> list[dict]: + policy: str | None = None, realistic: bool = False) -> list[dict]: """A day's steps: three desks (joint), one agent at all three (one), or none (idle: the crew and passenger systems - alone).""" + alone); `realistic`, the day in the realistic world under JetBlue's own cost.""" desks = DESKS.get(mode, []) caps = CAPS if caps is None else caps - load = {"task_id": task.task_id, "mode": mode} | ({"turn_timeout_s": turn_timeout} if turn_timeout else {}) + rules, real = (RULES_R, "-real") if realistic else (RULES, "") + load = {"task_id": task.task_id, "mode": mode} | ({"turn_timeout_s": turn_timeout} if turn_timeout else {}) | \ + ({"realistic": True} if realistic else {}) out = [{"id": "deploy", "type": "deploy_env", "env_id": ENV3}, {"id": "load-task", "type": "apply_server_config", "env_id": ENV3, "directives": [{"service": ENV3, "uri": LOAD_URI, "args": load}]}] @@ -203,7 +241,7 @@ def steps(task, mode: str, caps: dict | None = None, env_vars: dict | None = Non for role, name, _ in desks: plays.append(f"play-{name}") out.append({"id": f"play-{name}", "type": "prompt_agent", "agent_name": name, - "prompt_id": f"{task.task_id}-{mode}-{role}", "system_prompt": RULES[role], + "prompt_id": f"{task.task_id}-{mode}{real}-{role}", "system_prompt": rules[role], "prompt": OPENING[role].format(date=task.date), "max_turns": TURNS[mode] * watches + EXTRA_TURNS, "model_params": MODEL_PARAMS, "effort": EFFORT, "timeout_seconds": PLAY_SECONDS, "depends_on": ["clock"], "fail_task_on_error": False}) @@ -241,10 +279,12 @@ def readme() -> str: def generate(out: Path, *, task_ids: list[str] | None = None, split: str = "eval", modes: tuple[str, ...] = ( "joint", "one"), pack: Path | None = None, caps: dict | None = None, noplay: bool = False, env_vars: dict | None = None, turn_timeout: float | None = None, policy: str | None = None, - suffix: str = "") -> list[str]: + suffix: str | None = None, realistic: bool = False) -> list[str]: """Writes the bundle into `out`: README.md, the verifier and tasks/-.json for `task_ids`, else every - task of `split`, in date order; with `noplay`, the first one with no desk too.""" - p = Pack3(pack) + task of `split`, in date order; with `noplay`, the first one with no desk too; `realistic`, b6-v3r's days in the + realistic world under JetBlue's own cost, each --real.json unless `suffix` names the ending.""" + p = Pack3R(pack) if realistic else Pack3(pack) + suffix = ("-real" if realistic else "") if suffix is None else suffix chosen = [t for t, v in sorted(p.tasks.items(), key=lambda kv: kv[1]["date"]) if (t in task_ids if task_ids is not None else split in ("all", v["split"]))] _write(out / "README.md", readme().encode()) @@ -254,9 +294,10 @@ def generate(out: Path, *, task_ids: list[str] | None = None, split: str = "eval task = p.world(task_id).task for mode in modes: name = f"{task_id}-{mode}{suffix}" - _write(out / "tasks" / f"{name}.json", _dump(steps(task, mode, caps, env_vars, turn_timeout, policy))) + _write(out / "tasks" / f"{name}.json", _dump(steps(task, mode, caps, env_vars, turn_timeout, policy, + realistic))) names.append(name) if noplay and task_id == chosen[0]: - _write(out / "tasks" / f"{NOPLAY}.json", _dump(steps(task, "idle"))) + _write(out / "tasks" / f"{NOPLAY}.json", _dump(steps(task, "idle", realistic=realistic))) names.append(NOPLAY) return names diff --git a/src/agentenv_airops/programs/candidates.py b/src/agentenv_airops/programs/candidates.py index c312578..28ac53c 100644 --- a/src/agentenv_airops/programs/candidates.py +++ b/src/agentenv_airops/programs/candidates.py @@ -52,6 +52,7 @@ from ..core.model import Airport from . import advisories, rbs, repairs, unseen from .advisories import HUBS, Run, Stop +from .layers.crew import EDGAR, Rule from .model import Arrival, Flight, Programs, Task, coverage from .rbs import Covers @@ -309,9 +310,11 @@ def airports_of(codes: Iterable[str]) -> dict[str, Airport]: def build_candidate(d: str, hub: str, inputs: Path, derived: Path, e_fit: Mapping[str, float], e_median: Mapping[str, float], scale: Mapping[str, float], cutoff: int = repairs.CUTOFF, - window: tuple[int, int] | None = None, split: str = "", bases: Sequence[str] = ()) -> Built: + window: tuple[int, int] | None = None, split: str = "", bases: Sequence[str] = (), + crew: Rule = EDGAR) -> Built: """The task of (d, hub) at its rule window (or at `window`, as for a control), or None and the reasons; `e_fit` - and `e_median` are unseen.table's at `scale`; crew `bases` turn the crew model on (repairs.crews).""" + and `e_median` are unseen.table's at `scale`; crew `bases` turn the crew model on, its pairings cut by `crew` + (Edgar's, repairs.crews, or v4's, layers.crew).""" ctx = context(inputs, derived, d, hub) row = {"date": d, "hub": hub, "p0": None, "window": None, "slide": None, "task_id": None, "reason": "", "tried": []} @@ -331,7 +334,7 @@ def build_candidate(d: str, hub: str, inputs: Path, derived: Path, e_fit: Mappin if not sc.ok: row["tried"].append({"slide": slide, "window": list(w), "clauses": sc.clauses}) continue - task = assemble(ctx, sc, d, hub, e_fit, e_median, scale, cutoff, slide, split, bases) + task = assemble(ctx, sc, d, hub, e_fit, e_median, scale, cutoff, slide, split, bases, crew) if window or task.clauses["program_in_window"]: return Built(task, {**row, "window": list(w), "slide": slide, "task_id": task.task_id, "clauses": task.clauses}) @@ -349,9 +352,9 @@ def span(day: pack.Day, h: int) -> tuple[int, int] | None: def assemble(ctx: Context, sc: pack.Scope, d: str, hub: str, e_fit: Mapping[str, float], e_median: Mapping[str, float], scale: Mapping[str, float], cutoff: int, slide: int, split: str, - bases: Sequence[str] = ()) -> Task: + bases: Sequence[str] = (), crew: Rule = EDGAR) -> Task: day = ctx.day - flights = repairs.flights(day, sc, ctx.x, ctx.families, tests(ctx), cutoff=cutoff, bases=bases) + flights = repairs.flights(day, sc, ctx.x, ctx.families, tests(ctx), cutoff=cutoff, bases=bases, cut=crew.crews) tails, closure = repairs.tails(day, sc, flights, ctx.families, day.turn) programs = hub_programs(ctx, flights, sc.start, e_fit, e_median, scale) codes = ({f.orig for f in flights} | {f.dest for f in flights} | set(HUBS) diff --git a/src/agentenv_airops/programs/crewday.py b/src/agentenv_airops/programs/crewday.py index c14e6de..395d6ce 100644 --- a/src/agentenv_airops/programs/crewday.py +++ b/src/agentenv_airops/programs/crewday.py @@ -405,11 +405,14 @@ def columns_of(p: Problem, day: crewsim.CrewDay) -> dict[str, Column] | None: def named(day: crewsim.CrewDay) -> dict: """A crew sim day as a plan that names everything it did: each flight's minute or cancellation, its crews and - riders, and the reports.""" + riders, and the reports; a flight's `order`, the day's own among flights ready at one minute, so each takes the + aircraft it took when the aircraft aren't interchangeable (a realistic world's hub-only swaps).""" out: dict = {} + order = day.run.order for i, r in day.run.flights.items(): if r.dep is not None: - out[i] = {"not_before": r.dep, "crew": dict(day.crews.get(i, {}))} + out[i] = {"not_before": r.dep, "crew": dict(day.crews.get(i, {}))} | \ + ({"order": order[i]} if i in order else {}) if day.riders.get(i): out[i]["deadhead"] = list(day.riders[i]) elif r.status == "cancelled": diff --git a/src/agentenv_airops/programs/layers/__init__.py b/src/agentenv_airops/programs/layers/__init__.py new file mode 100644 index 0000000..8ccc7e6 --- /dev/null +++ b/src/agentenv_airops/programs/layers/__init__.py @@ -0,0 +1,12 @@ +"""v4's realism layers (model.Layers), each built for a task from pinned public inputs. + +A layer is a rule sim.execute applies and the hindsight MIP carries alike (docs/v4/DESIGN.md §1), so the anchor stays +a proven optimum. A builder returns its field of Layers; a task without layers is v2's world. +- depcaps: JetBlue's departure cap per hour at a hub, from every carrier's BTS pushbacks (§3.1). +- arrcaps: JetBlue's arrival cap per hour at an outstation, from every carrier's BTS arrivals (§3.3). +- closures: ramp closures at a hub from its METARs and SPECIs, a departure and an arrival cap over each (§3.2). +- weather: the hubs' METARs, SPECIs and TAFs as the desk's news (§4), each delivered after its kind's lag. +- config: the constants a pack fits, and every layer of a task at them (network.build's `layers`). +- calibrate: the constants fitted on a pack's train dates: closure rates and reserve pools (§3.4, §6.1). +- crew: v4's pairings legal on schedule, a build's crew Rule (docs/v4/CREW.md §1). +""" diff --git a/src/agentenv_airops/programs/layers/arrcaps.py b/src/agentenv_airops/programs/layers/arrcaps.py new file mode 100644 index 0000000..05e918f --- /dev/null +++ b/src/agentenv_airops/programs/layers/arrcaps.py @@ -0,0 +1,47 @@ +"""JetBlue's arrival cap per hour at its outstations, from what actually arrived there (docs/v4/DESIGN.md §3.3). + +v2 models the FAA's programs at JFK and BOS only, so a leg into an outstation under its own GDP, ground stop or weather +flies free. This is §3.1's rule on arrivals at every non-hub destination of the task's flights: an hour is constrained +when every carrier together arrived there (BTS gate arrival) less than `short` of what it had scheduled to arrive in it, +with at least `min_sched` scheduled. JetBlue's cap there is its in-scope share of the hour's schedule (the task's +flights scheduled to arrive at the station in it) times the hour's actual arrivals, rounded to the nearest flight, and +never below the task's flights into the station the airline replay lands in it. The replay lands a flight that pushed +back at its pushback plus its truth block, whatever its gate-in: a diversion flies to its scheduled destination, and a +gate-in held long after landing counts in the hour it landed. The floor keeps the recorded day's arrivals within the +caps; a replayed flight that a GDP slot or a late aircraft moves into a capped hour can still be held there, so the +recorded day is close to feasible, not feasible by construction (as with closures, §3.2). Other hours, and hours JetBlue +scheduled nothing in, stay uncapped. Hours are counted from the task's midnight, over the span of the hubs' nominal caps +(rbs.nominal): from the first watch's hour through the hour of the latest arrival a deadline allows. The caps join the +world's nominal arrival caps (sim.truth), so the sim and the MIP enforce them as they do a hub's. +""" + +import math +from collections import Counter + +import pandas as pd + +from ..advisories import HUBS +from ..model import Task + +SHORT = 0.9 +MIN_SCHED = 5 + + +def build(task: Task, frame: pd.DataFrame, short: float = SHORT, + min_sched: int = MIN_SCHED) -> dict[str, dict[int, int]]: + """The task's arrival caps per outstation and hour. `frame` holds every carrier's BTS flights of the task's dates + (candidates.frame: `Dest`, and `sched_arr` and `act_arr` in UTC epoch minutes, `act_arr` NaN when none).""" + lo, hi = task.start // 60, max(f.deadline + f.block for f in task.flights) // 60 + out: dict[str, dict[int, int]] = {} + for station in sorted({f.dest for f in task.flights} - set(HUBS)): + x = frame[frame.Dest == station] + sched = Counter(((x.sched_arr - task.base) // 60).astype(int)) + arrived = Counter(((x.act_arr.dropna() - task.base) // 60).astype(int)) + mine = Counter(f.sched_arr // 60 for f in task.flights if f.dest == station) + landed = Counter((f.actual.dep + f.block) // 60 for f in task.flights + if f.dest == station and f.actual.dep is not None) + caps = {h: max(landed[h], math.floor(mine[h] / sched[h] * arrived[h] + 0.5)) for h in range(lo, hi + 1) + if mine[h] and sched[h] >= min_sched and arrived[h] < short * sched[h]} + if caps: + out[station] = caps + return out diff --git a/src/agentenv_airops/programs/layers/calibrate.py b/src/agentenv_airops/programs/layers/calibrate.py new file mode 100644 index 0000000..45a0151 --- /dev/null +++ b/src/agentenv_airops/programs/layers/calibrate.py @@ -0,0 +1,210 @@ +"""v4's layer constants, fitted on a pack's train dates only (docs/v4/DESIGN.md §1, §6.1) and written to its manifest. + +- The ramp closures' rate per hub is closures.fit_rate over the train dates: every carrier's pushbacks inside the + closures that start on them over those in the same minutes just before (DECISIONS 2026-10-17, U3 Q2: refitted on + the pack's own split, not U3's scratch rates). +- The reserve pool per crew base (§3.4), as no crew data is public, is the smallest that keeps JetBlue's airline + replay within the airline-replay gate (world_check's and step 0's G1) on every train date: its auto-cancels among + the flights it flew by their deadline (references.counted) at most AUTO_SHARE of them and at most AUTO_MAX + (DECISIONS 2026-10-17, U6 Q2). A date whose replay is out of G1 even with unlimited reserves (the other layers' + doing) holds the pool to no more auto-cancels than that. On each date, with the other layers on, the replay calls a + reserve for each base departure its unlimited-reserve replay flies past its crew deadline + (references.airline_plan); per base, the smallest pool in 0 up to that date's calls there at which the replay, that + base pooled and the others unlimited, is kept so, and the base's pool is the largest of those over the dates. The + pools together are then replayed on every date; a date they don't keep (the bases' auto-cancels adding up past G1, + or one base's denial moving another's aircraft) lifts every base to its calls there, at which the replay is the + unlimited one, until every date is kept. +- The departure and outstation caps' clauses stay at depcaps' and arrcaps' SHORT and MIN_SCHED (0.9 and 5), not + fitted: the caps are floored at JetBlue's recorded pushbacks and arrivals, so any value fits the recorded day + (DECISIONS 2026-10-17, U6 Q1). +- v4's crew cut (layers.crew, docs/v4/CREW.md §1), when a pack fits it (`crew_fit`), comes before the pools: on a + seeded `sample` of at most SAMPLE train dates (SAMPLE_SEED), each date built at every Rule of the grid (BUFFERS x + SEGMENTS) with the other layers on at the fitted rates and unlimited reserves, and scored (`score`) by its naive day, + its recorded day, its anchor and its calm day at CREW_S, and so is each date built without crews, once (a score is + kept under the pack's `cache` by its task's signature, so a rerun solves only what changed). The cut is the one that + maximises do-nothing's median excess over the anchor on the sample while docs/v4/CREW.md §0's guards hold there + (`summary`): on every date whose calm day without crews is within JetBlue's fair-weather bar (layers.fairday, at the + pack's `rate`), the crew's calm day is within it too, so crews push no fair day past the cancellations JetBlue's own + fair days have (DECISIONS 2026-10-22); the recorded day costs no less than the anchor on at least RECORDED_SHARE of + the dates, and the anchors cancel (auto-cancels included) no more than JetBlue did, pooled; ties go to the larger + buffer, then the fewer segments (the laxer crew). A date counts where its MIPs proved (for the calm guard, its no-crew + build's too). None holds: no cut, and the pack stops. The pools are then fitted at that cut. +""" + +import hashlib +import json +import random +import statistics +from collections import Counter +from collections.abc import Callable, Iterable, Mapping, Sequence +from concurrent.futures import ProcessPoolExecutor +from contextlib import nullcontext +from dataclasses import replace +from itertools import product +from pathlib import Path + +import pandas as pd + +from .. import references +from ..model import Layers, Task +from . import closures, fairday +from .config import Config, build +from .crew import Rule + +AUTO_SHARE, AUTO_MAX = 0.01, 2 +BUFFERS, SEGMENTS = (0, 30, 60, 90, 120), (3, 4, 5, 6) +SAMPLE, SAMPLE_SEED, CREW_S = 12, 0, 600.0 +RECORDED_SHARE = 0.95 + + +def fit(dates: Iterable[str], tasks: Iterable[Task], frame_of: Callable[[str], pd.DataFrame], + spans: Mapping[str, Sequence[closures.Interval]], pushed: pd.DataFrame, workers: int = 1) -> Config: + """The constants fitted on the train `dates`: `tasks` their built tasks (crew on, no layers; others are ignored), + `frame_of` a date's every-carrier BTS flights (candidates.frame), `spans` each hub's closures, `pushed` every + carrier's BTS flights once each (`Origin`, `act_dep`) over the months the dates fall in.""" + return pooled_fit(rates(dates, pushed, spans), tasks, frame_of, spans, workers) + + +def rates(dates: Iterable[str], pushed: pd.DataFrame, spans: Mapping[str, Sequence[closures.Interval]]) -> Config: + """The closure rates fitted on the train `dates`, no pools.""" + dates = tuple(sorted(set(dates))) + return Config(rate=closures.fit_rate(dates, pushed, spans), dates=dates) + + +def pooled_fit(rated: Config, tasks: Iterable[Task], frame_of: Callable[[str], pd.DataFrame], + spans: Mapping[str, Sequence[closures.Interval]], workers: int = 1) -> Config: + """`rated` with the reserve pools fitted on its train dates' `tasks`, layered at its rates.""" + train = [replace(t, layers=build(t, rated, frame_of(t.date), spans)) for t in tasks if t.date in rated.dates] + return replace(rated, reserves=pools(train, workers)) + + +def sample(dates: Iterable[str], n: int = SAMPLE, seed: int = SAMPLE_SEED) -> tuple[str, ...]: + """At most n of the train `dates`, drawn with `seed`, in date order.""" + dates = sorted(set(dates)) + return tuple(sorted(random.Random(seed).sample(dates, min(n, len(dates))))) + + +def grid(buffers: Sequence[int] = BUFFERS, segments: Sequence[int] = SEGMENTS) -> list[Rule]: + return [Rule("v4", b, s) for b, s in product(buffers, segments)] + + +def crew_fit(tasks: Mapping[Rule, Sequence[Task]], plain: Sequence[Task], rate: float, workers: int = 1, + time_limit: float = CREW_S, cache: Path | None = None) -> tuple[Rule | None, list[dict]]: + """The cut of `tasks` (the sample's tasks built at each Rule, layered at the rates, unlimited reserves) that + maximises do-nothing's median excess over the anchor while the guards hold against `plain` (the sample built + without crews, alike), the calm days at the fair-weather bar `rate`, or None; and each Rule's row, then the + no-crew row.""" + jobs = [(rule, t) for rule, ts in tasks.items() for t in ts] + [(None, t) for t in plain] + args = [(t, time_limit, cache) for _, t in jobs] + with ProcessPoolExecutor(workers) if workers > 1 and len(jobs) > 1 else nullcontext() as pool: + scores = list(pool.map(kept_score, args) if pool else map(kept_score, args)) + by: dict[Rule | None, list[dict]] = {rule: [] for rule in tasks} | {None: []} + for (rule, _), s in zip(jobs, scores, strict=True): + by[rule].append(s) + base = {s["date"]: s for s in by.pop(None)} + rows = [summary(rule, s, base, rate) for rule, s in by.items()] + held = [(r, rule) for r, rule in zip(rows, by, strict=True) if r["guards"]] + rows.append(summary(None, list(base.values()), base, rate)) + if not held: + return None, rows + return max(held, key=lambda x: (x[0]["do_nothing"], x[1].buffer, -x[1].max_segments))[1], rows + + +def kept_score(job: tuple[Task, float, Path | None]) -> dict: + """`score`, read from the cache where the same task at the same time limit was scored, else written there.""" + task, time_limit, cache = job + if cache is None: + return score(task, time_limit) + sig = hashlib.sha256(f"{task.to_json()}|{time_limit}".encode()).hexdigest()[:16] + path = cache / f"{task.task_id}-{sig}.json" + try: + return json.loads(path.read_text()) + except (OSError, ValueError): + pass + out = score(task, time_limit) + cache.mkdir(parents=True, exist_ok=True) + path.with_suffix(".part").write_text(json.dumps(out)) + path.with_suffix(".part").replace(path) + return out + + +def score(task: Task, time_limit: float = CREW_S) -> dict: + """What the guards and the objective read of one task: the naive, recorded and anchor costs, the anchor's and + JetBlue's cancellations, the calm day's forced cancellations and ferries, the day's flights, and whether both + MIPs proved.""" + sol, run = references.anchor(task, time_limit) + calm = references.calm(task, time_limit) + return {"date": task.date, "naive": references.naive(task).cost.total, + "recorded": references.recorded(task)[0].total, "anchor": sol.objective, + "proven": sol.proven and calm.solution.proven, + "anchor_cancels": run.counts["cancelled"] + run.counts["auto_cancelled"], + "jetblue_cancels": sum(f.actual.dep is None for f in task.flights), "calm_cancels": calm.cancelled, + "calm_ferries": calm.ferries, "flights": len(task.flights)} + + +def summary(rule: Rule | None, scores: Sequence[dict], base: Mapping[str, dict], rate: float) -> dict: + """A Rule's row (None: the no-crew build's): do-nothing's median excess over the anchor and each guard on the + dates whose MIPs proved, the calm day against `base` (the no-crew scores by date) at the fair-weather bar + `rate`.""" + ok = [s for s in scores if s["proven"]] + paired = [(s, base[s["date"]]) for s in ok if s["date"] in base and base[s["date"]]["proven"]] + calm = share(calm_within(s, rate) or not calm_within(b, rate) for s, b in paired) + recorded = share(s["recorded"] >= s["anchor"] for s in ok) + cancels = sum(s["anchor_cancels"] for s in ok), sum(s["jetblue_cancels"] for s in ok) + checks = {"calm": calm == 1, "recorded": recorded is not None and recorded >= RECORDED_SHARE, + "cancels": cancels[0] <= cancels[1]} + head = rule.to_dict() if rule else {"crew": None, "buffer": None, "max_segments": None} + return {**head, "dates": len(scores), "proven": len(ok), + "do_nothing": round(statistics.median(s["naive"] / s["anchor"] - 1 for s in ok), 4) if ok else None, + "calm_share": calm, "recorded_share": recorded, "anchor_cancels": cancels[0], "jetblue_cancels": cancels[1], + "checks": checks, "guards": bool(ok) and all(checks.values()), "scores": list(scores)} + + +def calm_within(s: dict, rate: float) -> bool: + return fairday.within(s["calm_cancels"], s["calm_ferries"], s["flights"], rate) + + +def share(flags: Iterable[bool]) -> float | None: + flags = list(flags) + return round(sum(flags) / len(flags), 4) if flags else None + + +def pools(tasks: Sequence[Task], workers: int = 1) -> dict[str, int]: + """The smallest reserve pool per crew base of `tasks` (layered, crew on) that keeps every task's airline replay + within G1, or no worse than with unlimited reserves where that already isn't.""" + with ProcessPoolExecutor(workers) if workers > 1 and len(tasks) > 1 else nullcontext() as pool: + found = list(pool.map(least, tasks) if pool else map(least, tasks)) + free = [auto(t, {}) for t in tasks] + out = {b: max((k.get(b, 0) for k, _ in found), default=0) for b in sorted({b for t in tasks for b in t.bases})} + while True: + short = [calls for t, (_, calls), n in zip(tasks, found, free, strict=True) if not kept(t, out, n)] + if not short: + return out + for calls in short: + out |= {b: max(out[b], n) for b, n in calls.items()} + + +def least(task: Task) -> tuple[dict[str, int], dict[str, int]]: + """Per crew base of the task, the smallest pool (the others unlimited) that keeps its airline replay (`kept`), and + the replay's reserve calls there.""" + plan = references.airline_plan(pooled(task, dict.fromkeys(task.bases, len(task.flights)))) + calls = Counter(task.flight(i).orig for i, e in plan.items() if e.get("reserve")) + free = auto(task, {}) + return {b: next(k for k in range(calls[b] + 1) if k == calls[b] or kept(task, {b: k}, free)) + for b in task.bases}, {b: calls[b] for b in task.bases} + + +def kept(task: Task, pool: Mapping[str, int], free: int) -> bool: + """Whether the airline replay with `pool` is within G1 (auto-cancels at most AUTO_SHARE of its counted flights and + at most AUTO_MAX), or auto-cancels no more than the `free` it does with unlimited reserves.""" + replay = references.airline(pooled(task, {b: n for b, n in pool.items() if b in task.bases})) + return replay.auto <= free or (replay.auto <= AUTO_SHARE * replay.flew and replay.auto <= AUTO_MAX) + + +def auto(task: Task, pool: Mapping[str, int]) -> int: + """The airline replay's auto-cancels with `pool` at the task's crew bases it names, the others unlimited.""" + return references.airline(pooled(task, {b: n for b, n in pool.items() if b in task.bases})).auto + + +def pooled(task: Task, pool: Mapping[str, int]) -> Task: + return replace(task, layers=replace(task.layers or Layers(), reserves=dict(pool))) diff --git a/src/agentenv_airops/programs/layers/closures.py b/src/agentenv_airops/programs/layers/closures.py new file mode 100644 index 0000000..246be7a --- /dev/null +++ b/src/agentenv_airops/programs/layers/closures.py @@ -0,0 +1,152 @@ +"""Ramp closures at JFK and BOS from the METARs, as a cut in JetBlue's departure and gate-in rates (docs/v4/DESIGN.md +§3.2). + +An ASOS reports TS in present weather when it detects lightning within 5 nm of the field (VCTS is 5 to 10 nm, LTG DSNT +beyond: neither closes the ramp), and the remarks carry the thunderstorm's begin and end minutes (TSBmm or TSBhhmm, +TSEmm or TSEhhmm). Read oldest first, the routine METARs and the SPECIs give each closure: from a begin to its end; a +report with TS in present weather and none open opens one at the report; a report without TS closes one still open at +the report, unless its remarks end it first; the last report closes one still open. An end remark older than the open +closure's start belongs to an earlier storm (a SPECI's "TSE13B24" while a closure is open from :22) and is ignored. +Overlapping or touching intervals merge. The SPECIs, when the cache holds them, give the onset and end to the minute; +without them the routine reports alone, whose remarks carry the same minutes an hour later at most. + +Lightning doesn't stop the ramp: during such intervals every carrier together still pushed back at about 75% of its +rate in the same minutes just before (the spike: JFK 411 vs 563, BOS 449 vs 584), and gated in at 70 to 74% (BTS over +the 19 months: JFK 565 vs 807, BOS 732 vs 989). `fit_rate` measures the departure ratio per hub on train dates, leaving +out a closure whose minutes just before reach into another closure, as they hold no normal rate. `build` turns each +closure over a task into JetBlue's two caps there (§3.2), at the same rate: `rate` x JetBlue's normal hourly departures +from the hub (the task's flights scheduled to leave it in the clock hours the closure touches, per hour) x the +closure's length in hours, rounded to the nearest flight and never below what those flights actually pushed back inside +it; and the same of its flights scheduled to arrive at the hub, never below those that arrive inside it as the sim +times the recorded day: at the recorded pushback plus the truth block, a few minutes before the gate-in when the block +leaves out a held taxi-out, and at the hub even for a flight that diverted elsewhere (the airline replay flies it +there), and one diverted into the hub at the station it was scheduled into. The floors keep the recorded day's +pushbacks and arrivals inside a closure within its caps, so the airline replay leaves and lands there as on the open +day unless something else (a slot, a cap) moves a flight into a closure. `intervals` are in UTC epoch minutes, closures +in minutes from the task's midnight. +""" + +import csv +import io +import math +import re +from collections.abc import Iterable, Mapping, Sequence +from datetime import UTC, datetime, timedelta +from pathlib import Path + +import pandas as pd + +from ...build import bts +from ..candidates import midnight +from ..model import Task + +EVENT = re.compile(r"\bTS((?:[BE]\d{2,4})+)\b") +PART = re.compile(r"([BE])(\d{2,4})") +Interval = tuple[int, int] + + +def intervals(hub: str, metars: Iterable[Path], specis: Iterable[Path] = ()) -> list[Interval]: + """The hub's closures, [start, end) in UTC epoch minutes, from its routine METAR and SPECI CSVs (IEM ASOS); a + path that doesn't exist is skipped.""" + rows = [r for p in (*metars, *specis) if p.exists() for r in csv.DictReader(io.StringIO(p.read_text())) + if r["station"] == hub and (r["metar"] or "").strip()] + rows.sort(key=lambda r: r["valid"]) + out: list[Interval] = [] + start, now = None, None + for r in rows: + report = datetime.strptime(r["valid"], "%Y-%m-%d %H:%M").replace(tzinfo=UTC) + now = bts.epoch_minutes(report) + _, _, remarks = r["metar"].partition(" RMK ") + for m in EVENT.finditer(remarks): + for kind, digits in PART.findall(m.group(1)): + when = bts.epoch_minutes(_at(report, digits)) + if kind == "B" and start is None: + start = when + elif kind == "E" and start is not None and when >= start: + out.append((start, max(when, start + 1))) + start = None + here = any("TS" in c and not c.startswith("VC") for c in (r["wxcodes"] or "").split()) + if here and start is None: + start = now + elif not here and start is not None: + out.append((start, max(now, start + 1))) + start = None + if start is not None: + out.append((start, max(now, start + 1))) + merged: list[Interval] = [] + for a, b in sorted(out): + if merged and a <= merged[-1][1]: + merged[-1] = (merged[-1][0], max(merged[-1][1], b)) + else: + merged.append((a, b)) + return merged + + +def _at(report: datetime, digits: str) -> datetime: + """A remark's minute (mm, or hhmm), the latest such time at or before its report.""" + if len(digits) == 4: + t = report.replace(hour=int(digits[:2]), minute=int(digits[2:])) + return t - timedelta(days=1) if t > report else t + t = report.replace(minute=int(digits[-2:])) + return t - timedelta(hours=1) if t > report else t + + +def build(task: Task, spans: Mapping[str, Sequence[Interval]], + rate: Mapping[str, float]) -> dict[str, tuple[tuple[int, int, int, int], ...]]: + """The task's ramp closures per hub, (start, end, dep_cap, arr_cap) from its midnight: each of `spans` (UTC epoch + minutes, per hub) that overlaps the task's span, from its first watch to the end of the hour after its last + deadline. A hub closed over the task without a `rate` (`fit_rate` gives none to a hub with nothing pushed back + before its closures) is a ValueError: its caps can't be priced, and dropping the closure would hide the gap.""" + hi = 60 * (max(f.deadline for f in task.flights) // 60 + 2) + out = {} + for hub, xs in spans.items(): + shut = [] + for a, b in ((a - task.base, b - task.base) for a, b in xs): + if b <= task.start or a >= hi: + continue + if hub not in rate: + raise ValueError(f"{hub} closes {a}-{b} on {task.date} but has no fitted rate") + shut.append((a, b, *caps(task, hub, rate[hub], a, b))) + if shut: + out[hub] = tuple(shut) + return out + + +def caps(task: Task, hub: str, rate: float, a: int, b: int, by: int | None = None) -> tuple[int, int]: + """JetBlue's departure and arrival caps over a closure of `hub` from a to b at `rate`, as `build` prices them; `by` + floors them only at the pushbacks and arrivals before it, which is all the desk has seen at watch `by` + (sim.known prices a closure under way over its expected end so).""" + seen = (lambda m: True) if by is None else (lambda m: m < by) + leave = [f.sched_dep for f in task.flights if f.orig == hub] + land = [f.sched_arr for f in task.flights if f.dest == hub] + pushed = [f.actual.dep for f in task.flights if f.orig == hub and f.actual.dep is not None and seen(f.actual.dep)] + landed = [m for m in (f.actual.dep + f.block for f in task.flights if f.dest == hub and f.actual.dep is not None) + if seen(m)] + return _cap(rate, leave, pushed, a, b), _cap(rate, land, landed, a, b) + + +def _cap(rate: float, scheduled: Sequence[int], actual: Sequence[int], a: int, b: int) -> int: + """`rate` x the normal hourly count of `scheduled` minutes (per hour over the clock hours [a, b) touches) x the + hours in [a, b), to the nearest flight, and never below the `actual` minutes inside [a, b).""" + lo, top = 60 * (a // 60), 60 * -(-b // 60) + hourly = sum(lo <= t < top for t in scheduled) * 60 / (top - lo) + return max(sum(a <= t < b for t in actual), math.floor(rate * hourly * (b - a) / 60 + 0.5)) + + +def fit_rate(dates: Iterable[str], frame: pd.DataFrame, spans: Mapping[str, Sequence[Interval]]) -> dict[str, float]: + """Per hub, every carrier's pushbacks inside its closures that start on `dates` (local days) over its pushbacks in + the same number of minutes just before each, leaving out a closure whose minutes before reach into another of the + hub's closures. `frame` holds every carrier's BTS flights once each (`Origin`, and `act_dep` in UTC epoch minutes, + NaN when none); a hub with no pushback before any of its closures has no rate.""" + days = [(midnight(d), midnight(d) + 1440) for d in dates] + out = {} + for hub, xs in spans.items(): + pushed = frame.act_dep[frame.Origin == hub].dropna() + inside = before = 0 + for a, b in xs: + if any(lo <= a < hi for lo, hi in days) and not any(c < a and d > 2 * a - b for c, d in xs): + inside += int(((pushed >= a) & (pushed < b)).sum()) + before += int(((pushed >= 2 * a - b) & (pushed < a)).sum()) + if before: + out[hub] = inside / before + return out diff --git a/src/agentenv_airops/programs/layers/config.py b/src/agentenv_airops/programs/layers/config.py new file mode 100644 index 0000000..e162cd1 --- /dev/null +++ b/src/agentenv_airops/programs/layers/config.py @@ -0,0 +1,64 @@ +"""v4's layer config: the constants fitted on a pack's train dates (layers.calibrate), and the builder that turns them +into a task's Layers (docs/v4/DESIGN.md §2, §6.1). + +The constants are the ramp closures' departure-rate cut per hub (`rate`, closures.fit_rate), the reserve crews per +crew base (`reserves`), and the departure and outstation caps' clauses (`short`, `min_sched`: depcaps' and arrcaps' +0.9 and 5, kept as set, not fitted), with the train `dates` they were fitted on. `build` gives a task every layer at +them: JetBlue's departure caps at the hubs (depcaps) and arrival caps at the outstations (arrcaps) from every +carrier's BTS flights of its dates, the hubs' ramp closures over its span (closures, from `spans`: the IEM routine +METARs and SPECIs), and the pool at each of the task's crew bases (none without the crew model). A base the config +leaves out keeps unlimited reserves. The desk's view goes with them (docs/v4/DESIGN.md §4): the closure rates +(Layers.rates), at which the day as known prices a closure under way over its expected end (sim.known), the hubs' +weather news (layers.weather) and the default delivery lags (model.LAGS). +""" + +import functools +from collections.abc import Mapping, Sequence +from dataclasses import dataclass, field +from pathlib import Path + +import pandas as pd + +from ...build import weather +from ..advisories import HUBS +from ..model import Layers, Task +from . import arrcaps, closures, depcaps +from .weather import Item + + +@dataclass(frozen=True) +class Config: + rate: dict[str, float] = field(default_factory=dict) + reserves: dict[str, int] = field(default_factory=dict) + short: float = depcaps.SHORT + min_sched: int = depcaps.MIN_SCHED + dates: tuple[str, ...] = () + + @classmethod + def from_dict(cls, d: Mapping) -> "Config": + return cls(dict(d["rate"]), dict(d["reserves"]), d["short"], d["min_sched"], tuple(d["dates"])) + + def to_dict(self) -> dict: + return {"rate": dict(sorted(self.rate.items())), "reserves": dict(sorted(self.reserves.items())), + "short": self.short, "min_sched": self.min_sched, "dates": list(self.dates)} + + +def build(task: Task, cfg: Config, frame: pd.DataFrame, spans: Mapping[str, Sequence[closures.Interval]], + news: Sequence[Item] = ()) -> Layers: + """The task's layers at `cfg`. `frame` holds every carrier's BTS flights of its dates (candidates.frame), `spans` + each hub's closures in UTC epoch minutes (closures.intervals), `news` the hubs' weather (layers.weather.build).""" + return Layers(dep_caps=depcaps.build(task, frame, cfg.short, cfg.min_sched), + arr_caps=arrcaps.build(task, frame, cfg.short, cfg.min_sched), + closures=closures.build(task, spans, cfg.rate), + reserves={b: n for b, n in sorted(cfg.reserves.items()) if b in task.bases}, + weather=tuple(news), rates=dict(sorted(cfg.rate.items()))) + + +@functools.lru_cache(maxsize=8) +def spans(inputs: Path, months: tuple[str, ...]) -> dict[str, list[closures.Interval]]: + """Each hub's closures over `months`, from the cached routine METARs and SPECIs (IEM ASOS); flow.sources (which + needs Pillow, a build extra) loads only here, so the env's runtime imports stay the base install's.""" + from ...flow import sources + + return {h: closures.intervals(h, [weather.metar_path(inputs, h, m) for m in months], + [sources.speci_path(inputs, h, m) for m in months]) for h in HUBS} diff --git a/src/agentenv_airops/programs/layers/crew.py b/src/agentenv_airops/programs/layers/crew.py new file mode 100644 index 0000000..d21679e --- /dev/null +++ b/src/agentenv_airops/programs/layers/crew.py @@ -0,0 +1,107 @@ +"""v4's crew layer (docs/v4/CREW.md §1): pairings legal on schedule, cut in place of Edgar's proxy (repairs.pairings). + +Edgar's pairings chain each tail's legs from a base departure to the next base arrival with no duty limit, so a long +chain through outstations exceeds 14 CFR 117 on schedule and its later legs can never fly. Here each tail's scheduled +legs of the day (cancelled ones and those before the window included, as repairs.crews takes them), in order, are cut +into duties: +- a duty starts at the tail's first leg, at every departure from a crew base, and after a ground gap of REST or more + (the 10 h rest of 117.25(e), an overnight); +- it takes the tail's next leg while the duty, that leg its last, would still end on schedule `buffer` minutes inside + its Table B limit (repairs.fdp, for the report REPORT minutes before its first departure, at the report's local hour + at its first origin, and the duty's segments with that leg) and hold at most `max_segments` legs; otherwise a new + duty starts at that leg (a crew change at a base, an overnight crew at an outstation). A first leg longer than its + limit alone is still a duty of its own. +A duty is a pairing, and its legs get Edgar's records (model.Crew): its report, station, segments and limit; an +outstation departure must block in by the report + the limit + EXTENSION (117.19(a)(1)), so its crew deadline is that +less its scheduled block, its rest REST after it; a departure from a base has none in the record (reserves crew it), +and under a finite reserve pool there (Layers.reserves) Task.crew gives it the same deadline and rest. So every +duty is legal on schedule with `buffer` and the extension to spare, and the crew binds only when delays push a duty +past its limit, as real crews time out; the sim and the hindsight MIP enforce it as they do Edgar's. + +A build's crew mode is a Rule: Edgar's ('edgar', repairs.crews) or v4's ('v4', `crews` at its buffer and max_segments, +fitted on train dates by layers.calibrate.crew_fit; defaults BUFFER and MAX_SEGMENTS, the spike's). +""" + +from collections.abc import Collection, Hashable, Mapping, Sequence +from dataclasses import dataclass +from datetime import datetime +from zoneinfo import ZoneInfo + +import pandas as pd + +from .. import repairs +from ..model import EXTENSION, REST, Crew + +MODES = ("edgar", "v4") +BUFFER, MAX_SEGMENTS = 60, 4 + + +@dataclass(frozen=True) +class Rule: + """How a build cuts pairings: `mode` 'edgar' (repairs.crews; `buffer` and `max_segments` unused) or 'v4'.""" + + mode: str = "edgar" + buffer: int = BUFFER + max_segments: int = MAX_SEGMENTS + + def __post_init__(self): + if self.mode not in MODES: + raise ValueError(f"crew mode {self.mode!r} is none of {MODES}") + + def crews(self, legs: pd.DataFrame, base: int, bases: Collection[str], + zones: Mapping[str, str]) -> dict[Hashable, Crew]: + if self.mode == "edgar": + return repairs.crews(legs, base, bases, zones) + return crews(legs, base, bases, zones, self.buffer, self.max_segments) + + def to_dict(self) -> dict: + v4 = self.mode == "v4" + return {"crew": self.mode, "buffer": self.buffer if v4 else None, + "max_segments": self.max_segments if v4 else None} + + +EDGAR = Rule() + + +def crews(legs: pd.DataFrame, base: int, bases: Collection[str], zones: Mapping[str, str], buffer: int = BUFFER, + max_segments: int = MAX_SEGMENTS) -> dict[Hashable, Crew]: + """The crew of each of `legs` by row label, as repairs.crews takes them (Tail_Number, Origin, Dest, local sd and + CRSElapsedTime; `base` their midnight's UTC epoch minute, `zones` each origin's time zone), on duties cut legal on + schedule.""" + out = {} + for tail, x in legs.sort_values(["Tail_Number", "sd"]).groupby("Tail_Number"): + for k, duty in enumerate(duties(list(x.itertuples()), base, bases, zones, buffer, max_segments)): + first = duty[0] + report = int(first.sd) - repairs.REPORT + limit = repairs.fdp(hour(base, report, zones[first.Origin]), len(duty)) + for r in duty: + d = None if r.Origin in bases else report + limit + EXTENSION - int(r.CRSElapsedTime) + out[r.Index] = Crew(f"{tail}/{k}", report, first.Origin, len(duty), limit, d, + None if d is None else d + REST) + return out + + +def duties(legs: Sequence, base: int, bases: Collection[str], zones: Mapping[str, str], buffer: int = BUFFER, + max_segments: int = MAX_SEGMENTS) -> list[list]: + """A tail's legs (rows with Origin, Dest, sd and CRSElapsedTime) in scheduled order cut into duties legal on + schedule.""" + out: list[list] = [] + for r in legs: + if out and not (r.Origin in bases or int(r.sd) - end(out[-1][-1]) >= REST or len(out[-1]) >= max_segments): + report = int(out[-1][0].sd) - repairs.REPORT + limit = repairs.fdp(hour(base, report, zones[out[-1][0].Origin]), len(out[-1]) + 1) + if end(r) + buffer <= report + limit: + out[-1].append(r) + continue + out.append([r]) + return out + + +def end(r) -> int: + """A leg's scheduled arrival, its departure plus its scheduled block.""" + return int(r.sd) + int(r.CRSElapsedTime) + + +def hour(base: int, minute: int, zone: str) -> int: + """The local hour in `zone` of `minute` past the UTC epoch minute `base`.""" + return datetime.fromtimestamp(60 * (base + minute), ZoneInfo(zone)).hour diff --git a/src/agentenv_airops/programs/layers/depcaps.py b/src/agentenv_airops/programs/layers/depcaps.py new file mode 100644 index 0000000..d3a7404 --- /dev/null +++ b/src/agentenv_airops/programs/layers/depcaps.py @@ -0,0 +1,41 @@ +"""JetBlue's departure cap per hour at JFK and BOS, from what the airport actually pushed back (docs/v4/DESIGN.md +§3.1). + +v2 caps nothing leaving a hub. An hour is constrained when every carrier together pushed back (BTS gate departure) +less than `short` of what it had scheduled to leave the hub in it, with at least `min_sched` scheduled. JetBlue's cap +there is its in-scope share of the hour's schedule (the task's flights scheduled to leave the hub in it) times the +hour's actual pushbacks, rounded to the nearest flight (so a cap of 0 means JetBlue's expected share was under +half a flight), and never below what those flights actually pushed back in it, so the recorded +day stays feasible. Other hours, and hours JetBlue scheduled nothing in, stay uncapped. Hours are counted from the +task's midnight, over the task's span: from its first watch to the hour after its last deadline. +""" + +import math +from collections import Counter + +import pandas as pd + +from ..advisories import HUBS +from ..model import Task + +SHORT = 0.9 +MIN_SCHED = 5 + + +def build(task: Task, frame: pd.DataFrame, short: float = SHORT, + min_sched: int = MIN_SCHED) -> dict[str, dict[int, int]]: + """The task's departure caps per hub and hour. `frame` holds every carrier's BTS flights of the task's dates + (candidates.frame: `Origin`, and `sched_dep` and `act_dep` in UTC epoch minutes, `act_dep` NaN when none).""" + lo, hi = task.start // 60, max(f.deadline for f in task.flights) // 60 + 1 + out: dict[str, dict[int, int]] = {} + for hub in HUBS: + x = frame[frame.Origin == hub] + sched = Counter(((x.sched_dep - task.base) // 60).astype(int)) + pushed = Counter(((x.act_dep.dropna() - task.base) // 60).astype(int)) + mine = Counter(f.sched_dep // 60 for f in task.flights if f.orig == hub) + flew = Counter(f.actual.dep // 60 for f in task.flights if f.orig == hub and f.actual.dep is not None) + caps = {h: max(flew[h], math.floor(mine[h] / sched[h] * pushed[h] + 0.5)) for h in range(lo, hi + 1) + if mine[h] and sched[h] >= min_sched and pushed[h] < short * sched[h]} + if caps: + out[hub] = caps + return out diff --git a/src/agentenv_airops/programs/layers/fairday.py b/src/agentenv_airops/programs/layers/fairday.py new file mode 100644 index 0000000..0ef2590 --- /dev/null +++ b/src/agentenv_airops/programs/layers/fairday.py @@ -0,0 +1,57 @@ +"""The calm day's bar: JetBlue's own fair-weather cancellations (docs/v4/DESIGN.md §6.2). + +A fair day is a date on which JetBlue cancelled none of the flights in a whole-network task's scope (every leg of a +tail that touched a hub that day, and every flight to or from a hub) for weather (code B) or the National Airspace +System (code C). The cancellations such a day still has are the carrier's own (code A: mechanical, crew), the kind a +calm day in the model forces where an aircraft is short. `rate` is the QUANTILE of their share of the scope's flights +over the fair days of the months given, and a calm day of n flights is within the bar when it forces at most +`allowance(rate, n)` cancellations, max(1, floor(rate x n)), and at most FERRIES ferries: JetBlue's own fair days +are within it on QUANTILE of them. +""" + +import math +from collections.abc import Iterable + +import pandas as pd + +QUANTILE, FERRIES = 0.9, 2 +CARRIER = "B6" +CODES = "ABCD" + + +def days(frame: pd.DataFrame, hubs: Iterable[str], carrier: str = CARRIER) -> pd.DataFrame: + """Per date of `frame` (BTS on-time rows, as bts.read_ontime reads them), the scope's flights and its + cancellations by code.""" + hubs = set(hubs) + x = frame[frame.Reporting_Airline == carrier] + at_hub = x.Origin.isin(hubs) | x.Dest.isin(hubs) + tails = x[at_hub].groupby("FlightDate").Tail_Number.agg(lambda s: set(s.dropna())) + rows = [] + for day, g in x.groupby("FlightDate"): + scope = g[g.Tail_Number.isin(tails.get(day, set())) | g.Origin.isin(hubs) | g.Dest.isin(hubs)] + codes = scope.loc[scope.Cancelled.astype(float) == 1, "CancellationCode"].value_counts() + rows.append({"date": str(day)[:10], "flights": len(scope), **{c: int(codes.get(c, 0)) for c in CODES}}) + return pd.DataFrame(rows, columns=["date", "flights", *CODES]) + + +def rate(frames: Iterable[pd.DataFrame], hubs: Iterable[str], q: float = QUANTILE) -> dict: + """The bar over the fair days of `frames` (a month's BTS rows each): the q-quantile of the carrier's + cancellations' share of the scope's flights, with the counts it was read from.""" + hubs = tuple(hubs) + d = pd.concat([days(f, hubs) for f in frames], ignore_index=True) + fair = d[(d.B == 0) & (d.C == 0) & (d.flights > 0)] + if fair.empty: + raise ValueError("no fair day in the months given: no calm-day bar") + share = fair.A / fair.flights + return {"rate": round(float(share.quantile(q)), 6), "quantile": q, "fair_days": len(fair), "days": len(d), + "median_cancels": float(fair.A.median()), "ferries": FERRIES, "hubs": list(hubs)} + + +def allowance(rate: float, flights: int) -> int: + """The forced cancellations a calm day of `flights` flights may have: JetBlue's fair-day share of them, at least + one.""" + return max(1, math.floor(rate * flights)) + + +def within(cancelled: int, ferries: int, flights: int, rate: float) -> bool: + return cancelled <= allowance(rate, flights) and ferries <= FERRIES diff --git a/src/agentenv_airops/programs/layers/weather.py b/src/agentenv_airops/programs/layers/weather.py new file mode 100644 index 0000000..fdab8f7 --- /dev/null +++ b/src/agentenv_airops/programs/layers/weather.py @@ -0,0 +1,71 @@ +"""The hubs' weather as the desk reads it (docs/v4/DESIGN.md §4): every routine METAR, SPECI and TAF of the task's +hubs over its span, as news items (minute, kind, station, text) for Layers.weather. + +A METAR or SPECI is dated by its observation (IEM ASOS `valid`), a TAF by its AFOS issue time; minutes are from the +task's midnight. Each reaches the desk at that minute plus its kind's lag (Layers.lags: metar 5, speci 5, taf 10 by +default, advisories 0), the time the product takes to be filed and spread. The span starts LEAD_OBS before the first +watch for the observations and LEAD_TAF for the TAFs (the TAF cycle is six hours), so the first watch has the latest of +each, and ends at the last deadline. The SPECI archive holds some reports the routine one has too; a SPECI with the same +minute and text as a routine METAR is dropped. Only the observation's raw text is carried: the desk reads it as a +dispatcher reads the wire. +""" + +import csv +import io +from datetime import UTC, datetime +from pathlib import Path + +from ...build import bts +from ...build import weather as v1 +from ..model import Task + +LEAD_OBS = 90 +LEAD_TAF = 360 +Item = tuple[int, str, str, str] + + +def build(task: Task, inputs: Path) -> tuple[Item, ...]: + """The task's hubs' METARs, SPECIs and TAFs over its span, oldest first (minute, kind, hub, station order); a + month file the cache doesn't hold is skipped; flow.sources (which needs Pillow, a build extra) loads only here.""" + from ...flow import sources + + hi = max(f.deadline for f in task.flights) + months = _months(task.base + task.start - LEAD_TAF, task.base + hi) + out: dict[tuple[int, str, str], Item] = {} + for hub in task.storms: + for kind, path in [(k, p) for m in months for k, p in (("metar", v1.metar_path(inputs, hub, m)), + ("speci", sources.speci_path(inputs, hub, m)))]: + for minute, text in _observations(path, hub, task.base): + if task.start - LEAD_OBS <= minute <= hi: + out.setdefault((minute, hub, text), (minute, kind, hub, text)) + for m in months: + path = v1.afos_path(inputs, f"TAF{hub}", m) + if not path.exists(): + continue + for issued, body in v1.products(path.read_text(encoding="latin-1"), m): + minute = bts.epoch_minutes(issued) - task.base + if task.start - LEAD_TAF <= minute <= hi: + out.setdefault((minute, hub, body), (minute, "taf", hub, body)) + return tuple(sorted(out.values(), key=lambda x: (x[0], x[1], x[2], x[3]))) + + +def _observations(path: Path, hub: str, base: int) -> list[tuple[int, str]]: + if not path.exists(): + return [] + out = [] + for r in csv.DictReader(io.StringIO(path.read_text())): + text = (r["metar"] or "").strip() + if r["station"] == hub and text: + observed = datetime.strptime(r["valid"], "%Y-%m-%d %H:%M").replace(tzinfo=UTC) + out.append((bts.epoch_minutes(observed) - base, text)) + return out + + +def _months(lo: int, hi: int) -> list[str]: + """The UTC months epoch minutes lo to hi touch, as YYYY-MM.""" + first, last = (datetime.fromtimestamp(60 * x, UTC) for x in (lo, hi)) + out, y, m = [], first.year, first.month + while (y, m) <= (last.year, last.month): + out.append(f"{y}-{m:02d}") + y, m = (y + 1, 1) if m == 12 else (y, m + 1) + return out diff --git a/src/agentenv_airops/programs/mip.py b/src/agentenv_airops/programs/mip.py index 29cae0a..f731fd3 100644 --- a/src/agentenv_airops/programs/mip.py +++ b/src/agentenv_airops/programs/mip.py @@ -1,17 +1,22 @@ """The hindsight MIP (HiGHS): v1's time-line network (refs.mip) by split family, inside the programs. Each flight gets a copy per departure minute (every `step` minutes, at the EDCT of every slot into its destination, the -slot less its scheduled block, and at the end of its crew's rest) from its earliest to its deadline, the hub cutoff +slot less its scheduled block, at the end of every ramp closure at its origin, at the end of every one at its +destination less its block, and at the end of its crew's rest) from its earliest to its deadline, the hub cutoff included, less the minutes the world forbids: those a ground stop holds it, those at which a segment governs it with its slot time (the minute plus the scheduled block) inside the period but on none of JetBlue's slots, and those after its crew deadline before its crew's rest ends. A copy on a slot counts in that slot's row, at most one flight per slot; -any other copy into a hub counts in the nominal row of its arrival hour (the minute plus the block). A copy costs its -pax x minutes late at arrival and its aircraft's minutes (cost.flown_price: the minute's gate delay, and the world's -block overrun, the same for every copy of a flight); a cancellation costs `price`, the notice price at the solve time, -which keeps the model linear, less the cancellation's saving, and a caller pins a cancellation the sim decides (an -auto-cancel, `lapsed`) at the price the sim gives it (Task.auto_at). With `flat` every cancellation, the desk's or an -auto-cancel, costs the flat price less the saving, as Cost.flat prices it. Pinned flights keep their minute, their -cancellation or their auto-cancel, and use up the slots and nominal room they take. +any other copy into a hub, or into an outstation in an hour it caps (Layers.arr_caps, which join the nominal caps), +counts in the nominal row of its arrival hour (the minute plus the block); and any copy, slotted or not, leaving in an +hour its origin caps (Layers.dep_caps) counts in that hour's departure row, one leaving inside a ramp closure at its +origin (Layers.closures) in that closure's departure row, and one arriving (the minute plus the block) inside a ramp +closure at its destination in that closure's arrival row. A copy costs its pax x minutes late at arrival and its +aircraft's minutes (cost.flown_price: the minute's gate delay, and the world's block overrun, the same for every copy +of a flight); a cancellation costs `price`, the notice price at the solve time, which keeps the model linear, less the +cancellation's saving, and a caller pins a cancellation the sim decides (an auto-cancel, `lapsed`) at the price the +sim gives it (Task.auto_at). With `flat` every cancellation, the desk's or an auto-cancel, costs the flat price less +the saving, as Cost.flat prices it. Pinned flights keep their minute, their cancellation or their auto-cancel, and use +up the slots, nominal, departure and closure room they take. Aircraft flow as in v1, over nodes per (family, station) at every event time; a copy of a leg with a busy block reaches its destination's node at the block's ready time when that is later than its turn. A leg JetBlue turned into @@ -30,6 +35,13 @@ sim.execute reproduces from its departure minutes as holds, at the same cost, and every day sim.execute runs is a solution (`Network.vector`), which also starts the search. +With a finite reserve pool at a crew base (Layers.reserves, docs/v4/DESIGN.md §3.4) each departure from it that is +not pinned and has a copy past its crew deadline (Task.crew) before its crew's rest ends, or that may auto-cancel on +a line, gets a 0/1 reserve column, which bounds those copies (and, on a line, a column auto-cancelling it on the +reserve crew, at its deadline) and joins its base's pool row: room = the pool less the pinned flights the caller says +hold a reserve (`reserved`, sim.granted's of them), whose own auto-cancel is then priced at its deadline. The copies of +a reservable flight are Edgar's base departure's, every minute to its deadline, plus one at its crew's rest end. + Proven to GAP (0.01%); `canonical` then re-solves among the optimal plans for the one with the earliest weighted departures (v1's second pass, started from the first), so the plan, not only its cost, is the same on every run. Within the gap the first plan need not be the cheapest, so the second may cost less: the objective is the cost of the plan @@ -89,6 +101,7 @@ class Solution: started: bool canonical: bool lapsed: dict[int, int] + reserve: frozenset[int] = frozenset() @property def proven(self) -> bool: @@ -96,9 +109,10 @@ def proven(self) -> bool: def plan(self, decided: int) -> dict[int, dict]: """The solution as holds to each departure minute, its cancellations decided at `decided`; a flight it lets - auto-cancel is held past its deadline.""" - return {i: {"not_before": self.lapsed[i]} if i in self.lapsed else {"cancel": True, "decided": decided} - if d is None else {"not_before": d} for i, d in self.deps.items()} + auto-cancel is held past its deadline; one it flies past its crew deadline on a reserve crew calls one.""" + out = {i: {"not_before": self.lapsed[i]} if i in self.lapsed else {"cancel": True, "decided": decided} + if d is None else {"not_before": d} for i, d in self.deps.items()} + return {i: e | {"reserve": True} if i in self.reserve else e for i, e in out.items()} def to_dict(self) -> dict: return {"status": self.status, "objective": self.objective, "bound": round(self.bound, 1), @@ -110,12 +124,14 @@ def solve(task: Task, world: sim.World | None = None, price: Mapping[int, int] | fixed: Mapping[int, int | None] | None = None, earliest: int | None = None, step: int = 1, time_limit: float = 600.0, gap: float = GAP, threads: int = 1, canonical: bool = True, start: sim.Execution | None = None, lapsed: Collection[int] = (), fix: bool = True, - flat: bool = False, pax: Proxy | None = None) -> Solution: + flat: bool = False, pax: Proxy | None = None, + reserved: Collection[int] = ()) -> Solution: """The optimum in `world` (the truth by default) with each cancel arc at `price` (default: notice-priced at the first watch, or flat with `flat`). `fixed` pins flights to a departure minute (None: cancelled, or auto-cancelled for those in `lapsed`); the others leave at or after `earliest`. `start` is a day sim.execute ran in `world` that - the model holds, to start the search from; `pax` prices the passengers by their proxy.""" - net = Network(task, world, price, fixed, earliest, step, lapsed, flat, pax) + the model holds, to start the search from; `pax` prices the passengers by their proxy; `reserved` are the pinned + flights holding a reserve crew.""" + net = Network(task, world, price, fixed, earliest, step, lapsed, flat, pax, reserved) return net.solve(time_limit, gap, threads, canonical, None if start is None else net.vector(start), fix) @@ -141,7 +157,8 @@ class Network: def __init__(self, task: Task, world: sim.World | None = None, price: Mapping[int, int] | None = None, fixed: Mapping[int, int | None] | None = None, earliest: int | None = None, step: int = 1, - lapsed: Collection[int] = (), flat: bool = False, pax: Proxy | None = None): + lapsed: Collection[int] = (), flat: bool = False, pax: Proxy | None = None, + reserved: Collection[int] = ()): self.task, self.pax = task, pax self.world = world = sim.truth(task) if world is None else world if price is None: @@ -160,8 +177,11 @@ def __init__(self, task: Task, world: sim.World | None = None, price: Mapping[in else: lo = max(task.earliest(f, world.lb[f.id]), earliest or 0) jumps = {s - f.sched_block for seg in world.segments.get(f.dest, ()) for s in seg.slots} - if f.crew and f.crew.rest is not None: - jumps.add(f.crew.rest) + jumps |= {b for _, b, _, _ in world.closures.get(f.orig, ())} + jumps |= {b - world.block[f.id] for _, b, _, _ in world.closures.get(f.dest, ())} + crew = task.crew(f) + if crew and crew.rest is not None: + jumps.add(crew.rest) minutes = set(range(lo, f.deadline + 1, step)) | {d for d in jumps if lo <= d <= f.deadline} copies[f.id] = [(d, at) for d in sorted(minutes) if rules.held(f, d) == d and f.legal(d) == d and (at := _place(f, d, rules, pools))] @@ -236,6 +256,10 @@ def arc(u: int | None, v: int | None, c: int) -> None: self.xs: dict[tuple[int, int], int] = {} self.cancel: dict[int, int] = {} self.lapsed: dict[int, int] = {} + self.reserve: dict[int, int] = {} + self.on_reserve: dict[int, int] = {} + self.reserved = frozenset(i for i in reserved if i in fixed and task.reservable(task.flight(i))) + calls: dict[str, dict[int, int]] = defaultdict(dict) rows: dict[tuple, dict[int, int]] = defaultdict(dict) pinned: Counter = Counter() cover = [] @@ -250,21 +274,34 @@ def arc(u: int | None, v: int | None, c: int) -> None: arc(gate.get((f.id, d), node[((f.family, f.orig), d)]), lane.get((f.id, d), node[((f.family, f.dest), task.back(f, arr))]), c) r[c] = 1 - if kind != "free": + for row in ((kind, key),) + rules.leaving(f, d): + if row[0] == "free": + continue if f.id in fixed: - pinned[(kind, key)] += 1 + pinned[row] += 1 else: - rows[(kind, key)][c] = 1 + rows[row][c] = 1 ub = 0 if f.id in fixed and fixed[f.id] is not None else 1 lapses = f.id in on_line and (f.id not in fixed or f.id in lapsed) self.cancel[f.id] = c = m.col(price[f.id], 0 if lapses and f.id in fixed else ub, True, (f.deadline - f.sched_dep + 2) * w) r[c] = 1 if f.id in on_line: - auto = pax.price(f, task.auto_at(f), flat) if pax else flat_price(f) if flat else \ - cancel_price(f, task.auto_at(f)) + at = task.auto_at(f, f.id in self.reserved) + auto = pax.price(f, at, flat) if pax else flat_price(f) if flat else cancel_price(f, at) self.lapsed[f.id] = c = m.col(auto, ub if lapses else 0, True, (f.deadline - f.sched_dep + 3) * w) r[c] = 1 + if task.reservable(f) and f.id not in fixed: + bound = {self.xs[(f.id, d)]: 1 for d, _ in copies[f.id] if task.late(f, d)} + if f.id in on_line: + at = task.auto_at(f, True) + lapse = pax.price(f, at, flat) if pax else flat_price(f) if flat else cancel_price(f, at) + self.on_reserve[f.id] = c = m.col(lapse, 1, True, (f.deadline - f.sched_dep + 4) * w) + r[c] = bound[c] = 1 + if bound: + self.reserve[f.id] = c = m.col(0, 1, True) + m.row({**bound, c: -1}, -INF, 0) + calls[f.orig][c] = 1 cover.append(r) self.landed: dict[tuple[int, int], int] = dict(self.xs) self.turn: dict[tuple[int, int], int] = {} @@ -376,9 +413,15 @@ def arc(u: int | None, v: int | None, c: int) -> None: m.row(flow[v], -supply[v], -supply[v]) for r in cover: m.row(r, 1, 1) + shut = {(kind, (s, a)): cap for s, spans in world.closures.items() for a, _, dep, arr in spans + for kind, cap in (("closure_dep", dep), ("closure_arr", arr))} for (kind, key), r in rows.items(): - room = 1 if kind == "slot" else world.nominal[key[0]][key[1]] + room = (1 if kind == "slot" else world.nominal[key[0]][key[1]] if kind == "hour" + else world.dep_caps[key[0]][key[1]] if kind == "dep" else shut[(kind, key)]) m.row(r, -INF, max(0, room - pinned[(kind, key)])) + held = Counter(task.flight(i).orig for i in self.reserved) + for base, r in calls.items(): + m.row(r, -INF, max(0, task.layers.reserves[base] - held[base])) self.copies = copies self.at: dict[int, int] = {} self.links: dict[tuple[int, int], int] = {} @@ -469,9 +512,13 @@ def solve(self, time_limit: float = 600.0, gap: float = GAP, threads: int = 1, c for f in self.task.flights: flown = [d for d, _ in self.copies[f.id] if x[self.xs[(f.id, d)]] > 0.5] deps[f.id] = flown[0] if flown else None - lapsed = {i: self.task.flight(i).deadline + 1 for i, c in self.lapsed.items() if x[c] > 0.5} + lapsed = {i: self.task.flight(i).deadline + 1 for i, c in [*self.lapsed.items(), *self.on_reserve.items()] + if x[c] > 0.5} + reserve = {i for i, c in self.on_reserve.items() if x[c] > 0.5} | { + i for i in self.reserve if deps[i] is not None and self.task.late(self.task.flight(i), deps[i])} return Solution(status, round(objective), bound, round(100 * mip_gap, 4), round(wall, 2), deps, len(m.cost), - len(m.rows), sum(len(v) for v in self.copies.values()), start is not None, settled, lapsed) + len(m.rows), sum(len(v) for v in self.copies.values()), start is not None, settled, lapsed, + frozenset(reserve) | self.reserved) def rounded(self, time_limit: float = 600.0, threads: int = 1) -> dict[int, dict] | None: """The LP relaxation's optimum rounded to a plan the sim can play, a start for the search: a flight more @@ -508,8 +555,11 @@ def vector(self, run: sim.Execution) -> np.ndarray | None: via, fast = {}, set() for f in task.flights: r = run.flights[f.id] + if f.id in self.reserve: + x[self.reserve[f.id]] = f.id in run.reserved if r.status != FLOWN: - x[self.lapsed[f.id] if r.status == AUTO_CANCELLED and f.id in self.lapsed else self.cancel[f.id]] = 1 + lapse = self.on_reserve if f.id in run.reserved and f.id in self.on_reserve else self.lapsed + x[lapse[f.id] if r.status == AUTO_CANCELLED and f.id in lapse else self.cancel[f.id]] = 1 continue k = (f.id, r.dep) if k not in self.xs: diff --git a/src/agentenv_airops/programs/model.py b/src/agentenv_airops/programs/model.py index aeb6324..193128b 100644 --- a/src/agentenv_airops/programs/model.py +++ b/src/agentenv_airops/programs/model.py @@ -15,20 +15,34 @@ pieces, the VMC called arrival rate, and the demand: every BTS-scheduled arrival at the hub over the task, so the forecast RBS can run at any watch and the other carriers' controlled arrivals carry their BTS NAS delay for the fidelity clause. + +A task may carry v4's realism `layers` (Layers, programs.layers builds them from pinned public inputs); a task that +sets any is written as model version LAYERED, one without (None, or Layers() with nothing set) is v2's task byte +for byte. `version` is the v2 model's: LAYERED is read back as VERSION and written from the layers alone. + +With a finite reserve pool at a crew base (`Layers.reserves`, docs/v4/DESIGN.md §3.4) a departure from that base gets +its pairing's crew deadline as an outstation departure has it (the report + the FAR 117 limit + EXTENSION, less its +scheduled block, its rest REST after it: `Task.crew`), and may fly past it only on a reserve crew the plan calls +(`reserve`, granted while the base's pool lasts: sim.granted). A base the pool leaves out keeps Edgar's unlimited +reserves. """ import json import math from collections import Counter from collections.abc import Callable, Mapping -from dataclasses import dataclass, field +from dataclasses import dataclass, field, replace from functools import cached_property from pathlib import Path from ..core.model import Airport -from .advisories import Advisory, Scope, Stop, nm +from .advisories import HUBS, Advisory, Scope, Stop, nm VERSION = 3 +LAYERED = 4 +EXTENSION = 120 +REST = 600 +LAGS = {"advisory": 0, "metar": 5, "speci": 5, "taf": 10} @dataclass(frozen=True) @@ -324,10 +338,75 @@ def e_bar(self) -> float: return self.e_median +@dataclass(frozen=True) +class Layers: + """The world's realism layers beyond the FAA's programs, hours and minutes from the task's midnight: + JetBlue's departure cap per hour at a hub (`dep_caps`) and arrival cap per hour at an outstation (`arr_caps`), + ramp closures per station as (start, end, dep_cap, arr_cap): JetBlue's departures from the station leaving in + [start, end) total at most dep_cap, and its arrivals there inside it at most arr_cap; and the reserve crews per + crew base. A hub's arrivals answer to its programs and nominal cap, so an arrival cap at a hub is a ValueError. + + The desk's news beyond the advisories (docs/v4/DESIGN.md §4): the hubs' weather as (minute, kind, station, text), + kind "metar", "speci" or "taf", the minute its observation or issue (layers.weather), and the delivery lag per + product kind in minutes (`lags` over LAGS: advisory 0, metar 5, speci 5, taf 10, a kind it leaves out keeping + LAGS's; one LAGS doesn't have, a negative lag or an advisory lag other than 0 is a ValueError), each item + reaching the desk at its minute plus its kind's lag; and the closure rate per hub that priced its closures + (`rates`, closures.fit_rate's), with which the day as known prices a closure under way over its expected end, not + its true one (sim.known). All three are written only when set, so a task without them reads and writes as + before.""" + + dep_caps: dict[str, dict[int, int]] = field(default_factory=dict) + arr_caps: dict[str, dict[int, int]] = field(default_factory=dict) + closures: dict[str, tuple[tuple[int, int, int, int], ...]] = field(default_factory=dict) + reserves: dict[str, int] = field(default_factory=dict) + weather: tuple[tuple[int, str, str, str], ...] = () + lags: dict[str, int] = field(default_factory=lambda: dict(LAGS)) + rates: dict[str, float] = field(default_factory=dict) + + def __post_init__(self): + unknown = sorted(set(self.lags) - set(LAGS)) + if unknown: + raise ValueError(f"lags for unknown kind(s) {', '.join(unknown)}: the kinds are {', '.join(LAGS)}") + bad = sorted(k for k, v in self.lags.items() if not isinstance(v, int) or isinstance(v, bool) or v < 0) + if bad: + raise ValueError(f"lags for {', '.join(bad)} not a whole number of minutes >= 0: news can't reach the desk " + "before its minute") + if self.lags.get("advisory", 0): + raise ValueError("an advisory lag must be 0: the day as known reads the programs as signed by the watch " + "(advisories.issued_by), so a later delivery would act on news the desk hasn't had") + object.__setattr__(self, "lags", LAGS | dict(self.lags)) + hubs = sorted(set(self.arr_caps) & set(HUBS)) + if hubs: + raise ValueError(f"arrival caps at hub(s) {', '.join(hubs)}: a hub's arrivals answer to its programs " + "and nominal cap") + + @classmethod + def from_dict(cls, d: Mapping) -> "Layers": + return cls({s: {int(h): c for h, c in by.items()} for s, by in d["dep_caps"].items()}, + {s: {int(h): c for h, c in by.items()} for s, by in d["arr_caps"].items()}, + {s: tuple((a, b, c, e) for a, b, c, e in spans) for s, spans in d["closures"].items()}, + dict(d["reserves"]), tuple((m, k, s, x) for m, k, s, x in d.get("weather", ())), + dict(d.get("lags", {})), dict(d.get("rates", {}))) + + def to_dict(self) -> dict: + out = {"dep_caps": {s: {str(h): c for h, c in sorted(by.items())} for s, by in self.dep_caps.items()}, + "arr_caps": {s: {str(h): c for h, c in sorted(by.items())} for s, by in self.arr_caps.items()}, + "closures": {s: [list(x) for x in spans] for s, spans in self.closures.items()}, + "reserves": dict(self.reserves)} + if self.weather: + out["weather"] = [list(x) for x in self.weather] + if self.lags != LAGS: + out["lags"] = dict(self.lags) + if self.rates: + out["rates"] = dict(self.rates) + return out + + @dataclass(frozen=True) class Task: """`hubs` are a whole-network task's storm hubs (`hub` the one whose window starts it), `needs` tomorrow's - aircraft per family and station; a hub window has neither, its needs being its tails' `end_req`.""" + aircraft per family and station; a hub window has neither, its needs being its tails' `end_req`. `layers` None + (or Layers() with nothing set) is v2's world.""" task_id: str split: str @@ -353,6 +432,7 @@ class Task: bases: tuple[str, ...] = () hubs: tuple[str, ...] = () needs: dict[str, dict[str, int]] = field(default_factory=dict) + layers: Layers | None = None @classmethod def from_dict(cls, d: Mapping) -> "Task": @@ -361,19 +441,24 @@ def from_dict(cls, d: Mapping) -> "Task": {c: Airport.from_dict(c, a) for c, a in d["airports"].items()}, dict(d["artcc"]), tuple(Tail.from_dict(t) for t in d["tails"]), tuple(Flight.from_dict(f) for f in d["flights"]), {h: Programs.from_dict(p) for h, p in d["programs"].items()}, dict(d["clauses"]), - dict(d["sources"]), d["version"], tuple(d.get("bases", ())), tuple(d.get("hubs", ())), - {fam: dict(by) for fam, by in d.get("needs", {}).items()}) + dict(d["sources"]), VERSION if d["version"] == LAYERED else d["version"], + tuple(d.get("bases", ())), tuple(d.get("hubs", ())), + {fam: dict(by) for fam, by in d.get("needs", {}).items()}, + Layers.from_dict(d["layers"]) if "layers" in d else None) def to_dict(self) -> dict: - return {"version": self.version, "task_id": self.task_id, "split": self.split, "carrier": self.carrier, - "hub": self.hub, "date": self.date, "tz": self.tz, "base": self.base, "window": list(self.window), - "start": self.start, "slide": self.slide, "dmax": self.dmax, "cutoff": self.cutoff, - "turn": dict(self.turn), "airports": {c: a.to_dict() for c, a in self.airports.items()}, - "artcc": dict(self.artcc), "tails": [t.to_dict() for t in self.tails], - "flights": [f.to_dict() for f in self.flights], - "programs": {h: p.to_dict() for h, p in self.programs.items()}, "clauses": self.clauses, - "sources": self.sources, "bases": list(self.bases), "hubs": list(self.hubs), - "needs": {fam: dict(by) for fam, by in self.needs.items()}} + layered = self.layered + out = {"version": LAYERED if layered else self.version, "task_id": self.task_id, + "split": self.split, "carrier": self.carrier, + "hub": self.hub, "date": self.date, "tz": self.tz, "base": self.base, "window": list(self.window), + "start": self.start, "slide": self.slide, "dmax": self.dmax, "cutoff": self.cutoff, + "turn": dict(self.turn), "airports": {c: a.to_dict() for c, a in self.airports.items()}, + "artcc": dict(self.artcc), "tails": [t.to_dict() for t in self.tails], + "flights": [f.to_dict() for f in self.flights], + "programs": {h: p.to_dict() for h, p in self.programs.items()}, "clauses": self.clauses, + "sources": self.sources, "bases": list(self.bases), "hubs": list(self.hubs), + "needs": {fam: dict(by) for fam, by in self.needs.items()}} + return out | {"layers": self.layers.to_dict()} if layered else out def to_json(self) -> str: return json.dumps(self.to_dict(), separators=(",", ":")) @@ -416,14 +501,44 @@ def earliest(self, f: Flight, lb: int) -> int: """Nothing in scope leaves before the first watch; an early departure (lb < 0) leaves before its schedule.""" return max(f.sched_dep + lb, self.start) - def auto_at(self, f: Flight) -> int: + def auto_at(self, f: Flight, reserve: bool = False) -> int: """When f auto-cancels if it can't leave: at its crew deadline, never before the first watch, if its crew's - rest would end past its deadline, else at its deadline.""" - c = f.crew + rest would end past its deadline, else at its deadline; on a `reserve` crew (one granted) at its deadline.""" + c = f.crew if reserve else self.crew(f) if c and c.deadline is not None and c.deadline < f.deadline < c.rest: return max(c.deadline, self.start) return f.deadline + def reservable(self, f: Flight) -> bool: + """Whether f is a departure from a crew base with a finite reserve pool, past whose crew deadline it flies only + on a reserve crew.""" + return (self.layers is not None and f.crew is not None and f.crew.deadline is None + and f.orig in self.layers.reserves) + + def crew(self, f: Flight) -> Crew | None: + """f's crew as the world holds it without a reserve: a departure from a base with a finite pool gets its + pairing's crew deadline and rest as an outstation departure has them (repairs.crews).""" + return self._crews.get(f.id, f.crew) + + @cached_property + def _crews(self) -> dict[int, Crew]: + out = {} + for f in self.flights: + if self.reservable(f): + d = f.crew.report + f.crew.fdp + EXTENSION - f.sched_block + out[f.id] = replace(f.crew, deadline=d, rest=d + REST) + return out + + def legal(self, f: Flight, t: int, reserve: bool = False) -> int: + """Flight.legal with the crew the world holds without a reserve (Task.crew); on a `reserve` crew, f's own.""" + c = f.crew if reserve else self.crew(f) + return c.rest if c and c.deadline is not None and c.deadline < t < c.rest else t + + def late(self, f: Flight, t: int) -> bool: + """Whether f, leaving at t, flies on a reserve crew: a reservable flight past its crew deadline before its rest + ends.""" + return self.reservable(f) and self.legal(f, t) != t + @property def lb(self) -> dict[int, int]: return {f.id: f.lb for f in self.flights} @@ -435,6 +550,15 @@ def needed(self) -> Counter: return Counter({(fam, s): n for fam, by in self.needs.items() for s, n in by.items()}) return Counter((t.family, t.end_req) for t in self.tails) + @property + def layered(self) -> bool: + """Whether the task sets any of v4's layers (written as LAYERED).""" + return self.layers is not None and self.layers != Layers() + + def lag(self, kind: str) -> int: + """The minutes a news item of `kind` takes to reach the desk (Layers.lags; LAGS without layers).""" + return (self.layers.lags if self.layers else LAGS)[kind] + @property def storms(self) -> tuple[str, ...]: """The hubs whose programs the task is about: its storm hubs, or its hub.""" diff --git a/src/agentenv_airops/programs/network.py b/src/agentenv_airops/programs/network.py index f6f761c..b3c94e1 100644 --- a/src/agentenv_airops/programs/network.py +++ b/src/agentenv_airops/programs/network.py @@ -30,6 +30,15 @@ The calm day starts from the positions the schedule assumes (`scheduled`), the storm day from the record's; the record's morning against the schedule's is a finding (`shortfall`). + +With crew `bases` the build turns the crew model on, as candidates.build_candidate does (every leg's crew, Task.bases), +its pairings cut by the build's crew Rule: Edgar's proxy (repairs.crews) by default, or v4's, legal on schedule +(layers.crew). With a v4 layer config (layers.config.Config, the constants fitted on train dates) the task carries +v4's layers (docs/v4/DESIGN.md §2): the departure caps at the hubs and the arrival caps at the outstations from every +carrier's BTS flights of the date (candidates.frame), the hubs' ramp closures from their METARs and SPECIs at the +config's fitted rates, and the config's reserve pool at each of the task's crew bases, with the desk's view of them: +the closure rates, the storm hubs' METARs, SPECIs and TAFs as news (layers.weather) and the default lags (`layered`). +Without either the task is v2's, byte for byte. """ import functools @@ -50,6 +59,8 @@ from . import candidates, rbs, repairs from .advisories import HUBS from .candidates import CARRIER, EVAL_PER_HUB, EVAL_WINTER, EVAL_WINTER_SHORT, MIN_CONTROLLED, MONTHS, Built, season +from .layers import config, weather +from .layers.crew import EDGAR, Rule from .model import Busy, Flight, Move, Tail, Task Windows = dict[str, tuple[int, int]] @@ -77,9 +88,11 @@ def control_dates(inputs: Path, derived: Path, months: Sequence[str] = MONTHS) - def build(d: str, hubs: Sequence[str], inputs: Path, derived: Path, e_fit: Mapping[str, float], e_median: Mapping[str, float], scale: Mapping[str, float], cutoff: int = repairs.CUTOFF, - windows: Windows | None = None, split: str = "") -> Built: + windows: Windows | None = None, split: str = "", bases: Sequence[str] = (), + layers: config.Config | None = None, crew: Rule = EDGAR) -> Built: """The whole-network task of date d with storm `hubs`, from their rule windows or the given `windows` (a - control's), or None and the reason.""" + control's), or None and the reason; crew `bases` turn the crew model on, its pairings cut by `crew` (Edgar's or + v4's, layers.crew), a `layers` config v4's layers.""" row = {"date": d, "hubs": list(hubs), "windows": {}, "slides": {}, "first_hours": {}, "fallback": False, "task_id": None, "reason": ""} if windows: @@ -98,19 +111,31 @@ def build(d: str, hubs: Sequence[str], inputs: Path, derived: Path, e_fit: Mappi row["windows"] = {h: [60 * p0, 60 * (p0 + candidates.LENGTHS[-1])] for h, p0 in first.items()} hub = min(row["windows"], key=lambda h: (row["windows"][h][0], HUBS.index(h))) task = assemble(candidates.context(inputs, derived, d, hub), d, hub, tuple(hubs), row, e_fit, e_median, scale, - cutoff, split) + cutoff, split, bases, crew) held = task.clauses["controlled_or_held"] if not windows and held < MIN_CONTROLLED: return Built(None, {**row, "controlled_or_held": held, "reason": f"fewer than {MIN_CONTROLLED} JetBlue flights controlled or held"}) + if layers is not None: + task = layered(task, layers, inputs, derived) return Built(task, {**row, "task_id": task.task_id}) +def layered(task: Task, cfg: config.Config, inputs: Path, derived: Path) -> Task: + """The task with v4's layers at `cfg`, from the date's BTS flights of every carrier, the hubs' closures over the + months they span and the storm hubs' METARs, SPECIs and TAFs over the task's span as news.""" + spans = config.spans(inputs, tuple(candidates.months_of(task.date))) + return replace(task, layers=config.build(task, cfg, candidates.frame(inputs, derived, task.date), spans, + weather.build(task, inputs))) + + def assemble(ctx: candidates.Context, d: str, hub: str, hubs: tuple[str, ...], row: Mapping, e_fit: Mapping[str, float], - e_median: Mapping[str, float], scale: Mapping[str, float], cutoff: int, split: str) -> Task: + e_median: Mapping[str, float], scale: Mapping[str, float], cutoff: int, split: str, + bases: Sequence[str] = (), crew: Rule = EDGAR) -> Task: day = ctx.day sc = scope(day, (min(w[0] for w in row["windows"].values()), max(w[1] for w in row["windows"].values())), START) - flights = repairs.flights(day, sc, ctx.x, ctx.families, candidates.tests(ctx), cutoff=cutoff) + flights = repairs.flights(day, sc, ctx.x, ctx.families, candidates.tests(ctx), cutoff=cutoff, bases=bases, + cut=crew.crews) tails, counts = network_tails(day, sc, flights, ctx.families, day.turn) need, short = needs(tails, flights) programs = candidates.hub_programs(ctx, flights, sc.start, e_fit, e_median, scale) @@ -129,7 +154,7 @@ def assemble(ctx: candidates.Context, d: str, hub: str, hubs: tuple[str, ...], r airports=candidates.airports_of(codes), artcc={c: ctx.artcc[c] for c in sorted(codes) if c in ctx.artcc}, tails=tuple(tails), flights=tuple(flights), programs=programs, clauses=clauses, sources=candidates.pins(ctx, sc), - hubs=hubs, needs=need) + bases=tuple(bases), hubs=hubs, needs=need) return rbs.rescale(task, scale, e_fit, e_median) @@ -356,9 +381,11 @@ def shortfall(task: Task) -> dict: def build_all(jobs: Sequence[tuple[str, tuple[str, ...], Windows | None, str]], inputs: Path, derived: Path, e_fit: Mapping[str, float], e_median: Mapping[str, float], scale: Mapping[str, float], - cutoff: int = repairs.CUTOFF, workers: int = 1) -> list[Built]: + cutoff: int = repairs.CUTOFF, workers: int = 1, bases: Sequence[str] = (), + layers: config.Config | None = None, crew: Rule = EDGAR) -> list[Built]: """`build` for every (date, storm hubs, windows, split), in a pool of `workers` processes, in date order.""" - args = [(d, hubs, inputs, derived, dict(e_fit), dict(e_median), dict(scale), cutoff, windows, split) + args = [(d, hubs, inputs, derived, dict(e_fit), dict(e_median), dict(scale), cutoff, windows, split, tuple(bases), + layers, crew) for d, hubs, windows, split in sorted(jobs, key=lambda j: j[0])] with ProcessPoolExecutor(workers) if workers > 1 else nullcontext() as pool: if not pool: @@ -367,8 +394,8 @@ def build_all(jobs: Sequence[tuple[str, tuple[str, ...], Windows | None, str]], def build_one(args: tuple) -> Built: - d, hubs, inputs, derived, e_fit, e_median, scale, cutoff, windows, split = args - return build(d, hubs, inputs, derived, e_fit, e_median, scale, cutoff, windows, split) + d, hubs, inputs, derived, e_fit, e_median, scale, cutoff, windows, split, bases, layers, crew = args + return build(d, hubs, inputs, derived, e_fit, e_median, scale, cutoff, windows, split, bases, layers, crew) def split(hubs: Mapping[str, Sequence[str]], event: Mapping[str, int], seed: int) -> dict[str, str]: diff --git a/src/agentenv_airops/programs/probe.py b/src/agentenv_airops/programs/probe.py new file mode 100644 index 0000000..8d21417 --- /dev/null +++ b/src/agentenv_airops/programs/probe.py @@ -0,0 +1,125 @@ +"""Which of JetBlue's cancellations could have flown on the day as it happened (the v2 probe, ~/handoff/out/v2_probe.py, +and docs/v4/DESIGN.md §0's could-fly-for-free share). + +Each JetBlue cancellation is put back on the airline replay (references.airline_plan, played on the truth by +sim.execute): its plan entry becomes {}, so it flies as early as it can and takes whatever slot the sim gives it, and +the day is flown again. A restore that flies every restored leg is `clean` when no flight the replay flew is knocked out +and none moves more than SHIFT_OK minutes and the end-of-day ferries cost no more, `strands` when only the ferries cost +more, `displaces` otherwise; one that leaves a restored leg unflown is `forced`. A cancellation that is forced or +strands alone is tried again with its out-and-back partner (`partner`: the cancelled leg back the other way, same +family, within PAIR_WINDOW of it), and takes the pair's verdict as `pair_` unless the pair is forced too. + +A cancellation could have flown for free when its verdict is clean or pair_clean. A pair is one restore however many of +its legs reach it: `Probe.restores` keys each restore by the flights it put back, so the share over JetBlue's +cancellations counts a restored pair once (`Probe.feasible`), where the v2 probe's rows count it once per leg +(`Probe.feasible_rows`). +""" + +from collections import Counter +from collections.abc import Mapping, Sequence +from dataclasses import dataclass + +from ..core.sim import FLOWN +from . import references, sim +from .model import Flight, Task + +SHIFT_OK = 15 +PAIR_WINDOW = 360 +VERDICTS = ("clean", "strands", "displaces", "forced", "pair_clean", "pair_strands", "pair_displaces") +FEASIBLE = ("clean", "pair_clean") + + +@dataclass(frozen=True) +class Restore: + """What putting `restored` back on the replay did: whether each restored leg flew, the replay's flights it knocked + out, the most another flown flight moved, and the change in the day's cost and its ferries' cost.""" + + restored: tuple[int, ...] + flew: bool + knocked: tuple[int, ...] + max_shift: int + d_cost: int + d_ferry: int + + @property + def verdict(self) -> str: + if not self.flew: + return "forced" + if self.knocked or self.max_shift > SHIFT_OK: + return "displaces" + return "clean" if self.d_ferry <= 0 else "strands" + + +@dataclass(frozen=True) +class Probe: + """Each JetBlue cancellation's verdict (`rows`, flight id -> verdict) and the restore it took (`restores`, the + flights put back -> verdict, a pair once).""" + + rows: dict[int, str] + restores: dict[tuple[int, ...], str] + + @property + def cancelled(self) -> int: + return len(self.rows) + + @property + def feasible(self) -> int: + """The restores that fly for free, a pair once.""" + return sum(v in FEASIBLE for v in self.restores.values()) + + @property + def feasible_rows(self) -> int: + """The cancellations whose verdict flies for free, as the v2 probe counted them (a pair once per leg).""" + return sum(v in FEASIBLE for v in self.rows.values()) + + def to_dict(self) -> dict: + return {"cancelled": self.cancelled, "feasible": self.feasible, "feasible_rows": self.feasible_rows, + "restores": len(self.restores), "pairs": sum(len(k) > 1 for k in self.restores), + "verdicts": counted(self.rows.values()), "restore_verdicts": counted(self.restores.values())} + + +def probe(task: Task) -> Probe: + """Every JetBlue cancellation of `task` restored on the airline replay, alone and, when that is forced or strands, + with its partner.""" + plan = references.airline_plan(task) + base = sim.execute(task, plan) + cancelled = [f for f in task.flights if f.actual.dep is None] + rows, restores = {}, {} + for f in cancelled: + one = restore(task, plan, base, [f.id]) + use = one + g = partner(f, cancelled) if one.verdict in ("forced", "strands") else None + if g is not None: + two = restore(task, plan, base, [f.id, g.id]) + if two.verdict != "forced": + use = two + v = use.verdict if use is one else f"pair_{use.verdict}" + rows[f.id] = v + restores.setdefault(tuple(sorted(use.restored)), v) + return Probe(rows, restores) + + +def restore(task: Task, plan: Mapping[int, Mapping], base: sim.Execution, ids: Sequence[int]) -> Restore: + """The replay `plan` (run as `base`) with the flights `ids` put back ({}) and the day flown again.""" + run = sim.execute(task, {**plan, **{i: {} for i in ids}}) + others = [i for i in run.flights if i not in ids] + knocked = tuple(i for i in others if base.flights[i].status == FLOWN and run.flights[i].status != FLOWN) + shift = max((run.flights[i].dep - base.flights[i].dep for i in others + if base.flights[i].status == FLOWN and run.flights[i].status == FLOWN), default=0) + return Restore(tuple(ids), all(run.flights[i].status == FLOWN for i in ids), knocked, shift, + run.cost.total - base.cost.total, run.cost.ferry - base.cost.ferry) + + +def partner(f: Flight, cancelled: Sequence[Flight]) -> Flight | None: + """v1's rule: the cancelled leg back the other way, same family, within PAIR_WINDOW of this one's arrival or + departure; the return after it first, else the leg before it.""" + back = [g for g in cancelled if g.orig == f.dest and g.dest == f.orig and g.family == f.family] + after = [g for g in back if f.sched_arr <= g.sched_dep <= f.sched_arr + PAIR_WINDOW] + before = [g for g in back if g.sched_arr <= f.sched_dep <= g.sched_arr + PAIR_WINDOW] + pool = sorted(after, key=lambda g: g.sched_dep) + sorted(before, key=lambda g: -g.sched_arr) + return pool[0] if pool else None + + +def counted(verdicts) -> dict[str, int]: + c = Counter(verdicts) + return {v: c[v] for v in VERDICTS if c[v]} diff --git a/src/agentenv_airops/programs/references.py b/src/agentenv_airops/programs/references.py index f2386a4..02fa630 100644 --- a/src/agentenv_airops/programs/references.py +++ b/src/agentenv_airops/programs/references.py @@ -24,24 +24,33 @@ arrival order as slot claims (each claims the slot of its recorded departure plus its scheduled block); auto-cancels are counted over the flights it flew by their deadline, which covers dmax and the hub cutoff, less those whose aircraft a leg it flew past its deadline brought (`counted`); a flight it flew past its crew deadline still counts, - which is what tells a crew model too strict for JetBlue's day; + which is what tells a crew model too strict for JetBlue's day. With a finite reserve pool at a base (§3.4 of + docs/v4/DESIGN.md) it calls a reserve for each departure from that base its replay with unlimited reserves (the v2 + replay) flies past its crew deadline (Task.crew), JetBlue's own departure or a later one its aircraft or the + programs make, granted while the pool lasts (sim.granted), and one the pool can't crew auto-cancels; - recorded day: JetBlue's real outcome priced directly, its aircraft's minutes on its recorded departures and blocks, cancellations decided at sched - NOTICE, plus end-of-day ferries from its recorded end positions to tomorrow's needs. A diverted flight that never reached its destination counts as cancelled when it left, the moment the desk knew; - calm day: from the positions the schedule assumes, every aircraft ready at the start and tomorrow's needs following them (network.scheduled), no delay, scheduled blocks, no programs: the hindsight optimum's forced cancellations and ferries; with `record`, from the record's positions and the task's needs, as the storm day starts. +The watches are hourly (`watch_times`), and on a layered task they also wake on its events: an advisory or SPECI +reaching the desk, a ramp closure starting (docs/v4/DESIGN.md §4); the follower, the rule and the playable day share +them. Like for like, nothing is decided before the first watch: a JetBlue cancellation due earlier than the start counts as decided there, as the anchor's and every desk's do. At a watch the day so far is history, as the env shows it: a flight that has left on the truth leaves when it did in the follower's and the rule's projection and is pinned there in -the follower's MIP, and every other flight leaves no earlier than the watch. No reference may cost less than the +the follower's MIP, and every other flight leaves no earlier than the watch. With a reserve pool the follower's plan +calls the reserves its last solve flies past a crew deadline, a pinned flight keeps the reserve the plan gave it, and +the pinned flights the pool granted one hold it in every later solve. No reference may cost less than the anchor (`below_anchor`). """ +import hashlib from collections.abc import Mapping -from dataclasses import dataclass +from dataclasses import dataclass, replace -from ..core.known import FREEZE, LOOK, STEP +from ..core.known import CAP_LOOK, FREEZE, LOOK, STEP from ..core.sim import AUTO_CANCELLED, CANCELLED, FLOWN from . import mip, network, sim from .advisories import HUBS @@ -49,6 +58,7 @@ from .model import Flight, Task TIME_LIMIT = 2400.0 +WATCH_MIN = 15 @dataclass(frozen=True) @@ -108,17 +118,20 @@ def follow(task: Task, programs: bool = True, time_limit: float = TIME_LIMIT, ca pinned |= frozen(run, t) fixed = {i: run.flights[i].dep if run.flights[i].status == FLOWN else None for i in pinned} lapsed = {i for i in pinned if run.flights[i].status == AUTO_CANCELLED} - price = {f.id: _price(f, plan.get(f.id, {}), task.auto_at(f) if f.id in pinned else t) for f in task.flights} + reserved = run.reserved & pinned + price = {f.id: _price(f, plan.get(f.id, {}), task.auto_at(f, f.id in reserved) if f.id in pinned else t) + for f in task.flights} sol = mip.solve(task, world, price, fixed, t + FREEZE if t > task.start else None, time_limit=time_limit, - canonical=canonical, start=run, lapsed=lapsed) + canonical=canonical, start=run, lapsed=lapsed, reserved=reserved) if not sol.proven: raise RuntimeError(f"{task.task_id}: watch {t} solve {sol.status} at gap {sol.gap_pct}%") cancelled = sorted(i for i, d in sol.deps.items() if i not in pinned and d is None and i not in sol.lapsed) plan = {i: v for i, v in plan.items() if i in pinned} | {i: {"cancel": True, "decided": t} for i in cancelled} pinned |= set(cancelled) deps = {i: sol.lapsed.get(i, d) for i, d in sol.deps.items() if i not in pinned} + plan |= {i: {"reserve": True} for i in sorted(sol.reserve - pinned)} held = deliberate(task, plan, deps, world, t) - plan |= {i: {"not_before": d} for i, d in held.items()} + plan |= {i: {**plan.get(i, {}), "not_before": d} for i, d in held.items()} log.append({"t": t, "frozen": len(pinned), "cancelled": cancelled, "held": len(held), "planned": sol.objective, "status": sol.status, "gap_pct": sol.gap_pct, "canonical": sol.canonical, "wall_s": sol.wall_s, "cols": sol.cols, "rows": sol.rows}) @@ -154,8 +167,13 @@ def naive(task: Task) -> sim.Execution: def airline_plan(task: Task) -> dict[int, dict]: - return {f.id: {"cancel": True, "decided": max(f.sched_dep - NOTICE, task.start)} if f.actual.dep is None + plan = {f.id: {"cancel": True, "decided": max(f.sched_dep - NOTICE, task.start)} if f.actual.dep is None else {"not_before": f.actual.dep, "claim": f.actual.dep + f.sched_block} for f in task.flights} + if not any(task.reservable(f) for f in task.flights): + return plan + free = sim.execute(replace(task, layers=replace(task.layers, reserves={})), plan).flights + return {i: e | {"reserve": True} if free[i].dep is not None and task.late(task.flight(i), free[i].dep) else e + for i, e in plan.items()} def airline(task: Task) -> Replay: @@ -197,14 +215,42 @@ def calm(task: Task, time_limit: float = TIME_LIMIT, record: bool = False) -> Ca def watch_times(task: Task) -> list[int]: - """Every STEP minutes from the start, through the first watch that knows every delay, while some flight could - still leave unfrozen (t + FREEZE <= its deadline).""" - reveal = max(f.sched_dep - LOOK for f in task.flights) + """Every STEP minutes from the start, through the first watch that knows every delay (on a layered task every + departure and outstation arrival cap too, known CAP_LOOK ahead as v1's slot caps were), while some flight could + still leave unfrozen (t + FREEZE <= its deadline). A layered task's watches also wake on events (docs/v4/DESIGN.md + §4, `events`) up to that last minute: walking the hourly watches and the events in order, one closer than + WATCH_MIN to the watch before moves to WATCH_MIN after it (to the last minute if that is sooner), taking in any + others up to there, and a gap longer than STEP gets a watch every STEP, so consecutive watches are WATCH_MIN to + STEP apart but for a last one moved onto the last minute, and every event by the last minute reaches the desk.""" + hours = [h for caps in ((task.layers.dep_caps, task.layers.arr_caps) if task.layered else ()) + for by in caps.values() for h in by] + reveal = max([f.sched_dep - LOOK for f in task.flights] + [60 * h - CAP_LOOK for h in hours]) last = max(f.deadline for f in task.flights) - FREEZE ts = [task.start] while ts[-1] < reveal and ts[-1] + STEP <= last: ts.append(ts[-1] + STEP) - return ts + if not task.layered: + return ts + kept = [task.start] + for c in sorted(set(ts) | {e for e in events(task) if task.start < e <= last}): + if c <= kept[-1]: + continue + c = min(max(c, kept[-1] + WATCH_MIN), last) + while c - kept[-1] > STEP: + kept.append(kept[-1] + STEP) + kept.append(c) + return kept + + +def events(task: Task) -> list[int]: + """The minutes a desk learns something it should act on: each advisory and each SPECI as it reaches the desk + (its signature or observation plus its kind's lag, Task.lag) and each ramp closure's start and end (until its end + the day as known expects it GUESS after each watch, sim.known).""" + advs = [a.issued + task.lag("advisory") for p in task.programs.values() for a in p.advisories] + layers = task.layers + specis = [m + task.lag("speci") for m, kind, _, _ in layers.weather if kind == "speci"] + bounds = [m for spans in layers.closures.values() for a, b, _, _ in spans for m in (a, b)] + return sorted(set(advs + specis + bounds)) def frozen(run: sim.Execution, t: int) -> set[int]: @@ -223,7 +269,7 @@ def so_far(task: Task, plan: Mapping[int, Mapping], t: int, run: sim.Execution | for f in task.flights: entry, dep = plan.get(f.id, {}), run.flights[f.id].dep if dep is not None and dep < t: - out[f.id] = {"left": dep} + out[f.id] = {"left": dep} | ({"reserve": True} if entry.get("reserve") else {}) elif entry.get("cancel"): out[f.id] = dict(entry) else: @@ -251,5 +297,11 @@ def past_midnight(task: Task, run: sim.Execution | None = None) -> int: return out +def digest(task: Task) -> str: + """The task's sha256 (16 hex digits): what a pack's stored reference costs were run on (scripts/v4_pack.py + --refs), so a load can tell them from a stale file's.""" + return hashlib.sha256(task.to_json().encode()).hexdigest()[:16] + + def _price(f: Flight, entry: Mapping, at: int) -> int: return cancel_price(f, entry["decided"] if entry.get("cancel") else at) diff --git a/src/agentenv_airops/programs/repairs.py b/src/agentenv_airops/programs/repairs.py index 520042b..3d712f6 100644 --- a/src/agentenv_airops/programs/repairs.py +++ b/src/agentenv_airops/programs/repairs.py @@ -40,7 +40,8 @@ takeoff (117.19(a)(1)). A departure from a base has no crew deadline: reserves crew it. A departure from an outstation must block in by the report + the limit + EXTENSION, so its crew deadline is that less its scheduled block, static as the schedule is; after it the crew may fly again only after REST, the 10 consecutive hours of rest - 117.25(e) requires (`rest`). + 117.25(e) requires (`rest`). v4 cuts the pairings legal on schedule instead (layers.crew, a build's crew Rule), the + records alike. """ import math @@ -53,7 +54,7 @@ from ..build import fleet, pack from .advisories import HUBS -from .model import Busy, Connection, Crew, Flight, Move, Outcome, Tail +from .model import EXTENSION, REST, Busy, Connection, Crew, Flight, Move, Outcome, Tail FAMILIES = ("A320", "A321", "A321P", "A220", "E190") MIN_TURN = 25 @@ -69,8 +70,6 @@ CUTOFF = 1440 + 180 BASES = ("JFK", "BOS", "FLL", "MCO") REPORT = 45 -EXTENSION = 120 -REST = 600 ECFR = "https://www.ecfr.gov/current/title-14/chapter-I/subchapter-G/part-117" TABLE_B = {0: (9, 9, 9, 9, 9, 9, 9), 4: (10, 10, 10, 10, 9, 9, 9), 5: (12, 12, 12, 12, 11.5, 11, 10.5), 6: (13, 13, 12, 12, 11.5, 11, 10.5), 7: (14, 14, 13, 13, 12.5, 12, 11.5), @@ -107,14 +106,16 @@ def p5(values: Iterable[float]) -> int: def flights(day: pack.Day, sc: pack.Scope, x: pd.DataFrame, families: Mapping[str, str], tests: Callable[[str, str, int, int, int], tuple[str | None, str | None, bool]], dmax: int = DMAX, - cutoff: int = CUTOFF, bases: Collection[str] = ()) -> list[Flight]: + cutoff: int = CUTOFF, bases: Collection[str] = (), + cut: Callable[..., dict[Hashable, Crew]] | None = None) -> list[Flight]: """The scope's flights, ids in (scheduled departure, flight number, origin) order. `x` holds every carrier's rows around the date with local sd/sa/ad/aa and the repaired `lb`; `tests(orig, dest, sched_arr, departs, sched_dep)` - gives (controlled, gs_held, exempt); with crew `bases` each flight has its crew.""" + gives (controlled, gs_held, exempt); with crew `bases` each flight has its crew, from `crews` or from `cut`, a + function of the same arguments (layers.crew.Rule.crews).""" crew = {} if bases: legs = day.cur[day.cur.Tail_Number.isin(sc.tails)] - crew = crews(legs, day.base, bases, {c: a["tz"] for c, a in pack.airports(set(legs.Origin)).items()}) + crew = (cut or crews)(legs, day.base, bases, {c: a["tz"] for c, a in pack.airports(set(legs.Origin)).items()}) dec = sc.dec.assign(num=sc.dec.Flight_Number_Reporting_Airline.astype(int)).sort_values(["sd", "num", "Origin"]) seats = defaultdict(list) for t in sc.tails: diff --git a/src/agentenv_airops/programs/sim.py b/src/agentenv_airops/programs/sim.py index 111a49f..9a7ea8e 100644 --- a/src/agentenv_airops/programs/sim.py +++ b/src/agentenv_airops/programs/sim.py @@ -1,12 +1,14 @@ """The storm day: v1's dispatch inside the FAA's programs at JFK and BOS, with JetBlue's slots and EDCTs in place of hourly caps. -A plan maps flight ids to {"cancel", "not_before", "claim", "decided", "left", "holds", "claims"}, every key optional; +A plan maps flight ids to {"cancel", "not_before", "claim", "decided", "left", "holds", "claims", "reserve", "order"}, +every key optional; a flight left out flies as early as it can. A cancelled flight is cancelled for good, decided at "decided" (the first watch when absent). "holds" and "claims" are decisions taken during the day, each [decided, minute or None] in time order: a hold acts from its minute (the flight tries again then, and from then on waits for it), a claim binds from its minute the first slot at or after it that no flight took and no claim holds, and a later one replaces it (None lets -go); "not_before" and "claim" act from the start. +go); "not_before" and "claim" act from the start. "order" ranks a flight among those ready at one minute in place of +its first try (crewday.named keeps the day's own, so a named replay hands out the aircraft as the day did). A flight with "left" (the day so far, at a watch) leaves at that minute whatever would hold it, counting where the MIP counts a pinned departure: on its slot if the minute plus its scheduled block is a free one of the segment governing it, on nothing if that is off the pool, else under the nominal cap. The others are dispatched as in v1: in order of @@ -15,13 +17,23 @@ (Task.quick), through any Moves it passes on the way, and any other flight only after the turn. What else holds a flight ready at minute t, in this order: - a ground stop at its destination hub covering its origin holds it to the stop's end; airborne flights fly on; +- a departure in an hour its origin caps (Layers.dep_caps, docs/v4/DESIGN.md §3.1) needs room under the cap, else + it tries again at the next hour's start; every flight that left in the hour counts, a pinned one ("left") from the + start of the day, so the others share the cap less the pinned ones; a pinned flight whose aircraft is not ready by + its minute leaves when it is, and its count moves to the hour it leaves in, where the MIP pins it; +- a departure inside a ramp closure at its origin (Layers.closures, §3.2: lightning at the field cuts the ramp's + rate, it doesn't stop it) needs room under the closure's departure cap, counted the same way over the closure's + minutes, else it tries again at the closure's end; an arrival inside a ramp closure at its destination (the minute + plus the block, as the nominal cap counts it) needs room under the closure's arrival cap, the pinned flights' arrivals + counted from the start of the day too, else it leaves so as to arrive at the closure's end; - a flight governed at t by a GDP segment (the one in force when it departs, the departure-time lock; its origin in scope and its scheduled arrival in the period) needs one of JetBlue's slots in that segment's pool: its claimed slot if it can still make it, else the earliest free slot at or after its earliest arrival t + scheduled block. It leaves at the slot less its scheduled block (its EDCT) and lands after its own block. With no free slot left before the period ends it arrives after the period; - any other arrival at a hub (exempt, uncontrolled, or past its period) needs room under the nominal cap of its - arrival hour (rbs.nominal); a slot holder does not count against it. + arrival hour (rbs.nominal), and an arrival at an outstation in an hour the station caps (Layers.arr_caps, §3.3) + under that cap, which joins the nominal caps; a slot holder does not count against it. Claims standing at the start bind first, in (claim, flight id) order, each to the first unbound slot at or after it in the pool of the segment that would govern the flight leaving at its claim less its scheduled block. A flight that can't make its claimed slot loses it and the slot is wasted, as a missed EDCT is. Any other slot is taken when its flight @@ -31,6 +43,11 @@ crew deadline may leave only from the end of its crew's rest on, still by its deadline; one that can't, its rest ending past its deadline, auto-cancels at its crew deadline, decided there, never before the first watch (Task.auto_at). +With a finite reserve pool at a crew base (Layers.reserves, §3.4) a departure from it has its pairing's crew deadline +too (Task.crew) unless the plan calls a reserve crew for it ("reserve"), which the sim grants in flight-id order while +the base's pool lasts (`granted`); a granted flight has no crew deadline, as every base departure has without the +pool, and keeps its reserve whether it needs it or not. A pinned flight flies at its minute whatever its crew, as it +did. Moves are fixed and uncosted, and bound to an aircraft (model.Move, `routes`): the one that flies the leg before the Move's break, landed and turned, is ready at the Move's destination at the Move's `ready` or then, whichever is later, @@ -62,7 +79,15 @@ and the scheduled block, the programs as issued by then, proposed GDPs included, every stop to its as-issued end, every GDP too unless a base rate ends it earlier), or the calm day (no delay, scheduled blocks, no programs). Nominal caps, deadlines, the hub cutoff and the crew deadlines, which the schedule fixes and the desk knows from the start, are -the same in all of them. +the same in all of them. The calm day carries no layer. The day as known carries the layers as the desk sees them +(docs/v4/DESIGN.md §4): the departure caps and outstation arrival caps of the hours starting within CAP_LOOK of the +watch, as v1's slot caps were known, the later hours uncapped; the ramp closures begun by the watch, with their end +once it has come, else expected GUESS minutes after the watch (a placeholder for the train days' median remaining +duration, to be fitted with the pack's calibration), an ended one at its caps as given and one under way at the caps +its hub's rate (Layers.rates) gives that expected window, floored only at the pushbacks and arrivals before the watch +(closures.caps), so neither its true length nor what flew inside it later shows; and the reserve pools as they are +(Task.layers, which `granted` and the MIP read), whose sizes the desk knows. A task without layers knows the v2 day as +before. `switch` carries the layers' counts over as it does the slots and nominal hours. """ import bisect @@ -70,13 +95,14 @@ import math from collections import Counter, defaultdict from collections.abc import Collection, Mapping -from dataclasses import dataclass, replace +from dataclasses import dataclass, field, replace -from ..core.known import LOOK +from ..core.known import CAP_LOOK, LOOK from ..core.sim import AUTO_CANCELLED, CANCELLED, FLOWN, Ferry, _assign from . import pax as passengers from . import rbs, repairs from .cost import Cost, ferry_cost, priced +from .layers import closures from .model import Flight, Move, Segment, Task Plan = Mapping[int | str, Mapping] @@ -85,6 +111,7 @@ SlotKey = tuple[str, int] HourKey = tuple[str, int] Route = tuple[str, tuple[Move, ...]] +GUESS = 30 @dataclass(frozen=True) @@ -94,12 +121,20 @@ class World: segments: Mapping[str, tuple[Segment, ...]] stops: tuple nominal: Mapping[str, Mapping[int, int]] + dep_caps: Mapping[str, Mapping[int, int]] = field(default_factory=dict) + closures: Mapping[str, tuple[tuple[int, int, int, int], ...]] = field(default_factory=dict) + gates: Mapping[str, int] = field(default_factory=dict) + broken: Mapping[str, int] = field(default_factory=dict) + swaps: frozenset[str] | None = None def truth(task: Task) -> World: + layers = task.layers return World(task.lb, {f.id: f.block for f in task.flights}, {h: p.segments for h, p in task.programs.items()}, - tuple(s for p in task.programs.values() for s in p.stops), nominal_caps(task)) + tuple(s for p in task.programs.values() for s in p.stops), + nominal_caps(task) | (layers.arr_caps if layers else {}), + layers.dep_caps if layers else {}, layers.closures if layers else {}) def known(task: Task, t: int, programs: bool = True, early: int = 0) -> World: @@ -113,7 +148,39 @@ def known(task: Task, t: int, programs: bool = True, early: int = 0) -> World: issued = {h: rbs.forecast(task, h, t, early) for h in task.programs} segments = {h: s for h, (s, _) in issued.items()} stops = tuple(p for _, ps in issued.values() for p in ps) - return World(lb, block, segments, stops, nominal_caps(task)) + if not task.layered: + return World(lb, block, segments, stops, nominal_caps(task)) + layers = task.layers + return World(lb, block, segments, stops, nominal_caps(task) | _ahead(layers.arr_caps, t), + _ahead(layers.dep_caps, t), closures_known(task, t)) + + +def _ahead(caps: Mapping[str, Mapping[int, int]], t: int) -> dict[str, dict[int, int]]: + """The caps of the hours starting within CAP_LOOK of t, per station that has any.""" + out = {s: {h: c for h, c in by.items() if 60 * h <= t + CAP_LOOK} for s, by in caps.items()} + return {s: by for s, by in out.items() if by} + + +def closures_known(task: Task, t: int) -> dict[str, tuple[tuple[int, int, int, int], ...]]: + """The ramp closures begun by t: those ended by t as they ran, the others expected to end GUESS after t, priced + at their hub's rate over that window. A closure under way at a hub without a rate is a ValueError: the truth's caps + would show its true length.""" + layers, out = task.layers, {} + for s, spans in layers.closures.items(): + shut = [] + for a, b, dep, arr in spans: + if a > t: + continue + if b <= t: + shut.append((a, b, dep, arr)) + continue + if s not in layers.rates: + raise ValueError(f"{s}'s closure {a}-{b} is under way at {t} but {s} has no rate to price it as known") + end = max(t, a) + GUESS + shut.append((a, end, *closures.caps(task, s, layers.rates[s], a, end, by=t))) + if shut: + out[s] = tuple(shut) + return out def calm(task: Task) -> World: @@ -134,7 +201,8 @@ def with_cutoff(task: Task, cutoff: int) -> Task: class Rules: """What holds each flight at a minute in a world: the stops at its destination covering its origin, the segment - governing it (None when out of scope or outside the period), and the nominal cap of its arrival hour.""" + governing it (None when out of scope or outside the period), the nominal cap of its arrival hour, the departure + cap of its hour of leaving, and the ramp closures it leaves and arrives inside.""" def __init__(self, task: Task, world: World): self.world = world @@ -167,6 +235,37 @@ def hour(self, f: Flight, t: int) -> tuple[HourKey, int] | None: h = (t + self.world.block[f.id]) // 60 return ((f.dest, h), caps[h]) if caps is not None and h in caps else None + def dep_hour(self, f: Flight, t: int) -> tuple[HourKey, int] | None: + """The departure cap that f, leaving at t, counts in at its origin.""" + caps = self.world.dep_caps.get(f.orig) + h = t // 60 + return ((f.orig, h), caps[h]) if caps is not None and h in caps else None + + def closure_dep(self, f: Flight, t: int) -> tuple[HourKey, int, int] | None: + """The ramp closure f, leaving at t, leaves inside at its origin: (origin, its start), its departure cap, its + end.""" + for a, b, cap, _ in self.world.closures.get(f.orig, ()): + if a <= t < b: + return (f.orig, a), cap, b + return None + + def closure_arr(self, f: Flight, t: int) -> tuple[HourKey, int, int] | None: + """The ramp closure f, leaving at t, arrives inside at its destination (t plus the block): (destination, its + start), its arrival cap, its end.""" + arr = t + self.world.block[f.id] + for a, b, _, cap in self.world.closures.get(f.dest, ()): + if a <= arr < b: + return (f.dest, a), cap, b + return None + + def leaving(self, f: Flight, t: int) -> tuple[tuple[str, HourKey], ...]: + """The layers' rows f, leaving at t, counts in: ("dep", its capped hour), ("closure_dep", the closure it leaves + inside) and ("closure_arr", the closure it arrives inside).""" + rows = (("dep", self.dep_hour(f, t)), ("closure_dep", self.closure_dep(f, t)), + ("closure_arr", self.closure_arr(f, t))) + return tuple((kind, x[0]) for kind, x in rows if x is not None) + + @dataclass(frozen=True) class FlightRun: @@ -194,6 +293,8 @@ class Execution: moves: dict[int, int | None] cost: Cost pax: passengers.Day | None = None + reserved: frozenset[int] = frozenset() + order: dict[int, int] = field(default_factory=dict, compare=False, repr=False) @property def counts(self) -> dict[str, int]: @@ -226,6 +327,7 @@ def __init__(self, task: Task, plan: Plan, world: World, crew=None, pax: passeng start = task.start self.cancelled = {i: start if d.get("decided") is None else d["decided"] for i, d in want.items() if d.get("cancel")} + self.reserved = granted(task, want) self.runs: dict[int, FlightRun] = {} self.when: dict[int, float] = {} self.first: dict[int, int] = {} @@ -238,8 +340,8 @@ def __init__(self, task: Task, plan: Plan, world: World, crew=None, pax: passeng continue heap.append((self.cancelled[f.id], CANCEL, self.cancelled[f.id], f.id)) c = d["left"] if d.get("left") is not None else max(task.earliest(f, world.lb[f.id]), hold(d, start)) - self.when[f.id] = self.first[f.id] = c - heap.append((c, FLY, c, f.id)) + self.when[f.id], self.first[f.id] = c, d.get("order", c) + heap.append((c, FLY, self.first[f.id], f.id)) heap += [(m, WAKE, m, f.id) for m in changes(d, start)] if crew is not None: heap += [(m, WAKE, m, -1) for m in crew.changes()] @@ -259,9 +361,20 @@ def __init__(self, task: Task, plan: Plan, world: World, crew=None, pax: passeng self.bound, self.claimed = _claims(task, ((claim_at(d, start), i) for i, d in want.items() if i not in self.cancelled), self.rules, self.taken) self.used: Counter = Counter() + self.pin() self.legs: dict[str, list[int]] = {p: [] for p in self.ready} self.waiting: dict[tuple[str, str], set[int]] = defaultdict(set) self.quick: dict[str, tuple[int, int]] = {} + self.gate_wait: dict[str, set[int]] = defaultdict(set) + self.families = sorted(set(self.family.values())) + self.own = {i: t.tail for t in task.tails for i in t.legs} + + def pin(self) -> None: + """Count the pinned flights not gone yet ("left") in the layers' rows from now: the others share each cap less + them, and a pinned flight's count moves to the hour it leaves in when it goes.""" + for i, d in self.want.items(): + if d.get("left") is not None and i not in self.cancelled and i not in self.runs: + self.used.update(self.rules.leaving(self.task.flight(i), d["left"])) def until(self, stop: float = math.inf) -> None: heap = self.heap @@ -303,9 +416,10 @@ def step(self, t: int, kind: int, c: int, i: int) -> None: when[i] = held heapq.heappush(heap, (held, kind, c, i)) return - s = t if left else f.legal(t) + reserve = i in self.reserved + s = t if left else task.legal(f, t, reserve) if s > f.deadline: - runs[i] = FlightRun(i, AUTO_CANCELLED, decided=task.auto_at(f)) + runs[i] = FlightRun(i, AUTO_CANCELLED, decided=task.auto_at(f, reserve)) if crew is not None: crew.cancel(i, runs[i].decided) return @@ -315,15 +429,25 @@ def step(self, t: int, kind: int, c: int, i: int) -> None: heapq.heappush(heap, (s, kind, c, i)) return here = self.at[(f.family, f.orig)] - free = [p for p in here if self.soon(p, i) <= t] + broken = self.world.broken + able = [p for p in here if broken.get(p, math.inf) > t] if broken else here + swaps = self.world.swaps + if swaps is not None and f.orig not in swaps and self.own.get(i) in able: + able = [self.own[i]] + free = [p for p in able if self.soon(p, i) <= t] if not free: - _wait(self.waiting[(f.family, f.orig)], i, [self.soon(p, i) for p in here], when, heap, kind, c) + _wait(self.waiting[(f.family, f.orig)], i, [self.soon(p, i) for p in able], when, heap, kind, c) return self.waiting[(f.family, f.orig)].discard(i) + if not left and (full := self.full(i, f)) is not None: + self.gate_wait[full].add(i) + when[i] = math.inf + return if left: - s, slot, hour = t, *_history(f, t, self.rules, self.taken) + self.used.subtract(self.rules.leaving(f, entry["left"])) + s, slot, counts = t, *_history(f, t, self.rules, self.taken) else: - s, slot, hour = _leave(f, t, self.rules, self.claimed.get(i), self.bound, self.taken, self.used) + s, slot, counts = _leave(f, t, self.rules, self.claimed.get(i), self.bound, self.taken, self.used) if s != t: when[i] = s heapq.heappush(heap, (s, kind, c, i)) @@ -343,6 +467,10 @@ def step(self, t: int, kind: int, c: int, i: int) -> None: quick, ready = self.quick, self.ready p = min(free, key=lambda p: (quick.get(p, (None,))[0] != i, ready[p], p)) here.remove(p) + for j in self.gate_wait.pop(f.orig, ()): + if j not in runs: + when[j] = t + heapq.heappush(heap, (t, FLY, self.first[j], j)) arr = t + self.world.block[i] back = task.back(f, arr) carried = self.carried @@ -354,8 +482,7 @@ def step(self, t: int, kind: int, c: int, i: int) -> None: quick[p] = (task.quick[i][0], max([turned, *(m.ready for m in carried[i][1])]) if i in carried else turned) if slot is not None: self.taken.add(slot) - if hour is not None: - self.used[hour] += 1 + self.used.update(counts) self.legs[p].append(i) runs[i] = FlightRun(i, FLOWN, p, t, arr, slot[1] if slot else None, slot[0] if slot else None) if crew is not None: @@ -369,6 +496,18 @@ def step(self, t: int, kind: int, c: int, i: int) -> None: when[j] = self.soon(p, j) heapq.heappush(heap, (when[j], FLY, self.first[j], j)) + def full(self, i: int, f: Flight) -> str | None: + """The station whose gates f's aircraft would end at, when every gate there is taken by an aircraft on the + ground or inbound (World.gates), else None.""" + gates = self.world.gates + if not gates: + return None + s = self.carried[i][0] if i in self.carried else f.dest + cap = gates.get(s) + if cap is None or sum(len(self.at[(fam, s)]) for fam in self.families) < cap: + return None + return s + def rebind(self, i: int, t: int) -> list[int]: """Flight i's claim from minute t: the slot it held, if any, is free again, and a new claim binds the first slot at or after it in the pool that would govern it, neither taken nor bound.""" @@ -385,28 +524,29 @@ def rebind(self, i: int, t: int) -> list[int]: return [i] def switch(self, world: World, t: int) -> None: - """The day from minute t on in `world`: the flights gone keep their minutes and count again on its slots and - nominal hours, the claims standing at t bind again in its pools, and every flight not gone tries again from - t on its delays, blocks and programs, never earlier, its place among the flights of a minute read from them - too.""" + """The day from minute t on in `world`: the flights gone keep their minutes and count again on its slots, + nominal hours and layers' rows (the pinned flights not gone too), the claims standing at t bind again in its + pools, and every flight not gone tries again from t on its delays, blocks and programs, never earlier, its + place among the flights of a minute read from them too.""" task = self.task self.world, self.rules = world, Rules(task, world) + self.gate_wait = defaultdict(set) if self.crew is not None: self.crew.lb = world.lb self.taken, self.used = set(), Counter() for _, i in sorted((r.dep, i) for i, r in self.runs.items() if r.status == FLOWN): - slot, hour = _history(task.flight(i), self.runs[i].dep, self.rules, self.taken) + slot, counts = _history(task.flight(i), self.runs[i].dep, self.rules, self.taken) if slot is not None: self.taken.add(slot) - if hour is not None: - self.used[hour] += 1 + self.used.update(counts) + self.pin() self.bound, self.claimed = _claims(task, ((claim_at(d, t), i) for i, d in self.want.items() if i not in self.runs and i not in self.cancelled), self.rules, self.taken) for f in task.flights: if f.id not in self.runs: entry = self.want.get(f.id, {}) - self.first[f.id] = max(task.earliest(f, world.lb[f.id]), hold(entry, task.start)) + self.first[f.id] = entry.get("order", max(task.earliest(f, world.lb[f.id]), hold(entry, task.start))) self.when[f.id] = c = max(t, task.earliest(f, world.lb[f.id]), hold(entry, t)) heapq.heappush(self.heap, (c, FLY, self.first[f.id], f.id)) @@ -414,7 +554,7 @@ def execution(self) -> Execution: self.until() task, runs = self.task, self.runs for f in task.flights: - runs.setdefault(f.id, FlightRun(f.id, AUTO_CANCELLED, decided=task.auto_at(f))) + runs.setdefault(f.id, FlightRun(f.id, AUTO_CANCELLED, decided=task.auto_at(f, f.id in self.reserved))) ends = {p: station for (_, station), ps in self.at.items() for p in ps} out = ferries(task, ends, self.family) wasted = tuple(sorted(k for k, i in self.bound.items() if runs[i].status != FLOWN or @@ -426,7 +566,22 @@ def execution(self) -> Execution: {i: r.decided for i, r in runs.items() if r.status != FLOWN}, [(x.family, x.orig, x.dest) for x in out], day) return Execution({i: runs[i] for i in sorted(runs)}, {p: tuple(v) for p, v in self.legs.items()}, - dict(sorted(ends.items())), out, wasted, self.moves, cost, day) + dict(sorted(ends.items())), out, wasted, self.moves, cost, day, self.reserved, + dict(self.first)) + + +def granted(task: Task, plan: Plan) -> frozenset[int]: + """The flights whose reserve call (`reserve` in the plan) the pool grants: those not cancelled, reservable + (Task.reservable), in flight-id order while their base's pool lasts.""" + pool = Counter(task.layers.reserves if task.layers else {}) + want = {int(k): v for k, v in plan.items()} + out = set() + for f in sorted(task.flights, key=lambda f: f.id): + d = want.get(f.id, {}) + if d.get("reserve") and not d.get("cancel") and task.reservable(f) and pool[f.orig] > 0: + pool[f.orig] -= 1 + out.add(f.id) + return frozenset(out) def hold(entry: Mapping, t: int) -> int: @@ -513,13 +668,27 @@ def _claims(task: Task, claims, rules: Rules, taken: Collection[SlotKey], bound: def _leave(f: Flight, t: int, rules: Rules, claim: SlotKey | None, bound: Mapping[SlotKey, int], taken: set[SlotKey], - used: Counter) -> tuple[int, SlotKey | None, HourKey | None]: - """The first minute from t at which f may leave, with the slot it takes there or the nominal hour it counts in.""" + used: Counter) -> tuple[int, SlotKey | None, tuple]: + """The first minute from t at which f may leave, with the slot it takes there and the caps it counts in: the + nominal hour it lands in without a slot, and its layers' rows (Rules.leaving).""" while True: held = rules.held(f, t) if held != t: t = held continue + dep = rules.dep_hour(f, t) + if dep is not None and used[("dep", dep[0])] >= dep[1]: + t = 60 * (dep[0][1] + 1) + continue + shut = rules.closure_dep(f, t) + if shut is not None and used[("closure_dep", shut[0])] >= shut[1]: + t = shut[2] + continue + shut = rules.closure_arr(f, t) + if shut is not None and used[("closure_arr", shut[0])] >= shut[1]: + t = shut[2] - rules.world.block[f.id] + continue + out = rules.leaving(f, t) seg = rules.governs(f, t) if seg is not None: eta = t + f.sched_block @@ -532,7 +701,7 @@ def _leave(f: Flight, t: int, rules: Rules, claim: SlotKey | None, bound: Mappin if s - f.sched_block != t: t = s - f.sched_block continue - return t, (seg.id, s), None + return t, (seg.id, s), out if eta <= seg.end: t = seg.end + 1 - f.sched_block continue @@ -540,17 +709,19 @@ def _leave(f: Flight, t: int, rules: Rules, claim: SlotKey | None, bound: Mappin if cap is not None and used[cap[0]] >= cap[1]: t = 60 * (cap[0][1] + 1) - rules.world.block[f.id] continue - return t, None, cap[0] if cap else None + return t, None, out + ((cap[0],) if cap else ()) -def _history(f: Flight, t: int, rules: Rules, taken: set[SlotKey]) -> tuple[SlotKey | None, HourKey | None]: - """The slot or nominal hour a flight that left at t counts in (mip._place for a pinned copy).""" +def _history(f: Flight, t: int, rules: Rules, taken: set[SlotKey]) -> tuple[SlotKey | None, tuple]: + """The slot or nominal hour a flight that left at t counts in (mip._place for a pinned copy), and its layers' + rows.""" + out = rules.leaving(f, t) seg = rules.governs(f, t) if seg is not None and t + f.sched_block <= seg.end: key = (seg.id, t + f.sched_block) - return (key if t + f.sched_block in seg.slots and key not in taken else None), None + return (key if t + f.sched_block in seg.slots and key not in taken else None), out cap = rules.hour(f, t) - return None, cap[0] if cap else None + return None, out + ((cap[0],) if cap else ()) def ferries(task: Task, ends: Mapping[str, str], family: Mapping[str, str]) -> tuple[Ferry, ...]: diff --git a/src/agentenv_airops/realistic.py b/src/agentenv_airops/realistic.py new file mode 100644 index 0000000..fb84c14 --- /dev/null +++ b/src/agentenv_airops/realistic.py @@ -0,0 +1,255 @@ +"""Realistic mode (REALISTIC.md): a b6-v3 day in a world closer to the real one, beside the current mode, which it +leaves untouched. + +The realistic world is the day's truth with: +- v4's layers (docs/v4/DESIGN.md §3.1-§3.3 on Alec's fork): JetBlue's departure caps at the hubs and arrival caps at + the outstations, from every carrier's BTS pushbacks and arrivals, and the hubs' ramp closures from their METARs and + SPECIs at v4's measured departure rate inside a closure (JFK 411/563, BOS 449/584). v3's crew desk keeps its own + reserves, so v4's reserve pools are left out; +- gates: at most `gates[station]` JetBlue aircraft on the ground at or inbound to a station at once, the most the + pack's 70 recorded days ever had there (sim.World.gates); a flight whose station is full waits at its origin; +- carrier breakdowns: each leg's aircraft breaks an hour before its scheduled departure with JetBlue's fair-weather + code-A cancellation rate, seeded by the date, and is out for the rest of the day (sim.World.broken); +- aircraft swaps only at the hubs: at an outstation a flight takes the aircraft that flew into it on JetBlue's day + when that aircraft is there (sim.World.swaps), as an outstation rarely has a spare; the current mode swaps freely + anywhere, which absorbs the delay a late aircraft carries down its rotation; +- tomorrow: the end-of-day aircraft must cover the next day's first departures (BTS, d + 1) at each needed station; + each is priced as a ferry when it's elsewhere plus its first flight's passengers' delay when it gets there late, + never more than cancelling that flight, in place of v3's untimed end-of-day ferries. +The latest departure stays at the pack's 15 h: JetBlue's own records go past it (0.1% of its flown flights left over +18.8 h late across the pack's days), so the data give no tighter limit. No wait on the tarmac is modelled: a flight +held by its destination's programs or gates waits at the gate, so the DOT's 3-h rule can't bind. +""" + +import gzip +import hashlib +import json +import math +import random +from collections import Counter +from dataclasses import replace +from functools import cache +from pathlib import Path + +import numpy as np +import pandas as pd + +from . import realworld +from .build import bts +from .core.cost import miles +from .programs import candidates, crews, pax, sim +from .programs.advisories import HUBS +from .programs.layers import config, fairday +from .programs.model import Task +from .realworld import tomorrow, truth + +CACHE = Path.home() / "Code" / "airops-realistic-cache" +INPUTS, DERIVED = CACHE / "inputs", CACHE / "derived" +RATE = {"JFK": round(411 / 563, 3), "BOS": round(449 / 584, 3)} +BREAK_LEAD = 60 +SHORT, MIN_SCHED = 0.9, 5 +CAPACITY_Q, FAIR_LOST, FAIR_DAYS = 1.0, 0.02, 5 +CONGESTED = frozenset({"JFK", "LGA", "DCA", "EWR", "BOS"}) + + +def gates(dates_dir: Path) -> dict[str, int]: + """The most JetBlue aircraft of a pack day's tails on the ground at or inbound to each station at once, over every + recorded day in `dates_dir`: a tail counts at its start position, then at each flown leg's end from its pushback.""" + out: Counter = Counter() + for p in sorted(dates_dir.glob("*.json.gz")): + t = json.loads(gzip.decompress(p.read_bytes()))["task"] + by = {f["id"]: f["actual"] for f in t["flights"]} + ev = [] + for tl in t["tails"]: + ev.append((-math.inf, 1, tl["pos"])) + cur = tl["pos"] + for i in sorted((i for i in tl["legs"] if by[i]["dep"] is not None and by[i]["end"]), + key=lambda i: by[i]["dep"]): + ev += [(by[i]["dep"], -1, cur), (by[i]["dep"], 1, by[i]["end"])] + cur = by[i]["end"] + now, top = Counter(), Counter() + for _, d, s in sorted(ev, key=lambda e: (e[0], e[1])): + now[s] += d + top[s] = max(top[s], now[s]) + for s, n in top.items(): + out[s] = max(out[s], n) + return dict(sorted(out.items())) + + +@cache +def breakdown_rate(months: tuple[str, ...]) -> float: + """JetBlue's code-A cancellations per scope flight, pooled over the fair days (no B or C cancellation) of + `months`.""" + d = pd.concat([fairday.days(bts.read_ontime(bts.ontime_path(INPUTS, m), DERIVED), ("JFK", "BOS")) + for m in months], ignore_index=True) + fair = d[(d.B == 0) & (d.C == 0) & (d.flights > 0)] + return float(fair.A.sum() / fair.flights.sum()) + + +def layered(task: Task) -> Task: + """The task with v4's ramp closures (no reserve pools) and, in place of v4's caps, every station's departure and + arrival caps in every hour (`capacity`).""" + cfg = config.Config(rate=RATE) + months = tuple(candidates.months_of(task.date)) + frame = candidates.frame(INPUTS, DERIVED, task.date) + layers = config.build(task, cfg, frame, config.spans(INPUTS, months)) + fair = fair_capacity(months, task.tz) + return replace(recorded_slots(task), layers=replace(layers, dep_caps=capacity(task, frame, fair, "dep"), + arr_caps=capacity(task, frame, fair, "arr"))) + + +def recorded_slots(task: Task) -> Task: + """The task with each GDP segment's JetBlue pool holding, beside the slots ration by schedule gives JetBlue, the + slot each of JetBlue's controlled flights actually took under it: its recorded departure plus its scheduled block, + in the segment in force at that departure, past the period's end too (where the FAA's list spills on). + Rebuilding the FAA's allocation from schedules leaves JetBlue fewer slots than it used (its substitutions and the + slots others freed); the pool keeps those it used, which any of its flights may claim. Only the truth's segments + change: the day as known keeps the pools as issued (rbs.forecast).""" + programs = {} + for hub, p in task.programs.items(): + segments = [] + for s in p.segments: + took = {f.actual.dep + f.sched_block for f in task.flights + if f.controlled and f.dest == hub and f.actual.dep is not None and s.since <= f.actual.dep < s.until + and f.actual.dep + f.sched_block >= s.start} + slots = sorted(set(s.slots) | took) + holders = tuple(dict(zip(s.slots, s.holders, strict=True)).get(m, -1) for m in slots) + segments.append(replace(s, slots=tuple(slots), holders=holders)) + programs[hub] = replace(p, segments=tuple(segments)) + return replace(task, programs=programs) + + +@cache +def fair_capacity(months: tuple[str, ...], tz: str) -> dict[tuple[str, str], dict[int, int]]: + """Per (station, "dep" or "arr") and hour of the day on `tz`'s clock, the flights every carrier together pushed + back or landed in it on CAPACITY_Q (the most) of the station's fair days over `months` (days whose weather and NAS + cancellations, code B or C, are under FAIR_LOST of its flights), counting a fair day without any as 0.""" + x = pd.concat([candidates.month_frame(INPUTS, DERIVED, m) for m in months], ignore_index=True) + midnight = pd.to_datetime(x.FlightDate).dt.tz_localize(tz) + base = (midnight - pd.Timestamp("1970-01-01", tz="UTC")) // pd.Timedelta(minutes=1) + lost = (x.Cancelled == 1) & x.CancellationCode.isin(["B", "C"]) + ops = pd.concat([pd.DataFrame({"station": x.Origin, "day": x.FlightDate, "lost": lost}), + pd.DataFrame({"station": x.Dest, "day": x.FlightDate, "lost": lost})]) + share = ops.groupby(["station", "day"]).lost.mean() + fair = share[share < FAIR_LOST].reset_index()[["station", "day"]] + days = fair.groupby("station").day.nunique() + out = {} + for kind, station, when in (("dep", x.Origin, x.act_dep), ("arr", x.Dest, x.act_arr)): + hour = (when - base) // 60 + y = pd.DataFrame({"station": station, "day": x.FlightDate, "hour": hour})[when.notna() & hour.between(0, 23)] + n = y.merge(fair, on=["station", "day"]).groupby(["station", "day", "hour"]).size() + for (s, h), counts in n.groupby(level=["station", "hour"]): + if days[s] < FAIR_DAYS: + continue + values = np.concatenate([counts.to_numpy(), np.zeros(days[s] - len(counts))]) + out.setdefault((s, kind), {})[int(h)] = int(np.quantile(values, CAPACITY_Q)) + return out + + +def capacity(task: Task, frame: pd.DataFrame, fair: dict, kind: str) -> dict[str, dict[int, int]]: + """JetBlue's departure (`kind` "dep") or arrival ("arr") cap per station of the task and hour, from what every + carrier did there. An hour is short when every carrier together pushed back or landed under SHORT of what it had + scheduled in it, with at least MIN_SCHED scheduled: its capacity is what they did (v4's caps, docs/v4/DESIGN.md + §3.1, §3.3). At a CONGESTED station, where the schedule runs at what the airport handles (the FAA's slot-controlled + JFK, LGA and DCA, schedule-facilitated EWR, and JetBlue's hub BOS), any other hour's capacity is the most of what + they did and the most the station handled on its fair days in that hour of the day (`fair_capacity`), so a flight a + short hour pushes out finds the next hour as full as the other carriers left it, not empty; elsewhere, fair days + measure demand rather than capacity, and only short hours are capped. JetBlue's cap in a short hour is its share of + the hour's schedule times the capacity (v4's); in any other hour, what the capacity leaves after the other + carriers' flights, as a station's spare room goes to whoever comes for it. Either is never below what JetBlue's + flights did in the hour, nor, in an hour that isn't short, below its own schedule. A station without fair days to + measure keeps the short hours' caps only. The hubs' arrivals answer to their programs and nominal caps + (rbs.nominal) and get none. Hours run from the first watch to + the hour of the latest arrival a deadline allows.""" + lo, hi = task.start // 60, max(f.deadline + f.block for f in task.flights) // 60 + sched_col, done_col, where = (("sched_dep", "act_dep", "Origin") if kind == "dep" else + ("sched_arr", "act_arr", "Dest")) + out: dict[str, dict[int, int]] = {} + stations = {f.orig for f in task.flights} if kind == "dep" else {f.dest for f in task.flights} - set(HUBS) + for station in sorted(stations): + x = frame[frame[where] == station] + sched = Counter(((x[sched_col] - task.base) // 60).astype(int)) + done = Counter(((x[done_col].dropna() - task.base) // 60).astype(int)) + ours = [f for f in task.flights if (f.orig if kind == "dep" else f.dest) == station] + mine = Counter((f.sched_dep if kind == "dep" else f.sched_arr) // 60 for f in ours) + did = Counter((f.actual.dep if kind == "dep" else f.actual.dep + f.block) // 60 for f in ours + if f.actual.dep is not None) + by_hour = fair.get((station, kind)) + caps = {} + for h in range(lo, hi + 1): + short = sched[h] >= MIN_SCHED and done[h] < SHORT * sched[h] + if short and mine[h]: + caps[h] = max(did[h], math.floor(mine[h] / sched[h] * done[h] + 0.5)) + elif not short and by_hour is not None and station in CONGESTED: + room = max(done[h], by_hour.get(h % 24, 0)) + caps[h] = max(did[h], mine[h], room - (done[h] - did[h])) + if caps: + out[station] = caps + return out + + +def broken(task: Task, rate: float) -> dict[str, int]: + """Each tail that breaks, to the minute it does: every leg draws, in flight-id order from a seed the date fixes, + whether its scheduled tail breaks BREAK_LEAD before it; a tail breaks once, at its first draw.""" + rng = random.Random(int(hashlib.sha256(task.date.encode()).hexdigest()[:12], 16)) + tail_of = {i: t.tail for t in task.tails for i in t.legs} + out: dict[str, int] = {} + for f in sorted(task.flights, key=lambda f: f.id): + if rng.random() < rate and f.id in tail_of and tail_of[f.id] not in out: + out[tail_of[f.id]] = max(task.start, f.sched_dep - BREAK_LEAD) + return out + + +def world(task: Task, gate_caps: dict[str, int], rate: float) -> sim.World: + """The realistic truth of a layered task.""" + return truth(task, gate_caps, broken(task, rate)) + + +def first_departures(task: Task) -> dict[tuple[str, str], list[int]]: + """Per (family, station) the task needs an aircraft at tomorrow, the scheduled departures (minutes from the task's + midnight) of JetBlue's first flights of that family from it on d + 1, one per aircraft needed, earliest first; a + need with no flight found gets none (it's then priced as v3's ferry, untimed).""" + x = candidates.frame(INPUTS, DERIVED, task.date) + day = (pd.Timestamp(task.date) + pd.Timedelta(days=1)).strftime("%Y-%m-%d") + x = x[(x.Reporting_Airline == "B6") & (x.FlightDate.astype(str).str[:10] == day)] + family = {t.tail: t.family for t in task.tails} + out = {} + for (fam, s), n in task.needed.items(): + y = x[(x.Origin == s) & x.Tail_Number.map(family).eq(fam)] + out[(fam, s)] = sorted(int(v - task.base) for v in y.sched_dep)[:n] + return out + + +def rated(task: Task, run: sim.Execution, total: float, firsts: dict) -> dict: + """The day's costs in both modes' terms: v3's total, and realistic, v3's total with its untimed ferries replaced by + tomorrow's cost.""" + tmr = tomorrow(task, run, firsts) + return {"v3": total, "ferry": run.cost.ferry, "tomorrow": tmr, "realistic": total - run.cost.ferry + tmr} + + +@cache +def fare_line(year: int) -> tuple[float, float]: + """JetBlue's one-way fare against distance in `year` (the nearest year with data): a least-squares line through its + average fare in every top-1,000 city-pair market where it is the largest or the low-fare carrier (DOT Consumer + Airfare Report, Table 1).""" + d = pd.read_csv(INPUTS / "dot_airfare" / "table1.csv", usecols=["Year", "nsmiles", "carrier_lg", "fare_lg", + "carrier_low", "fare_low"]) + b6 = pd.concat([d[d.carrier_lg == "B6"].assign(f=lambda x: x.fare_lg), + d[d.carrier_low == "B6"].assign(f=lambda x: x.fare_low)]) + y = min(b6.Year.unique(), key=lambda v: abs(v - year)) + x = b6[b6.Year == y] + a, b = np.linalg.lstsq(np.vstack([np.ones(len(x)), x.nsmiles]).T, x.f, rcond=None)[0] + return float(a), float(b) + + +def od_fare(task: Task, legs: tuple[int, ...]) -> float: + """The fare of an itinerary over `legs`, by its origin-to-destination distance, in passenger-minutes.""" + a, b = fare_line(int(task.date[:4])) + o, d = task.flight(legs[0]).orig, task.flight(legs[-1]).dest + return crews.value(a + b * miles(task.airports[o], task.airports[d]), task.date) + + +def airline(task: Task, day, firsts: dict, passengers: pax.Passengers, fares: dict | None = None) -> dict: + """realworld.airline, each group's fare by od_fare unless `fares` gives it.""" + return realworld.airline(task, day, firsts, passengers, + fares or {g.id: od_fare(task, g.legs) for g in passengers.groups}) diff --git a/src/agentenv_airops/realworld.py b/src/agentenv_airops/realworld.py new file mode 100644 index 0000000..f103dc7 --- /dev/null +++ b/src/agentenv_airops/realworld.py @@ -0,0 +1,117 @@ +"""Realistic mode's world and cost at run time (REALISTIC.md), with nothing of the inputs cache: the realistic truth +and the day as known at a watch, tomorrow's cost, JetBlue's cost J of a played day and the reward on it. realistic.py +builds what these read (the layers, breakdowns, tomorrow's first departures, fares) from the cache.""" + +from collections import Counter, defaultdict +from dataclasses import replace + +from .core.cost import FERRY_BASE_MIN, FERRY_MILES_PER_MIN, K_CANCEL, miles +from .core.known import CAP_LOOK +from .core.sim import FLOWN, _assign +from .programs import pax, sim +from .programs.cost import RATES, ferry_cost +from .programs.model import Task + +SWAPS = frozenset({"JFK", "BOS"}) + + +def truth(task: Task, gate_caps: dict[str, int], down: dict[str, int]) -> sim.World: + """The realistic truth of a layered task whose tails `down` break at their minutes.""" + return replace(sim.truth(task), gates=gate_caps, broken=down, swaps=SWAPS) + + +def known(task: Task, t: int, gate_caps: dict[str, int], down: dict[str, int]) -> sim.World: + """The realistic day as known at t: sim.known's programs as issued and closures begun, the gates and the hub-only + swaps, which are rules, the tails broken by t, and each station's caps as they stand in t's hour carried over the + next CAP_LOOK minutes (a persistence forecast), not the caps those hours turn out to have.""" + w, layers = sim.known(task, t), task.layers + return replace(w, gates=gate_caps, broken={p: m for p, m in down.items() if m <= t}, swaps=SWAPS, + dep_caps=persisted(layers.dep_caps, t), + nominal=sim.nominal_caps(task) | persisted(layers.arr_caps, t)) + + +def persisted(caps: dict[str, dict[int, int]], t: int) -> dict[str, dict[int, int]]: + """Per station, the caps of the hours begun by t as they ran, and the cap of t's hour, if any, for every hour + starting within CAP_LOOK of t.""" + now, out = t // 60, {} + for s, by in caps.items(): + row = {h: c for h, c in by.items() if h < now} + if now in by: + row |= {h: by[now] for h in range(now, (t + CAP_LOOK) // 60 + 1)} + if row: + out[s] = row + return out + + +def tomorrow(task: Task, run: sim.Execution, firsts: dict[tuple[str, str], list[int]], airline: bool = False) -> int: + """The cost of covering tomorrow's first departures with the day's end-of-day aircraft: per family a min-cost + matching of aircraft (where and when each is ready) to needs (station, departure), each pair the ferry when the + aircraft is elsewhere plus the first flight's passengers' delay when it is ready late, at most cancelling it; + `airline`: the first flight's lateness at the aircraft's own crew and fixed cost (RATES gate) in place of its + passengers' delay.""" + pax = defaultdict(list) + for f in task.flights: + pax[f.family].append(f.pax) + seats = {fam: round(sum(v) / len(v)) for fam, v in pax.items()} + family = {t.tail: t.family for t in task.tails} + ready = {} + for t in task.tails: + legs = [i for i in run.legs.get(t.tail, ()) if run.flights[i].status == FLOWN] + ready[t.tail] = (run.flights[legs[-1]].arr + task.turn_for(t.family)) if legs else t.ready + total = 0 + for fam in sorted(set(family.values())): + planes = sorted(p for p in run.ends if family.get(p) == fam) + needs = [(s, d) for (g, s), n in sorted(task.needed.items()) if g == fam + for d in (firsts.get((g, s), []) + [None] * n)[:n]] + size = max(len(planes), len(needs)) + matrix = [[0] * size for _ in range(size)] + for a, p in enumerate(planes): + for b, (s, d) in enumerate(needs): + e = run.ends[p] + move = 0 if e == s else FERRY_BASE_MIN + miles(task.airports[e], task.airports[s]) / FERRY_MILES_PER_MIN + late = 0 if d is None else max(0, ready[p] + move - d) + wait = RATES[fam].gate * late if airline else min(seats[fam] * late, seats[fam] * K_CANCEL) + matrix[a][b] = ferry_cost(task, fam, e, s) + wait + total += sum(matrix[a][b] for a, b in enumerate(_assign(matrix))) + return total + + +def airline(task: Task, day, firsts: dict, passengers: pax.Passengers, fares: dict[int, float]) -> dict: + """JetBlue's own cost of a played day before its lost revenue is priced, in passenger-minutes: its aircraft's + minutes (gate, overrun, less what its cancellations saved, with that saving apart), its crews' deadheads, hotels + and end of day, tomorrow + at the aircraft's own cost; with the passenger counts and the fares that lost revenue is priced on (the report's + JetBlue cost): the fares of the refunded, of the stranded to a later day, and of every passenger booked on a + flight cancelled (`fares`: each group's itinerary fare).""" + run = day.run + c = run.cost + group = {g.id: g for g in passengers.groups} + rev = Counter() + for part in run.pax.parts: + rev[part.kind] += part.pax * fares[part.group] + gone = {i for i, r in run.flights.items() if r.status != FLOWN} + return {"aircraft": c.gate + c.overrun - c.saved, "saved": c.saved, "crew": day.crew_cost, + "tomorrow": tomorrow(task, run, firsts, airline=True), + "cancelled_pax": sum(f.pax for f in task.flights if f.id in gone), + "cancelled_rev": sum(g.pax * fares[g.id] for g in group.values() if gone.intersection(g.legs)), + "refunded_rev": rev[pax.REFUNDED], "stranded_rev": rev[pax.STRANDED], **run.pax.outcomes()} + + +def jetblue(a: dict, take: float, save: float) -> float: + """JetBlue's cost J of a day priced by `airline`, in passenger-minutes: its aircraft's minutes with `save` of what + its cancellations saved at FAA's rates, its crews, tomorrow, the refunded passengers' fares and `take` of the + stranded's.""" + return (a["aircraft"] + (1 - save) * a["saved"] + a["crew"] + a["tomorrow"] + a["refunded_rev"] + + take * a["stranded_rev"]) + + +def reward(cost: float, top: float, anchor: float) -> float: + """The realistic reward: 1.0 at the cheapest plan known (`top`) or under it, 0.5 at JetBlue's replay (`anchor`), + toward the 0.2 floor beyond it, as `score` of the cost over the top.""" + return score(cost - top, max(anchor - top, 1.0)) + + +def score(cost: float, anchor: float, k: float = 2.0) -> float: + """The realistic reward: 0.5 at JetBlue's replayed cost (`anchor`), toward 1 as cost falls to 0 and toward the + 0.2 floor as it grows, as a function of cost / anchor alone so days compare.""" + return 0.2 + 0.8 / (1 + 5 / 3 * (max(cost, 0) / anchor) ** k) diff --git a/tests/fixtures/occ3r-2025-01-19-astra-plan.json b/tests/fixtures/occ3r-2025-01-19-astra-plan.json new file mode 100644 index 0000000..77bfc69 --- /dev/null +++ b/tests/fixtures/occ3r-2025-01-19-astra-plan.json @@ -0,0 +1 @@ +{"plan":{"136":{"cancel":true,"decided":240},"202":{"rides":[[360,null,"RP-A220-BOS-S2"]]},"214":{"cancel":true,"decided":600},"231":{"names":[[660,null,"pilot","P-A32X-0392"]]},"262":{"cancel":true,"decided":660},"268":{"holds":[[540,777]]},"283":{"holds":[[720,802]]},"294":{"holds":[[600,832],[660,832]]},"320":{"cancel":true,"decided":780},"357":{"names":[[360,null,"pilot","RP-A220-BOS-S2"]]},"374":{"cancel":true,"decided":660},"392":{"claims":[[840,1251],[900,null]],"holds":[[900,null]]},"409":{"cancel":true,"decided":780},"429":{"rides":[[1080,null,"P-A32X-0412"]]},"443":{"names":[[780,null,"pilot","RP-A32X-MCO-S6"]]},"446":{"claims":[[840,1255],[900,null]],"holds":[[900,null]]},"449":{"cancel":true,"decided":780},"450":{"holds":[[900,1141],[960,1141]]},"451":{"names":[[780,null,"pilot","RP-A32X-MCO-S1"]]},"490":{"names":[[900,null,"pilot","RP-A32X-MCO-S3"]]},"501":{"holds":[[1020,1203]]},"503":{"cancel":true,"decided":1200},"526":{"names":[[900,null,"cabin","RC-A32X-FLL-S5"]]},"533":{"cancel":true,"decided":780},"534":{"cancel":true,"decided":1140},"558":{"names":[[1080,null,"pilot","P-A32X-0412"]]},"564":{"holds":[[1140,1368],[1200,1368]]},"565":{"holds":[[1200,1406],[1260,1406]]},"566":{"holds":[[1140,1423],[1200,1406]]},"calls":[[780,"RP-A32X-MCO-S1",1020],[780,"RP-A32X-MCO-S6",1020],[900,"RC-A32X-FLL-S5",1140],[900,"RP-A32X-MCO-S3",1080]],"swaps":[[420,"P-A220-0552","P-A220-0562"]]},"task_id":"b6-20250119-0400"} \ No newline at end of file diff --git a/tests/hub/test_hub_dataset.py b/tests/hub/test_hub_dataset.py index a325650..2b49883 100644 --- a/tests/hub/test_hub_dataset.py +++ b/tests/hub/test_hub_dataset.py @@ -20,7 +20,7 @@ from agent_env.task.store import TASK_INSTANCES_COLLECTION from huggingface_hub import DatasetCard -from agentenv_airops import export, stage, tasks +from agentenv_airops import export, realworld, stage, tasks from agentenv_airops.core.model import load_pack from agentenv_airops.core.reward import reward from agentenv_airops.day import Day @@ -31,7 +31,7 @@ from agentenv_airops.occ.day import EXTRA_TURNS from agentenv_airops.occ.day import reward as occ_reward from agentenv_airops.occ.day import timetable as occ3_timetable -from agentenv_airops.occ.pack import Pack, Pack3 +from agentenv_airops.occ.pack import Pack, Pack3, Pack3R from agentenv_airops.schedule import TOOLS, max_turns, timetable ROOT = Path(__file__).resolve().parents[2] @@ -40,6 +40,7 @@ DAY = E2E / "e2e-day.jsonl" ID = "b6-jfk-20240109-1800" OCC_EVAL, OCC_CONTROL, OCC_OTHER = "b6-20240713-0400", "b6-20240703-0400", "b6-20250111-0400" +REAL_TRAIN, REAL_EVAL = "b6-20250119-0400", "b6-20250111-0400" SONNET, GPT = "anthropic/claude-sonnet-5-5", "openai/gpt-6.1-sol" OPUS, ASTRA = "anthropic/claude-opus-5-5", "openai/gpt-6-astra" TAG = f"v{tomllib.loads((ROOT / 'pyproject.toml').read_text())['project']['version']}" @@ -149,6 +150,7 @@ def _dry_build(hub, tmp_path, monkeypatch, *extra: str) -> Path: occ = [] if "--occ-pack" in extra else ["--occ-pack", str(_occ_pack(tmp_path))] occ += [] if "--occ3-pack" in extra else ["--occ3-pack", str(_occ3_pack(tmp_path, (OCC_EVAL, OCC_CONTROL, OCC_OTHER)))] + occ += [] if "--occ3r-pack" in extra else ["--occ3r-pack", str(_occ3r_pack(tmp_path))] monkeypatch.setattr(sys, "argv", ["hub_dataset.py", "--runs", str(tmp_path / "runs"), "--out", str(out), "--cache", str(tmp_path / "cache"), *occ, *extra]) hub.main() @@ -198,6 +200,15 @@ def test_a_dry_build_without_a_sweep_has_the_days_the_bundle_and_no_runs(hub, lo assert parse_bundle(out / "bundles/airops-occ").id_root and (out / "bundles/airops-occ/README.md").is_file() assert [r["task_id"] for r in _rows(out, "tasks/v2-eval.parquet")] == [OCC_EVAL] assert not list(out.glob("episodes/v2-*")) and not list(out.glob("desks/*")) + assert data["agentenv"]["bundles"]["airops-occ3-real"] == {"plugins": [PLUGIN], + "setup": "agent-env airops-occ3 setup"} + real = sorted(p.stem for p in (out / "bundles/airops-occ3-real/tasks").glob("*.json")) + assert real == sorted([f"{i}-{m}-real" for i in (REAL_TRAIN, REAL_EVAL) for m in ("joint", "one")] + + ["wiring-noplay"]) + assert [r["task_id"] for r in _rows(out, "tasks/v3r-train.parquet")] == [REAL_TRAIN] + assert [r["task_id"] for r in _rows(out, "tasks/v3r-eval.parquet")] == [REAL_EVAL] + names = [c["config_name"] for c in data["configs"]] + assert names.index("v3r_tasks") > names.index("v3_tasks") and "v3r_episodes" not in names def _sweep(tmp_path: Path, transcript: bytes) -> tuple[Path, str]: @@ -270,8 +281,8 @@ def test_a_dry_build_with_a_sweep_publishes_its_run_in_both_kinds_of_table_on_th text = (out / "README.md").read_text() assert "| Claude Sonnet 5.5 | Anthropic | 1 of 1 | **0.841** (0.841 to 0.841) |" in text names = [c["config_name"] for c in DatasetCard(text).data.to_dict()["configs"]] - assert names == ["v3_tasks", "v3_references", "v2_tasks", "v1_tasks", "results", "v1_references", "v1_episodes", - "airops_tasks", "airops_episodes"] + assert names == ["v3_tasks", "v3_references", "v3r_tasks", "v2_tasks", "v1_tasks", "results", "v1_references", + "v1_episodes", "airops_tasks", "airops_episodes"] def test_a_key_in_a_transcript_stops_the_build_before_anything_is_written(hub, local_stores, tmp_path, monkeypatch): @@ -502,6 +513,17 @@ def _occ3_pack(tmp_path: Path, ids: tuple[str, ...] = (OCC_EVAL, OCC_CONTROL, OC return out +def _occ3r_pack(tmp_path: Path, ids: tuple[str, ...] = (REAL_TRAIN, REAL_EVAL)) -> Path: + """The packaged b6-v3r pack cut down to ``ids``.""" + pack, out = Pack3R(), tmp_path / "b6-v3r" + (out / "dates").mkdir(parents=True, exist_ok=True) + for i in ids: + name = f"{pack.tasks[i]['date']}.json.gz" + (out / "dates" / name).write_bytes((pack.path / "dates" / name).read_bytes()) + (out / "manifest.json").write_text(json.dumps(pack.manifest | {"tasks": {i: pack.tasks[i] for i in ids}})) + return out + + def _raw3(pack: Path, task_id: str) -> dict: return json.loads(gzip.decompress((pack / "dates" / f"{Pack3(pack).tasks[task_id]['date']}.json.gz").read_bytes())) @@ -784,3 +806,39 @@ def test_a_dry_build_publishes_v3s_tables_first_its_runs_and_the_three_desk_bund assert data["configs"][0] == {"config_name": "v3_tasks", "default": True, "data_files": [ {"split": "eval", "path": "tasks/v3-eval.parquet"}, {"split": "control", "path": "tasks/v3-control.parquet"}]} assert json.loads((out / "results/eval-v3.json").read_text())["date"] + + +def test_a_realistic_date_row_scores_jetblues_decisions_half_and_the_cheapest_plan_found_one(hub): + pack = Pack3R() + row = hub.occ3r_date(pack, REAL_TRAIN) + refs = json.loads(gzip.decompress((pack.path / "dates" / "2025-01-19.json.gz").read_bytes()))["realistic"]["refs"] + assert row["split"] == "train" and row["anchor"] == row["jetblue_J"] == refs["jetblue"] + assert row["jetblue_reward"] == 0.5 and row["best_j_reward"] == 1.0 and row["top"] == row["best_j_J"] + assert row["jetblue_usd"] == round(refs["jetblue"] * 1.07) + assert json.loads(row["not_flown_by_plan"]) == refs["cancelled"] and row["recorded_not_flown"] == 157 + assert row["jetblue_not_flown"] == refs["cancelled"]["jbfull@360"] and row["recorded_late_60"] == 59 + assert row["desks_reward"] == round(realworld.reward(refs["priced"]["desks"], refs["top"], refs["jetblue"]), 6) + assert json.loads(row["world"])["realistic"]["refs"] == refs + + +def test_the_realistic_card_table_sets_each_scored_run_beside_jetblues_recorded_day(hub): + day = {"jetblue_usd": 3_284_000, "best_j_usd": 1_269_000, "recorded_not_flown": 157, "recorded_late_60": 59} + run = {"task_id": REAL_TRAIN, "date": "2025-01-19", "model": ASTRA, "model_name": "GPT-6 Astra", + "outcome": "scored", "reward": 0.940398, "cost_usd": 1_710_000, "not_flown": 21, "late_60": 102, + "spend_usd": 20.777861} + md = hub.occ3r_markdown([run, run | {"outcome": "unscored"}], {REAL_TRAIN: day}) + assert md.splitlines()[2] == ("| 2025-01-19 | GPT-6 Astra | 0.940 | $3.28M | $1.71M | $1.27M | 157 / 21 | 59 / 102 " + "| $20.78 |") + assert len(md.splitlines()) == 3 and hub.occ3r_markdown([run | {"outcome": "unscored"}], {}) == "" + + +def test_a_realistic_pilot_is_kept_out_of_v3s_tables_and_its_folder_is_published(hub, local_stores, tmp_path, + monkeypatch, capsys): + folder = tmp_path / "runs" / "occ3-real-astra" + folder.mkdir(parents=True) + (folder / "pilot.json").write_text(json.dumps({"name": "occ3-real-astra", "model": ASTRA, "caps": {"joint": 23.5}})) + (folder / "results.jsonl").write_text("") + out = _dry_build(hub, tmp_path, monkeypatch) + printed = capsys.readouterr().out + assert "three-desk pilots: none" in printed and "realistic pilots: occ3-real-astra" in printed + assert (out / "runs/occ3-real-astra/pilot.json").is_file() and not list(out.glob("episodes/v3*")) diff --git a/tests/occ/test_occ3r.py b/tests/occ/test_occ3r.py new file mode 100644 index 0000000..5827988 --- /dev/null +++ b/tests/occ/test_occ3r.py @@ -0,0 +1,123 @@ +"""airops-occ3 in the realistic world (b6-v3r): a day with no desks graded on JetBlue's own cost as the realism report +priced the standard desks; the day as known holding a breakdown back until it happens, and no page before it changing +with it; the desks' cost lines in dollars; the bundle's realistic days and the env loading one; a named replay of a +model's day (the audit's A5) handing out the aircraft as the day did, which only the day's order makes so.""" + +import json +from dataclasses import replace +from pathlib import Path + +import pytest +from occ_helpers import ROLE + +from agentenv_airops import realworld +from agentenv_airops.occ import run, tasks3 +from agentenv_airops.occ.day3r import CRITERION, OccDay3R +from agentenv_airops.occ.live3 import OccEnv3 +from agentenv_airops.occ.pack import PACK3R, Pack3R +from agentenv_airops.programs import crewday, crewplan, crewsim + +END = {"note": None, "stand_down": False} +FIXTURES = Path(__file__).resolve().parents[1] / "fixtures" + + +@pytest.fixture(scope="module") +def pack(): + return Pack3R(PACK3R) + + +def task_on(pack, date: str) -> str: + return next(k for k, v in pack.tasks.items() if v["date"] == date) + + +def test_a_day_with_no_desks_is_graded_on_jetblues_cost_as_the_report_priced_the_standard_desks(pack): + w = pack.world(task_on(pack, "2025-02-06")) + refs = w.real["refs"] + d = OccDay3R(w, "idle") + g = d.grade + assert abs(g["cost"] - refs["priced"]["desks"]) < 0.5 + assert g["best"] == refs["top"] and g["anchor"] == refs["jetblue"] + assert g["reward"] == round(realworld.reward(g["cost"], refs["top"], refs["jetblue"]), 6) + assert 0.2 < g["reward"] < 1.0 and g["reward_L"] is None and g["criterion"] == CRITERION + assert set(g["P"]) == {"cost", "reward", "reward_L", "best"} and g["P"]["cost"] > g["cost"] + assert g["costs"]["total"] == g["realistic"]["usd"] and d.audit()["ok"] + + +def test_the_day_as_known_holds_a_breakdown_back_until_it_happens(pack): + w = pack.world(task_on(pack, "2026-07-09")) + d = OccDay3R(w, "idle") + tail, at = max(w.real["broken"].items(), key=lambda kv: kv[1]) + assert d.world.broken == w.real["broken"] and d.world.swaps == realworld.SWAPS and d.world.gates + assert tail not in d.known_world(at - 1).broken and d.known_world(at).broken[tail] == at + assert d.known_world(at - 1).gates == d.world.gates and d.known_world(at - 1).swaps == realworld.SWAPS + + +def pages_until(w, stop: int) -> list[str]: + d = OccDay3R(w, "one") + out = [] + while not d.done and d.t <= stop: + for n in list(d.pending): + d.notice(n, "trigger") + out.append(d.call(ROLE["one"], "wait_turn", {})["page"]) + d.call(ROLE["one"], "end_turn", END) + return out + + +def test_no_page_before_a_breakdown_changes_with_it_and_the_cost_lines_are_jetblues_in_dollars(pack): + w = pack.world(task_on(pack, "2026-07-15")) + used = {i for t in w.task.tails if t.tail in w.real["broken"] for i in t.legs} + tail = next(t for t in w.task.tails if t.tail not in w.real["broken"] and t.legs and + not used & set(t.legs) and w.task.flight(t.legs[0]).sched_dep > w.task.start + 240) + at = w.task.flight(tail.legs[0]).sched_dep - 60 + more = replace(w, real={**w.real, "broken": {**w.real["broken"], tail.tail: at}}) + a, b = pages_until(w, at + 60), pages_until(more, at + 60) + before = [k for k, t in enumerate(OccDay3R(w, "idle").times) if t < at][-1] + 1 + assert a[:before] == b[:before] and a[before:] != b[before:] + assert "JetBlue's cost of the plan as projected: $" in a[0] and "pax-min" not in a[0] + assert "Crew cost of the day as projected: deadhead $" in a[0] and "Passengers as projected: of " in a[0] + + +def test_the_bundle_writes_realistic_days_that_load_the_realistic_world_under_jetblues_cost(pack, tmp_path): + task = pack.world(task_on(pack, "2023-06-27")).task + steps = tasks3.steps(task, "joint", realistic=True) + [load] = [s for s in steps if s["type"] == "apply_server_config" and s["id"] == "load-task"] + assert load["directives"][0]["args"] == {"task_id": task.task_id, "mode": "joint", "realistic": True} + plays = [s for s in steps if s["type"] == "prompt_agent"] + assert all(s["prompt_id"].endswith(f"-joint-real-{s['agent_name'].split('-')[0]}") for s in plays) + assert all("JetBlue's own, in dollars" in s["system_prompt"] and "passenger-minutes" not in s["system_prompt"] + and "aircraft that break" in s["system_prompt"] for s in plays) + assert tasks3.RULES_R["ops"] != tasks3.RULES["ops"] and "interchangeable" not in tasks3.RULES_R["ops"] + names = tasks3.generate(tmp_path, task_ids=[task.task_id], realistic=True, modes=("one",)) + assert names == [f"{task.task_id}-one-real"] and (tmp_path / "tasks" / f"{names[0]}.json").exists() + + +def test_the_env_loads_a_realistic_day(pack): + env = OccEnv3() + out = env.occ_load(task_on(pack, "2023-07-01"), "one", realistic=True) + assert out["realistic"] and isinstance(env.day, OccDay3R) and env.day.w.real is not None + + +def test_a_named_replay_hands_out_the_aircraft_as_the_day_did_where_only_the_hubs_swap(pack): + raw = json.loads(FIXTURES.joinpath("occ3r-2025-01-19-astra-plan.json").read_text()) + w = pack.world(raw["task_id"]) + plan = {int(k) if k.isdigit() else k: v for k, v in raw["plan"].items()} + world = realworld.truth(w.task, w.real["gates"], w.real["broken"]) + day = run.truth(w, plan, world) + named = crewday.named(day) + again = crewsim.execute(w.task, named, w.units, world, w.local, fill=False, airports=w.airports, pax=w.passengers) + assert again.run.rows() == day.run.rows() and crewplan.worth(again, False) == crewplan.worth(day, False) + unordered = {i: {k: v for k, v in e.items() if k != "order"} if isinstance(i, int) else e for i, e in named.items()} + loose = crewsim.execute(w.task, unordered, w.units, world, w.local, fill=False, airports=w.airports, + pax=w.passengers) + assert loose.run.rows() != day.run.rows() + + +def test_the_day_as_known_carries_each_stations_caps_as_they_stand_not_as_they_turn_out(pack): + w = pack.world(task_on(pack, "2024-08-03")) + d = OccDay3R(w, "idle") + caps = w.task.layers.dep_caps["JFK"] + t = next(60 * h for h in sorted(caps) if h + 2 in caps and caps[h + 2] != caps[h] and 60 * h >= w.task.start) + seen = d.known_world(t).dep_caps["JFK"] + assert all(seen[h] == caps[t // 60] for h in range(t // 60, (t + 240) // 60 + 1)) + assert seen[t // 60 + 2] != caps[t // 60 + 2] and (t + 300) // 60 not in seen + assert d.world.dep_caps["JFK"] == caps