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
29 changes: 29 additions & 0 deletions domains/cognition/towers/examples/advice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//! What advice is worth, and what lateness costs: a commander with the lookahead as its advisor,
//! at several delays, against a commander with no advisor, on paired seeds, both sides:
//! `cargo run --release -p cognition_towers --example advice [SEEDS] [DELAY...]`.
//! The table in `plans/actors.md` (step 4) is read off its output.

use cognition_towers::{Captain, Comparison, compare_captains};
use recreation_towers::{Commander, Lookahead, Rules};

fn main() -> Result<(), String> {
let mut args = std::env::args().skip(1);
let n: u64 = args.next().and_then(|s| s.parse().ok()).unwrap_or(100);
let mut delays: Vec<f32> = args.filter_map(|s| s.parse().ok()).collect();
if delays.is_empty() {
delays = vec![0.0, 2.0, 5.0, 10.0, 30.0];
}
let rules = Rules::default();
let seeds: Vec<u64> = (1..=n).collect();
println!("advice: the lookahead as an advisor against no advisor, {n} seeds ({} games each)\n", 2 * n);
println!("| delay | margin | won | lost | drawn |\n|---|---|---|---|---|");
for delay in delays {
let advised = || -> Box<dyn Captain> {
Box::new(Commander::new(rules).with_lookahead(false).with_advisor(Lookahead::new(rules), delay))
};
let alone = || -> Box<dyn Captain> { Box::new(Commander::new(rules).with_lookahead(false)) };
let Comparison { margin, error, won, lost, drawn, .. } = compare_captains(rules, &advised, &alone, &seeds)?;
println!("| {delay} s | {margin:+.2} ± {error:.2} | {won} | {lost} | {drawn} |");
}
Ok(())
}
69 changes: 67 additions & 2 deletions domains/cognition/towers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ use glam::Vec2;
use recreation_towers::Commander;
use recreation_towers::flight::{ORBIT_RADIUS, low_height, orbit_height};
use recreation_towers::{
Autopilot, Command, CommanderTuning, Directive, Game, Instruction, OnBoard, Order, Outcome, Phase, Queued, Rules,
Standing, Team, TeamView, Unit,
Advise, 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};
Expand Down Expand Up @@ -74,6 +74,11 @@ pub trait Captain {
fn unit(&self, _team: Team, _k: usize) -> Box<dyn Unit> {
Box::new(Autopilot)
}

/// Gives the captain an advisor (see [`Command::advised_by`]); whether it takes advice.
fn advised_by(&mut self, _advisor: Box<dyn Advise>, _delay: f32) -> bool {
false
}
}

impl<C: Captain + ?Sized> Captain for &mut C {
Expand All @@ -92,12 +97,26 @@ impl<C: Captain + ?Sized> Captain for &mut C {
fn unit(&self, team: Team, k: usize) -> Box<dyn Unit> {
(**self).unit(team, k)
}

fn advised_by(&mut self, advisor: Box<dyn Advise>, delay: f32) -> bool {
(**self).advised_by(advisor, delay)
}
}

impl Captain for recreation_towers::Commander {
fn decide(&mut self, view: &TeamView) -> Result<Vec<(usize, Order)>, String> {
Ok(recreation_towers::Commander::decide(self, view))
}

/// The queues of the proposals that it took.
fn queue(&mut self, _view: &TeamView, orders: &[(usize, Order)]) -> Vec<(usize, Vec<Queued>)> {
orders.iter().map(|&(k, _)| (k, self.advised_queue(k))).filter(|(_, q)| !q.is_empty()).collect()
}

fn advised_by(&mut self, advisor: Box<dyn Advise>, delay: f32) -> bool {
self.add_advisor(advisor, delay);
true
}
}

/// A captain as a [`Command`]: its orders go out with the queues it prepares for them.
Expand All @@ -115,6 +134,10 @@ impl<C: Captain> Command for Directing<C> {
})
.collect())
}

fn advised_by(&mut self, advisor: Box<dyn Advise>, delay: f32) -> bool {
self.0.advised_by(advisor, delay)
}
}

/// Who plays: a [`Command`] for each team and a [`Unit`] for each drone, the drones of Blue first.
Expand Down Expand Up @@ -147,6 +170,23 @@ impl<'a> Roster<'a> {
Roster { blue: Box::new(Directing(blue)), red: Box::new(Directing(red)), units }
}

