Estimate a track's class from its measurements, not from the target (#133)
build / mod (push) Failing after 1m18s

A radar track is what the network measured, and it never carried the
target's own answers: the tactical Track loses `kind: DroneKind` and the
true velocity, and gains what the measurements say instead.

- Track now holds a ClassEst (unknown / probable / firm) plus an observed
  velocity: none from the first blip, then the change of the measured
  places between real measurements, smoothed so one noisy pair does not
  flap the class. The classifier's bands sit between the game's speed
  clusters (Class::of_speed); a new contrary measurement replaces the
  hypothesis, an agreeing one firms it.
- The class estimate is separate from track confirmation (Track::ident):
  a track can be confirmed with its type still unknown. The estimate
  grows only on real measurements — a forecast and a relayed copy add
  nothing — and it travels with the measurement it grew from, ageing
  with the track and gone when the track drops.
- Roles, last chance, the reserve and the interceptor count read the
  estimate, never the target's class. Explicit policy for the unknown:
  an unidentified track stays a candidate under the general rules (and
  the system with no roles holds its fire at everything); the map, the
  monitor, the summary panel and /airdef why read the same estimate and
  name the reason — «Трек подтверждён · тип не определён», «вероятно
  крылатая ракета». The exact model stays in the sandbox's truth mode.
- A gun or a launcher lets go of a target that classified out of the
  roles since it was taken; a first blip no longer rules a contact out
  as a threat to guarded objects (it has no measured course yet).

Old saves and replays are untouched (tracks are derived, not persisted);
the simulation stays deterministic (the classifier is pure arithmetic on
the measurement noise). The doctrine/first-blip/last-chance tests gained
classified tracks where the contract provably changed; new tests cover
the classifier's steps, kind-independent estimates, first-blip blindness,
and relays and forecasts adding no confidence.

