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44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# Importing the required libraries
import numpy as np
import pandas as pd
import math
from pathlib import Path

# ── Paths ─────────────────────────────────────────────────────────────────────
PROJECT_ROOT  = Path(__file__).resolve().parent.parent
DATA_ROOT     = PROJECT_ROOT / "data"
OUTPUT_PATH   = DATA_ROOT / "synthetic_engagements.csv"
METADATA_PATH = DATA_ROOT / "aircraft_metadata.csv"

# ── Generation config ─────────────────────────────────────────────────────────
N_ROWS = 1_000_000           # Python ignores underscores in numbers, just readability

# ── Feature ranges ────────────────────────────────────────────────────────────
# Launch distance: how far the missile was fired from (metres)
# Floor is 500m β€” below this, guns are more effective than missiles and
# the missile doesn't have enough distance to arm and track properly
# Upper bound is capped to each aircraft's missile_range at row generation
LAUNCH_DISTANCE_MIN = 500

# Your aircraft speed range - 61 m/s (TB2 drone) β†’ 983 m/s (SR-71), full metadata range
YOUR_SPEED_RANGE       = (61, 983)

# Your aircraft altitude (metres)
# 0 = ground level, 30,000m = upper combat/operational ceiling
# SR-71 operates at ~24,000m, most fighters top out around 20,000m
# 30,000m gives headroom for all 102 aircraft in metadata
YOUR_ALTITUDE_RANGE    = (0, 30_000)

# Enemy aircraft altitude (metres) β€” same envelope as your own aircraft
# Determines vertical geometry of the engagement alongside elevation angle
ENEMY_ALTITUDE_RANGE   = (0, 30_000)

# Azimuth: horizontal angle of incoming threat, clockwise from North
# 0Β° = head-on, 90Β° = right side, 180Β° = tail-chase, 270Β° = left side
AZIMUTH_RANGE          = (0, 360)

# Elevation: vertical angle of incoming threat
# 0Β° = same altitude, +90Β° = directly above, -90Β° = directly below
ELEVATION_RANGE        = (-90, 90)

# Maneuverability: 0 = low (bomber/transport), 1 = medium (older jets),
#                  2 = high (modern fighters)
MANEUVERABILITY_VALUES = [0, 1, 2]

# Countermeasure: 0 = not deployed, 1 = deployed (flares/chaff)
COUNTERMEASURE_VALUES  = [0, 1]


# ── Runs once β€” loads combat-capable aircraft from metadata CSV ───────────────
def _load_metadata():
    """
    Loads combat-capable aircraft from the metadata CSV (no_aa_capability == 0).

    Returns:
        pd.DataFrame: Filtered metadata containing only aircraft with air-to-air missile capability.
    """
    
    if not METADATA_PATH.exists():
        raise FileNotFoundError(f"Metadata CSV not found: {METADATA_PATH}")
    metadata_df = pd.read_csv(METADATA_PATH)
    metadata_df = metadata_df[metadata_df["no_aa_capability"]==0]
    return metadata_df


# ── Derives missile phase from how far it has already traveled ────────────────
# phase 0 = boost (just launched), 1 = mid-course, 2 = terminal (final approach)
def _derive_missile_phase(remaining_distance, launch_distance):
    """
    Derives missile flight phase from how much of its journey it has completed.

    Args:
        remaining_distance (float): Distance remaining between missile and target (metres).
        launch_distance (float): Total distance at the moment of launch (metres).

    Returns:
        int: 0 = boost, 1 = mid-course, 2 = terminal.
        
        Phase 0 β€” engine burning, accelerating, just launched
        Phase 1 β€” flying toward your predicted position, guided but not actively tracking you yet
        Phase 2 β€” active seeker on, tracking you specifically, hardest to fool
    """
    
    traveled_distance = launch_distance - remaining_distance
    ratio = traveled_distance / launch_distance
    
    if ratio < 0.33:
        return 0
    elif 0.33 <= ratio < 0.66:
        return 1
    else:
        return 2


