cppyabm  1.0.17
An agent-based library to integrate C++ and Python
bases.h
Go to the documentation of this file.
1 
2 #pragma once
3 #include <map>
4 #include <algorithm>
5 #include <random>
6 #include <set>
7 #include "common.h"
8 #include "mesh.h"
9 #include "tools.h"
10 using std::shared_ptr;
11 using std::vector;
12 // #define MEMORY_MONITOR
13 #ifdef MEMORY_MONITOR
14 #include<mach/mach.h>
15 #endif
16 template<class ENV, class AGENT, class PATCH>
17 struct Env;
18 template<class ENV, class AGENT, class PATCH>
19 struct Agent;
20 template<class ENV, class AGENT, class PATCH>
21 struct Patch;
22 
23 //! Base class for patch
24 /*!
25  Patch class abstracts non-movable discrete elements of the simulation domain.
26 */
27 template<class ENV, class AGENT, class PATCH>
28 struct Patch: public enable_shared_from_this<PATCH>{
29  Patch(shared_ptr<ENV> env,MESH_ITEM mesh_item){
30  this->env = env;
31  this->index = mesh_item.index; // copy index
32  this->coords = mesh_item.coords; // copy coords
33  this->neighbors_indices = mesh_item.neighbors_indices; // copy neighbors indices
34  try {
35  this->layer_index = mesh_item.layer_index; // copy layer index
36 
37  }
38  catch (...){
39 
40  }
41  try {
42  this->on_border = mesh_item.on_border;
43  }
44  catch (...){
45 
46  }
47  }
48  virtual ~Patch(){}
49 
50  virtual void step(){
51  throw undefined_member("Step function is not defined inside Patch");
52  }; //!< To define patch behavior
53  void set_agent(shared_ptr<AGENT> agent){
54  this->agent = agent;
55  // this->empty = false;
56  this->agent_count ++;
57  agent->patch = this->shared_from_this();
58  agent->has_patch = true;
59  }; //!< Assigns agent to the patch
60  shared_ptr<AGENT> get_agent(){
61  shared_ptr<AGENT> p = this->agent.lock();
62  if (p){
63  return p;
64  }else{
65  throw invalid_pointer("poiner to agent is invalid inside patch");
66 
67  }
68  }
69  bool empty(){
70  shared_ptr<AGENT> p = this->agent.lock();
71  if (p){
72  return false;
73  }else{
74  return true;
75 
76  }
77  }
78  void remove_agent(){
79  this->get_agent()->has_patch = false;
80  this->get_agent()->patch = nullptr;
81  this->agent.reset();
82  this->agent_count = 0;
83  } //!< Removes agent from the patch
84  shared_ptr<PATCH> empty_neighbor(bool quiet = false); //!< Returns an arbitrary adjacent patch without an agent
85  vector<shared_ptr<AGENT>> find_neighbor_agents(bool include_self = true); //!< Returns agents in one patch neighbors
86  unsigned agent_count = 0; //!< Keeps the record of residing agents count
87  std::weak_ptr<AGENT> agent; //!< Pointer to stores agents
88  std::shared_ptr<ENV> env; //!< Pointer to the simulation environment
89  // bool empty = true; //!< whether the patch host an agent
90  bool on_border = false;//!< whether the patch residing on the border of the domain
91  unsigned index; //!< unique index that associate patch to mesh
92  unsigned layer_index; //!< the index of the layer in which the patch is residing
93  vector<double> coords; //!< the coordinates of the patch
94  vector<unsigned> neighbors_indices; //!< the indices of the neighbor patches
95  vector<shared_ptr<PATCH>> neighbors; //!< list of neighbor patches
96 };
97 
98 //! Base class for Agent
99 /*!
100  Agent class simulates movable objects
101 */
102 template<class ENV, class AGENT, class PATCH>
103 struct Agent: public enable_shared_from_this<AGENT>{
104  Agent(shared_ptr<ENV> env , string class_name){
105  this->env = env;
106  this->class_name = class_name;
107  }
108  virtual ~Agent(){};
109 
110  virtual void step(){
111  throw undefined_member("Agent step function is not defined");
112  }; //!< To define agent behavior
113  virtual void inherit(shared_ptr<AGENT> father){
114  cout<<"Inherit is not defined"<<endl;
115  };//!< To define inheritage
116  virtual void update(){};
117  void move(shared_ptr<PATCH> dest, bool quiet = false); //!< Moves the agent to the given destination patch
118  void order_hatch(shared_ptr<PATCH> patch = nullptr,
119  bool inherit = false, bool quiet = false, bool reset = false); //!< Orders agent to hatch. This will execute during `Env::update`.
120  void order_move(shared_ptr<PATCH> patch = nullptr,
121  bool quiet = false, bool reset = false); //!< Orders agent to move. This will execute during `Env::update`.
122  void order_switch(string to){
123  this->_switch = SWITCH_CONFIG(true,to);
124  }; //!< Orders agent to switch to another agent. This will execute during `Env::update`.
125 
128  void reset_switch(){ this->_switch = SWITCH_CONFIG();}
129  std::pair <bool,std::string> switch_info = std::make_pair(false,"");
133  std::shared_ptr<PATCH> patch; //!< Pointer to the residing patch
134  std::shared_ptr<ENV> env; //!< Pointer to the simulation world
135  bool disappear = false; //!< if set to true, the agent will be removed from the simulation. This will execute during `Env::update`
136  string class_name; //!< Unique ID of this class
137  bool has_patch = false; //!< Indicates if an agent residing on a patch
138 };
139 
140 //! Base class for environment
141 /*!
142 Env class stores and coordinates agents and patches.
143 */
144 template<class ENV, class AGENT, class PATCH>
145 struct Env: public enable_shared_from_this<ENV>{
146  Env(){};
147  virtual ~Env(){};
148  virtual shared_ptr<PATCH> generate_patch(MESH_ITEM) {
149  throw undefined_member("Generate patch is not defined inside Env");
150  }; //!< A template class to generate patch.
151  virtual shared_ptr<AGENT> generate_agent(string class_name) {
152  throw undefined_member("Generate agent is not defined inside Env");
153  }; //!< A template class to generate agent.
154  virtual void update_repo(){} //!< To remove the dead agents from the repo. This needs to be implemented.
155  void setup_domain(vector<MESH_ITEM> mesh); //!< Sets up the domain by creating patch objects in accordance to mesh objects.
156  void setup_agents(map<string,unsigned> config); //!< Creates agents and randomly distributes them in the simulation domain.
157  void step_agents(); //!< Calls step function of agents
158  void step_patches(); //!< Calls step function of patches
159  virtual void update(); //!< Update the world. All built-in utilities such as `Agent::order_move` are executed here.
160  void place_agent(shared_ptr<PATCH> patch,shared_ptr<AGENT> agent); //!< Places the given agent in the given patch. Raises an exception if the patch is not available.
161  void place_agent(unsigned patch_index,shared_ptr<AGENT> agent); //!< Places the given agent in the given patch index. Raises an exception if the patch is not available.
162 
163  void place_agent_randomly(shared_ptr<AGENT> agent); //!< Places the given agent randomly in the domain. Raises exception if no patch is available.
164  shared_ptr<PATCH> find_empty_patch(); //!< Finds empty patches in the entire domain
165  void remove_agent(shared_ptr<AGENT> agent){
166  this->agents.erase(std::remove(this->agents.begin(), this->agents.end(), agent), this->agents.end());
167  // swap the one to be removed with the last element
168 
169  auto it = std::find(this->agents.begin(), this->agents.end(), agent);
170 
171  if (it != this->agents.end()) {
172  using std::swap;
173  // swap the one to be removed with the last element
174  // and remove the item at the end of the container
175  // to prevent moving all items after '5' by one
176  swap(*it, this->agents.back());
177  this->agents.pop_back();
178  }
179  }
180  void process_switch(); //!< Process swtich requests
181  void process_hatch(); //!< Process hatch requests
182  void process_move(); //!< Process move requests
183  void process_disappear(); //!< Process disappear requests
184 
185  virtual void step() {
186  throw undefined_member("Step function is not defined inside Env");
187  }; //!< steps the simulation
188 
189  map<string,unsigned> count_agents(); //!< Counts the agents according to `Agent::class_name`.
190 
191  std::map<std::string,unsigned> agents_count; //!< Keeps the record the agents according to `Agent::class_name`.
192  std::set<string> agent_classes; //!< stores a list of `Agent::class_name`.
193  vector<shared_ptr<AGENT>> agents; //!< Agent container
194  map<unsigned,shared_ptr<PATCH>> patches; //!< Patch container
195  double memory_usage(){
196 #ifdef MEMORY_MONITOR
197  struct task_basic_info t_info;
198  mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
199 
200 
201  if (KERN_SUCCESS != task_info(mach_task_self(),
202  TASK_BASIC_INFO, (task_info_t)&t_info,
203  &t_info_count))
204  {
205 
206  }
207  return t_info.resident_size;
208 #else
209  return 0;
210 #endif
211  }
212 };
213 template<class ENV, class AGENT, class PATCH>
214 inline void Agent<ENV,AGENT,PATCH>::move(shared_ptr<PATCH> dest, bool quiet){
215  if (!dest->empty()) {
216  if (!quiet) throw patch_availibility("Given patch for move is not empty");
217  else return;
218  }
219  try{
220  this->patch->remove_agent(); // remove it from the current patch
221  this->env->place_agent(dest,this->shared_from_this());
222 
223  }
224  catch(patch_availibility & ee){
225  return;
226  }
227  }
228 template<class ENV, class AGENT, class PATCH>
229 inline void Agent<ENV,AGENT,PATCH>::order_hatch(shared_ptr<PATCH> patch, bool inherit, bool quiet, bool reset)
230  {
231  this->_hatch = HATCH_CONFIG<ENV,AGENT,PATCH>(true,patch,inherit,quiet,reset);
232 
233 }
234 template<class ENV, class AGENT, class PATCH>
235 inline void Agent<ENV,AGENT,PATCH>::order_move(shared_ptr<PATCH> patch,
236  bool quiet, bool reset ){
237  this->_move = MOVE_CONFIG<ENV,AGENT,PATCH>(true,patch,quiet,reset);
238 
239 }
240 
241 template<class ENV, class AGENT, class PATCH>
243  for (unsigned i = 0; i < this->agents.size(); i++){
244  this->agents[i]->step();
245  }
246 }
247 template<class ENV, class AGENT, class PATCH>
249 
250  for (auto &[index,patch]:this->patches){
251  patch->step();
252  }
253 }
254 template<class ENV, class AGENT, class PATCH>
255 inline void Env<ENV,AGENT,PATCH>::setup_agents(map<string,unsigned> config){
256  for (auto const [agent_type,count]:config){
257  for (unsigned i = 0; i < count; i++){
258  auto patch = this->find_empty_patch();
259  auto agent = this->generate_agent(agent_type);
260  this->place_agent(patch,agent);
261  }
262  this->agent_classes.insert(agent_type);
263  }
264 }
265 template<class ENV, class AGENT, class PATCH>
266 inline void Env<ENV,AGENT,PATCH>::update(){
267  auto g = tools::randomly_seeded_MT();
268  std::shuffle(this->agents.begin(),this->agents.end(),g);
269  /// move
270  this->process_move();
271  /// hatch
272  this->process_hatch();
273  /// switch
274  this->process_switch();
275  /// process disappearing
276  this->process_disappear();
277  this->update_repo(); // to remove the agents from repo
278  // update agent counts
279  this->count_agents();
280 };
281 template<class ENV, class AGENT, class PATCH>
283  unsigned agent_count = this->agents.size();
284  for (unsigned i = 0; i < agent_count; i++){
285  if (!this->agents[i]->_move._flag) continue;
286  auto dest = this->agents[i]->_move._patch;
287  if (dest == nullptr){ // find a random patch
288  try{
289  dest = this->agents[i]->patch->empty_neighbor();
290  }
291  // failure: manage how to respond
292  catch(patch_availibility&ee){
293 
294  if (!this->agents[i]->_move._quiet){ // only throw when it's forced
295  if (this->agents[i]->_move._reset){ // the try failed so reset the order
296  this->agents[i]->reset_move();
297  }
298  throw ee;
299  }
300  else{ // go to the next agent
301  if (this->agents[i]->_move._reset){ // the try failed so reset the order
302  this->agents[i]->reset_move();
303  }
304  continue;
305  }
306  }
307 
308  }
309  // check if another agent hasn't already taken it
310  if (!dest->empty()){
311  // failure: manage how to respond
312 
313  if (this->agents[i]->_move._quiet){ // only throw when it's forced
314  if (this->agents[i]->_move._reset){ // the try failed so reset the order
315  this->agents[i]->reset_move();
316  }
317  continue;
318  }
319  else{
320  if (this->agents[i]->_move._reset){ // the try failed so reset the order
321  this->agents[i]->reset_move();
322  }
323  throw patch_availibility("No patch for moving. If you want to silent this error, pass argumen quiet as true");
324  }
325  }
326 
327  this->agents[i]->move(dest,this->agents[i]->_move._quiet);
328  this->agents[i]->reset_move();
329  }
330 }
331 template<class ENV, class AGENT, class PATCH>
333  auto agent_count = this->agents.size();
334  for (unsigned i = 0; i < agent_count; i++){
335  if (this->agents[i]->_hatch._flag){
336  auto inherit = this->agents[i]->_hatch._inherit;
337  auto patch = this->agents[i]->_hatch._patch;
338 
339  if (patch == nullptr){ // find a random patch
340  try{
341  patch = this->agents[i]->patch->empty_neighbor();
342  }
343  // failure: manage how to respond
344  catch(patch_availibility&ee){
345 
346  if (!this->agents[i]->_hatch._quiet){ // only throw when it's forced
347  if (this->agents[i]->_hatch._reset){ // the try failed so reset the order
348  this->agents[i]->reset_hatch();
349  }
350  throw ee;
351  }
352  else{ // go to the next agent
353  if (this->agents[i]->_hatch._reset){ // the try failed so reset the order
354  this->agents[i]->reset_hatch();
355  }
356  break;
357  }
358  }
359 
360  }
361  // check if another agent hasn't already taken it
362  if (!patch->empty()){
363  // failure: manage how to respond
364 
365  if (this->agents[i]->_hatch._quiet){ // only throw when it's forced
366  if (this->agents[i]->_hatch._reset){ // the try failed so reset the order
367  this->agents[i]->reset_hatch();
368  }
369  continue;
370  }
371  else{
372  if (this->agents[i]->_hatch._reset){ // the try failed so reset the order
373  this->agents[i]->reset_hatch();
374  }
375  throw patch_availibility("No patch for hatching. If you want to silent this error, pass argumen quiet as true");
376  }
377  }
378 
379  auto new_agent = this->generate_agent(this->agents[i]->class_name);
380  if (this->agents[i]->_hatch._inherit){
381  new_agent->inherit(this->agents[i]);
382  }
383  this->place_agent(patch,new_agent);
384  this->agents[i]->reset_hatch();
385 
386 
387  };
388  }
389 }
390 template<class ENV, class AGENT, class PATCH>
392  unsigned size = this->agents.size();
393  auto counter = 0;
394  unsigned i = 0;
395  while(i<(size-counter)){
396  if(this->agents[i]->disappear == true){
397  swap(this->agents[i], this->agents[size-counter-1]); // we swap the agent location in the vector with the last agent
398  counter++;
399  }else{
400  i++;
401  }
402  }
403  if (counter>0){//if anything there to erase
404  this->agents.erase(this->agents.end()-counter,this->agents.end());
405  }
406 }
407 template<class ENV, class AGENT, class PATCH>
409  auto agent_count = this->agents.size();
410  for (unsigned i = 0; i < agent_count; i++){
411  auto agent = this->agents[i];
412  if (!agent->_switch._flag) continue;
413  auto to = agent->_switch._to;
414  // auto dest = agent->patch;
415  // dest->remove_agent(); // get the patch empty
416  // auto new_agent = this->generate_agent(to);
417  // this->place_agent(dest,new_agent);
418  // agent->disappear = true;
419  agent->class_name = to;
420  agent->reset_switch();
421  }
422 }
423 template<class ENV, class AGENT, class PATCH>
424 inline map<string,unsigned> Env<ENV,AGENT,PATCH>::count_agents(){
425  map<string,unsigned> agents_count;
426  for (unsigned i = 0; i < this->agents.size(); i++) {
427  auto agent = this->agents[i];
428  agents_count[agent->class_name]++;
429 
430  agent_classes.insert(agent->class_name);
431 
432  }
433  // to add those agents that were present at setup time but disappeared
434  for (auto const & agent_class:this->agent_classes){
435  if (agents_count.find(agent_class) == agents_count.end()){
436  agents_count[agent_class] = 0;
437  }
438  }
439  this->agents_count = agents_count;
440  return agents_count;
441 }
442 template<class ENV, class AGENT, class PATCH>
443 inline void Env<ENV,AGENT,PATCH>::place_agent(shared_ptr<PATCH> patch,shared_ptr<AGENT> agent){
444  if (!patch->empty()) {
445  if (patch->get_agent() == agent) return;
446  else throw patch_availibility("Patch is not empty");
447  }
448  else{
449  if (agent->has_patch){
450  agent->patch->remove_agent();
451  }
452  patch->set_agent(agent);
453  }
454 
455 }
456 template<class ENV, class AGENT, class PATCH>
457 inline void Env<ENV,AGENT,PATCH>::place_agent(unsigned patch_index,shared_ptr<AGENT> agent){
458  for (auto &[index,patch]:this->patches){
459  if (index == patch_index){
460  if (!patch->empty()) {
461  if (patch->get_agent() == agent) return;
462  else throw patch_availibility("Patch is not empty");
463  }
464  if (agent->has_patch){
465  agent->patch->remove_agent();
466  }
467  patch->set_agent(agent);
468  return;
469  }
470  }
471  throw patch_availibility("The given index for patch is not defined.");
472 
473 
474 }
475 template<class ENV, class AGENT, class PATCH>
476 inline void Env<ENV,AGENT,PATCH>::place_agent_randomly(shared_ptr<AGENT> agent){
477  auto patch = this->find_empty_patch();
478  place_agent(patch,agent);
479 }
480 template<class ENV, class AGENT, class PATCH>
481 inline shared_ptr<PATCH> Env<ENV,AGENT,PATCH>::find_empty_patch(){
482  std::vector<int> patch_keys;
483  for(auto const& patch: this->patches)
484  patch_keys.push_back(patch.first);
485 
486  auto g = tools::randomly_seeded_MT();
487 
488  std::shuffle(patch_keys.begin(), patch_keys.end(), g);
489 
490  for (auto const&i:patch_keys){
491  auto potential_patch = this->patches.at(i);
492  if (potential_patch->empty()){
493  return potential_patch;
494  }
495  }
496  throw patch_availibility("All patches are occupied.");
497 }
498 template<class ENV, class AGENT, class PATCH>
499  inline void Env<ENV,AGENT,PATCH>::setup_domain(vector<MESH_ITEM> mesh){
500  // step 1: create patches from info of meshes
501  for (auto & mesh_item:mesh){
502  this->generate_patch(mesh_item); // create patch
503 
504  }
505  // step 2: assign neighbor patches
506  for (auto &[index,patch]:patches){
507  vector<shared_ptr<PATCH>> neighbors;
508  auto neighbors_indices = patch->neighbors_indices;
509  for (auto const &neighbor_index:neighbors_indices){
510  auto neighbor_patch = patches.at(neighbor_index);
511  neighbors.push_back(neighbor_patch);
512  }
513  patch->neighbors = neighbors;
514  }
515  // log_mesh(mesh,"mesh.txt");
516  }
517 template<class ENV, class AGENT, class PATCH>
518 shared_ptr<PATCH> Patch<ENV,AGENT,PATCH>::empty_neighbor(bool quiet){
519  auto neighbors = this->neighbors;
520  std::random_device rd;
521  std::mt19937 g(rd());
522  std::shuffle(neighbors.begin(),neighbors.end(),g);
523  for (auto &neighbor:neighbors){
524  if (neighbor->empty()){
525  return neighbor;
526  }
527  }
528  if (!quiet) throw patch_availibility("No available patch around the agent");
529  return nullptr;
530 }
531 template<class ENV, class AGENT, class PATCH>
532 vector<shared_ptr<AGENT>> Patch<ENV,AGENT,PATCH>::find_neighbor_agents(bool include_self){
533  vector<shared_ptr<AGENT>> neighbor_agents;
534  if (!this->empty() & include_self) neighbor_agents.push_back(this->get_agent());
535  for (auto const &patch:this->neighbors){
536  if (!patch->empty()) neighbor_agents.push_back(patch->get_agent());
537  }
538  return neighbor_agents;
539  }
540 
Agent::reset_move
void reset_move()
Definition: bases.h:127
Agent::order_move
void order_move(shared_ptr< PATCH > patch=nullptr, bool quiet=false, bool reset=false)
Orders agent to move. This will execute during Env::update.
Env::memory_usage
double memory_usage()
Definition: bases.h:195
Env::setup_domain
void setup_domain(vector< MESH_ITEM > mesh)
Sets up the domain by creating patch objects in accordance to mesh objects.
test_multiple_inheritance.i
i
Definition: test_multiple_inheritance.py:22
common.h
Env::generate_patch
virtual shared_ptr< PATCH > generate_patch(MESH_ITEM)
Definition: bases.h:148
invalid_pointer
Definition: common.h:71
Env::step_agents
void step_agents()
Calls step function of agents.
Env::place_agent
void place_agent(shared_ptr< PATCH > patch, shared_ptr< AGENT > agent)
Places the given agent in the given patch. Raises an exception if the patch is not available.
Patch::neighbors
vector< shared_ptr< PATCH > > neighbors
list of neighbor patches
Definition: bases.h:95
Env::setup_agents
void setup_agents(map< string, unsigned > config)
Creates agents and randomly distributes them in the simulation domain.
SWITCH_CONFIG
Configuration of switch order from one agent type to another.
Definition: common.h:42
Agent::disappear
bool disappear
if set to true, the agent will be removed from the simulation. This will execute during Env::update
Definition: bases.h:135
Agent::patch
std::shared_ptr< PATCH > patch
Pointer to the residing patch.
Definition: bases.h:133
Env::step_patches
void step_patches()
Calls step function of patches.
Env::place_agent_randomly
void place_agent_randomly(shared_ptr< AGENT > agent)
Places the given agent randomly in the domain. Raises exception if no patch is available.
Env::process_move
void process_move()
Process move requests.
Patch::get_agent
shared_ptr< AGENT > get_agent()
Assigns agent to the patch.
Definition: bases.h:60
MESH_ITEM::coords
vector< double > coords
Coordinates.
Definition: mesh.h:12
Agent::step
virtual void step()
Definition: bases.h:110
mesh.h
Patch::step
virtual void step()
Definition: bases.h:50
tools::randomly_seeded_MT
auto randomly_seeded_MT()
Generate random numbers.
Definition: tools.h:16
undefined_member
Definition: common.h:68
Patch::empty_neighbor
shared_ptr< PATCH > empty_neighbor(bool quiet=false)
Returns an arbitrary adjacent patch without an agent.
Patch::neighbors_indices
vector< unsigned > neighbors_indices
the indices of the neighbor patches
Definition: bases.h:94
Agent::reset_switch
void reset_switch()
Definition: bases.h:128
Agent::move
void move(shared_ptr< PATCH > dest, bool quiet=false)
Moves the agent to the given destination patch.
Agent::_switch
SWITCH_CONFIG _switch
Definition: bases.h:132
Patch::coords
vector< double > coords
the coordinates of the patch
Definition: bases.h:93
Agent::_move
MOVE_CONFIG< ENV, AGENT, PATCH > _move
Definition: bases.h:131
Env::count_agents
map< string, unsigned > count_agents()
steps the simulation
Patch::remove_agent
void remove_agent()
Removes agent from the patch.
Definition: bases.h:78
MESH_ITEM
A class for mesh items.
Definition: mesh.h:10
Agent::inherit
virtual void inherit(shared_ptr< AGENT > father)
To define agent behavior.
Definition: bases.h:113
Agent::env
std::shared_ptr< ENV > env
Pointer to the simulation world.
Definition: bases.h:134
Agent::_hatch
HATCH_CONFIG< ENV, AGENT, PATCH > _hatch
Definition: bases.h:130
Agent::reset_hatch
void reset_hatch()
Orders agent to switch to another agent. This will execute during Env::update.
Definition: bases.h:126
Agent::class_name
string class_name
Unique ID of this class.
Definition: bases.h:136
Patch::~Patch
virtual ~Patch()
Definition: bases.h:48
Patch::layer_index
unsigned layer_index
the index of the layer in which the patch is residing
Definition: bases.h:92
Patch::set_agent
void set_agent(shared_ptr< AGENT > agent)
To define patch behavior.
Definition: bases.h:53
Agent::switch_info
std::pair< bool, std::string > switch_info
Definition: bases.h:129
Env::generate_agent
virtual shared_ptr< AGENT > generate_agent(string class_name)
A template class to generate patch.
Definition: bases.h:151
MOVE_CONFIG
Configuration of move order.
Definition: common.h:28
Env::process_switch
void process_switch()
Process swtich requests.
Agent::has_patch
bool has_patch
Indicates if an agent residing on a patch.
Definition: bases.h:137
Agent::update
virtual void update()
To define inheritage.
Definition: bases.h:116
Agent::~Agent
virtual ~Agent()
Definition: bases.h:108
MESH_ITEM::on_border
bool on_border
Whether the mesh is located on the border.
Definition: mesh.h:15
Agent::order_switch
void order_switch(string to)
Definition: bases.h:122
HATCH_CONFIG
Configuration of hatch order.
Definition: common.h:8
Env::update_repo
virtual void update_repo()
A template class to generate agent.
Definition: bases.h:154
Agent::order_hatch
void order_hatch(shared_ptr< PATCH > patch=nullptr, bool inherit=false, bool quiet=false, bool reset=false)
Orders agent to hatch. This will execute during Env::update.
Env::remove_agent
void remove_agent(shared_ptr< AGENT > agent)
Definition: bases.h:165
Patch::env
std::shared_ptr< ENV > env
Pointer to the simulation environment.
Definition: bases.h:88
Env::process_disappear
void process_disappear()
Process disappear requests.
empty
Definition: test_copy_move.cpp:16
Patch::empty
bool empty()
Definition: bases.h:69
Env::process_hatch
void process_hatch()
Process hatch requests.
Env::find_empty_patch
shared_ptr< PATCH > find_empty_patch()
Finds empty patches in the entire domain.
benchmark.size
size
Definition: benchmark.py:90
tools.h
Env::step
virtual void step()
Definition: bases.h:185
patch_availibility
Definition: common.h:65
Agent::Agent
Agent(shared_ptr< ENV > env, string class_name)
Definition: bases.h:104
Patch::agent_count
unsigned agent_count
Keeps the record of residing agents count.
Definition: bases.h:86
Env::update
virtual void update()
Update the world. All built-in utilities such as Agent::order_move are executed here.
env
Definition: env.py:1
Env::Env
Env()
Definition: bases.h:146
MESH_ITEM::neighbors_indices
std::vector< unsigned > neighbors_indices
Neighbor mesh indices.
Definition: mesh.h:13
Patch::index
unsigned index
unique index that associate patch to mesh
Definition: bases.h:91
Env::~Env
virtual ~Env()
Definition: bases.h:147
MESH_ITEM::index
unsigned index
Definition: mesh.h:11
Patch::find_neighbor_agents
vector< shared_ptr< AGENT > > find_neighbor_agents(bool include_self=true)
Returns agents in one patch neighbors.
Env::agents
vector< shared_ptr< AGENT > > agents
Agent container.
Definition: bases.h:193
Env::agents_count
std::map< std::string, unsigned > agents_count
Keeps the record the agents according to Agent::class_name.
Definition: bases.h:191
Patch::on_border
bool on_border
whether the patch residing on the border of the domain
Definition: bases.h:90
Patch::agent
std::weak_ptr< AGENT > agent
Pointer to stores agents.
Definition: bases.h:87
MESH_ITEM::layer_index
unsigned layer_index
The layer in which the mesh resides.
Definition: mesh.h:14
Patch::Patch
Patch(shared_ptr< ENV > env, MESH_ITEM mesh_item)
Definition: bases.h:29
Env::agent_classes
std::set< string > agent_classes
stores a list of Agent::class_name.
Definition: bases.h:192
Env::patches
map< unsigned, shared_ptr< PATCH > > patches
Patch container.
Definition: bases.h:194