Utopia 2
Framework for studying models of complex & adaptive systems.
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
Utopia::Models::PredatorPrey::PredatorPrey Class Reference

PredatorPrey Model on grid cells. More...

#include <PredatorPrey.hh>

Inheritance diagram for Utopia::Models::PredatorPrey::PredatorPrey:
Inheritance graph
[legend]
Collaboration diagram for Utopia::Models::PredatorPrey::PredatorPrey:
Collaboration graph
[legend]

Public Types

using Base = Model< PredatorPrey, ModelTypes >
 The base model.
 
using DataSet = typename Base::DataSet
 Data type for a dataset.
 
using CellManager = Utopia::CellManager< CellTraits, PredatorPrey >
 The type of the cell manager.
 
using Cell = typename CellManager::Cell
 The type of a cell.
 
using Rule = typename CellManager::RuleFunc
 Type of the update rules.
 
- Public Types inherited from Utopia::Model< PredatorPrey, ModelTypes >
using Config = typename ModelTypes::Config
 Data type that holds the configuration.
 
using DataManager = DataIO::Default::DefaultDataManager< PredatorPrey >
 The data manager to use, specialized with the derived model.
 
using DataGroup = typename ModelTypes::DataGroup
 Data type that is used for storing datasets.
 
using DataSet = typename ModelTypes::DataSet
 Data type that is used for storing data.
 
using RNG = typename ModelTypes::RNG
 Data type of the shared RNG.
 
using Space = typename ModelTypes::Space
 Data type of the space this model resides in.
 
using Time = typename ModelTypes::Time
 Data type for the model time.
 
using Monitor = typename ModelTypes::Monitor
 Data type for the monitor.
 
using MonitorManager = typename ModelTypes::MonitorManager
 Data type for the monitor manager.
 
using Level = typename ModelTypes::Level
 Data type for the hierarchical level.
 

Public Member Functions

template<class ParentModel >
 PredatorPrey (const std::string &name, ParentModel &parent_model, const DataIO::Config &custom_cfg={})
 Construct the PredatorPrey model.
 
void perform_step ()
 Perform an iteration step.
 
void monitor ()
 Monitor model information.
 
void write_data ()
 Write data.
 
- Public Member Functions inherited from Utopia::Model< PredatorPrey, ModelTypes >
 Model (const std::string &name, const ParentModel &parent_model, const Config &custom_cfg={}, std::tuple< WriterArgs... > w_args={}, const DataIO::Default::DefaultDecidermap< PredatorPrey > &w_deciders=DataIO::Default::default_deciders< PredatorPrey >, const DataIO::Default::DefaultTriggermap< PredatorPrey > &w_triggers=DataIO::Default::default_triggers< PredatorPrey >)
 Constructs a Model instance.
 
const std::shared_ptr< Space > & get_space () const
 Return the space this model resides in.
 
Time get_time () const
 Return the current time of this model.
 
Time get_time_max () const
 Return the maximum time possible for this model.
 
Config get_cfg () const
 Return the config node of this model.
 
std::string get_name () const
 Return the name of this model instance.
 
std::string get_full_name () const
 Return the full name of this model within the model hierarchy.
 
std::shared_ptr< DataGroupget_hdfgrp () const
 Return a pointer to the HDF group this model stores data in.
 
Time get_write_start () const
 Return the parameter that controls when write_data is called first.
 
Time get_write_every () const
 Return the parameter that controls how often write_data is called.
 
DataManager get_datamanager () const
 return the datamanager
 
hsize_t get_remaining_num_writes () const
 Return the number of remaining write_data calls this model will make.
 
std::shared_ptr< RNGget_rng () const
 Return a pointer to the shared RNG.
 
std::shared_ptr< spdlog::logger > get_logger () const
 Return a pointer to the logger of this model.
 
Monitor get_monitor () const
 Return the monitor of this model.
 
std::shared_ptr< MonitorManagerget_monitor_manager () const
 Get the monitor manager of the root model.
 
Level get_level () const
 Return the hierarchical level within the model hierarchy.
 
virtual void prolog ()
 A function that is called before starting model iteration.
 