# ── Derives closure rate using 3D geometry (azimuth + elevation + speeds) ─────
# How fast the gap between you and the missile is closing, in m/s
# Enemy altitude adds the vertical dimension to the engagement geometry
def _derive_closure_rate(missile_speed, your_speed,
                         azimuth, elevation):
    """
    Derives the closure rate - how fast the gap between the missile and the target is closing (m/s).

    Uses 3D geometry: azimuth accounts for horizontal approach angle,
    elevation accounts for vertical approach angle.

    Args:
        missile_speed (float): Speed of the incoming missile (m/s).
        your_speed (float): Speed of the friendly aircraft (m/s).
        azimuth (float): Horizontal angle of incoming threat in degrees (0Β° = head-on).
        elevation (float): Vertical angle of incoming threat in degrees (0Β° = same altitude).

    Returns:
        float: Closure rate in m/s.
    """
    
    # Convert the angles into radians and prepare for cosine
    azimuth = math.radians(azimuth)
    elevation = math.radians(elevation)
    
    # Extract the closure rate
    closure_rate = missile_speed + (your_speed * (math.cos(azimuth) * math.cos(elevation)))
    
    return closure_rate


"""
Note: enemy_speed is not included as a feature. This is a known simplification, in real engagements,
enemy aircraft velocity at launch contributes to effective missile speed. This can be added in a future iteration 
of the physics generator.
"""


def _derive_evasion_time(remaining_distance, closure_rate,
                         missile_phase, enemy_generation,
                         your_speed, your_altitude, enemy_altitude):
    """
    Derives the minimum evasion time - seconds before the missile reaches you.

    Base calculation is pure kinematics: remaining_distance / closure_rate.
    Four modifiers are applied to account for factors the base formula cannot capture.

    Modifiers:
        - missile_phase == 2 (terminal): seeker has locked on, countermeasures
          need 2-3s overhead to be effective. Shrinks window by 15%. (x 0.85)
        - enemy_generation == 5: HOBS (High Off-Boresight)
          seeker + ECCM make the missile harder to
          defeat, compressing effective reaction time by ~10%. (x 0.90)
        - your_speed > 522 m/s (above median): high energy state gives more
          lateral geometry per second during evasion. Slight expansion. (x 1.05)
        - abs(your_altitude - enemy_altitude) > 5000m: large altitude gap pushes
          engagement toward edge of missile performance envelope, degrading
          terminal accuracy. Slight expansion. (x 1.10)

    Args:
        remaining_distance (float): Distance between missile and you right now (metres).
        closure_rate (float): Combined closing speed from _derive_closure_rate() (m/s).
        missile_phase (int): 0 = boost, 1 = mid-course, 2 = terminal.
        enemy_generation (float): Enemy aircraft generation (3.5, 4, 4.5, or 5).
        your_speed (float): Your current airspeed (m/s).
        your_altitude (float): Your current altitude (metres).
        enemy_altitude (float): Enemy aircraft altitude (metres).

    Returns:
        float: Evasion time in seconds.
    """
    
    # Calculate the evasion time
    evasion_time = remaining_distance / closure_rate
    
    # Terminal phase - seeker locked on, countermeasures need 2–3s overhead
    if missile_phase == 2:
        evasion_time *= 0.85
        
    # Gen 5 aircraft missile (HOBS + ECCM) compresses effective reaction time
    if enemy_generation == 5:
        evasion_time *= 0.90
        
    # High speed β€” more room to maneuver
    if your_speed > 522:
        evasion_time *= 1.05
        
    # Large altitude gap - missile at edge of performance envelope
    if abs(your_altitude - enemy_altitude) > 5000:
        evasion_time *= 1.10
        
    return evasion_time
    
    