Co-Authored-By: Kimi Code <noreply@moonshot.ai>
This commit is contained in:
2026-11-20 22:37:16 +03:00
parent 95739c95b1
commit ac7521936d
13 changed files with 641 additions and 128 deletions
+166
View File
@@ -122,6 +122,9 @@ fn a_failed_radar_gives_no_own_tracks_but_the_network_still_feeds_it() {
let early = radar_at(&mut sim, SamKind::Radar, 120.0);
let id = drone_from(&mut sim, -150.0);
sim.sams[p].comps.0[Part::Sensor as usize] = 0.0;
// Hold fire: the fed track is now acted on (an unidentified contact is a candidate under the
// general rules, #133), and this test is about the feed, not about the intercept.
sim.sams[p].roles = 0;
for _ in 0..500 {
sim.step(&mut ground, None, &mut out);
}
@@ -254,3 +257,166 @@ fn the_same_seed_rolls_the_same_scan_and_damage() {
};
assert_eq!(run(), run());
}
// ---------------------------------------------------------------- #133: the class estimate
#[test]
fn the_classifier_steps_by_the_measurements_alone() {
// The same observed speeds give the same estimate whatever the contact truly is, and a
// contrary measurement replaces the hypothesis instead of being ignored.
let mut est = ClassEst::Unknown;
assert_eq!(est.step(1, 0.6), ClassEst::Unknown, "one blip says nothing of the class");
est = est.step(2, 0.6);
assert_eq!(est, ClassEst::Probable(Class::Drone));
est = est.step(3, 0.62);
assert_eq!(est, ClassEst::Firm(Class::Drone), "agreeing measurements firm the guess");
// The contact proves much faster (or the first readings misled): the hypothesis gives way,
// and one measurement is not enough to firm the new one.
est = est.step(4, 5.0);
assert_eq!(est, ClassEst::Probable(Class::Supersonic));
est = est.step(5, 5.1);
assert_eq!(est, ClassEst::Firm(Class::Supersonic));
// Whatever the true kind, the same observations read the same: a Kinzhal measured as a slow
// propeller is, to the network, a drone until its measurements say otherwise.
assert_eq!(ClassEst::Unknown.step(2, 0.5), ClassEst::Probable(Class::Drone));
}
#[test]
fn the_speed_bands_read_the_games_own_kinds_right() {
// The bands sit between the game's speed clusters: the measured speed of a kind reads as its
// class, so a slow Lancet is no jet and a Shahed no cruise missile. A fast ballistic and a
// supersonic missile fly alike (the Iskander and the Zircon): speed alone leaves that
// ambiguity, documented on `Class::of_speed`.
for kind in DroneKind::ALL {
let spec = kind.spec();
if spec.class == Class::Ballistic && spec.speed < 6.5 {
continue;
}
assert_eq!(Class::of_speed(spec.speed), spec.class, "{kind:?} at {} blocks a tick", spec.speed);
}
assert_eq!(Class::of_speed(5.0), Class::Supersonic, "a fast ballistic and a supersonic fly alike");
}
#[test]
fn the_first_blip_gives_no_class_and_no_speed() {
let mut sim = Sim::new(5);
let (mut ground, mut out) = flat();
let r = radar_at(&mut sim, SamKind::Radar, 0.0);
let id = drone_from(&mut sim, -150.0);
let mut first = None;
for _ in 0..300 {
sim.step(&mut ground, None, &mut out);
if let Some(t) = sim.sams[r].track(id) {
first = Some(*t);
break;
}
}
let t = first.expect("a track of the drone");
assert_eq!(t.hits, 1);
assert_eq!(t.class, ClassEst::Unknown, "the first measurement must not name a class");
assert_eq!(t.vel, DVec3::ZERO, "the true velocity must not leak into the track");
}
#[test]
fn tracks_of_different_kinds_one_class_firm_the_same_estimate() {
// A Shahed and a Gerbera are different machines of one class, flying alike; the picture both
// networks end up holding is the same: a firm slow drone, never a model name. (Faster kinds
// cross this little radar's reach between beam passes: too few measurements to firm anything.)
let roles = SamKind::S400.spec().roles;
let mut seen = Vec::new();
for kind in [DroneKind::Shahed, DroneKind::Gerbera] {
let mut sim = Sim::new(7);
let (mut ground, mut out) = flat();
// One radar: an uninterrupted chain of measurements (two early-warning radars do not
// exchange tracks — a radar is fed only when it serves fire control — so the second one
// would measure from a blank picture, its own chain apart).
radar_at(&mut sim, SamKind::Radar, 0.0);
let from = DVec3::new(-150.0, 95.0, 40.0);
sim.add_drone(kind, from, DVec3::X, DVec3::new(5000.0, 95.0, 40.0));
let id = sim.drones.last().unwrap().id;
let mut est = None;
for _ in 0..700 {
sim.step(&mut ground, None, &mut out);
if let Some(t) = sim.picture().get(&id) {
est = Some(t.class);
}
}
assert_eq!(est, Some(ClassEst::Firm(Class::Drone)), "{kind:?}: {est:?}");
// The wording and the class-dependent decisions come from the estimate, not the machine.
let est = est.unwrap();
seen.push((est, est.name(), est.mark(), est.in_roles(roles)));
}
assert_eq!(seen[0], seen[1], "the same observations read differently for two kinds: {seen:?}");
}
#[test]
fn the_role_policy_keeps_the_unidentified_and_holds_fire_with_no_roles() {
let some = Class::Drone.bit() | Class::Jet.bit();
// An unidentified contact is not a block: it stays a candidate under the general rules.
assert!(ClassEst::Unknown.in_roles(some));
// No roles at all is holding fire: nothing is a candidate, the unidentified included.
assert!(!ClassEst::Unknown.in_roles(0), "hold fire must block the unidentified too");
// A probable guess is a block when its class is out of the roles, and a pass when in them.
assert!(ClassEst::Probable(Class::Drone).in_roles(some));
assert!(!ClassEst::Probable(Class::Cruise).in_roles(some));
assert!(ClassEst::Firm(Class::Jet).in_roles(some));
assert!(!ClassEst::Firm(Class::Ballistic).in_roles(0));
}
#[test]
fn a_gun_lets_go_of_a_track_classified_out_of_its_roles() {
// The Gepard's roles are drones, jets and cruise missiles: a supersonic missile is out of them.
assert_eq!(SamKind::Gepard.spec().roles & Class::Supersonic.bit(), 0);
let mut sim = Sim::new(11);
let (mut ground, mut out) = flat();
let p = radar_at(&mut sim, SamKind::Gepard, 0.0);
sim.add_drone(DroneKind::Zircon, DVec3::new(0.0, 95.0, -60.0), DVec3::Z, DVec3::new(0.0, 70.0, -700.0));
let id = sim.drones.last().unwrap().id;
sim.tick = 10;
let seed = |sim: &mut Sim, class| {
let d = sim.drones[0].clone();
sim.sams[p].tracks.retain(|t| t.id != d.id);
sim.sams[p].tracks.push(Track { id: d.id, class, pos: d.pos, vel: d.vel, tick: sim.tick, own_tick: sim.tick, ttl: 400, hits: 3, locked: false });
};
// Taken while the class was unknown: the general rules make it a candidate.
seed(&mut sim, ClassEst::Unknown);
sim.step_sams(&mut ground, &mut out);
assert_eq!(sim.sams[p].gun_target, Some(id), "an unidentified track is not its gun target");
// Classified: no longer a class the gun engages, and it is let go instead of being fired at.
seed(&mut sim, ClassEst::Firm(Class::Supersonic));
sim.step_sams(&mut ground, &mut out);
assert_eq!(sim.sams[p].gun_target, None, "the gun kept a track its roles do not cover");
}
#[test]
fn forecasts_and_relays_add_no_confidence() {
let mut sim = Sim::new(7);
let (mut ground, mut out) = flat();
let a = radar_at(&mut sim, SamKind::Radar, 0.0);
let _b = radar_at(&mut sim, SamKind::Radar, 500.0);
let n = radar_at(&mut sim, SamKind::P1sun, 250.0);
let id = drone_from(&mut sim, -50.0);
// The source's own track: while no new measurement lands (the tick unchanged, the track only
// forecast onwards) the estimate must not move — a forecast is not data.
let mut prev: Option<(u64, ClassEst)> = None;
let mut relayed = 0;
for _ in 0..800 {
sim.step(&mut ground, None, &mut out);
if let Some(t) = sim.sams[a].track(id).copied() {
if let Some((tick, class)) = prev {
assert!(t.tick > tick || t.class == class, "a forecast or a re-read firmed the class: {tick}->{:?} {:?}->{:?}", t.tick, class, t.class);
}
prev = Some((t.tick, t.class));
}
// The network's copy carries the estimate as it was, with its measurement's age; passing
// it on adds nothing (the receiver never measures: own_tick stays 0).
if let Some(t) = sim.sams[n].track(id).copied() {
relayed += 1;
assert_eq!(t.own_tick, 0);
let freshest = sim.sams.iter().filter(|s| s.id != sim.sams[n].id).filter_map(|s| s.track(id)).max_by_key(|t| t.tick);
assert_eq!(Some(t.class), freshest.map(|f| f.class), "a relay altered the estimate");
}
}
assert!(relayed > 100, "the network never passed the track ({relayed})");
assert!(prev.is_some_and(|(_, c)| c.class().is_some()), "the source never classified the contact");
}