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: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions domains/recreation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,42 @@ the drones on their last orders until a person comes back). A seat that watches
whole game. At the end the server prints `RESULT` lines (the outcome, drones in AGENT, refused
commands, crashes, towers each team took).

#### Advisors beside a commander

A commander has an advisor that looks at the team's map in its own time and proposes moves
(`recreation_towers::Advise`: a `Proposal` is a drone, an order and queue, the points it expects
to gain and the game time until which it is good). At a seat, the advisors do not run in the
commander's loop: each runs on a thread of its own at the station behind a mailbox
(`recreation_towers_ground::advisors::Runner`: the newest map and hand go in, older ones that were
not looked at are dropped, proposals come out when they are ready). The computer commander puts
what has arrived in hand with `Commander::advise` before each decision, takes the best proposal
that gains more than 2 points, is not stale and still fits, and then shows the advisors the map
and its hand. It never waits. Which advisor each computer team has:

```bash
towers_server --blue-advisor lookahead --red-advisor none # the default is lookahead on both
towers_server --blue-advisor "towers_advisor --claude" # asks claude -p every 8 s; costs money
towers_commander --team blue --advisor "towers_advisor" # the same flag on the stand-alone commander
```

`lookahead` is the lookahead on a thread of the station, `none` no advisor, and anything else a
command (run with `sh -c`; `towers_advisor` is found beside the other binaries) that serves an
advisor: one JSON line in for each map, `{"Look": {"view": TeamView, "hand": Hand}}`, one line out,
a list of proposals (`[]` for none), after one `{"Start": {"team": "Blue", "rules": {...}}}` that is
answered `"ok"` (`recreation_towers_ground::advisors::{ToAdvisor, serve}`; any `Advise` can be such a
process). A process that dies, hangs (90 s), writes what is not a list of proposals or proposes for a
drone or tower that is not in the game is dropped with a line on stderr, and the game goes on. The
binary `towers_advisor` serves the lookahead; `towers_advisor --claude [--model NAME] [--every S]`
every few seconds gives `claude -p` (JSON output, a budget of 0.25 dollars a question, no tools, the
command from `SIM001_CLAUDE` if set) a text picture of the map and the hand, asks for at most two
proposals as JSON, reads them strictly and stamps `until` itself. The server's log shows each
proposal that arrives and each that a commander takes (`advice Blue: ...`), and the final `RESULT`
lines count them (`proposals`, `proposals_taken`).

A person's seat runs advisors too (`advisors::attend`, in the commander app, `--advisor` as above,
lookahead by default): the app's ADVISOR panel lists the live proposals, best first, and a click on
one, or the key Y for the first, sends that order and queue for that drone.

The rules and the commander are specified in [expectations/09-towers](../../expectations/09-towers)
and run with no window by `cargo test --release -p recreation_towers`; a whole game with
ArduPilot, with the fog checked from a seat against a spectator, by
Expand Down
7 changes: 4 additions & 3 deletions domains/recreation/towers/src/advice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@

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

/// A proposal: one order for one drone, with what the advisor expects to gain by it.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Proposal {
/// The drone, by its number in the team.
pub drone: usize,
Expand All @@ -32,7 +33,7 @@ pub struct Proposal {

/// 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)]
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Hand {
/// The order each drone is to fly, by its number in the team.
pub orders: Vec<Option<Order>>,
Expand All @@ -44,7 +45,7 @@ pub struct Hand {

/// 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 {
pub trait Advise: std::fmt::Debug + Send {
/// 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>;
Expand Down
26 changes: 25 additions & 1 deletion domains/recreation/towers/src/commander.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ pub struct Commander {
advice_margin: f32,
/// The queue that goes with the order a proposal gave a drone, while it still flies it.
queues: Vec<Option<(Order, Vec<Queued>)>>,
/// What the commander had in hand at its last decision (for advisors beside it).
hand: Option<Hand>,
}

impl Commander {
Expand All @@ -127,6 +129,7 @@ impl Commander {
proposals: vec![],
advice_margin: ADVICE_MARGIN,
queues: vec![None; n],
hand: None,
}
.with_lookahead(std::env::var("SIM001_LOOKAHEAD").as_deref() != Ok("0"))
}
Expand Down Expand Up @@ -183,6 +186,14 @@ impl Commander {
}
}

/// What the commander had in hand at its last decision: its orders after its own rules, the
/// drones it may still change and the towers each must stay away from. It is what an advisor
/// that runs beside the commander (at a seat) is shown with the map. `None` before the first
/// decision.
pub fn hand(&self) -> Option<&Hand> {
self.hand.as_ref()
}

pub fn tuning(&self) -> CommanderTuning {
self.tuning
}
Expand Down Expand Up @@ -356,8 +367,9 @@ impl Commander {
.min_by(|&&a, &&b| view.towers[a].at.distance(at).total_cmp(&view.towers[b].at.distance(at)));
*order = spare.map(|&t| Order::Take(t));
}
let free: Vec<bool> = (0..self.orders.len()).map(|k| up[k] && charge[k].is_none()).collect();
self.hand = Some(Hand { orders: self.orders.clone(), free: free.clone(), away: away.clone() });
if !self.advisors.is_empty() || !self.late.is_empty() || !self.proposals.is_empty() {
let free: Vec<bool> = (0..self.orders.len()).map(|k| up[k] && charge[k].is_none()).collect();
self.take_advice(view, free, away);
}
for (k, order) in charge.into_iter().enumerate() {
Expand Down Expand Up @@ -435,6 +447,18 @@ mod tests {
(0..3).map(|k| c.order(k)).collect()
}

#[test]
fn after_a_decision_it_has_a_hand_for_the_advisors_beside_it() {
let g = Game::new(Rules { drones: 3, ..Rules::default() });
let mut c = Commander::new(g.rules).with_lookahead(false);
assert!(c.hand().is_none(), "no hand before the first decision");
c.decide(&g.view(Team::Blue));
let hand = c.hand().expect("a hand");
assert_eq!(hand.orders, (0..3).map(|k| c.order(k)).collect::<Vec<_>>());
assert_eq!(hand.free.len(), 3);
assert_eq!(hand.away.len(), 3);
}

#[test]
fn a_tuning_goes_to_numbers_and_back_and_refuses_what_is_out_of_bounds() {
let d = CommanderTuning::DEFAULT;
Expand Down
6 changes: 6 additions & 0 deletions domains/recreation/towers_ground/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ version = "0.1.0"
edition = "2024"
description = "Recreation domain: the ground of Towers: the MAVLink side of a team's drones (AGENT mode, plans, standing orders, traffic) behind a Transport, as the commander app runs it and as the game server runs it for a computer team"

[[bin]]
name = "towers_advisor"
path = "src/bin/towers_advisor.rs"

[[bin]]
name = "towers_commander"
path = "src/bin/towers_commander.rs"
Expand All @@ -14,3 +18,5 @@ simulation_sitl = { path = "../../simulation/sitl" }
cognition_agent = { path = "../../cognition/agent" }
cognition_plan = { path = "../../cognition/plan" }
glam.workspace = true
serde.workspace = true
serde_json.workspace = true
Loading
Loading