def _derive_hit_label(countermeasure_deployed, your_maneuverability,
                      azimuth, elevation, missile_phase, enemy_generation,
                      your_altitude, enemy_altitude):
    """
    Derives whether the missile hits after an evasion attempt.

    Starts from a neutral survival score of 0.5. Modifiers push it up (more
    likely to hit) or down (more likely to miss). Final label is 1 if the
    missile hits, 0 if it misses.

    This is where azimuth and elevation belong β€” they affect survival after
    evasion, not when the missile arrives (that is handled by closure_rate
    in _derive_evasion_time).

    Modifiers:
        - countermeasure_deployed == 1: active countermeasures significantly
          reduce hit probability. Score drops.
        - your_maneuverability == 2: high maneuverability makes evasion more
          effective. Score drops.
        - azimuth near 0Β° (head-on): least time and geometry to evade.
          Score rises.
        - missile_phase == 2 (terminal): seeker locked on, hardest to defeat.
          Score rises.
        - enemy_generation == 5: HOBS + ECCM make the missile more lethal.
          Score rises.
        - elevation far from 0Β°: steep approach angle reduces evasion options.
          Score rises.
        - large altitude differential > 5000m: pushes missile toward edge of
          performance envelope. Score drops.

    Args:
        countermeasure_deployed (int): 0 = not deployed, 1 = deployed.
        your_maneuverability (int): 0 = low, 1 = medium, 2 = high.
        azimuth (float): Horizontal angle of incoming threat in degrees (0Β° = head-on).
        elevation (float): Vertical angle of incoming threat in degrees (0Β° = level).
        missile_phase (int): 0 = boost, 1 = mid-course, 2 = terminal.
        enemy_generation (float): Enemy aircraft generation (3.5, 4, 4.5, or 5).
        your_altitude (float): Your current altitude (metres).
        enemy_altitude (float): Enemy aircraft altitude (metres).

    Returns:
        int: 1 if missile hits, 0 if missile misses.
    """
    
    # Neutral starting score - modifiers push it toward hit or miss
    score = 0.5
    
    # Countermeasures active - significantly reduces hit chance
    if countermeasure_deployed == 1:
        score -= 0.20
        
    # High maneuverability - evasion more effective
    if your_maneuverability == 2:
        score -= 0.10
        
    # Head-on approach - least time and geometry to evade
    if azimuth < 30:
        score += 0.15
        
    # Terminal phase - seeker locked on, hardest to defeat
    if missile_phase == 2:
        score += 0.15
        
    # Gen 5 missile - HOBS + ECCM make it more lethal
    if enemy_generation == 5:
        score += 0.10
        
    # Steep approach angle - reduces evasion options
    if abs(elevation) > 45:
        score += 0.10
        
    # Large altitude gap - missile at edge of performance envelope
    if abs(your_altitude - enemy_altitude) > 5000:
        score -= 0.10

    # Hit if score crosses 0.5
    return 1 if score >= 0.5 else 0
    

# ── Generates one complete engagement scenario as a dict ─────────────────────
def _generate_row(aircraft_row):
    """
    Generates one synthetic engagement scenario for a given aircraft.

    Samples random values for all situational features, derives computed
    features using physics, and returns a complete row with labels.

    Args:
        aircraft_row (pd.Series): One row from the metadata CSV.

    Returns:
        dict: All 14 features plus evasion_time and hit labels.
    """
    
    # Sample situational features
    your_speed    = np.random.uniform(*YOUR_SPEED_RANGE)
    your_altitude = np.random.uniform(*YOUR_ALTITUDE_RANGE)
    enemy_altitude = np.random.uniform(*ENEMY_ALTITUDE_RANGE)
    azimuth       = np.random.uniform(*AZIMUTH_RANGE)
    elevation     = np.random.uniform(*ELEVATION_RANGE)
    
    # Sample categorical features
    your_maneuverability    = np.random.choice(MANEUVERABILITY_VALUES)
    countermeasure_deployed = np.random.choice(COUNTERMEASURE_VALUES)
    
    # Pull threat specs from metadata
    missile_speed    = aircraft_row["missile_speed"]
    missile_range    = aircraft_row["missile_range"]
    enemy_generation = aircraft_row["enemy_generation"]
    
    # Sample engagement distances
    launch_distance    = np.random.uniform(LAUNCH_DISTANCE_MIN, missile_range)
    remaining_distance = np.random.uniform(0, launch_distance)    # remaining_distance <= launch_distance always
    
    # Derive computed features
    missile_phase = _derive_missile_phase(remaining_distance, launch_distance)
    closure_rate  = _derive_closure_rate(missile_speed, your_speed,
                                         azimuth, elevation)

    # Derive labels
    evasion_time = _derive_evasion_time(remaining_distance, closure_rate,
                                        missile_phase, enemy_generation,
                                        your_speed, your_altitude, enemy_altitude)
    hit          = _derive_hit_label(countermeasure_deployed, your_maneuverability,
                                     azimuth, elevation, missile_phase, enemy_generation,
                                     your_altitude, enemy_altitude)
    
    # Return complete row
    return {
        "launch_distance":        launch_distance,
        "remaining_distance":     remaining_distance,
        "closure_rate":           closure_rate,
        "azimuth":                azimuth,
        "elevation":              elevation,
        "missile_phase":          missile_phase,
        "your_speed":             your_speed,
        "your_altitude":          your_altitude,
        "your_maneuverability":   your_maneuverability,
        "enemy_altitude":         enemy_altitude,
        "missile_speed":          missile_speed,
        "missile_range":          missile_range,
        "enemy_generation":       enemy_generation,
        "countermeasure_deployed": countermeasure_deployed,
        "evasion_time":           evasion_time,
        "hit":                    hit
    }
    


