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
34  double memory_usage_max;
35 };
36 struct Tissue: public Patch<Domain,Cell,Tissue> {
38  using basePatch::basePatch;
39  Tissue(shared_ptr<Domain> env,MESH_ITEM mesh):basePatch(env,mesh){
40  this->setup();
41  }
42  void setup(){
43  }
44  bool damage_center = false;
45  double ECM = 100;
46 
47 };
48 struct Cell: public Agent<Domain,Cell,Tissue> {
50  using baseAgent::baseAgent;
51  Cell(shared_ptr<Domain> env,std::string agent_name):baseAgent(env,agent_name){
52  this->setup();
53  }
54  void update(){
55  this->clock++;
56  }
57  virtual void step();
58  virtual void setup(){
59  this->cycle_t = 12;
60  }
61  int cycle_t;
62  int clock = 0;
63 };
64 inline shared_ptr<Cell> Domain::generate_agent(std::string agent_name){
65  auto agent_obj = make_shared<Cell>(this->shared_from_this(),agent_name);
66  this->agents.push_back(agent_obj);
67  return agent_obj;
68  }
69 inline shared_ptr<Tissue> Domain::generate_patch(MESH_ITEM mesh){
70  auto patch_obj = make_shared<Tissue>(this->shared_from_this(),mesh);
71  this->patches.insert(pair<unsigned,shared_ptr<Tissue>>(patch_obj->index,patch_obj));
72  return patch_obj;
73  }
74 inline void Domain::setup(){
75  auto mesh = space::grid2(1.5, 1.5, 0.015, true);
76  this->setup_domain(mesh);
77  std::map<std::string,unsigned> settings = {{"cell",2000}};
78  this->setup_agents(settings);
79  this->damage();
80  this->update();
81  }
82 inline void Domain::damage(){
83  for (auto &[index,patch]:this->patches){
84  auto x = patch->coords[0];
85  auto y = patch->coords[1];
86  if ((x >= 0.25 and x <=1.25) and (y>=0.25 and y<=1.25)){
87  patch->damage_center = true;
88  patch->ECM = 0;
89  if (patch->empty() == false){
90  patch->get_agent()->disappear = true;
91  }
92 
93  }
94  }
95  }
96 inline void Domain::update(){
98  for (auto &agent: this->agents){
99  agent->update();
100  }
101  int cell_count = this->agents.size();
102  this->data["cell_count"].push_back(cell_count);
103 
104  }
105 inline void Domain::step(){
106  auto usage = this->memory_usage();
107  if (usage > memory_usage_max){
108  memory_usage_max = usage;
109  }
110 
111  for (auto &cell:this->agents){
112  cell->step();
113  }
114  this->update();
115  this->tick ++;
116 }
117 inline void Cell::step(){
118  this->order_move(nullptr,true,false);
119  auto neighbor_cell_count = this->patch->find_neighbor_agents().size();
120  if (this->patch->damage_center and this->clock >= this->cycle_t){
121  if (neighbor_cell_count <= 6){
122  this->order_hatch(nullptr,false,true,false);
123  this->clock = 0 ;
124  }
125  }
126  if (this->patch->ECM < 100) {
127  this->patch->ECM += 1;
128  }
129 
130  if (neighbor_cell_count >7){
131  this->disappear = true;
132  }
133 }
134 inline void Domain::episode(){
135  for (unsigned i = 0; i < 336; i++){
136  cout<<"iteration "<<i<<" agents "<<this->agents.size()<<endl;
137  this->step();
138  if (this->output_flag) this->output();
139  }
140  ofstream file1;
141  file1.open("memory_usage.csv");
142  file1<<memory_usage_max<<"\n";
143  file1.close();
144  cout<<"Memory usage "<<memory_usage_max<<endl;
145 }
146 inline void Domain::output(){
147  // plot agents on the domain
148  ofstream file1;
149  file1.open("cells.csv");
150  file1 << "x,y,type,size\n";
151  for (auto &agent:this->agents){
152  file1<<agent->patch->coords[0]<<","<<agent->patch->coords[1]<<","<< agent->class_name<<", "<<10<<std::endl;
153  }
154  file1.close();
155  //plot ECM density on the domain
156  ofstream file2;
157  file2.open("ECM.csv");
158  file2<<"x,y,type,size\n";
159  for (auto &[index,patch]:this->patches){
160  file2<<patch->coords[0]<<","<<patch->coords[1]<<","<< patch->ECM<<", "<<10<<std::endl;
161  }
162  file2.close();
163  // cell counts
164  ofstream file3;
165  file3.open("cell_count.csv");
166  file3<<"cell_count\n";
167  for (unsigned i=0;i<this->data["cell_count"].size(); i++){
168  file3<<i<<","<<this->data["cell_count"][i]<<std::endl;
169  }
170  file3.close();
171 
172 }
Domain::setup
void setup()
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()
Cell::Cell
Cell(shared_ptr< Domain > env, std::string agent_name)
Definition: cpp_example.h:51
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
Domain::update
virtual void update()
Update the world. All built-in utilities such as Agent::order_move are executed here.
Domain::step
virtual void step()
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
Domain::generate_agent
virtual shared_ptr< Cell > generate_agent(std::string agent_name)
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::generate_patch
virtual shared_ptr< Tissue > generate_patch(MESH_ITEM)
Domain::Domain
Domain(bool _output_flag)
Definition: cpp_example.h:16
Cell::update
void update()
To define inheritage.
Definition: cpp_example.h:54
Cell::clock
int clock
Definition: cpp_example.h:58
Tissue::Tissue
Tissue(shared_ptr< Domain > env, MESH_ITEM mesh)
Definition: cpp_example.h:39
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
Cell::step
virtual void step()
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()
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()
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