virtual void epilog ()
 A function that is called after the last iteration of a model.
 
void iterate ()
 Iterate one (time) step of this model.
 
void run ()
 Run the model from the current time to the maximum time.
 
std::shared_ptr< DataSetcreate_dset (const std::string name, const std::shared_ptr< DataGroup > &hdfgrp, std::vector< hsize_t > add_write_shape, const std::size_t compression_level=1, const std::vector< hsize_t > chunksize={})
 Create a new dataset within the given group.
 
std::shared_ptr< DataSetcreate_dset (const std::string name, const std::vector< hsize_t > add_write_shape, const std::size_t compression_level=1, const std::vector< hsize_t > chunksize={})
 Create a new dataset within the model's base data group.
 
std::shared_ptr< DataSetcreate_cm_dset (const std::string name, const CellManager &cm, const std::size_t compression_level=1, const std::vector< hsize_t > chunksize={})
 Create a dataset storing data from a CellManager.
 
std::shared_ptr< DataSetcreate_am_dset (const std::string name, const AgentManager &am, const std::size_t compression_level=1, const std::vector< hsize_t > chunksize={})
 Create a dataset storing data from a AgentManager.
 

Private Member Functions

void move_predator_to_nb_cell (const std::shared_ptr< Cell > &cell, const std::shared_ptr< Cell > &nb_cell)
 Move a predator to a neighboring cell.
 
void move_prey_to_nb_cell (const std::shared_ptr< Cell > &cell, const std::shared_ptr< Cell > &nb_cell)
 Move a prey to a neighboring cell.
 

Private Attributes

CellManager _cm
 The cell manager.
 
SpeciesParams _params
 Predator-specific model parameters.
 
CellContainer< Cell_prey_cell
 A container to temporarily accumulate the prey neighbour cells.
 
CellContainer< Cell_empty_cell
 A container to temporarily accumulate empty neighbour cells.
 
CellContainer< Cell_repro_cell
 A container to temporarily accumulate neighbour cells for reproduction.
 
std::uniform_real_distribution< double_prob_distr
 
const std::shared_ptr< DataSet_dset_prey
 Dataset of Prey locations on the grid.
 
const std::shared_ptr< DataSet_dset_predator
 Dataset of Predator locations on the grid.
 
const std::shared_ptr< DataSet_dset_resource_prey
 Dataset of Prey resources on the grid.
 
const std::shared_ptr< DataSet_dset_resource_predator
 Dataset of Predator resources on the grid.
 
Rule _cost_of_living
 Cost of Living.
 
Rule _move_predator
 Define the movement rule of predator.
 
Rule _flee_prey
 Define the movement rule of prey.
 
Rule _eat
 Define the eating rule.
 
Rule _repro
 Define the reproduction rule.
 
Rule _reset_predator_movement
 Resets the movement flag of predators to "false" for next turn.
 

Additional Inherited Members

- Protected Member Functions inherited from Utopia::Model< PredatorPrey, ModelTypes >
void __perform_step ()
 Perform the computation of a step.
 
void __monitor ()
 Monitor information in the terminal.
 
void __write_data ()
 Write data; calls the implementation's write_data method.
 
void __write_initial_state ()
 Write the initial state.
 
void increment_time (const Time dt=1)
 Increment time.
 
void __prolog ()
 The default prolog of a model.
 
void __epilog ()
 The default epilog of a model.
 
PredatorPrey & impl ()
 cast to the derived class
 
const PredatorPrey & impl () const
 const cast to the derived interface
 
- Protected Attributes inherited from Utopia::Model< PredatorPrey, ModelTypes >
const std::string _name
 Name of the model instance.
 
const std::string _full_name
 The full name within the model hierarchy.
 
const Level _level
 The level within the model hierarchy.
 
const Config _cfg
 Config node belonging to this model instance.
 
const std::shared_ptr< RNG_rng
 The RNG shared between models.
 
const std::shared_ptr< spdlog::logger > _log
 The (model) logger.
 
std::shared_ptr< Space_space
 The space this model resides in.
 
