Compare commits
No commits in common. "main" and "6294d721909f47403c26bc955ce4f9e576a69db0" have entirely different histories.
main
...
6294d72190
8 changed files with 90 additions and 216 deletions
|
|
@ -7,10 +7,3 @@ authors = ["Adrian Heine <mail@adrianheine.de>"]
|
|||
[dependencies]
|
||||
tui = "0.15"
|
||||
termion = "1.5"
|
||||
|
||||
[dev-dependencies]
|
||||
criterion = "0.3"
|
||||
|
||||
[[bench]]
|
||||
name = "benchmark"
|
||||
harness = false
|
||||
|
|
|
|||
37
README.md
37
README.md
|
|
@ -1,37 +0,0 @@
|
|||
# gntag
|
||||
|
||||
gntag (from German »Guten Tag«: »good day«, roughly equivalent to »g'day«) is an agent-based simulation of [tag](https://en.wikipedia.org/wiki/Tag_(game)) implemented in rust.
|
||||
|
||||
## Running
|
||||
|
||||
```sh
|
||||
git clone https://git.adrianheine.de/adrian/gntag.git
|
||||
cd gntag
|
||||
cargo run
|
||||
```
|
||||
|
||||
To exit the simulation, press q, ESC or Ctrl+c.
|
||||
|
||||
## Using as a library
|
||||
|
||||
`src/main.rs` is a pretty minimal example of the currently exposed high-level functionality. `gntag::agent` includes simpler agent iplementations and all the types, `gntag::world` contains `ActualWorld` and `WorldState`.
|
||||
|
||||
## Tests
|
||||
|
||||
A few tests can be run with:
|
||||
|
||||
```sh
|
||||
cargo test
|
||||
```
|
||||
|
||||
## Benchmarks
|
||||
|
||||
`cargo bench` runs benchmarks. Their results are available under `target/criterion/report/index.html`.
|
||||
|
||||
## Simulation
|
||||
|
||||
* The world is a simple two-dimensional rectangle with integer positions
|
||||
* Agents can occupy the same space
|
||||
* Agents can make one move per simulation step, either by trying to move by at most one unit in both directions or by trying to tag another agent
|
||||
* Agents need to be at most one unit away in both directions from others in order to tag them
|
||||
* Agents act at the same time and their actions are validated against the previous state
|
||||
|
|
@ -1,39 +0,0 @@
|
|||
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
|
||||
use gntag::get_world;
|
||||
|
||||
fn world(c: &mut Criterion) {
|
||||
let mut group = c.benchmark_group("world");
|
||||
let width: isize = 1000;
|
||||
for spacing in (50..=100).step_by(25) {
|
||||
group.throughput(Throughput::Elements((width / spacing).pow(2) as u64));
|
||||
group.bench_with_input(
|
||||
BenchmarkId::new("validating", spacing),
|
||||
&spacing,
|
||||
|b, &spacing| {
|
||||
let mut world = get_world(width, width, spacing as usize, true);
|
||||
b.iter(|| world.do_step());
|
||||
},
|
||||
);
|
||||
group.bench_with_input(
|
||||
BenchmarkId::new("non-validating", spacing),
|
||||
&spacing,
|
||||
|b, &spacing| {
|
||||
let mut world = get_world(width, width, spacing as usize, false);
|
||||
b.iter(|| world.do_step());
|
||||
},
|
||||
);
|
||||
}
|
||||
group.finish();
|
||||
}
|
||||
|
||||
criterion_group!(benches, world);
|
||||
criterion_main!(benches);
|
||||
|
||||
// 5000/1000 -> 9,993
|
||||
// 5/01 -> 23,252
|
||||
// 50/10 -> 14,000
|
||||
// 100/20 -> 14,000
|
||||
// 100/10 -> 96,000
|
||||
// 100/05 -> 917,767
|
||||
// 200/10 -> 916,172
|
||||
// 1000/10 -> 443,482,526
|
||||
|
|
@ -1,6 +1,5 @@
|
|||
pub type Distance = isize;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Position {
|
||||
pub x: Distance,
|
||||
pub y: Distance,
|
||||
|
|
@ -26,12 +25,12 @@ impl From<(Distance, Distance)> for Direction {
|
|||
|
||||
pub type AgentId = usize;
|
||||
|
||||
pub struct WorldView<'o> {
|
||||
pub struct WorldView {
|
||||
pub self_id: AgentId,
|
||||
pub tagged: AgentId,
|
||||
pub tagged_by: Option<AgentId>,
|
||||
pub bounds_distance: (Distance, Distance, Distance, Distance),
|
||||
pub other_agents: &'o mut dyn Iterator<Item = (Direction, AgentId)>,
|
||||
pub other_agents: Vec<(Direction, AgentId)>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
@ -110,7 +109,7 @@ fn random_move_within(
|
|||
loop {
|
||||
let mv = random_move();
|
||||
if let Move::TryMove(Direction { x, y }) = mv {
|
||||
if top + y >= 0 && bottom - y >= 0 && left + x >= 0 && right - x >= 0 {
|
||||
if top + y > 0 && bottom - y > 0 && left + x > 0 && right - x > 0 {
|
||||
return mv;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
64
src/lib.rs
64
src/lib.rs
|
|
@ -1,67 +1,3 @@
|
|||
pub mod agent;
|
||||
pub mod view;
|
||||
pub mod world;
|
||||
|
||||
use agent::{Agent, SimpleAgent};
|
||||
use std::error::Error;
|
||||
use std::io;
|
||||
use std::io::Read;
|
||||
use std::process::exit;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread;
|
||||
use view::{RawTerminal, TerminalView, TermionBackend};
|
||||
use world::ActualWorld;
|
||||
|
||||
pub fn get_world(width: isize, height: isize, spacing: usize, validating: bool) -> ActualWorld {
|
||||
let mut agents: Vec<(_, Box<dyn Agent>)> = vec![];
|
||||
for x in (0..width).step_by(spacing) {
|
||||
for y in (0..height).step_by(spacing) {
|
||||
agents.push(((x, y).into(), Box::new(SimpleAgent)));
|
||||
}
|
||||
}
|
||||
ActualWorld::new((width, height).into(), agents, validating)
|
||||
}
|
||||
|
||||
pub type DefaultView = Arc<Mutex<Option<TerminalView<TermionBackend<RawTerminal<io::Stdout>>>>>>;
|
||||
pub fn get_view() -> DefaultView {
|
||||
let view = Arc::new(Mutex::new(Some(TerminalView::try_new().unwrap())));
|
||||
|
||||
// Exit on q, ESC and Ctrl-C and reset terminal
|
||||
let view2 = view.clone();
|
||||
thread::spawn(move || {
|
||||
let stdin = io::stdin();
|
||||
for byte in stdin.bytes().flatten() {
|
||||
if byte == b'q' || byte == 0x1b || byte == 0x03 {
|
||||
if let Ok(mut view) = view2.lock() {
|
||||
*view = None; // drop view
|
||||
println!("\n");
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
});
|
||||
view
|
||||
}
|
||||
|
||||
pub fn draw_world(
|
||||
world: &ActualWorld,
|
||||
gen: usize,
|
||||
view: &DefaultView,
|
||||
) -> Result<bool, Box<dyn Error>> {
|
||||
(*view.lock().unwrap()).as_mut().unwrap().draw(
|
||||
gen,
|
||||
world
|
||||
.state
|
||||
.agent_positions
|
||||
.get(&world.state.tagged)
|
||||
.map(|pos| (pos.x, pos.y))
|
||||
.unwrap(),
|
||||
world
|
||||
.state
|
||||
.agent_positions
|
||||
.iter()
|
||||
.map(|(_id, pos)| (pos.x, pos.y))
|
||||
.collect::<Vec<_>>()
|
||||
.as_ref(),
|
||||
)
|
||||
}
|
||||
|
|
|
|||
87
src/main.rs
87
src/main.rs
|
|
@ -1,28 +1,69 @@
|
|||
use gntag::{draw_world, get_view, get_world, DefaultView};
|
||||
use gntag::agent::{Agent, SimpleAgent};
|
||||
use gntag::view::TerminalView;
|
||||
use gntag::world::ActualWorld;
|
||||
use std::io;
|
||||
use std::io::Read;
|
||||
use std::process::exit;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::thread;
|
||||
|
||||
fn main() {
|
||||
let view = get_view();
|
||||
|
||||
let view = Arc::new(Mutex::new(Some(TerminalView::try_new().unwrap())));
|
||||
let view2 = view.clone();
|
||||
thread::spawn(move || {
|
||||
let stdin = io::stdin();
|
||||
for byte in stdin.bytes().flatten() {
|
||||
if byte == b'q' || byte == 0x1b || byte == 0x03 {
|
||||
if let Ok(mut view) = view2.lock() {
|
||||
*view = None; // drop view
|
||||
println!("\n");
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
});
|
||||
loop {
|
||||
run_simulation(&view);
|
||||
}
|
||||
}
|
||||
|
||||
fn run_simulation(view: &DefaultView) {
|
||||
let (width, height) = (*view.lock().unwrap())
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.content_size()
|
||||
.unwrap();
|
||||
let mut world = get_world(width, height, 10, true);
|
||||
|
||||
let mut gen = 0;
|
||||
loop {
|
||||
let resized = draw_world(&world, gen, view).unwrap();
|
||||
if resized {
|
||||
return;
|
||||
};
|
||||
world.do_step();
|
||||
gen += 1;
|
||||
let (width, height) = (*view.lock().unwrap())
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.content_size()
|
||||
.unwrap();
|
||||
let mut agents: Vec<(_, Box<dyn Agent>)> = vec![];
|
||||
for x in (0..width).step_by(10) {
|
||||
for y in (0..height).step_by(10) {
|
||||
agents.push(((x, y).into(), Box::new(SimpleAgent)));
|
||||
}
|
||||
}
|
||||
let mut world = ActualWorld::new((width, height).into(), agents);
|
||||
|
||||
let mut gen = 0;
|
||||
loop {
|
||||
let resized = (*view.lock().unwrap())
|
||||
.as_mut()
|
||||
.unwrap()
|
||||
.draw(
|
||||
gen,
|
||||
world
|
||||
.state
|
||||
.agent_positions
|
||||
.get(&world.state.tagged)
|
||||
.map(|pos| (pos.x, pos.y))
|
||||
.unwrap(),
|
||||
world
|
||||
.state
|
||||
.agent_positions
|
||||
.iter()
|
||||
.map(|(_id, pos)| (pos.x, pos.y))
|
||||
.collect::<Vec<_>>()
|
||||
.as_ref(),
|
||||
)
|
||||
.unwrap();
|
||||
if resized {
|
||||
break;
|
||||
};
|
||||
world.do_step();
|
||||
gen += 1;
|
||||
//std::thread::sleep(std::time::Duration::from_millis(500));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,10 +2,8 @@ use std::convert::TryInto;
|
|||
use std::error::Error;
|
||||
use std::io::{stdout, Stdout, Write};
|
||||
use termion::clear;
|
||||
use termion::raw::IntoRawMode;
|
||||
pub use termion::raw::RawTerminal;
|
||||
use tui::backend::Backend;
|
||||
pub use tui::backend::TermionBackend;
|
||||
use termion::raw::{IntoRawMode, RawTerminal};
|
||||
use tui::backend::{Backend, TermionBackend};
|
||||
use tui::layout::{Alignment, Constraint, Direction, Layout, Rect};
|
||||
use tui::style::{Color, Modifier, Style};
|
||||
use tui::text::{Span, Spans};
|
||||
|
|
|
|||
59
src/world.rs
59
src/world.rs
|
|
@ -5,7 +5,6 @@ pub struct ActualWorld {
|
|||
size: Position,
|
||||
pub agents: HashMap<AgentId, Box<dyn Agent>>,
|
||||
pub state: WorldState,
|
||||
validating: bool,
|
||||
}
|
||||
|
||||
pub struct WorldState {
|
||||
|
|
@ -17,11 +16,7 @@ pub struct WorldState {
|
|||
impl ActualWorld {
|
||||
/// Agents receive incrementing ids starting with 0
|
||||
/// The last agent is 'It'
|
||||
pub fn new(
|
||||
size: Position,
|
||||
input_agents: Vec<(Position, Box<dyn Agent>)>,
|
||||
validating: bool,
|
||||
) -> Self {
|
||||
pub fn new(size: Position, input_agents: Vec<(Position, Box<dyn Agent>)>) -> Self {
|
||||
let agent_count = input_agents.len();
|
||||
assert!(agent_count > 0);
|
||||
let mut id = 0;
|
||||
|
|
@ -41,7 +36,6 @@ impl ActualWorld {
|
|||
size,
|
||||
agents,
|
||||
state,
|
||||
validating,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -57,13 +51,8 @@ impl ActualWorld {
|
|||
self_id: *id,
|
||||
tagged_by: self.state.tagged_by,
|
||||
tagged: self.state.tagged,
|
||||
bounds_distance: (
|
||||
pos.y,
|
||||
self.size.x - pos.x - 1,
|
||||
self.size.y - pos.y - 1,
|
||||
pos.x,
|
||||
),
|
||||
other_agents: &mut self
|
||||
bounds_distance: (pos.y, self.size.x - pos.x, self.size.y - pos.y, pos.x),
|
||||
other_agents: self
|
||||
.state
|
||||
.agent_positions
|
||||
.iter()
|
||||
|
|
@ -76,7 +65,8 @@ impl ActualWorld {
|
|||
},
|
||||
*id,
|
||||
)
|
||||
}),
|
||||
})
|
||||
.collect(),
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
|
@ -87,11 +77,9 @@ impl ActualWorld {
|
|||
tagged_by: self.state.tagged_by,
|
||||
};
|
||||
for (id, mv) in moves {
|
||||
if self.validating {
|
||||
self.check_move(id, &mv);
|
||||
}
|
||||
self.check_move(id, &mv);
|
||||
//println!("{} {:?}", id, mv);
|
||||
let mut new_pos = None;
|
||||
let pos = self.state.agent_positions.get(&id).unwrap();
|
||||
match mv {
|
||||
Move::Noop => {}
|
||||
Move::TryTag(other_id) => {
|
||||
|
|
@ -99,12 +87,14 @@ impl ActualWorld {
|
|||
new_state.tagged_by = Some(id);
|
||||
}
|
||||
Move::TryMove(dir) => {
|
||||
let pos = self.state.agent_positions.get(&id).unwrap();
|
||||
new_pos = Some((pos.x + dir.x, pos.y + dir.y).into());
|
||||
}
|
||||
}
|
||||
new_state
|
||||
.agent_positions
|
||||
.insert(id, new_pos.unwrap_or_else(|| pos.clone()));
|
||||
new_state.agent_positions.insert(
|
||||
id,
|
||||
new_pos.unwrap_or_else(|| self.state.agent_positions.remove(&id).unwrap()),
|
||||
);
|
||||
}
|
||||
self.state = new_state;
|
||||
}
|
||||
|
|
@ -112,10 +102,7 @@ impl ActualWorld {
|
|||
match mv {
|
||||
Move::Noop => {}
|
||||
Move::TryTag(other_id) => {
|
||||
let my_pos = self.state.agent_positions.get(&id).unwrap();
|
||||
let other_pos = self.state.agent_positions.get(&other_id).unwrap();
|
||||
assert!((my_pos.x - other_pos.x).abs() <= 1);
|
||||
assert!((my_pos.y - other_pos.y).abs() <= 1);
|
||||
// FIXME check distance
|
||||
assert_eq!(self.state.tagged, id);
|
||||
assert_ne!(self.state.tagged_by, Some(*other_id));
|
||||
assert!(
|
||||
|
|
@ -124,13 +111,12 @@ impl ActualWorld {
|
|||
);
|
||||
}
|
||||
Move::TryMove(dir) => {
|
||||
assert!(dir.x.abs() <= 1);
|
||||
assert!(dir.y.abs() <= 1);
|
||||
// FIXME check speed
|
||||
let pos = self.state.agent_positions.get(&id).unwrap();
|
||||
let size = &self.size;
|
||||
assert!(pos.x + dir.x >= 0);
|
||||
assert!(pos.x + dir.x > 0);
|
||||
assert!(pos.x + dir.x < size.x);
|
||||
assert!(pos.y + dir.y >= 0);
|
||||
assert!(pos.y + dir.y > 0);
|
||||
assert!(pos.y + dir.y < size.y);
|
||||
}
|
||||
}
|
||||
|
|
@ -145,7 +131,7 @@ mod test {
|
|||
#[test]
|
||||
#[should_panic]
|
||||
fn empty() {
|
||||
ActualWorld::new(Position { x: 0, y: 0 }, vec![], true);
|
||||
ActualWorld::new(Position { x: 0, y: 0 }, vec![]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -153,11 +139,10 @@ mod test {
|
|||
let mut world = ActualWorld::new(
|
||||
Position { x: 0, y: 0 },
|
||||
vec![(Position { x: 0, y: 0 }, Box::new(NullAgent))],
|
||||
true,
|
||||
);
|
||||
world.do_step();
|
||||
assert_eq!(world.state.tagged, 0);
|
||||
assert_eq!(world.state.tagged_by, None);
|
||||
assert_eq!(world.tagged, 0);
|
||||
assert_eq!(world.tagged_by, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -174,11 +159,10 @@ mod test {
|
|||
Box::new(ScriptedAgent::new(vec![Move::TryTag(0)])),
|
||||
),
|
||||
],
|
||||
true,
|
||||
);
|
||||
world.do_step();
|
||||
assert_eq!(world.state.tagged, 0);
|
||||
assert_eq!(world.state.tagged_by, Some(1));
|
||||
assert_eq!(world.tagged, 0);
|
||||
assert_eq!(world.tagged_by, Some(1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -190,7 +174,6 @@ mod test {
|
|||
Position { x: 0, y: 0 },
|
||||
Box::new(ScriptedAgent::new(vec![Move::TryMove((-1, -1).into())])),
|
||||
)],
|
||||
true,
|
||||
);
|
||||
world.do_step();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue