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

PredatorPreyPlant Model on grid cells. More...

#include <PredatorPreyPlant.hh>

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

Public Types

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

Public Member Functions

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

Private Member Functions

std::shared_ptr< Cellget_random_neighbor (const CellContainer< Cell > &nbs) const
 Returns a random neighbor. More...
 
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. More...
 
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. More...
 
auto move_prey (std::shared_ptr< Cell > cell)
 Move the prey looking for resources. More...
 
auto move_predator (std::shared_ptr< Cell > cell)
 Move the predator looking for preys. More...
 
void move_entities (std::shared_ptr< Cell > cell)
 Define the movement rule of an individual. More...
 
void setup_cell_states_from_file (const Config &cs_cfg)
 Sets predator, prey, and plant positions from loaded HDF5 data. More...
 

Private Attributes

CellManager _cm
 The cell manager. More...
 
SpeciesParams _params
 All species-specific parameters. More...
 
std::size_t _num_moves
 How many cells the movement rule should be applied to each time step. More...
 
CellContainer< Cell_prey_cell
 A container to temporarily accumulate the prey neighbour cells. More...
 
CellContainer< Cell_empty_cell
 A container to temporarily accumulate empty neighbour cells. More...
 
CellContainer< Cell_repro_cell
 A container to temporarily accumulate neighbour cells for reproduction. More...
 
CellContainer< Cell_resource_cell
 A container to temporarily accumulate neighbour cells with mature plants. More...
 
std::uniform_real_distribution< double > _prob_distr
 Uniform real distribution [0, 1) for evaluating probabilities. More...
 
std::uniform_int_distribution _cm_dist
 Distribution for randomly selecting a cell in your cellmanager. More...
 
const std::shared_ptr< DataSet_dset_prey
 Dataset of Prey locations on the grid. More...
 
const std::shared_ptr< DataSet_dset_predator
 Dataset of Predator locations on the grid. More...
 
const std::shared_ptr< DataSet_dset_resource_prey
 Dataset of Prey resources on the grid. More...
 
const std::shared_ptr< DataSet_dset_resource_predator
 Dataset of Predator resources on the grid. More...
 
const std::shared_ptr< DataSet_dset_plant
 Dataset of Plant resources. More...
 
Rule _cost_of_living
 Cost of Living. More...
 
Rule _flee_prey
 If a prey is on the cell, determine whether it may flee and where to. More...
 
Rule _eat
 Define the eating rule. More...
 
Rule _reproduce
 Define the reproduction rule. More...
 

Additional Inherited Members

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

Detailed Description

PredatorPreyPlant 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. Prey looks for resources if there are none on its cell. 3) predators eat prey if on the same cell, else if there is only a prey it takes up resources, when this are present 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. The plant grows if it is set in mode other than 0.

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

◆ PredatorPreyPlant()

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

Construct the PredatorPreyPlant 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

Member Function Documentation

◆ get_random_neighbor()

std::shared_ptr<Cell> Utopia::Models::PredatorPreyPlant::PredatorPreyPlant::get_random_neighbor ( const CellContainer< Cell > &  nbs) const
inlineprivate

Returns a random neighbor.

This method chooses uniform randomly a cell from the neighborhood of the given cell.

◆ monitor()

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

Monitor model information.

◆ move_entities()

void Utopia::Models::PredatorPreyPlant::PredatorPreyPlant::move_entities ( std::shared_ptr< Cell cell)
inlineprivate

Define the movement rule of an individual.

◆ move_predator()

auto Utopia::Models::PredatorPreyPlant::PredatorPreyPlant::move_predator ( std::shared_ptr< Cell cell)
inlineprivate

Move the predator looking for preys.

Predators looks for prey in the neighborhood. If there are multiple cells with prey on them randomly choose one cell to move to. If there is no cell with prey just do a movement towards a randomly selected neighboring cell. A predator can never move to a cell with another predator on it.

◆ move_predator_to_nb_cell()

void Utopia::Models::PredatorPreyPlant::PredatorPreyPlant::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.

◆ move_prey()

auto Utopia::Models::PredatorPreyPlant::PredatorPreyPlant::move_prey ( std::shared_ptr< Cell cell)
inlineprivate

Move the prey looking for resources.

If plants are enabled, prey might need to look for available resources in the form of plants in the neighboring cells. If there is a plant on a neighboring cell and no predator, the prey will move there. If there are multiple cells with plants on them, one is selected at random. If there are no free cells just take your chance and randomly select a cell, if there is a predator on it nevertheless move to it.

◆ move_prey_to_nb_cell()

void Utopia::Models::PredatorPreyPlant::PredatorPreyPlant::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.

◆ perform_step()

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

Perform an iteration step.

An iteration step consists of:

  1. Subtracting costs of living
  2. Let predator and prey move
  3. Lunch time: Prey eats grass and predator eats prey if on the same cell
  4. Reproduction: Create offspring and grow plant

