cppyabm  1.0.17
An agent-based library to integrate C++ and Python
cpp_example.h
Go to the documentation of this file.
1 #include <iostream>
2 #include <fstream>
3 #include <cppyabm/bases.h>
4 #include "cppyabm/mesh.h"
5 
6 
7 
8 
9 struct Domain;
10 struct Tissue;
11 struct Cell;
12 //! Domain class to coordinate the simulation
13 struct Domain: public Env<Domain,Cell,Tissue> {
15  using baseEnv::baseEnv;
16  Domain(bool _output_flag):Env<Domain,Cell,Tissue>(){
17  output_flag = _output_flag;
18  }
19  bool output_flag = false;
20  virtual shared_ptr<Cell> generate_agent(std::string agent_name);
21  virtual shared_ptr<Tissue> generate_patch(MESH_ITEM);
22  virtual void update();
23  void damage();
24  void setup();
25  virtual void step();
26  void episode();
27  void output();
28  std::map<std::string,std::vector<int>> data= {{"cell_count",{}}};
29  int tick=0;
30 #ifdef MEMORY_MONITOR
31  struct task_basic_info t_info;
32  mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
33 #endif //MEMORY_MONITOR
35 };
36 struct Tissue: public Patch<Domain,Cell,Tissue> {
37  Tissue(shared_ptr<Domain> env,MESH_ITEM mesh):Patch<Domain,Cell,Tissue>(env,mesh){
38  this->setup();
39  }
40  void setup(){
41  }
42  bool damage_center = false;
43  double ECM = 100;
44 
45 };
46 struct Cell: public Agent<Domain,Cell,Tissue> {
47  Cell(shared_ptr<Domain> env,std::string agent_name):Agent<Domain,Cell,Tissue>(env,agent_name){
48  this->setup();
49  }
50  void update(){
51  this->clock++;
52  }
53  virtual void step();
54  virtual void setup(){
55  this->cycle_t = 12;
56  }
57  int cycle_t;
58  int clock = 0;
59 };
60 inline shared_ptr<Cell> Domain::generate_agent(std::string agent_name){
61  auto agent_obj = make_shared<Cell>(this->shared_from_this(),agent_name);
62  this->agents.push_back(agent_obj);
63  return agent_obj;
64  }
65 inline shared_ptr<Tissue> Domain::generate_patch(MESH_ITEM mesh){
66  auto patch_obj = make_shared<Tissue>(this->shared_from_this(),mesh);
67  this->patches.insert(pair<unsigned,shared_ptr<Tissue>>(patch_obj->index,patch_obj));
68  return patch_obj;
69  }
70 inline void Domain::setup(){
71  auto mesh = space::grid2(1.5, 1.5, 0.015, true);
72  this->setup_domain(mesh);
73  std::map<std::string,unsigned> settings = {{"cell",2000}};
74  this->setup_agents(settings);
75  this->damage();
76  this->update();
77  }
78 inline void Domain::damage(){
79  for (auto &[index,patch]:this->patches){
80  auto x = patch->coords[0];
81  auto y = patch->coords[1];
82  if ((x >= 0.25 and x <=1.25) and (y>=0.25 and y<=1.25)){
83  patch->damage_center = true;
84  patch->ECM = 0;
85  if (patch->empty() == false){
86  patch->get_agent()->disappear = true;
87  }
88 
89  }
90  }
91  }
92 inline void Domain::update(){
94  for (auto &agent: this->agents){
95  agent->update();
96  }
97  int cell_count = this->agents.size();
98  this->data["cell_count"].push_back(cell_count);
99 
100  }
101 inline void Domain::step(){
102  auto usage = this->memory_usage();
103  if (usage > memory_usage_max){
104  memory_usage_max = usage;
105  }
106 
107  for (auto &cell:this->agents){
108  cell->step();
109  }
110  this->update();
111  this->tick ++;
112 }
113 inline void Cell::step(){
114  this->order_move(nullptr,true,false);
115  auto neighbor_cell_count = this->patch->find_neighbor_agents().size();
116  if (this->patch->damage_center and this->clock >= this->cycle_t){
117  if (neighbor_cell_count <= 6){
118  this->order_hatch(nullptr,false,true,false);
119  this->clock = 0 ;
120  }
121  }
122  if (this->patch->ECM < 100) {
123  this->patch->ECM += 1;
124  }
125 
126  if (neighbor_cell_count >7){
127  this->disappear = true;
128  }
129 }
130 inline void Domain::episode(){
131  for (unsigned i = 0; i < 336; i++){
132  cout<<"iteration "<<i<<" agents "<<this->agents.size()<<endl;
133  this->step();
134  if (this->output_flag) this->output();
135  }
136  ofstream file1;
137  file1.open("memory_usage.csv");
138  file1<<memory_usage_max<<"\n";
139  file1.close();
140  cout<<"Memory usage "<<memory_usage_max<<endl;
141 }
142 inline void Domain::output(){
143  // plot agents on the domain
144  ofstream file1;
145  file1.open("cells.csv");
146  file1 << "x,y,type,size\n";
147  for (auto &agent:this->agents){
148  file1<<agent->patch->coords[0]<<","<<agent->patch->coords[1]<<","<< agent->class_name<<", "<<10<<std::endl;
149  }
150  file1.close();
151  //plot ECM density on the domain
152  ofstream file2;
153  file2.open("ECM.csv");
154  file2<<"x,y,type,size\n";
155  for (auto &[index,patch]:this->patches){
156  file2<<patch->coords[0]<<","<<patch->coords[1]<<","<< patch->ECM<<", "<<10<<std::endl;
157  }
158  file2.close();
159  // cell counts
160  ofstream file3;
161  file3.open("cell_count.csv");
162  file3<<"cell_count\n";
163  for (unsigned i=0;i<this->data["cell_count"].size(); i++){
164  file3<<i<<","<<this->data["cell_count"][i]<<std::endl;
165  }
166  file3.close();
167 
168 }
Domain::setup
void setup()
Definition: cpp_example.h:70
Agent< Domain, Cell, Tissue >::order_move
void order_move(shared_ptr< Tissue > patch=nullptr, bool quiet=false, bool reset=false)
Orders agent to move. This will execute during Env::update.
Env< Domain, Cell, Tissue >::memory_usage
double memory_usage()
Definition: bases.h:195
Domain::output_flag
bool output_flag
Definition: cpp_example.h:19
Env< Domain, Cell, Tissue >::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
Tissue
Definition: cpp_example.h:36
Env< Domain, Cell, Tissue >::setup_agents
void setup_agents(map< string, unsigned > config)
Creates agents and randomly distributes them in the simulation domain.
test_builtin_casters.x
x
Definition: test_builtin_casters.py:467
Tissue::setup
void setup()
Definition: cpp_example.h:40
Cell::cycle_t
int cycle_t
Definition: cpp_example.h:57
Agent< Domain, Cell, Tissue >::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< Domain, Cell, Tissue >::patch
std::shared_ptr< Tissue > patch
Pointer to the residing patch.
Definition: bases.h:133
monitor_script.settings
dictionary settings
Definition: monitor_script.py:3
Domain::episode
void episode()
Definition: cpp_example.h:130
Cell::Cell
Cell(shared_ptr< Domain > env, std::string agent_name)
Definition: cpp_example.h:47
Patch
Base class for patch.
Definition: bases.h:28
Cell::step
virtual void step()
Definition: cpp_example.h:113
MESH_ITEM
A class for mesh items.
Definition: mesh.h:10
Domain::step
virtual void step()
Definition: cpp_example.h:101
Agent
Base class for Agent.
Definition: bases.h:103
Domain::generate_agent
virtual shared_ptr< Cell > generate_agent(std::string agent_name)
Definition: cpp_example.h:60
Tissue::damage_center
bool damage_center
Definition: cpp_example.h:42
Cell::setup
virtual void setup()
Definition: cpp_example.h:54
Cell
Definition: cpp_example.h:46
Domain::Domain
Domain(bool _output_flag)
Definition: cpp_example.h:16
Cell::update
void update()
To define inheritage.
Definition: cpp_example.h:50
Cell::clock
int clock
Definition: cpp_example.h:58
Tissue::Tissue
Tissue(shared_ptr< Domain > env, MESH_ITEM mesh)
Definition: cpp_example.h:37
Domain
Domain class to coordinate the simulation.
Definition: cpp_example.h:13
Domain::memory_usage_max
double memory_usage_max
Definition: cpp_example.h:34
Agent< Domain, Cell, Tissue >::order_hatch
void order_hatch(shared_ptr< Tissue > patch=nullptr, bool inherit=false, bool quiet=false, bool reset=false)
Orders agent to hatch. This will execute during Env::update.
Domain::generate_patch
virtual shared_ptr< Tissue > generate_patch(MESH_ITEM)
Definition: cpp_example.h:65
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
Domain::output
void output()
Definition: cpp_example.h:142
Env
Base class for environment.
Definition: bases.h:145
space::grid2
vector< MESH_ITEM > grid2(double length, double width, double mesh_length, bool share)
A function to create 2D rectangular mesh.
Definition: mesh.h:20
Domain::update
virtual void update()
Update the world. All built-in utilities such as Agent::order_move are executed here.
Definition: cpp_example.h:92
Env< Domain, Cell, Tissue >::agents
vector< shared_ptr< Cell > > agents
Agent container.
Definition: bases.h:193
Domain::data
std::map< std::string, std::vector< int > > data
Definition: cpp_example.h:28
Domain::tick
int tick
Definition: cpp_example.h:29
Domain::damage
void damage()
Definition: cpp_example.h:78
Tissue::ECM
double ECM
Definition: cpp_example.h:43
Env< Domain, Cell, Tissue >::patches
map< unsigned, shared_ptr< Tissue > > patches
Patch container.
Definition: bases.h:194