Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion domains/cognition/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`) |

Expand Down
15 changes: 8 additions & 7 deletions domains/cognition/towers/examples/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u64> = (1..=n).collect();
let policies: Vec<Box<dyn Edge>> = 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<Rc<dyn Edge>> = 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(),
Expand Down
79 changes: 60 additions & 19 deletions domains/cognition/towers/src/edge.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -57,14 +62,59 @@ impl Edge for FinishThenReturn {
}
}

impl<E: Edge + ?Sized> Edge for Rc<E> {
fn name(&self) -> String {
(**self).name()
}

fn act(&self, team: Team, obs: &EdgeObs, order: Order) -> Option<Order> {
(**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<E> {
team: Team,
edge: E,
next: f32,
}

impl<E: Edge> EdgeUnit<E> {
pub fn new(team: Team, edge: E) -> EdgeUnit<E> {
EdgeUnit { team, edge, next: 0.0 }
}
}

impl<E: Edge> Unit for EdgeUnit<E> {
fn fly(&mut self, on: &OnBoard, told: &mut Instruction) -> Option<Order> {
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<C, E> {
pub captain: C,
pub edge: E,
}

impl<C: Captain, E: Edge> Captain for Edged<C, E> {
impl<C: Captain, E: Edge + Clone + 'static> Captain for Edged<C, E> {
fn decide(&mut self, view: &TeamView) -> Result<Vec<(usize, Order)>, String> {
self.captain.decide(view)
}
Expand All @@ -73,26 +123,17 @@ impl<C: Captain, E: Edge> Captain for Edged<C, E> {
orders.iter().map(|&(k, o)| (k, Standing { after: 0.0, then: o })).collect()
}

fn edge(&mut self, team: Team, obs: &EdgeObs, order: Order) -> Option<Order> {
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<dyn Unit> {
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<Comparison, String> {
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<Order> {
self.0.act(team, obs, order)
}
}
pub fn compare_edge(rules: Rules, edge: Rc<dyn Edge>, seeds: &[u64]) -> Result<Comparison, String> {
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,
)
Expand Down Expand Up @@ -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);
}
Expand All @@ -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:?}");
}
}
Loading
Loading