◆ setup_cell_states_from_file()

void Utopia::Models::PredatorPreyPlant::PredatorPreyPlant::setup_cell_states_from_file ( const Config cs_cfg)
inlineprivate

Sets predator, prey, and plant positions from loaded HDF5 data.

◆ write_data()

void Utopia::Models::PredatorPreyPlant::PredatorPreyPlant::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.

Member Data Documentation

◆ _cm

CellManager Utopia::Models::PredatorPreyPlant::PredatorPreyPlant::_cm
private

The cell manager.

◆ _cm_dist

std::uniform_int_distribution Utopia::Models::PredatorPreyPlant::PredatorPreyPlant::_cm_dist
private

Distribution for randomly selecting a cell in your cellmanager.

◆ _cost_of_living

Rule Utopia::Models::PredatorPreyPlant::PredatorPreyPlant::_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;
}
SpeciesParams _params
All species-specific parameters.
Definition: PredatorPreyPlant.hh:159
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
PreyParams prey
Prey parameters.
Definition: species.hh:145
PredatorParams predator
Predator parameters.
Definition: species.hh:148

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. This function applies only to predator and prey, but not to plants.

◆ _dset_plant

const std::shared_ptr<DataSet> Utopia::Models::PredatorPreyPlant::PredatorPreyPlant::_dset_plant
private

Dataset of Plant resources.

◆ _dset_predator

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

Dataset of Predator locations on the grid.

◆ _dset_prey

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

Dataset of Prey locations on the grid.

◆ _dset_resource_predator

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

Dataset of Predator resources on the grid.

◆ _dset_resource_prey

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

Dataset of Prey resources on the grid.

◆ _eat

Rule Utopia::Models::PredatorPreyPlant::PredatorPreyPlant::_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 plants which increases its resources; if the plant growth model is not none the plant is removed from the cell.

◆ _empty_cell

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

A container to temporarily accumulate empty neighbour cells.

◆ _flee_prey

Rule Utopia::Models::PredatorPreyPlant::PredatorPreyPlant::_flee_prey
private
Initial value:
= [this](const auto& cell) {
auto& state = cell->state;
if (state.prey.on_cell and state.predator.on_cell and
_empty_cell.clear();
for (const auto& nb : this->_cm.neighbors_of(cell)) {
if ( not nb->state.prey.on_cell
and not nb->state.predator.on_cell)
_empty_cell.push_back(nb);
}
if (_empty_cell.size() > 0) {
std::uniform_int_distribution<>
dist(0, _empty_cell.size() - 1);
auto nb_cell = _empty_cell[dist(*this->_rng)];
move_prey_to_nb_cell(cell, nb_cell);
}
}
return state;
}
CellContainer< Cell > neighbors_of(const Cell &cell) const
Retrieve the given cell's neighbors.
Definition: cell_manager.hh:458
const std::shared_ptr< RNG > _rng
The RNG shared between models.
Definition: model.hh:161
std::uniform_real_distribution< double > _prob_distr
Uniform real distribution [0, 1) for evaluating probabilities.
Definition: PredatorPreyPlant.hh:179
CellManager _cm
The cell manager.
Definition: PredatorPreyPlant.hh:155
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: PredatorPreyPlant.hh:266
CellContainer< Cell > _empty_cell
A container to temporarily accumulate empty neighbour cells.
Definition: PredatorPreyPlant.hh:169
double p_flee
Probability to flee from a predator if on the same cell.
Definition: species.hh:116

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

A prey should only flee if there is a predator on the cell, too. First empty neighboring cells are collected to which a prey can potentially flee. Choose a random cell out of the selection to flee to. If there is no empty cell nothing happens.

◆ _num_moves

std::size_t Utopia::Models::PredatorPreyPlant::PredatorPreyPlant::_num_moves
private

How many cells the movement rule should be applied to each time step.

◆ _params

SpeciesParams Utopia::Models::PredatorPreyPlant::PredatorPreyPlant::_params
private

All species-specific parameters.

◆ _prey_cell

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

A container to temporarily accumulate the prey neighbour cells.

◆ _prob_distr

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

Uniform real distribution [0, 1) for evaluating probabilities.

◆ _repro_cell

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

A container to temporarily accumulate neighbour cells for reproduction.

◆ _reproduce

Rule Utopia::Models::PredatorPreyPlant::PredatorPreyPlant::_reproduce
private

Define the reproduction rule.

If a neighboring cell if not already occupied by an individual of the same species, individuals reproduce with reproduction probabilities of predator and prey respectively. Plants reproduce according to their GrowthMode.

◆ _resource_cell

CellContainer<Cell> Utopia::Models::PredatorPreyPlant::PredatorPreyPlant::_resource_cell
private

A container to temporarily accumulate neighbour cells with mature plants.


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