Time _time
 Model-internal current time stamp.
 
const Time _time_max
 Model-internal maximum time stamp.
 
const std::shared_ptr< DataGroup_hdfgrp
 The HDF group this model instance should write its data to.
 
const Time _write_start
 First time at which write_data is called.
 
const Time _write_every
 How often to call write_data from iterate.
 
Monitor _monitor
 The monitor.
 
DataManager _datamanager
 Manager object for handling data output; see DataManager.
 
- Static Protected Attributes inherited from Utopia::Model< PredatorPrey, ModelTypes >
static constexpr WriteMode _write_mode
 Which data-writing mode the base model should use.
 

Detailed Description

PredatorPrey Model on grid cells.

Predators and prey correspond to the Population state of each cell, either empty, prey, predator or both. Cells are updated based on the following interactions: 1) resource levels are reduced by a cost_of_living for both species and individuals are removed if resource <= 0 2) predators move to neighbouring cells if there is a no prey on their own cell. Prey flees with a certain probability, if there is a predator on the same cell 3) predators eat prey if on the same cell, else if there is only a prey it takes up resources 4) both predators and prey reproduce if resources are sufficient and if there is a cell in their neighbourhood not already occupied by the same species

Member Typedef Documentation

◆ Base

The base model.

◆ Cell

The type of a cell.

◆ CellManager

The type of the cell manager.

◆ DataSet

Data type for a dataset.

◆ Rule

Type of the update rules.

Constructor & Destructor Documentation

◆ PredatorPrey()

template<class ParentModel >
Utopia::Models::PredatorPrey::PredatorPrey::PredatorPrey ( const std::string &  name,
ParentModel parent_model,
const DataIO::Config custom_cfg = {} 
)
inline

Construct the PredatorPrey model.

