10 using std::shared_ptr;
16 template<
class ENV,
class AGENT,
class PATCH>
18 template<
class ENV,
class AGENT,
class PATCH>
20 template<
class ENV,
class AGENT,
class PATCH>
27 template<
class ENV,
class AGENT,
class PATCH>
28 struct Patch:
public enable_shared_from_this<PATCH>{
57 agent->patch = this->shared_from_this();
58 agent->has_patch =
true;
61 shared_ptr<AGENT> p = this->
agent.lock();
70 shared_ptr<AGENT> p = this->
agent.lock();
88 std::shared_ptr<ENV>
env;
102 template<
class ENV,
class AGENT,
class PATCH>
103 struct Agent:
public enable_shared_from_this<AGENT>{
113 virtual void inherit(shared_ptr<AGENT> father){
114 cout<<
"Inherit is not defined"<<endl;
117 void move(shared_ptr<PATCH> dest,
bool quiet =
false);
119 bool inherit =
false,
bool quiet =
false,
bool reset =
false);
121 bool quiet =
false,
bool reset =
false);
129 std::pair <bool,std::string>
switch_info = std::make_pair(
false,
"");
144 template<
class ENV,
class AGENT,
class PATCH>
145 struct Env:
public enable_shared_from_this<ENV>{
160 void place_agent(shared_ptr<PATCH> patch,shared_ptr<AGENT> agent);
166 this->
agents.erase(std::remove(this->
agents.begin(), this->agents.end(), agent), this->agents.end());
169 auto it = std::find(this->
agents.begin(), this->agents.end(), agent);
171 if (it != this->
agents.end()) {
176 swap(*it, this->
agents.back());
196 #ifdef MEMORY_MONITOR
197 struct task_basic_info t_info;
198 mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT;
201 if (KERN_SUCCESS != task_info(mach_task_self(),
202 TASK_BASIC_INFO, (task_info_t)&t_info,
207 return t_info.resident_size;
213 template<
class ENV,
class AGENT,
class PATCH>
215 if (!dest->empty()) {
220 this->patch->remove_agent();
221 this->
env->place_agent(dest,this->shared_from_this());
228 template<
class ENV,
class AGENT,
class PATCH>
234 template<
class ENV,
class AGENT,
class PATCH>
236 bool quiet,
bool reset ){
241 template<
class ENV,
class AGENT,
class PATCH>
243 for (
unsigned i = 0;
i < this->agents.size();
i++){
244 this->agents[
i]->step();
247 template<
class ENV,
class AGENT,
class PATCH>
250 for (
auto &[index,patch]:this->patches){
254 template<
class ENV,
class AGENT,
class PATCH>
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);
262 this->agent_classes.insert(agent_type);
265 template<
class ENV,
class AGENT,
class PATCH>
268 std::shuffle(this->agents.begin(),this->agents.end(),g);
270 this->process_move();
272 this->process_hatch();
274 this->process_switch();
276 this->process_disappear();
279 this->count_agents();
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){
289 dest = this->agents[
i]->patch->empty_neighbor();
294 if (!this->agents[
i]->_move._quiet){
295 if (this->agents[
i]->_move._reset){
296 this->agents[
i]->reset_move();
301 if (this->agents[
i]->_move._reset){
302 this->agents[
i]->reset_move();
313 if (this->agents[
i]->_move._quiet){
314 if (this->agents[
i]->_move._reset){
315 this->agents[
i]->reset_move();
320 if (this->agents[
i]->_move._reset){
321 this->agents[
i]->reset_move();
323 throw patch_availibility(
"No patch for moving. If you want to silent this error, pass argumen quiet as true");
327 this->agents[
i]->move(dest,this->agents[
i]->_move._quiet);
328 this->agents[
i]->reset_move();
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;
339 if (patch ==
nullptr){
341 patch = this->agents[
i]->patch->empty_neighbor();
346 if (!this->agents[
i]->_hatch._quiet){
347 if (this->agents[
i]->_hatch._reset){
348 this->agents[
i]->reset_hatch();
353 if (this->agents[
i]->_hatch._reset){
354 this->agents[
i]->reset_hatch();
362 if (!patch->empty()){
365 if (this->agents[
i]->_hatch._quiet){
366 if (this->agents[
i]->_hatch._reset){
367 this->agents[
i]->reset_hatch();
372 if (this->agents[
i]->_hatch._reset){
373 this->agents[
i]->reset_hatch();
375 throw patch_availibility(
"No patch for hatching. If you want to silent this error, pass argumen quiet as true");
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]);
383 this->place_agent(patch,new_agent);
384 this->agents[
i]->reset_hatch();
390 template<
class ENV,
class AGENT,
class PATCH>
392 unsigned size = this->agents.size();
395 while(
i<(
size-counter)){
396 if(this->agents[
i]->disappear ==
true){
397 swap(this->agents[
i], this->agents[
size-counter-1]);
404 this->agents.erase(this->agents.end()-counter,this->agents.end());
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;
419 agent->class_name = to;
420 agent->reset_switch();
423 template<
class ENV,
class AGENT,
class PATCH>
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]++;
430 agent_classes.insert(agent->class_name);
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;
439 this->agents_count = agents_count;
442 template<
class ENV,
class AGENT,
class PATCH>
444 if (!patch->empty()) {
445 if (patch->get_agent() == agent)
return;
449 if (agent->has_patch){
450 agent->patch->remove_agent();
452 patch->set_agent(agent);
456 template<
class ENV,
class AGENT,
class PATCH>
458 for (
auto &[index,patch]:this->patches){
459 if (index == patch_index){
460 if (!patch->empty()) {
461 if (patch->get_agent() == agent)
return;
464 if (agent->has_patch){
465 agent->patch->remove_agent();
467 patch->set_agent(agent);
475 template<
class ENV,
class AGENT,
class PATCH>
477 auto patch = this->find_empty_patch();
478 place_agent(patch,agent);
480 template<
class ENV,
class AGENT,
class PATCH>
482 std::vector<int> patch_keys;
483 for(
auto const& patch: this->patches)
484 patch_keys.push_back(patch.first);
488 std::shuffle(patch_keys.begin(), patch_keys.end(), g);
490 for (
auto const&
i:patch_keys){
491 auto potential_patch = this->patches.at(
i);
492 if (potential_patch->empty()){
493 return potential_patch;
498 template<
class ENV,
class AGENT,
class PATCH>
501 for (
auto & mesh_item:mesh){
502 this->generate_patch(mesh_item);
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);
513 patch->neighbors = neighbors;
517 template<
class ENV,
class AGENT,
class PATCH>
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()){
531 template<
class ENV,
class AGENT,
class PATCH>
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());
538 return neighbor_agents;