/// Gives `team`'s commander an advisor, whose proposals reach it `delay` s of game time after the
/// map it was shown (the arena has no threads: the delay is in game time, so a game is
/// reproducible for its seed). Fails if the commander takes no advice.
pub fn with_advisor(
mut self,
team: Team,
advisor: impl Advise + 'static,
delay: f32,
) -> Result<Roster<'a>, String> {
let command = if team == Team::Blue { &mut self.blue } else { &mut self.red };
if command.advised_by(Box::new(advisor), delay) {
Ok(self)
} else {
Err(format!("the {team:?} commander takes no advice"))
}
}

/// 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);
Expand Down Expand Up @@ -922,6 +962,31 @@ mod tests {
assert_eq!(by_roster, play_with(rules, &mut commander(), &mut commander(), 4).unwrap());
}

#[test]
fn a_roster_gives_a_team_an_advisor_that_is_late_in_game_time() {
use recreation_towers::Lookahead;
let rules = Rules::default();
let bare = || Commander::new(rules).with_lookahead(false);
let thinking = || Commander::new(rules);
let play = |roster: Result<Roster<'_>, String>| play_roster(rules, roster.unwrap(), 6).unwrap();
// No delay: the lookahead as an advisor is the commander that thinks ahead.
let at_once = Roster::of_captains(&rules, bare(), bare()).with_advisor(Team::Blue, Lookahead::new(rules), 0.0);
let by_default = Roster::of_captains(&rules, thinking(), bare());
assert_eq!(play(at_once), play(Ok(by_default)));
// Late, it is another game, and the same game each time.
let late = || Roster::of_captains(&rules, bare(), bare()).with_advisor(Team::Blue, Lookahead::new(rules), 7.0);
assert_eq!(play(late()), play(late()));
// A commander that takes no advice says so.
struct Deaf;
impl Command for Deaf {
fn decide(&mut self, _: &TeamView) -> Result<Vec<Directive>, String> {
Ok(vec![])
}
}
let deaf = Roster::new(&rules, Deaf, bare(), |_, _| Box::new(Autopilot));
assert!(deaf.with_advisor(Team::Blue, Lookahead::new(rules), 0.0).is_err());
}

/// Plays Blue's commander against Red's, with `unit(team, k)` for each drone.
fn mixed(seed: u64, unit: impl FnMut(Team, usize) -> Box<dyn Unit>) -> TowersResult {
let rules = Rules::default();
Expand Down
18 changes: 14 additions & 4 deletions domains/recreation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ In the sim, **G** starts a pylon race and **T** a game of tag. The same key agai
| `recreation_tag` | [tag/](tag) | Tag: the arena and its referee (who is it, touches, no tag-backs, leaving the arena, the clock). Depends only on `glam` |
| `recreation_pilot` | [pilot/](pilot) | The computer pilot: hands that hold a bank and a climb rate, and steering to a point, along a line or onto another plane. Its [`Skill`](pilot/src/lib.rs) sets how hard it turns and how fast it flies |
| `recreation_towers` | [towers/](towers) | Towers, a team game for drones flown by plan: the towers and their circles, capture, points, the end of the game, the valley (forest, lanes, lake) and what each team sees through the fog and the trees. Depends only on `glam`. See [plans/towers.md](../../plans/towers.md) |
| `recreation_towers_server` | [towers_server/](towers_server) | The game server of Towers (binary `towers_server`): an ArduPlane SITL in AGENT mode for each drone, the flight model (with the trees), the referee, and a seat for each team over TCP |
| `recreation_towers_server` | [towers_server/](towers_server) | The game server of Towers (binary `towers_server`): an ArduPlane SITL in AGENT mode for each drone, the flight model (with the trees), the referee, and a seat for each team over TCP, for a person or a computer commander |
| `recreation_towers_view` | [towers_view/](towers_view) | The 3D view of a game of Towers (binary `towers_view`): the valley, the six drones, and the camera of each drone on the sensor feed of the sim (TCP 5601 to 5606) |
| `recreation_games` | [games/](games) | A game in play: the countdown, the computer pilots (Kestrel the ace, Merlin the club pilot, Swift the rookie) and what they decide to do, their planes on the flight model, and the referee. Also a stand-in that flies your plane, which the acceptance tests use |

Expand Down Expand Up @@ -164,9 +164,19 @@ QHOVER to 15 m, FBWA); once it flies on the wing, the ground switches it to AGEN
plan for its order about once a second (a tower: an orbit 40 m out, at a height of its own) and the
other drones as `ADSB_VEHICLE` traffic. A crashed drone is back at its base after 15 s.