Parameters
nameName of this model instance; is used to extract the configuration from the parent model and set up a HDFGroup for this instance
parent_modelThe parent model this model instance resides in
custom_cfgA custom configuration to use instead of the one extracted from the parent model using the instance name
455 {}
456 )
457 :
459 // Initialize the cell manager, binding it to this model
460 _cm(*this),
461
462 // Extract model parameters
463 _params(this->_cfg),
464
465 // Temporary cell containers
466 _prey_cell(),
467 _empty_cell(),
468 _repro_cell(),
469
470 // Initialize distributions
471 _prob_distr(0., 1.),
472
473 // Create datasets
475 _dset_predator(this->create_cm_dset("predator", _cm)),
476 _dset_resource_prey(this->create_cm_dset("resource_prey", _cm)),
477 _dset_resource_predator(this->create_cm_dset("resource_predator", _cm))
478 {
479 // Load the cell state from a file, overwriting the current state
480 if (this->_cfg["cell_states_from_file"]) {
481 const auto& cs_cfg = this->_cfg["cell_states_from_file"];
482 const auto hdf5_file = get_as<std::string>("hdf5_file", cs_cfg);
483 const auto predator_init_resources = get_as<double>("init_resources",
484 this->_cfg["cell_manager"]["cell_params"]["predator"]);
485 const auto prey_init_resources = get_as<double>("init_resources",
486 this->_cfg["cell_manager"]["cell_params"]["prey"]);
487 if (get_as<bool>("load_predator", cs_cfg)) {
488 this->_log->info("Loading predator positions from file ...");
489
490 // Use the CellManager to set the cell state from the data
491 // given by the `predator` dataset. Load as int to be able to
492 // detect that a user supplied invalid values (better than
493 // failing silently, which would happen with booleans).
494 _cm.set_cell_states<int>(hdf5_file, "predator",
495 [predator_init_resources](auto& cell, const int on_cell){
496 if (on_cell == 0 or on_cell == 1) {
497 cell->state.predator.on_cell = on_cell;
498 if(on_cell == 1){
499 cell->state.predator.resources =
501 }
502 else{
503 cell->state.predator.resources = 0;
504 }
505 return;
506 }
507 throw std::invalid_argument("While setting predator "
508 "positions, encountered an invalid value: "
509 + std::to_string(on_cell) + ". Allowed: 0 or 1.");
510 }
511 );
512
513 this->_log->info("Predator positions loaded.");
514 }
515
516 if (get_as<bool>("load_prey", cs_cfg)) {
517 this->_log->info("Loading prey positions from file ...");
518
519 _cm.set_cell_states<int>(hdf5_file, "prey",
520 [prey_init_resources](auto& cell, const int on_cell){
521 if (on_cell == 0 or on_cell == 1) {
522 cell->state.prey.on_cell = on_cell;
523 if(on_cell == 1){
524 cell->state.prey.resources =
526 }
527 else{
528 cell->state.prey.resources = 0;
529 }
530 return;
531 }
532 throw std::invalid_argument("While setting prey "
533 "positions, encountered an invalid value: "
534 + std::to_string(on_cell) + ". Allowed: 0 or 1.");
535 }
536 );
537
538 this->_log->info("Prey positions loaded.");
539 }
540 }
541
542 // Reserve memory in the size of the neighborhood for the temp. vectors
543 const auto nb_size = _cm.nb_size();
544 _prey_cell.reserve(nb_size);
545 _empty_cell.reserve(nb_size);
546 _repro_cell.reserve(nb_size);
547
548 // Initialization finished
549 this->_log->info("{} model fully set up.", this->_name);
550 }
void set_cell_states(const std::string &hdf5_file, const std::string &dset_path, const SetterFunc &setter_func)
Set all cell states using information from a HDF5 file.
Definition cell_manager.hh:388
auto nb_size() const
Return the (maximum) size of the currently selected neighborhood.
Definition cell_manager.hh:450
std::shared_ptr< DataSet > create_cm_dset(const std::string name, const CellManager &cm, const std::size_t compression_level=1, const std::vector< hsize_t > chunksize={})
Create a dataset storing data from a CellManager.
Definition model.hh:849
const Config _cfg
Config node belonging to this model instance.
Definition model.hh:158
const std::string _name
Name of the model instance.
Definition model.hh:149
const std::shared_ptr< spdlog::logger > _log
The (model) logger.
Definition model.hh:164
CellContainer< Cell > _empty_cell
A container to temporarily accumulate empty neighbour cells.
Definition PredatorPrey.hh:129
SpeciesParams _params
Predator-specific model parameters.
Definition PredatorPrey.hh:122
const std::shared_ptr< DataSet > _dset_predator
Dataset of Predator locations on the grid.
Definition PredatorPrey.hh:143
std::uniform_real_distribution< double > _prob_distr
Definition PredatorPrey.hh:135
const std::shared_ptr< DataSet > _dset_prey
Dataset of Prey locations on the grid.
Definition PredatorPrey.hh:140
const std::shared_ptr< DataSet > _dset_resource_predator
Dataset of Predator resources on the grid.
Definition PredatorPrey.hh:149
CellContainer< Cell > _repro_cell
A container to temporarily accumulate neighbour cells for reproduction.
Definition PredatorPrey.hh:132
Model< PredatorPrey, ModelTypes > Base
The base model.
Definition PredatorPrey.hh:97
const std::shared_ptr< DataSet > _dset_resource_prey
Dataset of Prey resources on the grid.
Definition PredatorPrey.hh:146
CellManager _cm
The cell manager.
Definition PredatorPrey.hh:118
CellContainer< Cell > _prey_cell
A container to temporarily accumulate the prey neighbour cells.
Definition PredatorPrey.hh:126
Container select_entities(const Manager &mngr, const DataIO::Config &sel_cfg)
Select entities according to parameters specified in a configuration.
Definition select.hh:213

Member Function Documentation

◆ monitor()

void Utopia::Models::PredatorPrey::PredatorPrey::monitor ( )
inline

Monitor model information.

Calculate the densities for both species

