Watch
1
Fork
You've already forked adversarial_foraging
0

hawk dove algorithmic search, added spawn zone conf via deepseek

This commit is contained in:
Andy Killorin 2026-11-11 18:48:37 -05:00
commit e34c92db91
Signed by: ank
GPG key ID: 80BA307A6BD7A7E4

View file

@ -407,6 +407,28 @@ struct Weapon {
model: WeaponType,
}
/// An axis-aligned spawn region. When a team (or the food) is given one, its
/// members are scattered uniformly inside it instead of the legacy fixed
/// columns. This is the lever that lets an experiment control who starts next
/// to whom, which in turn controls how much of the weapon's blast is spent on
/// opponents versus on kin and food.
#[derive(Deserialize, Clone, Copy)]
struct SpawnRect {
x0: f32,
x1: f32,
y0: f32,
y1: f32,
}
impl SpawnRect {
fn sample(&self, rng: &mut SmallRng) -> Vec2 {
Vec2 {
x: rng.random_range(self.x0..self.x1),
y: rng.random_range(self.y0..self.y1),
}
}
}
#[derive(Deserialize)]
struct SimConfig {
food_amount: usize,
@ -416,6 +438,14 @@ struct SimConfig {
blu_count: usize,
blu_policy: Policy,
weapon: Weapon,
/// Optional spawn regions. Omitted fields reproduce the original fixed
/// columns (`blu` at x=100, `red` at x=1200, both on a vertical grid).
#[serde(default)]
blu_spawn: Option<SpawnRect>,
#[serde(default)]
red_spawn: Option<SpawnRect>,
#[serde(default)]
food_spawn: Option<SpawnRect>,
}
struct Simulation {
@ -448,22 +478,35 @@ impl Simulation {
// blue robots
for i in 0..config.blu_count {
let rand_angle = angle.sample(&mut rng);
let pose:Pose = Pose::from_parts(Vec2 { x: 100.0, y: i as f32*50.0 + 250.0}, Rot2::from_angle(rand_angle));
let pos = match config.blu_spawn {
Some(r) => r.sample(&mut rng),
None => Vec2 { x: 100.0, y: i as f32 * 50.0 + 250.0 },
};
let pose: Pose = Pose::from_parts(pos, Rot2::from_angle(rand_angle));
robots.push(Robot::new(pose, BLUE, config.blu_policy.clone(), config.weapon.clone(), config.view_dist));
}
//red robots
for i in 0..config.red_count {
let rand_angle = angle.sample(&mut rng);
let pose:Pose = Pose::from_parts(Vec2 { x: 1200.0, y: i as f32*50.0 + 250.0}, Rot2::from_angle(rand_angle));
let pos = match config.red_spawn {
Some(r) => r.sample(&mut rng),
None => Vec2 { x: 1200.0, y: i as f32 * 50.0 + 250.0 },
};
let pose: Pose = Pose::from_parts(pos, Rot2::from_angle(rand_angle));
robots.push(Robot::new(pose, RED, config.red_policy.clone(), config.weapon.clone(), config.view_dist));
}
let mut food = Vec::new();
for _ in 0..config.food_amount {
let x = rng.random_range(200.0 .. 1100.0);
let y = rng.random_range(200.0 .. 900.0);
let (x, y) = match config.food_spawn {
Some(r) => {
let p = r.sample(&mut rng);
(p.x, p.y)
}
None => (rng.random_range(200.0 .. 1100.0), rng.random_range(200.0 .. 900.0)),
};
food.push(Food::new(Vec2 { x, y }));
}
let cell = grid_cell(config.view_dist);
@ -984,6 +1027,74 @@ mod tests {
toml::from_str(include_str!("../equal.toml")).unwrap()
}
/// Spawn regions are honoured: every robot/food lands inside its declared
/// rectangle, and omitting the regions keeps the legacy fixed columns.
#[test]
fn spawn_rects_are_respected() {
let text = r#"
food_amount = 5
view_dist = 200
red_count = 3
blu_count = 4
[weapon]
kills_user = false
type = "ball"
radius = 10
[red_spawn]
x0 = 500
x1 = 600
y0 = 400
y1 = 450
[blu_spawn]
x0 = 100
x1 = 200
y0 = 300
y1 = 350
[food_spawn]
x0 = 700
x1 = 800
y0 = 600
y1 = 650
[red_policy]
aggressive = true
food = [1,0]
kin = [-0.3,0.02]
nonkin = [0.3,0.02]
none = [0.3,0.01]
[blu_policy]
aggressive = false
food = [1,0]
kin = [-0.3,0.02]
nonkin = [-0.3,-0.02]
none = [0.3,0.01]
"#;
let config: SimConfig = toml::from_str(text).unwrap();
let sim = Simulation::new(7, &config);
let in_rect = |p: Vec2, r: SpawnRect| {
p.x >= r.x0 && p.x <= r.x1 && p.y >= r.y0 && p.y <= r.y1
};
let red = config.red_spawn.unwrap();
let blu = config.blu_spawn.unwrap();
let food = config.food_spawn.unwrap();
for r in sim.robots.iter().filter(|r| r.team == RED) {
assert!(in_rect(r.pose.translation, red), "red out of region: {:?}", r.pose.translation);
}
for r in sim.robots.iter().filter(|r| r.team == BLUE) {
assert!(in_rect(r.pose.translation, blu), "blue out of region: {:?}", r.pose.translation);
}
for f in sim.food.iter() {
assert!(in_rect(f.pose.translation, food), "food out of region: {:?}", f.pose.translation);
}
// Without spawn regions the legacy columns are still used.
let legacy: SimConfig = toml::from_str(include_str!("../conf.toml")).unwrap();
let sim = Simulation::new(0, &legacy);
assert!(sim.robots.iter().filter(|r| r.team == BLUE).all(|r| r.pose.translation.x == 100.0));
assert!(sim.robots.iter().filter(|r| r.team == RED).all(|r| r.pose.translation.x == 1200.0));
}
/// The analytic replacements must agree with parry bit-for-bit, both on
/// whether a contact exists and on the reported `point1.distance(origin)`.
/// This is the load-bearing test behind the whole speedup: the hot path no