# ── Saves completed DataFrame to CSV ─────────────────────────────────────────
def _save_dataset(df):
    """
    Saves the generated dataset to a CSV file.

    Args:
        df (pd.DataFrame): The complete synthetic engagement dataset.
    """
    
    # Create data directory if it doesn't exist
    OUTPUT_PATH.parent.mkdir(parents=True, exist_ok=True)
    
    # Save to CSV
    df.to_csv(OUTPUT_PATH, index=False)


# ── Public entry point β€” the only function the notebook calls ─────────────────
def generate_dataset():
    """
    Generates 1,000,000 synthetic engagement scenarios and saves to CSV.

    Loads combat-capable aircraft from metadata, samples each aircraft
    equally across all rows, derives all features and labels using physics,
    and writes the final dataset to disk.

    Only valid threat scenarios are included (closure_rate > 0).

    Returns:
        pd.DataFrame: The complete synthetic engagement dataset.
    """
    metadata = _load_metadata()
    
    rows_per_aircraft = N_ROWS // len(metadata)  # 1_000_000 // 56 β†’ 17,857 rows per aircraft
    rows = []
    
    for _, aircraft_row in metadata.iterrows():
        count = 0
        while count < rows_per_aircraft:
            row = _generate_row(aircraft_row)
            if row["closure_rate"] > 0:
                rows.append(row)
                count += 1
    
    # Fill remaining rows to hit exactly N_ROWS
    remaining_rows = N_ROWS - len(rows)
    if remaining_rows:
        count = 0
        while count < remaining_rows:
            row = _generate_row(metadata.sample(1).iloc[0])
            if row["closure_rate"] > 0:
                rows.append(row)
                count += 1
    
    df = pd.DataFrame(rows)
    _save_dataset(df)
    print(f"Done. {len(rows):,} rows saved to {OUTPUT_PATH}")
    
    
    
# Inference only - assembles 14-feature array from HUD inputs and metadata for model prediction    
def build_feature_array(
    # From HUD sliders
    launch_distance, remaining_distance,
    azimuth, elevation,
    your_speed, your_altitude, enemy_altitude,
    countermeasure_deployed,
    # From metadata lookup (enemy aircraft)
    missile_speed, missile_range, enemy_generation,
    # From metadata lookup (friendly aircraft)
    your_maneuverability
):
    """
    Assembles the 14-feature array required by the ETA and hit models at inference time.

    All inputs arrive pre-collected from the HUD sliders and metadata lookups.
    Closure rate and missile phase are derived here from those inputs using the
    same physics helpers used during training. The returned dict is in the exact
    column order the models were trained on.

    Args:
        launch_distance (float): Distance at moment of missile launch (metres).
        remaining_distance (float): Distance remaining between missile and target (metres).
        azimuth (float): Horizontal angle of incoming threat in degrees (0 = head-on).
        elevation (float): Vertical angle of incoming threat in degrees (0 = level).
        your_speed (float): Friendly aircraft airspeed (m/s).
        your_altitude (float): Friendly aircraft altitude (metres).
        enemy_altitude (float): Enemy aircraft altitude (metres).
        countermeasure_deployed (int): 0 = not deployed, 1 = deployed.
        missile_speed (float): Incoming missile speed (m/s), from enemy metadata.
        missile_range (float): Missile maximum effective range (metres), from enemy metadata.
        enemy_generation (float): Enemy aircraft generation, from enemy metadata.
        your_maneuverability (int): Friendly aircraft maneuverability, from friendly metadata.
            0 = low, 1 = medium, 2 = high. Maneuverability is an aircraft property
            and follows the same metadata lookup pattern for both friendly and enemy platforms.

    Returns:
        dict: 14 features in training column order, ready for model inference.
    """

    # Derive the two features that are computed from inputs rather than sourced directly
    missile_phase = _derive_missile_phase(remaining_distance, launch_distance)
    closure_rate  = _derive_closure_rate(missile_speed, your_speed,
                                         azimuth, elevation)

    # Assemble and return all 14 features in exact training column order
    return {
        "launch_distance":         launch_distance,
        "remaining_distance":      remaining_distance,
        "closure_rate":            closure_rate,
        "azimuth":                 azimuth,
        "elevation":               elevation,
        "missile_phase":           missile_phase,
        "your_speed":              your_speed,
        "your_altitude":           your_altitude,
        "your_maneuverability":    your_maneuverability,
        "enemy_altitude":          enemy_altitude,
        "missile_speed":           missile_speed,
        "missile_range":           missile_range,
        "enemy_generation":        enemy_generation,
        "countermeasure_deployed": countermeasure_deployed
    }