585 {
587 auto [pred_density, prey_density] = [this](){
588 double predator_sum = 0.;
589 double prey_sum = 0.;
590 double num_cells = this->_cm.cells().size();
591
592 for (const auto& cell : this->_cm.cells()) {
593 auto state = cell->state;
594
595 if (state.prey.on_cell) {
596 prey_sum++;
597 }
598
599 if (state.predator.on_cell) {
600 predator_sum++;
601 }
602 }
603 return std::pair{predator_sum / num_cells, prey_sum / num_cells};
604 }();
605
606 this->_monitor.set_entry("predator_density", pred_density);
607 this->_monitor.set_entry("prey_density", prey_density);
608 }
const CellContainer< Cell > & cells() const
Return const reference to the managed CA cells.
Definition cell_manager.hh:219
Monitor _monitor
The monitor.
Definition model.hh:188

◆ move_predator_to_nb_cell()

void Utopia::Models::PredatorPrey::PredatorPrey::move_predator_to_nb_cell ( const std::shared_ptr< Cell > &  cell,
const std::shared_ptr< Cell > &  nb_cell 
)
inlineprivate

Move a predator to a neighboring cell.

This function resets the states predator state and updates the neighboring predator state.

192 {
193 auto& state = cell->state;
194 auto& nb_state = nb_cell->state;
195
196 nb_state.predator.on_cell = true;
197 nb_state.predator.resources = state.predator.resources;
198
199 state.predator.on_cell = false;
200 state.predator.resources = 0.;
201 }

◆ move_prey_to_nb_cell()

void Utopia::Models::PredatorPrey::PredatorPrey::move_prey_to_nb_cell ( const std::shared_ptr< Cell > &  cell,
const std::shared_ptr< Cell > &  nb_cell 
)
inlineprivate

Move a prey to a neighboring cell.

This function resets the states prey state and updates the neighboring prey state.

208 {
209 auto& state = cell->state;
210 auto& nb_state = nb_cell->state;
211
212 nb_state.prey.on_cell = true;
213 nb_state.prey.resources = state.prey.resources;
214
215 state.prey.on_cell = false;
216 state.prey.resources = 0.;
217 }

◆ perform_step()

void Utopia::Models::PredatorPrey::PredatorPrey::perform_step ( )
inline

Perform an iteration step.

An iteration step consists of:

  1. Subtracting costs of living
  2. Let predator and prey move to neighboring cells
  3. Lunch time: Prey eats grass and predator eats prey if on the same cell
  4. Reproduction: Create offspring
564 {
567
569 (_move_predator, _cm.cells(), *this->_rng);
570
572 (_flee_prey, _cm.cells(), *this->_rng);
573
575 (_eat, _cm.cells());
576
578 (_repro, _cm.cells(), *this->_rng);
579
582 }
Rule _flee_prey
Define the movement rule of prey.
Definition PredatorPrey.hh:288
Rule _cost_of_living
Cost of Living.
Definition PredatorPrey.hh:158
Rule _reset_predator_movement
Resets the movement flag of predators to "false" for next turn.
Definition PredatorPrey.hh:433
Rule _repro
Define the reproduction rule.
Definition PredatorPrey.hh:363
Rule _eat
Define the eating rule.
Definition PredatorPrey.hh:327
Rule _move_predator
Define the movement rule of predator.
Definition PredatorPrey.hh:225

◆ write_data()

void Utopia::Models::PredatorPrey::PredatorPrey::write_data ( )
inline

Write data.

When invoked, stores cell positions and resources of both prey and predators.

Note
Positions are cast to char and therefore stored as such. This is because C has no native boolean type, and the HDF5 library thus cannot store it directly. With 8 bit width, char is the smallest data type available; short int is already 16 bit.
619 {
620 // Predator
621 _dset_predator->write(_cm.cells().begin(), _cm.cells().end(),
622 [](const auto& cell) {
623 return static_cast<char>(cell->state.predator.on_cell);
624 }
625 );
626
627 // Prey
628 _dset_prey->write(_cm.cells().begin(), _cm.cells().end(),
629 [](const auto& cell) {
630 return static_cast<char>(cell->state.prey.on_cell);
631 }
632 );
633
634 // resource of predator
635 _dset_resource_predator->write(_cm.cells().begin(), _cm.cells().end(),
636 [](const auto& cell) {
637 return cell->state.predator.resources;
638 }
639 );
640
641 // resource of prey
642 _dset_resource_prey->write(_cm.cells().begin(), _cm.cells().end(),
643 [](const auto& cell) {
644 return cell->state.prey.resources;
645 }
646 );
647 }

