forked from ethz-gtc/npc-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
264 lines (243 loc) · 8.46 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
* SPDX-License-Identifier: Apache-2.0 OR MIT
* © 2020-2022 ETH Zurich and other contributors, see AUTHORS.txt for details
*/
#[allow(unused_imports)]
use std::collections::HashMap;
use std::fs::File;
use std::io::prelude::*;
use std::{collections::HashSet, iter, num::NonZeroU64};
use behavior::world::WORLD_AGENT_ID;
use constants::*;
use domain::EcosystemDomain;
use map::{GridAccess, Map, Tile};
use npc_engine_core::{ActiveTask, ActiveTasks, AgentId, IdleTask, MCTSConfiguration, MCTS};
use npc_engine_utils::{
plot_tree_in_tmp_with_task_name, run_threaded_executor, Coord2D, ExecutorState,
ExecutorStateGlobal,
};
use rand::Rng;
use state::{Agents, Diff, GlobalState, LocalState};
use task::{eat_grass::EatGrass, eat_herbivore::EatHerbivore, world::WorldStep};
use crate::state::{AgentState, AgentType};
mod behavior;
mod constants;
mod domain;
mod map;
mod state;
mod task;
#[derive(Debug)]
struct EcosystemExecutorState {
herbivore_eat_count: u32,
carnivore_eat_count: u32,
mcts_visits: Vec<usize>,
stat_file: Option<File>,
}
impl Default for EcosystemExecutorState {
fn default() -> Self {
Self {
herbivore_eat_count: 0,
carnivore_eat_count: 0,
mcts_visits: Default::default(),
stat_file: File::create("stats.txt").ok(),
}
}
}
impl ExecutorStateGlobal<EcosystemDomain> for EcosystemExecutorState {
const MINIMUM_VISITS: u32 = PLANNING_MINIMUM_VISITS;
fn create_initial_state(&self) -> GlobalState {
let mut map = Map::new(MAP_SIZE, Tile::Grass(0));
// helper for terrain
let mut add_random_and_hotspots =
|random_count, hotspot_count, tile_factory: &dyn Fn() -> Tile| {
for _ in 0..random_count {
let pos = Coord2D::rand_uniform(MAP_SIZE);
*map.at_mut(pos).unwrap() = tile_factory();
}
let mut rng = rand::thread_rng();
for _ in 0..hotspot_count {
let x = rng.gen_range(2..MAP_SIZE.x - 2);
let y = rng.gen_range(2..MAP_SIZE.y - 2);
let pos = Coord2D::new(x, y);
for _ in 0..10 {
let x = rng.gen_range(pos.x - 2..=pos.x + 2);
let y = rng.gen_range(pos.y - 2..=pos.y + 2);
let pos = Coord2D::new(x, y);
*map.at_mut(pos).unwrap() = tile_factory();
}
}
};
// obstacles
add_random_and_hotspots(OBSTACLE_RANDOM_COUNT, OBSTACLE_HOTSPOT_COUNT, &|| {
Tile::Obstacle
});
// plants
add_random_and_hotspots(PLANT_RANDOM_COUNT, PLANT_HOTSPOT_COUNT, &|| {
let mut rng = rand::thread_rng();
Tile::Grass(rng.gen_range(2..=3))
});
// helper for animals
let mut agents = Agents::new();
let mut used_poses = HashSet::new();
let mut agent_id = 0;
let mut add_animals = |ty, count, food| {
for _i in 0..count {
loop {
let pos = Coord2D::rand_uniform(MAP_SIZE);
if !used_poses.contains(&pos) && *map.at(pos).unwrap() != Tile::Obstacle {
used_poses.insert(pos);
agents.insert(
AgentId(agent_id),
AgentState {
ty,
birth_date: 0,
position: pos,
food,
death_date: None,
},
);
agent_id += 1;
break;
}
}
}
};
// animals
add_animals(AgentType::Herbivore, HERBIVORE_COUNT, HERBIVORE_MAX_FOOD);
add_animals(AgentType::Carnivore, CARNIVORE_COUNT, CARNIVORE_MAX_FOOD);
//*map.at_mut(Coord2D::new(0, 1)).unwrap() = Tile::Obstacle;
//*map.at_mut(Coord2D::new(0, 0)).unwrap() = Tile::Grass(3);
// let agents = HashMap::from([
// (
// AgentId(0),
// AgentState {
// ty: AgentType::Herbivore,
// birth_date: 0,
// position: Coord2D::new(0, 0),
// food: 3,
// alive: true
// }
// ),
// (
// AgentId(1),
// AgentState {
// ty: AgentType::Carnivore,
// birth_date: 0,
// position: Coord2D::new(2, 0),
// food: 5,
// alive: true
// }
// )
// ]);
GlobalState::from_map_and_agents(map, agents)
}
fn init_task_queue(&self, state: &GlobalState) -> ActiveTasks<EcosystemDomain> {
state
.agents
.iter()
.map(|(id, _)| ActiveTask::new_with_end(0, *id, Box::new(IdleTask)))
.chain(iter::once(ActiveTask::new_with_end(
10,
WORLD_AGENT_ID,
Box::new(WorldStep),
)))
.collect()
}
fn keep_agent(&self, _tick: u64, state: &GlobalState, agent: AgentId) -> bool {
agent == WORLD_AGENT_ID
|| state
.agents
.get(&agent)
.map_or(false, |agent_state| agent_state.alive())
}
fn keep_execution(
&self,
_tick: u64,
queue: &ActiveTasks<EcosystemDomain>,
_state: &GlobalState,
) -> bool {
queue.len() > 1
}
fn post_step_hook(&mut self, tick: u64, state: &mut GlobalState) {
// garbage collect dead agents
state.garbage_collect_deads(tick, TOMB_DURATION);
// show screen
let agents_count = state.agents_alive_count();
let grass_count = state.map.grass_count();
let mcts_visit_count = self.mcts_visits.len();
let mcts_visit = if mcts_visit_count > 0 {
self.mcts_visits.iter().sum::<usize>() as f32 / mcts_visit_count as f32
} else {
0.
};
self.mcts_visits.clear();
print!(
"\x1B[H\
T{tick} 🌿:{grass_count} 🐄:{} ({}🌿) 🐅:{} ({}🍖) Visits: {:.2}, Next: A{} \n\
{}",
agents_count.0,
self.herbivore_eat_count,
agents_count.1,
self.carnivore_eat_count,
mcts_visit,
state.next_agent_id,
*state
);
// write log
if let Some(file) = &mut self.stat_file {
writeln!(
file,
"{}, {}, {}, {}",
grass_count, agents_count.0, agents_count.1, mcts_visit
)
.expect("Cannot happened stats to file");
}
}
}
impl ExecutorState<EcosystemDomain> for EcosystemExecutorState {
fn post_action_execute_hook(
&mut self,
_state: &LocalState,
_diff: &Diff,
active_task: &ActiveTask<EcosystemDomain>,
_queue: &mut ActiveTasks<EcosystemDomain>,
) {
let task = &active_task.task;
if task.downcast_ref::<EatGrass>().is_some() {
self.herbivore_eat_count += 1;
}
if task.downcast_ref::<EatHerbivore>().is_some() {
self.carnivore_eat_count += 1;
}
}
fn post_mcts_run_hook(
&mut self,
mcts: &MCTS<EcosystemDomain>,
last_active_task: &ActiveTask<EcosystemDomain>,
) {
if let Err(e) = plot_tree_in_tmp_with_task_name(mcts, "ecosystem", last_active_task) {
println!("Cannot write search tree: {e}");
}
self.mcts_visits.push(mcts.node_count());
}
}
fn main() {
// These parameters control the MCTS algorithm.
let mcts_config = MCTSConfiguration {
allow_invalid_tasks: false,
visits: PLANNING_VISITS,
depth: PLANNING_DEPTH,
exploration: PLANNING_EXPLORATION,
discount_hl: PLANNING_DISCOUNT_HL,
seed: None,
planning_task_duration: Some(NonZeroU64::new(PLANNING_DURATION).unwrap()),
};
// Enable logging if specified in the RUST_LOG environment variable.
env_logger::init();
// First clear the screen.
clearscreen::clear().unwrap();
// State of the execution.
let mut executor_state = EcosystemExecutorState::default();
// Run as long as there is at least one agent alive.
run_threaded_executor(&mcts_config, &mut executor_state, EXECUTION_STEP_DURATION);
}