Each team has a seat on TCP port 5955 (`recreation_towers::seat`, a JSON message a line): a
computer commander, or a person (`--blue person`, `--red person`; GO waits for them). A seat that
commands or watches a team gets only that team's map, four times a second; a spectator gets the
Each team has a seat on TCP port 5955 (`recreation_towers::seat`, a JSON message a line), and it is
one seat for a person and for a computer. A seat that commands is the team's ground station: it
gets the umpire's word, what its towers and base report, and the way to its drones (the air), makes
its own map from its drones' telemetry, flies the drones from it, and sends orders. The server decides and sends
no plan for any team. `--blue computer` (the default) makes the server start a computer commander
(`recreation_towers_ground::commander`, also the binary `towers_commander --team blue|red [--host H]
[--port P] [--idle]`) that takes the seat over loopback TCP, as `Join::Compute`; `nobody` starts one
that gives no orders; `person` keeps the seat for a person, and GO waits for them. One person for
each team may play. A seat changes hands during a game: a person who joins a team that a computer
commands takes the seat (the computer is relieved; the person's seat starts with the orders the
drones have, which the server sends once after the welcome), and when a person leaves, a second later
a computer commander takes the seat (`--on-leave computer`, the default; `--on-leave wait` leaves
the drones on their last orders until a person comes back). A seat that watches a team gets only that team's map, four times a second; a spectator gets the
whole game. At the end the server prints `RESULT` lines (the outcome, drones in AGENT, refused
commands, crashes, towers each team took).

Expand Down
22 changes: 21 additions & 1 deletion domains/recreation/towers/src/actors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,43 @@ 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<Vec<Directive>, String>;

/// Gives the command an advisor, whose proposals reach it `delay` s of game time after the map
/// it is shown. Returns whether the command takes advice at all (by default it does not).
fn advised_by(&mut self, _advisor: Box<dyn crate::Advise>, _delay: f32) -> bool {
false
}
}

impl<C: Command + ?Sized> Command for &mut C {
fn decide(&mut self, view: &TeamView) -> Result<Vec<Directive>, String> {
(**self).decide(view)
}

fn advised_by(&mut self, advisor: Box<dyn crate::Advise>, delay: f32) -> bool {
(**self).advised_by(advisor, delay)
}
}

impl<C: Command + ?Sized> Command for Box<C> {
fn decide(&mut self, view: &TeamView) -> Result<Vec<Directive>, String> {
(**self).decide(view)
}

fn advised_by(&mut self, advisor: Box<dyn crate::Advise>, delay: f32) -> bool {
(**self).advised_by(advisor, delay)
}
}

impl Command for crate::Commander {
fn decide(&mut self, view: &TeamView) -> Result<Vec<Directive>, String> {
Ok(crate::Commander::decide(self, view).into_iter().map(|(k, o)| Directive::new(k, o)).collect())
let orders = crate::Commander::decide(self, view);
Ok(orders.into_iter().map(|(k, o)| Directive::with_queue(k, o, self.advised_queue(k))).collect())
}

fn advised_by(&mut self, advisor: Box<dyn crate::Advise>, delay: f32) -> bool {
self.add_advisor(advisor, delay);
true
}
}

Expand Down
128 changes: 128 additions & 0 deletions domains/recreation/towers/src/advice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
//! Advisors that propose moves to a commander (plans/actors.md): the [`Advise`] trait, the
//! [`Proposal`] it makes, and the first advisor, the [`Lookahead`]. Nothing here does I/O: the
//! arena, the game server and a seat in another process all drive the same trait.
//!
//! An advisor sees what a seat gets, the team's map ([`TeamView`]), and what the commander has in
//! hand ([`Hand`]: the orders it means to give, the drones it may still change, the towers a
//! drone must stay away from). It may keep what state it likes, a belief about the enemy for one.
//! It answers with proposals; it does not give orders. The commander decides whether to take one
//! (`Commander::advise`), and an advisor that is slow is only late: its proposals reach the
//! commander some seconds after the map they were made from.

use crate::lookahead::{self, HORIZON};
use crate::{Belief, Order, Queued, Rules, Team, TeamView};

/// A proposal: one order for one drone, with what the advisor expects to gain by it.
#[derive(Clone, Debug, PartialEq)]
pub struct Proposal {
/// The drone, by its number in the team.
pub drone: usize,
pub order: Order,
/// The queue that goes with the order (what the drone does out of contact).
pub queue: Vec<Queued>,
/// The points the advisor expects the order to gain over what the commander means to do.
pub gain: f32,
/// The game time (s) of the map the proposal was made from.
pub made: f32,
/// The game time (s) after which the proposal is stale and is not taken.
pub until: f32,
/// What the advisor says to a person who is shown the proposal.
pub why: String,
}

/// What a commander has in hand when an advisor looks: the part of its own state that an advisor
/// needs and that the map does not show. A seat has it, for the seat is the commander.
#[derive(Clone, Debug, PartialEq)]
pub struct Hand {
/// The order each drone is to fly, by its number in the team.
pub orders: Vec<Option<Order>>,
/// The drones that an order may be given to (in contact, up, not on their way to charge).
pub free: Vec<bool>,
/// For each drone, the towers it must stay away from (it gave them up).
pub away: Vec<Vec<bool>>,
}

/// Looks at the team's map and proposes moves. It may be slow; it is called when the map is shown
/// to it, and what it answers reaches the commander later.
pub trait Advise: std::fmt::Debug {
/// The proposals on this map, if any. Called with each map the advisor is shown (the
/// commander shows it one each second); it decides for itself how often it thinks.
fn advise(&mut self, view: &TeamView, hand: &Hand) -> Vec<Proposal>;

/// A copy of the advisor, with its state (a commander can be cloned).
fn clone_box(&self) -> Box<dyn Advise>;
}

impl Clone for Box<dyn Advise> {
fn clone(&self) -> Box<dyn Advise> {
self.clone_box()
}
}

/// A lookahead thinks ahead this often (s)...
pub const LOOK_EVERY: f32 = 5.0;
/// ...and not before this time in the game (s): at the start the belief is only a prior.
pub const LOOK_AFTER: f32 = 10.0;

/// The first advisor: the lookahead. Every [`LOOK_EVERY`] s it plays the game forward
/// [`HORIZON`] s in a small model (`lookahead`), with the enemy drones guessed from its own
/// [`Belief`], and proposes the one change of orders that gains most. Its proposal is good for
/// the horizon it looked over.
#[derive(Clone, Debug)]
pub struct Lookahead {
rules: Rules,
belief: Belief,
/// The game time of the last look.
last: f32,
/// For measuring: the belief is never updated, so the model knows nothing of the enemy.
blind: bool,
}

impl Lookahead {
pub fn new(rules: Rules) -> Lookahead {
Lookahead { rules, belief: Belief::new(rules, Team::Blue), last: f32::NEG_INFINITY, blind: false }
}

/// A lookahead whose belief is never updated (for measuring what the belief is worth).
pub fn blind(mut self, blind: bool) -> Lookahead {
self.blind = blind;
self
}
}

impl Advise for Lookahead {
fn advise(&mut self, view: &TeamView, hand: &Hand) -> Vec<Proposal> {
if self.belief.team() != view.team {
self.belief = Belief::new(self.rules, view.team);
}
if !self.blind {
self.belief.update(view);
}
if view.time < LOOK_AFTER || view.time - self.last < LOOK_EVERY {
return vec![];
}
self.last = view.time;
let Some((drone, tower, gain)) =
lookahead::best_change(&self.rules, view, &hand.orders, &hand.free, &hand.away, &self.belief)
else {
return vec![];
};
vec![Proposal {
drone,
order: Order::Take(tower),
queue: vec![],
gain,
made: view.time,
until: view.time + HORIZON,
why: format!(
"Send drone {} to the {} tower: about {gain:.0} points over the next {HORIZON:.0} s.",
drone + 1,
view.towers[tower].name
),
}]
}

fn clone_box(&self) -> Box<dyn Advise> {
Box::new(self.clone())
}
}
Loading
Loading