Member Data Documentation

◆ _cm

CellManager Utopia::Models::PredatorPrey::PredatorPrey::_cm
private

The cell manager.

◆ _cost_of_living

Rule Utopia::Models::PredatorPrey::PredatorPrey::_cost_of_living
private
Initial value:
= [this](const auto& cell) {
auto& state = cell->state;
state.predator.resources =
std::clamp( state.predator.resources
state.prey.resources =
std::clamp( state.prey.resources
if (state.predator.on_cell and state.predator.resources <= 0.) {
state.predator.on_cell = false;
}
if (state.prey.on_cell and state.prey.resources <= 0.) {
state.prey.on_cell = false;
}
return state;
}
double resource_max
Maximal resource level.
Definition species.hh:39
double cost_of_living
Cost of living that is taken each time step.
Definition species.hh:30
PredatorParams predator
Predator parameters.
Definition species.hh:111
PreyParams prey
Prey parameters.
Definition species.hh:108

Cost of Living.

subtract the cost of living from the resources of an individual and map the values below zero back to zero, then remove all individuals that do not have sufficient resources

158 {
159 // Get the state of the cell
160 auto& state = cell->state;
161
162 // Subtract the cost of living and clamp the resources to the limits:
163 // If the resources exceed the maximal resources they are equal to
164 // the maximal resources and if they go below 0 they are mapped to 0.
165 state.predator.resources =
166 std::clamp( state.predator.resources
169 state.prey.resources =
170 std::clamp( state.prey.resources
173
174 // Remove predators that have no resources.
175 if (state.predator.on_cell and state.predator.resources <= 0.) {
176 state.predator.on_cell = false;
177 }
178
179 // Remove prey that have no resources.
180 if (state.prey.on_cell and state.prey.resources <= 0.) {
181 state.prey.on_cell = false;
182 }
183
184 return state;
185 };

◆ _dset_predator

const std::shared_ptr<DataSet> Utopia::Models::PredatorPrey::PredatorPrey::_dset_predator
private

Dataset of Predator locations on the grid.

◆ _dset_prey

const std::shared_ptr<DataSet> Utopia::Models::PredatorPrey::PredatorPrey::_dset_prey
private

Dataset of Prey locations on the grid.

◆ _dset_resource_predator

const std::shared_ptr<DataSet> Utopia::Models::PredatorPrey::PredatorPrey::_dset_resource_predator
private

Dataset of Predator resources on the grid.

◆ _dset_resource_prey

const std::shared_ptr<DataSet> Utopia::Models::PredatorPrey::PredatorPrey::_dset_resource_prey
private

Dataset of Prey resources on the grid.

◆ _eat

Rule Utopia::Models::PredatorPrey::PredatorPrey::_eat
private

Define the eating rule.

Update procedure is as follows:

  • prey is consumed by a predator if they are on the same cell. The predator increases its resources.
  • prey eats grass which increases its resources
327 {
328 // Get the state of the cell
329 auto& state = cell->state;
330
331 // Predator eats prey
332 if (state.predator.on_cell and state.prey.on_cell) {
333 // Increase the predator's resources and assure that the resource
334 // maximum is not exceeded by clamping into the correct interval.
335 state.predator.resources = std::clamp(
336 state.predator.resources
338 0.,
340
341 // Remove the prey from the cell
342 state.prey.on_cell = false;
343 state.prey.resources = 0.;
344 }
345
346 // Prey eats grass
347 else if (state.prey.on_cell == true) {
348 // Increase the resources and clamp to the allowed range
349 // [0, resource_max]
350 state.prey.resources = std::clamp(
351 state.prey.resources
353 0.,
355 }
356 return state;
357 };
double resource_intake
Resource intake from eating.
Definition species.hh:33

◆ _empty_cell

CellContainer<Cell> Utopia::Models::PredatorPrey::PredatorPrey::_empty_cell
private

A container to temporarily accumulate empty neighbour cells.

◆ _flee_prey

Rule Utopia::Models::PredatorPrey::PredatorPrey::_flee_prey
private

Define the movement rule of prey.

If a prey is on the same cell as a predator, determine whether it may flee and where to.

288 {
289 auto& state = cell->state;
290
291 if (state.prey.on_cell and state.predator.on_cell) {
292 // Flee, if possible, with a given flee probability
293 if (this->_prob_distr(*this->_rng) < _params.prey.p_flee){
294 // Collect empty neighboring cells to which the prey could flee
295 _empty_cell.clear();
296 for (const auto& nb : this->_cm.neighbors_of(cell)) {
297 if ( (not nb->state.prey.on_cell)
298 and (not nb->state.predator.on_cell)) {
299 _empty_cell.push_back(nb);
300 }
301 }
302
303 // If there is an empty cell, move there
304 if (_empty_cell.size() > 0) {
305 // Choose a random cell to move to
306 std::uniform_int_distribution<>
307 dist(0, _empty_cell.size() - 1);
308
309 auto nb_cell = _empty_cell[dist(*this->_rng)];
311 }
312 }
313 }
314 return state;
315 // NOTE Should the case be added that the neighboring cell has a predator?
316 // In case that there are no empty cells.
317 // Then the prey has again a small probability to flee in the next
318 // round.
319 };
const std::shared_ptr< RNG > _rng
The RNG shared between models.
Definition model.hh:161
void move_prey_to_nb_cell(const std::shared_ptr< Cell > &cell, const std::shared_ptr< Cell > &nb_cell)
Move a prey to a neighboring cell.
Definition PredatorPrey.hh:207
double p_flee
Probability to flee from a predator if on the same cell.
Definition species.hh:87

◆ _move_predator

Rule Utopia::Models::PredatorPrey::PredatorPrey::_move_predator
private

Define the movement rule of predator.

If a predator is on the given cell, it moves random to a neighbour cell with prey on it. If no prey is available on neighbour cells, it moves randomly to an empty cell, if there is on. Otherwise it stays on its current cell.

225 {
226 // Get the state of the Cell
227 auto& state = cell->state;
228
229 // Case: only a predator is on the cell
230 if ((state.predator.on_cell) and (not state.moved_predator)) {
231 // checking neighbouring cells for prey and moving to that cell
232
233 // clear the containers for cells that contain prey or empty cells
234 // in the neighbourhood
235 _prey_cell.clear();
236 _empty_cell.clear();
237
238 for (const auto& nb : this->_cm.neighbors_of(cell)) {
239 auto& nb_state = nb->state;
240
241 if ((nb_state.prey.on_cell) and (not nb_state.predator.on_cell)) {
242 _prey_cell.push_back(nb);
243 }
244 // if neither a prey nor a predator is on the cell, then
245 // it is empty
246 else if (not nb_state.predator.on_cell) {
247 _empty_cell.push_back(nb);
248 }
249 }
250
251 // if there is prey in the vicinity move the predator to a random
252 // prey cell
253 if (_prey_cell.size() > 0) {
254 // distribution to choose a random cell for the movement if
255 // there is more than one
256 std::uniform_int_distribution<>
257 dist_prey(0, _prey_cell.size() - 1);
258
259 // Select a random neighbor cell and move the predator to it
260 auto nb_cell = _prey_cell[dist_prey(*this->_rng)];
261 // Mark target cell as cell with moved predator
262 nb_cell->state.moved_predator=true;
264 }
265
266 // if there is no prey the predator makes a random move
267 else if (_empty_cell.size() > 0) {
268 // distribution to choose a random cell for the movement if
269 // there is more than one
270 std::uniform_int_distribution<>
271 dist_empty(0, _empty_cell.size() - 1);
272
273 // Select a random empty neighbor cell and move the predator
274 // to it
275 auto nb_cell = _empty_cell[dist_empty(*this->_rng)];
276 // Mark target cell as cell with moved predator
277 nb_cell->state.moved_predator=true;
279 }
280 }
281 return state;
282 };
void move_predator_to_nb_cell(const std::shared_ptr< Cell > &cell, const std::shared_ptr< Cell > &nb_cell)
Move a predator to a neighboring cell.
Definition PredatorPrey.hh:191

◆ _params

SpeciesParams Utopia::Models::PredatorPrey::PredatorPrey::_params
private

Predator-specific model parameters.

◆ _prey_cell

CellContainer<Cell> Utopia::Models::PredatorPrey::PredatorPrey::_prey_cell
private

A container to temporarily accumulate the prey neighbour cells.

◆ _prob_distr

std::uniform_real_distribution<double> Utopia::Models::PredatorPrey::PredatorPrey::_prob_distr
private

◆ _repro

Rule Utopia::Models::PredatorPrey::PredatorPrey::_repro
private

Define the reproduction rule.

If space is available reproduce with reproduction probabilities of predator and prey respectively.

363 {
364 // Get the state of the cell
365 auto& state = cell->state;
366
367 // Reproduce predators
368 if ( state.predator.on_cell
370 and (state.predator.resources >= _params.predator.repro_resource_requ))
371 {
372 // Collect available neighboring spots without predators
373 _repro_cell.clear();
374 for (const auto& nb : this->_cm.neighbors_of(cell)) {
375 if (not nb->state.predator.on_cell) {
376 _repro_cell.push_back(nb);
377 }
378 }
379
380 // Choose an available neighboring cell to put an offspring on
381 if (_repro_cell.size() > 0) {
382 // choose a random cell for the offspring to be placed on
383 std::uniform_int_distribution<> dist(0, _repro_cell.size()-1);
384 const auto& nb_cell = _repro_cell[dist(*this->_rng)];
385 auto& nb_state = nb_cell->state;
386
387 // neighboring cell has now a predator.
388 // Congratulations to your new baby! :)
389 nb_state.predator.on_cell = true;
390
391 // transfer resources from parent to offspring
392 nb_state.predator.resources = _params.predator.repro_cost;
393 state.predator.resources -= _params.predator.repro_cost;
394 }
395 }
396
397 // Reproduce prey
398 if ( state.prey.on_cell
399 and this->_prob_distr(*this->_rng) < _params.prey.repro_prob
400 and state.prey.resources >= _params.prey.repro_resource_requ)
401 {
402 _repro_cell.clear();
403
404 for (const auto& nb : this->_cm.neighbors_of(cell)) {
405 if (not nb->state.prey.on_cell) {
406 _repro_cell.push_back(nb);
407 }
408 }
409
410 if (_repro_cell.size() > 0) {
411 // choose a random cell for the offspring to be placed on
412 std::uniform_int_distribution<> dist(0, _repro_cell.size() - 1);
413 // TODO Use std::sample
414
415 auto nb_cell = _repro_cell[dist(*this->_rng)];
416
417 // neighboring cell has now a prey.
418 // Congratulations to your new baby! :)
419 nb_cell->state.prey.on_cell = true;
420
421 // transfer energy from parent to offspring
422 nb_cell->state.prey.resources = _params.prey.repro_cost;
423 state.prey.resources -= _params.prey.repro_cost;
424
425 // NOTE Should there be some dissipation of resources during
426 // reproduction?
427 }
428 }
429 return state;
430 };
double repro_cost
Cost of reproduction.
Definition species.hh:43
double repro_resource_requ
Minimal reproduction resources requirements.
Definition species.hh:36
double repro_prob
Reproduction probability.
Definition species.hh:46

◆ _repro_cell

CellContainer<Cell> Utopia::Models::PredatorPrey::PredatorPrey::_repro_cell
private

A container to temporarily accumulate neighbour cells for reproduction.

◆ _reset_predator_movement

Rule Utopia::Models::PredatorPrey::PredatorPrey::_reset_predator_movement
private
Initial value:
= [](const auto& cell) {
auto& state = cell->state;
state.moved_predator = false;
return state;
}

Resets the movement flag of predators to "false" for next turn.

433 {
434 auto& state = cell->state;
435 state.moved_predator = false;
436 return state;
437 };

The documentation for this class was generated from the following file: