From c7a744d89c668ae6311093c9887d15cc93f993aa Mon Sep 17 00:00:00 2001 From: Patrick Tescher Date: Sat, 14 Nov 2026 09:25:10 -0800 Subject: [PATCH] Split Captain into a commander and a unit for each drone, and play from a roster Command and Unit are traits in recreation_towers (no I/O). The default unit, Autopilot, is Instruction::tick_with; the edge policies are units (EdgeUnit). Match is built from units, and Roster puts commanders and units together. Captain is kept, as Directing (its Command) and Captain::unit. The seat reply carries each order's queue. Co-Authored-By: Claude --- domains/cognition/README.md | 2 +- domains/cognition/towers/examples/edge.rs | 15 +- domains/cognition/towers/src/edge.rs | 79 ++++- domains/cognition/towers/src/lib.rs | 412 ++++++++++++++++++---- domains/recreation/towers/src/actors.rs | 219 ++++++++++++ domains/recreation/towers/src/lib.rs | 2 + expectations/glossary.expect | 11 + plans/actors.md | 29 ++ 8 files changed, 676 insertions(+), 93 deletions(-) create mode 100644 domains/recreation/towers/src/actors.rs diff --git a/domains/cognition/README.md b/domains/cognition/README.md index 46c391bf..e3b2671e 100644 --- a/domains/cognition/README.md +++ b/domains/cognition/README.md @@ -8,7 +8,7 @@ AI control of planes, from the sim to the real thing: see [plans/sim2real-tag.md | `cognition_executor` | [executor/](executor) | What flies a plan on board: `no_std`, with a C ABI, linked into the ArduPlane fork's AGENT mode and used by the arena, so both run the same code. Its guidance and energy numbers are a `Tuning` (`src/tuning.rs`); the safety envelope's are fixed (`safety` in `src/lib.rs`) | | `cognition_agent` | [agent/](agent) | The ground control agent: the scripted brain (with `Tactics`), and the MAVLink link to each plane | | `cognition_arena` | [arena/](arena) | The fast arena (F0): a game of tag with no window and no SITL, the executor against the three computer pilots, about 7 000 times faster than real time on all cores | -| `cognition_towers` | [towers/](towers) | The fast Towers arena: two captains (in this process, or each in a process of its own) play a seeded game of Towers with drones that fly their orders on their own, about 200 000 times faster than real time | +| `cognition_towers` | [towers/](towers) | The fast Towers arena: a roster plays a seeded game of Towers: a commander for each team (`recreation_towers::Command`) and a unit for each drone (`recreation_towers::Unit`, by default the autopilot that flies the order and its queue, also out of contact). A `Captain` is a commander and the units of its drones in one object, in this process or in a process of its own. About 200 000 times faster than real time | | `cognition_league` | [league/](league) | The league: strategies (an evolution strategy, Claude agents) give challengers, the arena plays them on the champion's games, and a challenger that is clearly better and safe becomes the champion. Two contests: the executor at tag (`--game executor`) and a captain at Towers (`--game captain`) | | `cognition_runner` | [runner/](runner) | A game of tag between planes flown by real ArduPlane SITL instances (F1), with no window and faster than real time: `cargo run --release -p cognition_runner -- --speedup 10` (needs `ardupilot/build_native.sh`) | diff --git a/domains/cognition/towers/examples/edge.rs b/domains/cognition/towers/examples/edge.rs index 856ea17f..3ecf11dd 100644 --- a/domains/cognition/towers/examples/edge.rs +++ b/domains/cognition/towers/examples/edge.rs @@ -3,23 +3,24 @@ use cognition_towers::{Edge, FinishThenReturn, Keep, Outnumbered, compare_edge}; use recreation_towers::Rules; +use std::rc::Rc; fn main() -> Result<(), String> { let n: u64 = std::env::args().nth(1).and_then(|s| s.parse().ok()).unwrap_or(100); let seeds: Vec = (1..=n).collect(); - let policies: Vec> = vec![ - Box::new(Keep), - Box::new(Outnumbered { n: 1 }), - Box::new(Outnumbered { n: 2 }), - Box::new(Outnumbered { n: 3 }), - Box::new(FinishThenReturn), + let policies: Vec> = vec![ + Rc::new(Keep), + Rc::new(Outnumbered { n: 1 }), + Rc::new(Outnumbered { n: 2 }), + Rc::new(Outnumbered { n: 3 }), + Rc::new(FinishThenReturn), ]; println!( "{:<34} {:>8} {:>8} {:>5} {:>5} {:>5} {:>8} {:>8} {:>6}", "against persist", "margin", "± err", "won", "lost", "draw", "dark", "dark(p)", "downs" ); for p in &policies { - let c = compare_edge(Rules::default(), p.as_ref(), &seeds)?; + let c = compare_edge(Rules::default(), p.clone(), &seeds)?; println!( "{:<34} {:>+8.3} {:>8.3} {:>5} {:>5} {:>5} {:>7.0}s {:>7.0}s {:>6.2}", p.name(), diff --git a/domains/cognition/towers/src/edge.rs b/domains/cognition/towers/src/edge.rs index d2845606..19be02c8 100644 --- a/domains/cognition/towers/src/edge.rs +++ b/domains/cognition/towers/src/edge.rs @@ -1,9 +1,14 @@ //! Decisions on the drone, for when the radio is blocked (plans/edge.md): an [`Edge`] policy looks //! at what a drone knows on board ([`EdgeObs`]) each second it is out of contact, and may change -//! its order. [`Edged`] puts one on any captain, over the standing order "fly the order on". +//! its order. [`Edged`] puts one on any captain, over the standing order "fly the order on". A policy +//! is not a unit of its own: [`EdgeUnit`] is the default unit (the autopilot) with the policy asked +//! once a second while the drone is out of contact. use crate::{Captain, Comparison, Persist, Prepared, compare_captains}; -use recreation_towers::{Commander, EdgeObs, Order, Rules, Standing, Team, TeamView}; +use recreation_towers::{ + Autopilot, Commander, EdgeObs, Instruction, OnBoard, Order, Rules, Standing, Team, TeamView, Unit, +}; +use std::rc::Rc; /// What a cut-off drone decides on its own. pub trait Edge { @@ -57,6 +62,51 @@ impl Edge for FinishThenReturn { } } +impl Edge for Rc { + fn name(&self) -> String { + (**self).name() + } + + fn act(&self, team: Team, obs: &EdgeObs, order: Order) -> Option { + (**self).act(team, obs, order) + } +} + +/// The default unit with an on-board policy: it flies as the autopilot does, and once a second, +/// while the drone is flying out of contact, asks `edge` what to do from what the drone sees. A +/// new order from the policy replaces the queue: the failsafe still holds. +pub struct EdgeUnit { + team: Team, + edge: E, + next: f32, +} + +impl EdgeUnit { + pub fn new(team: Team, edge: E) -> EdgeUnit { + EdgeUnit { team, edge, next: 0.0 } + } +} + +impl Unit for EdgeUnit { + fn fly(&mut self, on: &OnBoard, told: &mut Instruction) -> Option { + Autopilot.fly(on, told); + let now = on.time() >= self.next; + if now { + self.next += 1.0; + } + if now + && !on.in_contact() + && on.flying() + && let Some(order) = told.order + && let Some(new) = self.edge.act(self.team, on.obs(), order) + && new != order + { + told.decide(new); + } + told.order + } +} + /// A captain with an on-board policy: its orders go out with "fly the order on", and the policy /// decides for a drone that is out of contact. pub struct Edged { @@ -64,7 +114,7 @@ pub struct Edged { pub edge: E, } -impl Captain for Edged { +impl Captain for Edged { fn decide(&mut self, view: &TeamView) -> Result, String> { self.captain.decide(view) } @@ -73,26 +123,17 @@ impl Captain for Edged { orders.iter().map(|&(k, o)| (k, Standing { after: 0.0, then: o })).collect() } - fn edge(&mut self, team: Team, obs: &EdgeObs, order: Order) -> Option { - self.edge.act(team, obs, order) + /// Each drone gets its own copy of the policy: the policy is all the captain gives a drone. + fn unit(&self, team: Team, _: usize) -> Box { + Box::new(EdgeUnit::new(team, self.edge.clone())) } } /// `edge` against a captain that flies the order on (the baseline), on paired seeds. -pub fn compare_edge(rules: Rules, edge: &dyn Edge, seeds: &[u64]) -> Result { - struct Dyn<'a>(&'a dyn Edge); - impl Edge for Dyn<'_> { - fn name(&self) -> String { - self.0.name() - } - - fn act(&self, team: Team, obs: &EdgeObs, order: Order) -> Option { - self.0.act(team, obs, order) - } - } +pub fn compare_edge(rules: Rules, edge: Rc, seeds: &[u64]) -> Result { compare_captains( rules, - &|| Box::new(Edged { captain: Commander::new(rules).with_lookahead(false), edge: Dyn(edge) }), + &|| Box::new(Edged { captain: Commander::new(rules).with_lookahead(false), edge: edge.clone() }), &|| Box::new(Prepared { captain: Commander::new(rules).with_lookahead(false), autonomy: Persist }), seeds, ) @@ -145,7 +186,7 @@ mod tests { /// A team that keeps its order plays the same game as one with the standing order "fly on". #[test] fn an_edge_that_goes_on_plays_like_persist() { - let c = compare_edge(Rules::default(), &Keep, &[1, 2, 3]).unwrap(); + let c = compare_edge(Rules::default(), Rc::new(Keep), &[1, 2, 3]).unwrap(); assert!(c.margin.abs() < 1e-6, "{c:?}"); assert_eq!(c.won, c.lost); } @@ -163,7 +204,7 @@ mod tests { Some(Order::Home) } } - let c = compare_edge(Rules::default(), &AlwaysHome, &[1, 2, 3]).unwrap(); + let c = compare_edge(Rules::default(), Rc::new(AlwaysHome), &[1, 2, 3]).unwrap(); assert!(c.margin.abs() > 1e-3, "the policy never ran: {c:?}"); } } diff --git a/domains/cognition/towers/src/lib.rs b/domains/cognition/towers/src/lib.rs index cc353fae..95493def 100644 --- a/domains/cognition/towers/src/lib.rs +++ b/domains/cognition/towers/src/lib.rs @@ -3,9 +3,11 @@ //! game's own; each captain sees only `Game::view(team)`. The drones fly their orders on their own, //! as the server's plans make them fly (see [`Flyer`]). //! -//! A captain is anything that turns a team's view into orders ([`Captain`]): a `Commander` in this -//! process, or one in a process of its own ([`SeatProcess`]), so that two captains with different -//! code can play each other. +//! A game is played by a roster ([`Roster`]): a [`Command`] for each team, which turns the team's +//! view into orders, and a [`Unit`] for each drone, which flies on what the drone has on board +//! (`recreation_towers::actors`). A [`Captain`] is the two together, as one object that is the +//! team's commander and makes each drone's unit: a `Commander` in this process, or one in a process +//! of its own ([`SeatProcess`]), so that two captains with different code can play each other. mod autonomy; mod edge; @@ -15,19 +17,20 @@ pub use autonomy::{ After, Autonomy, Chain, Comparison, Failsafe, Persist, Prepared, Surface, Timed, compare, compare_captains, compare_with, }; -pub use edge::{Edge, Edged, FinishThenReturn, Keep, Outnumbered, compare_edge}; +pub use edge::{Edge, EdgeUnit, Edged, FinishThenReturn, Keep, Outnumbered, compare_edge}; pub use formation::{Formation, Post}; use glam::Vec2; // Where a drone circles and how high: the game's own numbers, as the server flies them. +use recreation_towers::Commander; use recreation_towers::flight::{ORBIT_RADIUS, low_height, orbit_height}; use recreation_towers::{ - Commander, CommanderTuning, EdgeObs, Game, Instruction, Order, Outcome, Phase, Queued, Rules, Standing, Team, - TeamView, + Autopilot, Command, CommanderTuning, Directive, Game, Instruction, OnBoard, Order, Outcome, Phase, Queued, Rules, + Standing, Team, TeamView, Unit, }; use serde::{Deserialize, Serialize}; use std::io::{BufRead, BufReader, Write}; -use std::process::{Child, ChildStdin, ChildStdout, Command, Stdio}; +use std::process::{Child, ChildStdin, ChildStdout, Stdio}; /// The arena's step (s), and how often each captain decides (s). const DT: f32 = 0.1; @@ -47,7 +50,9 @@ const G: f32 = 9.81; const WIND_MAX: f32 = 5.0; const DELAY: (f32, f32) = (0.3, 2.0); -/// What turns a team's view into orders. +/// A team's commander and the brain of each of its drones, as one object. It splits into a +/// [`Command`] (see [`Directing`]) and a [`Unit`] for each drone ([`Captain::unit`]): nothing but the +/// unit's own state goes on board. pub trait Captain { /// The orders for the team's drones: `(drone, order)` for each. fn decide(&mut self, view: &TeamView) -> Result, String>; @@ -63,10 +68,11 @@ pub trait Captain { self.standing(view, orders).into_iter().map(|(k, s)| (k, vec![Queued::from(s)])).collect() } - /// A drone that is out of contact flies `order`: its own decision from what it knows on - /// board, once a second. `None` goes on with the order. - fn edge(&mut self, _team: Team, _obs: &EdgeObs, _order: Order) -> Option { - None + /// The unit of drone `k` of `team`: its own brain on board. By default the [`Autopilot`]. It + /// is made once, before the game, and shares nothing with the captain that a drone could not + /// have on board. + fn unit(&self, _team: Team, _k: usize) -> Box { + Box::new(Autopilot) } } @@ -83,14 +89,68 @@ impl Captain for &mut C { (**self).queue(view, orders) } - fn edge(&mut self, team: Team, obs: &EdgeObs, order: Order) -> Option { - (**self).edge(team, obs, order) + fn unit(&self, team: Team, k: usize) -> Box { + (**self).unit(team, k) } } -impl Captain for Commander { +impl Captain for recreation_towers::Commander { fn decide(&mut self, view: &TeamView) -> Result, String> { - Ok(Commander::decide(self, view)) + Ok(recreation_towers::Commander::decide(self, view)) + } +} + +/// A captain as a [`Command`]: its orders go out with the queues it prepares for them. +pub struct Directing(pub C); + +impl Command for Directing { + fn decide(&mut self, view: &TeamView) -> Result, String> { + let orders = self.0.decide(view)?; + let queues = self.0.queue(view, &orders); + Ok(orders + .into_iter() + .map(|(k, order)| { + let queue = queues.iter().find(|(j, _)| *j == k).map(|(_, q)| q.clone()).unwrap_or_default(); + Directive { drone: k, order, queue } + }) + .collect()) + } +} + +/// Who plays: a [`Command`] for each team and a [`Unit`] for each drone, the drones of Blue first. +pub struct Roster<'a> { + pub blue: Box, + pub red: Box, + pub units: Vec>, +} + +impl<'a> Roster<'a> { + /// The two commanders, and `unit(team, k)` for drone `k` of each team. + pub fn new( + rules: &Rules, + blue: impl Command + 'a, + red: impl Command + 'a, + mut unit: impl FnMut(Team, usize) -> Box, + ) -> Roster<'a> { + let units = + Team::BOTH.into_iter().flat_map(|t| (0..rules.drones).map(move |k| (t, k))).map(|(t, k)| unit(t, k)); + Roster { blue: Box::new(blue), red: Box::new(red), units: units.collect() } + } + + /// Two captains, each with the units it makes. + pub fn of_captains(rules: &Rules, blue: impl Captain + 'a, red: impl Captain + 'a) -> Roster<'a> { + let units = Team::BOTH.into_iter().flat_map(|t| (0..rules.drones).map(move |k| (t, k))).map(|(t, k)| { + let captain = if t == Team::Blue { &blue as &dyn Captain } else { &red }; + captain.unit(t, k) + }); + let units = units.collect(); + Roster { blue: Box::new(Directing(blue)), red: Box::new(Directing(red)), units } + } + + /// Replaces the unit of drone `k` of `team`. + pub fn with_unit(mut self, rules: &Rules, team: Team, k: usize, unit: impl Unit + 'a) -> Roster<'a> { + self.units[team.index() * rules.drones + k] = Box::new(unit); + self } } @@ -192,7 +252,7 @@ impl TowersResult { } /// A game in progress: the referee, the drones flying their orders, and the orders on their way. -pub struct Match { +pub struct Match<'u> { pub game: Game, pub flyers: Vec, seed: u64, @@ -204,23 +264,31 @@ pub struct Match { /// What each drone was told: its order and its queue. told: Vec, next_decision: f32, - next_edge: f32, dark: [f32; 2], /// Each drone is down now, and how many times each team's drones went down. down: Vec, downs: [usize; 2], /// Each drone has taken off. took_off: Vec, + /// The unit of each drone: its own brain, run on every step. + units: Vec>, } -impl Match { +impl<'u> Match<'u> { /// The start of a game on `seed`: the drones on the ground, the wind and the delay drawn. - pub fn new(seed: u64) -> Match { + pub fn new(seed: u64) -> Match<'u> { Match::with_rules(Rules::default(), seed) } - /// The same, with the rules of the game. - pub fn with_rules(rules: Rules, seed: u64) -> Match { + /// The same, with the rules of the game. Every drone has the default unit. + pub fn with_rules(rules: Rules, seed: u64) -> Match<'u> { + let units = (0..2 * rules.drones).map(|_| Box::new(Autopilot) as Box).collect(); + Match::with_units(rules, seed, units) + } + + /// The same, with a unit for each drone, the drones of Blue first. + pub fn with_units(rules: Rules, seed: u64, units: Vec>) -> Match<'u> { + assert_eq!(units.len(), 2 * rules.drones, "a unit for each drone"); let mut rng = fastrand::Rng::with_seed(seed); let mut game = Game::new(rules); let wind = { @@ -254,29 +322,31 @@ impl Match { pending: vec![], told: vec![Instruction::default(); 2 * rules.drones], next_decision: 0.0, - next_edge: 0.0, dark: [0.0; 2], down: vec![false; 2 * rules.drones], downs: [0; 2], took_off: vec![false; 2 * rules.drones], + units, } } - /// One step: the captains decide when it is time, the orders arrive, the drones fly, the - /// referee steps. + /// One step with captains: see [`Match::step_with`]. Each team's commander is its captain's + /// [`Directing`]; the drones' units are the ones the match was built with. pub fn step(&mut self, blue: &mut dyn Captain, red: &mut dyn Captain) -> Result<(), String> { + self.step_with(&mut Directing(blue), &mut Directing(red)) + } + + /// One step: the commanders decide when it is time, the orders arrive, each unit flies its + /// drone, the referee steps. + pub fn step_with(&mut self, blue: &mut dyn Command, red: &mut dyn Command) -> Result<(), String> { let (game, flyers) = (&mut self.game, &mut self.flyers); let drones = game.rules.drones; if game.time >= self.next_decision { self.next_decision += DECIDE_EVERY; for team in Team::BOTH { - let captain: &mut dyn Captain = if team == Team::Blue { &mut *blue } else { &mut *red }; - let view = game.view(team); - let orders = captain.decide(&view)?; - let queues = captain.queue(&view, &orders); - for &(k, order) in orders.iter().filter(|(k, _)| *k < drones) { - let q = queues.iter().find(|(j, _)| *j == k).map(|(_, q)| q.clone()).unwrap_or_default(); - self.pending.push((game.time + self.delay, game.drone(team, k), order, q)); + let commander: &mut dyn Command = if team == Team::Blue { &mut *blue } else { &mut *red }; + for d in commander.decide(&game.view(team))?.into_iter().filter(|d| d.drone < drones) { + self.pending.push((game.time + self.delay, game.drone(team, d.drone), d.order, d.queue)); } } } @@ -291,33 +361,13 @@ impl Match { } false }); - let edge_now = game.time >= self.next_edge; - if edge_now { - self.next_edge += DECIDE_EVERY; - } for (i, told) in self.told.iter_mut().enumerate() { - // Out of contact, a drone works down its queue, or flies home. - told.tick_with(game.silent[i], game.rules.link_timeout, game.charge(i), game.rules.bingo, |o| { - game.order_done(i, o) - }); - // A drone out of contact decides for itself, once a second, from what it knows on board. - if edge_now - && game.silent[i] > 0.0 - && game.drones[i].phase == Phase::Flying - && let Some(order) = told.order - { - let team = game.drones[i].team; - let captain: &mut dyn Captain = if team == Team::Blue { &mut *blue } else { &mut *red }; - if let Some(new) = captain.edge(team, &game.edge_obs(i), order) - && new != order - { - // Its own decision replaces the queue: the failsafe still holds. - told.decide(new); - } - } - if flyers[i].order != told.order { + // Each drone's unit runs on every step, in contact or not, from what the drone has on + // board and what it was told. What it returns is the order the drone flies. + let flown = self.units[i].fly(&OnBoard::new(game, i), told); + if flyers[i].order != flown { flyers[i].hold = flyers[i].at; - flyers[i].order = told.order; + flyers[i].order = flown; } } for (i, f) in flyers.iter_mut().enumerate() { @@ -338,6 +388,7 @@ impl Match { let heading = if d.team == Team::Blue { 0.0 } else { std::f32::consts::PI }; *f = Flyer { at: d.at, alt: 0.0, heading, flying: false, order: None, hold: d.at, ..*f }; self.told[i] = Instruction::default(); + self.units[i].restart(); } f.step(game, self.wind, TAKEOFF_APART * (i % drones) as f32, DT); self.took_off[i] |= f.flying; @@ -393,7 +444,12 @@ pub fn play_with( red: &mut dyn Captain, seed: u64, ) -> Result { - let mut m = Match::with_rules(rules, seed); + let units = Team::BOTH + .into_iter() + .flat_map(|t| (0..rules.drones).map(move |k| (t, k))) + .map(|(t, k)| if t == Team::Blue { blue.unit(t, k) } else { red.unit(t, k) }) + .collect(); + let mut m = Match::with_units(rules, seed, units); loop { if let Some(result) = m.result() { return Ok(result); @@ -402,13 +458,58 @@ pub fn play_with( } } +/// Plays one game by the rules, with a roster: a commander for each team and a unit for each drone. +pub fn play_roster(rules: Rules, roster: Roster<'_>, seed: u64) -> Result { + let Roster { mut blue, mut red, units } = roster; + let mut m = Match::with_units(rules, seed, units); + loop { + if let Some(result) = m.result() { + return Ok(result); + } + m.step_with(&mut blue, &mut red)?; + } +} + /// A captain in a process of its own: the seat's JSON lines on its stdin and stdout. It gets /// `{"Start": {"team": ..., "tuning": [...]}}` before each game and a `TeamView` for each -/// decision, and answers each with a line: `"ok"`, or a list of `[drone, order]`. +/// decision, and answers each with a line: `"ok"`, or a list of `[drone, order]` or +/// `[drone, order, queue]` (the queue that goes with the order, as a list of [`Queued`]). Only +/// `decide` crosses: the drones' units are the default. pub struct SeatProcess { child: Child, to: ChildStdin, from: BufReader, + /// The queues of the last reply. + queues: Vec<(usize, Vec)>, +} + +/// One order of a captain's reply: the old reply is `[drone, order]`, a reply with a queue +/// `[drone, order, queue]`. Both are read. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +#[serde(untagged)] +pub enum Reply { + Plain(usize, Order), + Queued(usize, Order, Vec), +} + +impl From<&Directive> for Reply { + /// An order with no queue is sent as it always was. + fn from(d: &Directive) -> Reply { + if d.queue.is_empty() { + Reply::Plain(d.drone, d.order) + } else { + Reply::Queued(d.drone, d.order, d.queue.clone()) + } + } +} + +impl From for Directive { + fn from(r: Reply) -> Directive { + match r { + Reply::Plain(drone, order) => Directive::new(drone, order), + Reply::Queued(drone, order, queue) => Directive::with_queue(drone, order, queue), + } + } } /// A line to a captain's process. @@ -421,7 +522,7 @@ pub enum ToCaptain { impl SeatProcess { /// Starts `command` (with `args`, in `dir`), a program that runs [`serve`]. pub fn spawn(command: &str, args: &[&str], dir: &std::path::Path) -> Result { - let mut child = Command::new(command) + let mut child = std::process::Command::new(command) .args(args) .current_dir(dir) .stdin(Stdio::piped()) @@ -431,7 +532,7 @@ impl SeatProcess { .map_err(|e| format!("{command}: {e}"))?; let to = child.stdin.take().unwrap(); let from = BufReader::new(child.stdout.take().unwrap()); - Ok(SeatProcess { child, to, from }) + Ok(SeatProcess { child, to, from, queues: vec![] }) } fn ask(&mut self, msg: &ToCaptain) -> Result { @@ -453,7 +554,14 @@ impl SeatProcess { impl Captain for SeatProcess { fn decide(&mut self, view: &TeamView) -> Result, String> { let line = self.ask(&ToCaptain::View(Box::new(view.clone())))?; - serde_json::from_str(&line).map_err(|e| format!("the captain said {line:?}: {e}")) + let reply: Vec = serde_json::from_str(&line).map_err(|e| format!("the captain said {line:?}: {e}"))?; + let directives: Vec = reply.into_iter().map(Directive::from).collect(); + self.queues = directives.iter().filter(|d| !d.queue.is_empty()).map(|d| (d.drone, d.queue.clone())).collect(); + Ok(directives.into_iter().map(|d| (d.drone, d.order)).collect()) + } + + fn queue(&mut self, _: &TeamView, _: &[(usize, Order)]) -> Vec<(usize, Vec)> { + std::mem::take(&mut self.queues) } } @@ -466,8 +574,18 @@ impl Drop for SeatProcess { /// Serves a captain on `input` and `output` (a [`SeatProcess`]'s other end): this build's /// `Commander`, with the tuning each game starts with. -pub fn serve(input: impl BufRead, mut output: impl Write) -> Result<(), String> { - let mut captain: Option = None; +pub fn serve(input: impl BufRead, output: impl Write) -> Result<(), String> { + serve_with(input, output, |tuning| Box::new(Commander::with_tuning(league_rules(), tuning))) +} + +/// The same with any [`Command`], made from the tuning each game starts with. Each reply carries +/// the queue of each order that has one. +pub fn serve_with( + input: impl BufRead, + mut output: impl Write, + make: impl Fn(CommanderTuning) -> Box, +) -> Result<(), String> { + let mut captain: Option> = None; for line in input.lines() { let line = line.map_err(|e| e.to_string())?; let reply = match serde_json::from_str::(&line).map_err(|e| e.to_string())? { @@ -476,12 +594,13 @@ pub fn serve(input: impl BufRead, mut output: impl Write) -> Result<(), String> .ok() .and_then(|a| CommanderTuning::from_array(&a)) .ok_or("a tuning out of bounds")?; - captain = Some(Commander::with_tuning(league_rules(), tuning)); + captain = Some(make(tuning)); "\"ok\"".to_string() } ToCaptain::View(view) => { let c = captain.as_mut().ok_or("a view before the start")?; - serde_json::to_string(&Commander::decide(c, &view)).unwrap() + let reply: Vec = c.decide(&view)?.iter().map(Reply::from).collect(); + serde_json::to_string(&reply).unwrap() } }; writeln!(output, "{reply}").map_err(|e| e.to_string())?; @@ -739,4 +858,165 @@ mod tests { run(&mut m, 11.0, &mut Silent); assert_eq!(m.result().unwrap().downs, [1, 0]); } + + // ---- The roster: a commander for each team, a unit for each drone ---- + + use std::cell::RefCell; + use std::rc::Rc; + + /// What a counting unit saw, for each drone: steps it ran, steps it ran flying in contact, and + /// flying out of contact. + type Calls = Rc>>; + + /// A unit that counts its calls and flies as the autopilot does. + struct Counting(usize, Calls); + + impl Unit for Counting { + fn fly(&mut self, on: &OnBoard, told: &mut Instruction) -> Option { + let mut calls = self.1.borrow_mut(); + calls[self.0][0] += 1; + if on.flying() { + calls[self.0][if on.in_contact() { 1 } else { 2 }] += 1; + } + Autopilot.fly(on, told) + } + } + + /// A unit that always flies home, whatever it was told, in contact or not. + struct Recall; + + impl Unit for Recall { + fn fly(&mut self, _: &OnBoard, told: &mut Instruction) -> Option { + told.decide(Order::Home); + told.order + } + } + + fn commander() -> Commander { + Commander::new(Rules::default()).with_lookahead(false) + } + + #[test] + fn a_unit_runs_on_every_step_for_every_drone_in_contact_or_not() { + let rules = Rules::default(); + let calls: Calls = Rc::new(RefCell::new(vec![[0; 3]; 2 * rules.drones])); + let units = (0..2 * rules.drones).map(|i| Box::new(Counting(i, calls.clone())) as Box).collect(); + let mut m = Match::with_units(rules, 1, units); + // Blue's first drone flies in a radio shadow; the others take off and fly in contact. + let shadowed = put_blue(&mut m, true); + let steps = 200; + for _ in 0..steps { + m.step_with(&mut Directing(Silent), &mut Directing(Silent)).unwrap(); + } + let calls = calls.borrow(); + assert!(calls.iter().all(|c| c[0] == steps), "each drone's unit ran on every step: {calls:?}"); + assert!(calls[shadowed][2] > 50, "a unit flying out of contact runs: {:?}", calls[shadowed]); + let linked: usize = calls.iter().enumerate().filter(|(i, _)| *i != shadowed).map(|(_, c)| c[1]).sum(); + assert!(linked > 50, "units flying in contact run too: {calls:?}"); + } + + #[test] + fn a_roster_of_default_units_plays_the_game_that_play_does() { + let rules = Rules::default(); + let by_roster = play_roster(rules, Roster::of_captains(&rules, commander(), commander()), 4).unwrap(); + assert_eq!(by_roster, play_with(rules, &mut commander(), &mut commander(), 4).unwrap()); + } + + /// Plays Blue's commander against Red's, with `unit(team, k)` for each drone. + fn mixed(seed: u64, unit: impl FnMut(Team, usize) -> Box) -> TowersResult { + let rules = Rules::default(); + play_roster(rules, Roster::new(&rules, commander(), commander(), unit), seed).unwrap() + } + + fn default_units(_: Team, _: usize) -> Box { + Box::new(Autopilot) + } + + #[test] + fn the_two_teams_can_fly_different_units() { + // Blue's drones on the autopilot; Red's go home when one enemy is near, as they take a tower. + let red_edge = |t: Team, _| -> Box { + if t == Team::Blue { Box::new(Autopilot) } else { Box::new(EdgeUnit::new(t, Outnumbered { n: 1 })) } + }; + let mut differs = 0; + for seed in 1..=6 { + let (a, b) = (mixed(seed, red_edge), mixed(seed, red_edge)); + assert_eq!(a, b, "the same seed plays the same game"); + differs += (a != mixed(seed, default_units)) as usize; + } + assert!(differs > 0, "Red's unit never changed a game"); + } + + #[test] + fn one_drone_can_fly_a_unit_unlike_its_team_mates() { + // Blue's first drone ignores its orders and flies home; its team-mates fly them. + let recall = |t: Team, k: usize| -> Box { + if (t, k) == (Team::Blue, 0) { Box::new(Recall) } else { Box::new(Autopilot) } + }; + let (a, b) = (mixed(2, recall), mixed(2, recall)); + assert_eq!(a, b); + assert_ne!(a, mixed(2, default_units), "the drone that flew home changed the game"); + } + + #[test] + fn a_roster_can_swap_the_unit_of_one_drone() { + let rules = Rules::default(); + let play = || { + let roster = Roster::of_captains(&rules, commander(), commander()).with_unit(&rules, Team::Red, 1, Recall); + play_roster(rules, roster, 2).unwrap() + }; + assert_eq!(play(), play()); + assert_ne!(play(), play_with(rules, &mut commander(), &mut commander(), 2).unwrap()); + } + + #[test] + fn an_old_reply_with_no_queue_is_read_and_a_queue_comes_with_the_order() { + let old: Vec = serde_json::from_str(r#"[[0,"Hold"],[3,{"Take":2}]]"#).unwrap(); + assert_eq!(old, vec![Reply::Plain(0, Order::Hold), Reply::Plain(3, Order::Take(2))]); + let queue = vec![Queued::silent(5.0, Order::Home), Queued::done(Order::Hold)]; + let new = Reply::from(&Directive::with_queue(1, Order::Take(2), queue.clone())); + let line = serde_json::to_string(&vec![new.clone()]).unwrap(); + assert_eq!(serde_json::from_str::>(&line).unwrap(), vec![new]); + // An order with no queue goes out as before. + let plain = serde_json::to_string(&vec![Reply::from(&Directive::new(0, Order::Hold))]).unwrap(); + assert_eq!(plain, r#"[[0,"Hold"]]"#); + assert_eq!(Directive::from(Reply::Queued(1, Order::Home, queue.clone())).queue, queue); + } + + #[test] + fn a_commander_served_in_a_process_sends_each_order_with_its_queue() { + struct Queueing; + impl Command for Queueing { + fn decide(&mut self, _: &TeamView) -> Result, String> { + Ok(vec![Directive::with_queue(0, Order::Hold, vec![Queued::silent(5.0, Order::Home)])]) + } + } + let (to_r, mut to_w) = std::io::pipe().unwrap(); + let (from_r, from_w) = std::io::pipe().unwrap(); + std::thread::spawn(move || serve_with(BufReader::new(to_r), from_w, |_| Box::new(Queueing))); + let mut from = BufReader::new(from_r); + let start = ToCaptain::Start { team: Team::Blue, tuning: CommanderTuning::DEFAULT.to_array().to_vec() }; + let view = ToCaptain::View(Box::new(Game::new(Rules::default()).view(Team::Blue))); + let mut line = String::new(); + for msg in [start, view] { + writeln!(to_w, "{}", serde_json::to_string(&msg).unwrap()).unwrap(); + line.clear(); + from.read_line(&mut line).unwrap(); + } + let reply: Vec = serde_json::from_str(&line).unwrap(); + assert_eq!(reply, vec![Reply::Queued(0, Order::Hold, vec![Queued::silent(5.0, Order::Home)])]); + } + + #[test] + fn a_seat_process_hands_the_queues_of_its_reply_to_the_match() { + // A shell that answers every line with one order and a queue, and one order with none. + let reply = r#"[[0,"Hold",[{"when":{"Silent":5.0},"order":"Home"}]],[1,"Hold"]]"#; + let script = format!("while read l; do echo '{reply}'; done"); + let mut seat = SeatProcess::spawn("sh", &["-c", &script], std::path::Path::new(".")).unwrap(); + let view = Game::new(Rules::default()).view(Team::Blue); + let ds = Directing(&mut seat).decide(&view).unwrap(); + assert_eq!((ds[0].order, ds[1].order), (Order::Hold, Order::Hold)); + assert_eq!(ds[0].queue, vec![Queued::silent(5.0, Order::Home)]); + assert!(ds[1].queue.is_empty()); + } } diff --git a/domains/recreation/towers/src/actors.rs b/domains/recreation/towers/src/actors.rs new file mode 100644 index 00000000..71a2580b --- /dev/null +++ b/domains/recreation/towers/src/actors.rs @@ -0,0 +1,219 @@ +//! The two roles that play Towers, each its own trait so that each can be a person or a computer +//! (plans/actors.md): the [`Command`], one for a team, and the [`Unit`], one for a drone. Neither +//! does I/O: the arena, the game server and a seat in another process all drive the same traits. +//! +//! A command sees what its seat gets, the team's map ([`TeamView`]), and answers with a +//! [`Directive`] for each drone it wants to change: an order and the queue that goes with it. +//! +//! A unit sees what its drone has on board ([`OnBoard`]): its own camera and battery, whether it +//! is in contact, and the [`Instruction`] it last received. It runs on every step of the drone's +//! loop, in contact or not. What it returns is the order the drone flies. A unit can read +//! nothing else: [`OnBoard`] has no way to another drone, or to the team's map. + +use crate::{EdgeObs, Game, Instruction, Order, Phase, Queued, Team, TeamView, edge}; +use std::cell::OnceCell; + +/// What a command gives a drone: the order, and the queue for when it is out of contact. +#[derive(Clone, Debug, PartialEq)] +pub struct Directive { + pub drone: usize, + pub order: Order, + pub queue: Vec, +} + +impl Directive { + /// An order with no queue: out of contact, the drone flies home when its link is lost. + pub fn new(drone: usize, order: Order) -> Directive { + Directive { drone, order, queue: vec![] } + } + + pub fn with_queue(drone: usize, order: Order, queue: Vec) -> Directive { + Directive { drone, order, queue } + } +} + +/// A team's commander: looks at the team's map and gives orders. One for each team. +pub trait Command { + /// The directives for the drones whose order it changes. A drone it does not name goes on + /// with what it was told. + fn decide(&mut self, view: &TeamView) -> Result, String>; +} + +impl Command for &mut C { + fn decide(&mut self, view: &TeamView) -> Result, String> { + (**self).decide(view) + } +} + +impl Command for Box { + fn decide(&mut self, view: &TeamView) -> Result, String> { + (**self).decide(view) + } +} + +impl Command for crate::Commander { + fn decide(&mut self, view: &TeamView) -> Result, String> { + Ok(crate::Commander::decide(self, view).into_iter().map(|(k, o)| Directive::new(k, o)).collect()) + } +} + +/// What a drone knows on board, and no more. The fields are private, so a unit cannot get at the +/// referee's game, the other drones or the team's map. +pub struct OnBoard<'a> { + game: &'a Game, + i: usize, + obs: OnceCell, +} + +impl<'a> OnBoard<'a> { + /// What drone `i` has on board in `game`. + pub fn new(game: &'a Game, i: usize) -> OnBoard<'a> { + OnBoard { game, i, obs: OnceCell::new() } + } + + pub fn team(&self) -> Team { + self.game.drones[self.i].team + } + + /// Which of the team's drones this is, from 0. + pub fn number(&self) -> usize { + self.i % self.game.rules.drones + } + + /// The drone is in the air (not on the ground, not down). + pub fn flying(&self) -> bool { + self.game.drones[self.i].phase == Phase::Flying + } + + /// The drone's own clock (s since the game began). + pub fn time(&self) -> f32 { + self.game.time + } + + /// How long it has been out of contact (s); 0 while it is in contact. + pub fn silent(&self) -> f32 { + self.game.silent[self.i] + } + + pub fn in_contact(&self) -> bool { + self.silent() <= 0.0 + } + + /// What is left of the battery (s); `None` in a game with no batteries. + pub fn battery(&self) -> Option { + self.game.charge(self.i) + } + + /// How long out of contact before the link counts as lost (s). + pub fn link_timeout(&self) -> f32 { + self.game.rules.link_timeout + } + + /// The battery (s) at which a drone with no plan flies home. + pub fn bingo(&self) -> f32 { + self.game.rules.bingo + } + + /// Its camera, its position and the rest of [`EdgeObs`]. Worked out when first asked. + pub fn obs(&self) -> &EdgeObs { + self.obs.get_or_init(|| self.game.edge_obs(self.i)) + } + + /// Whether `order` is done, as the drone can tell from its own camera and position. + pub fn done(&self, order: Order) -> bool { + edge::order_done(self.obs(), self.team(), order) + } +} + +/// A drone's own brain. It runs on each step of the drone's loop (ten a second in the arena), +/// whether the drone is in contact or not, and keeps what state it likes. +pub trait Unit { + /// One step. `told` is what the drone last received from its commander, and how far down the + /// queue it is; the unit may change it (the drone's memory is its own). Returns the order the + /// drone flies now: `None` while it has none. + fn fly(&mut self, on: &OnBoard, told: &mut Instruction) -> Option; + + /// The drone is back on the ground after it went down: it forgets what it was doing. + fn restart(&mut self) {} +} + +impl Unit for Box { + fn fly(&mut self, on: &OnBoard, told: &mut Instruction) -> Option { + (**self).fly(on, told) + } + + fn restart(&mut self) { + (**self).restart() + } +} + +/// The default unit: flies the order it has, works down the queue when it is out of contact, and +/// flies home when its link is lost or its battery is low and the queue has nothing for it. This is +/// [`Instruction::tick_with`], the rule the flight controller and the server fly. +#[derive(Clone, Copy, Debug, Default)] +pub struct Autopilot; + +impl Unit for Autopilot { + fn fly(&mut self, on: &OnBoard, told: &mut Instruction) -> Option { + told.tick_with(on.silent(), on.link_timeout(), on.battery(), on.bingo(), |o| on.done(o)); + told.order + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{Rules, Standing}; + use glam::Vec2; + + fn game() -> Game { + let mut g = Game::new(Rules::default()); + g.look(); + g + } + + #[test] + fn the_autopilot_flies_the_order_it_was_told_in_contact() { + let g = game(); + let mut told = Instruction::default(); + told.give(Order::Hold, None); + assert_eq!(Autopilot.fly(&OnBoard::new(&g, 0), &mut told), Some(Order::Hold)); + } + + #[test] + fn the_autopilot_works_down_the_queue_out_of_contact_and_then_flies_home() { + let mut g = game(); + let mut told = Instruction::default(); + told.give(Order::Hold, Some(Standing { after: 5.0, then: Order::Goto(Vec2::ZERO) })); + g.silent[0] = 4.0; + assert_eq!(Autopilot.fly(&OnBoard::new(&g, 0), &mut told), Some(Order::Hold)); + g.silent[0] = 5.0; + assert_eq!(Autopilot.fly(&OnBoard::new(&g, 0), &mut told), Some(Order::Goto(Vec2::ZERO))); + // With nothing queued, a lost link is the failsafe. + let mut bare = Instruction::default(); + bare.give(Order::Hold, None); + g.silent[0] = g.rules.link_timeout; + assert_eq!(Autopilot.fly(&OnBoard::new(&g, 0), &mut bare), Some(Order::Home)); + } + + #[test] + fn on_board_is_what_the_drone_knows() { + let mut g = game(); + g.silent[3] = 7.0; + let on = OnBoard::new(&g, 3); + assert_eq!((on.team(), on.number()), (Team::Blue, 3 % g.rules.drones)); + assert!(!on.in_contact()); + assert_eq!(on.obs().silent, 7.0); + assert_eq!(on.battery(), g.charge(3)); + let ground = OnBoard::new(&g, 0); + assert!(ground.in_contact() && !ground.flying()); + } + + #[test] + fn a_command_is_a_commander_that_answers_with_directives() { + let g = game(); + let mut c = crate::Commander::new(Rules::default()); + let ds = Command::decide(&mut c, &g.view(Team::Blue)).unwrap(); + assert!(!ds.is_empty() && ds.iter().all(|d| d.queue.is_empty())); + } +} diff --git a/domains/recreation/towers/src/lib.rs b/domains/recreation/towers/src/lib.rs index fd765b75..10e54111 100644 --- a/domains/recreation/towers/src/lib.rs +++ b/domains/recreation/towers/src/lib.rs @@ -13,6 +13,7 @@ //! The referee only needs where each drone is and whether it flies; the host moves the drones. //! Positions are on the field: north and east of its centre (m), and a height above it (m). +mod actors; mod advisor; mod belief; mod commander; @@ -26,6 +27,7 @@ pub mod seat; pub mod svg; mod view; +pub use actors::{Autopilot, Command, Directive, OnBoard, Unit}; pub use advisor::{Advice, AdviceKind, Advisor}; pub use belief::Belief; pub use commander::{Commander, CommanderTuning}; diff --git a/expectations/glossary.expect b/expectations/glossary.expect index fac40065..f2b86cb9 100644 --- a/expectations/glossary.expect +++ b/expectations/glossary.expect @@ -383,6 +383,17 @@ Term: Captain The computer in the seat of a team in [[Towers]]: `recreation_towers::Commander`. It reads the map of its team and gives each drone an order. The [[league]] makes better captains. +Term: Commander + In [[Towers]], what gives the orders of a team: one for each team. It sees only the map of its + team, and gives a drone an order and its [[queue]]. It is a computer ([[captain]]) or a person at + the commander app, in the team's [[seat]]. + +Term: Unit + In [[Towers]], what flies one drone: one for each drone. It runs all the time, in contact or not, + and knows only what its drone has on board: what its camera sees, its [[battery]], whether it is + in contact, and the order and [[queue]] it was last given. By default it flies the order, works + down the queue when it is out of contact, and flies home at the end of it. + Term: PreArm A check that ArduPilot does before it arms. If a check fails, ArduPilot does not arm, and it sends a message that starts with "PreArm:" and says why. diff --git a/plans/actors.md b/plans/actors.md index 6b045418..ebc7aae7 100644 --- a/plans/actors.md +++ b/plans/actors.md @@ -111,6 +111,35 @@ server. 2. **Split `Captain`.** In `cognition_towers`: a commander for a team, a unit for each drone (the default flies the queue), advisors. `Match` takes a roster. The out-of-process seat carries the queue. Gate: the same results on the same seeds. + **Results (commander, units and a roster).** Done in the arena; advisors are step 4's. The two + roles are traits in `recreation_towers` (`actors.rs`, no I/O, for the arena and the server): + `Command { decide(&mut self, &TeamView) -> Result, String> }`, a `Directive` being + `{ drone, order, queue }`, and `Unit { fly(&mut self, &OnBoard, &mut Instruction) -> Option; + restart() }`. `OnBoard` has private fields and methods for what a drone has on board only: its team + and number, whether it flies, its clock, `silent()` and `in_contact()`, battery, the link timeout + and bingo level, `obs()` (its `EdgeObs`, worked out when asked) and `done(order)`. The default unit + is `Autopilot`: `Instruction::tick_with`. `Match::with_units` takes a unit for each of the ten + drones; `Match::step_with(&mut dyn Command, &mut dyn Command)` takes the commanders; `Roster` + (`Roster::new(rules, blue, red, |team, k| unit)`, `Roster::of_captains`, `with_unit`) and + `play_roster` put them together. A unit runs on every step of the arena (ten a second), for every + drone, in contact or not: the queue has to be worked at that rate for the games to stay the same. + The edge policies run once a second inside `EdgeUnit`, as before. + + `Captain` is kept: `Directing(captain)` is its `Command` (orders with `queue`), and + `Captain::unit(team, k)` replaces `Captain::edge` and makes the unit of each drone (default + `Autopilot`; `Edged` makes an `EdgeUnit` with its own copy of the policy). No `edge` read captain + state, so nothing was shared: `Edged`'s policy is stateless and cloned (`compare_edge` takes an + `Rc` for that). `Prepared` only changes the queue that goes with an order, so it is on the + command side. The seat protocol: a reply is a list of `[drone, order]` or `[drone, order, queue]` + (`Reply`); an old reply is read as before, and `serve_with` serves any `Command`. + + Same results on the same seeds, digit for digit: `denial`, `ruleset v2 200`, `edge 30` and + `cognition_league judge --game captain --champion timid --challenger default --seeds 24` print the + same before and after. New tests: a counting unit runs on every step of every drone, in contact + or not; a roster with different units for the two teams, and for one drone, plays the same game + twice and not the game of the default units; the old and the new reply; a served and a spawned seat + with a queue. + 3. **One commander path.** The computer commander takes a `Command` seat like a person, with its own `Ground` and a map from telemetry. A seat can be given up and taken during a game, and the computer takes a seat that a person leaves. Two people can play (phase 4 of