1#ifndef UTOPIA_MODELS_PREDATORPREYPLANT_HH 
    2#define UTOPIA_MODELS_PREDATORPREYPLANT_HH 
   34    template<
class RNGType>
 
   41        std::uniform_real_distribution<double> 
dist(0., 1.);
 
   63            throw::std::invalid_argument(
"The upper limit for the initial " 
   64            "predator resources needs to be higher than the lower limit.");
 
   68            throw::std::invalid_argument(
"The upper limit for the initial " 
   69            "prey resources needs to be higher than the lower limit.");
 
 
 
  130    : 
public Model<PredatorPreyPlant, ModelTypes>
 
  211        auto& state = 
cell->state;
 
  216        state.predator.resources =
 
  217            std::clamp(  state.predator.resources
 
  220        state.prey.resources =
 
  221            std::clamp(  state.prey.resources 
 
  226        if (state.predator.on_cell 
and state.predator.resources <= 0.)
 
  227            state.predator.on_cell = 
false;
 
  230        if (state.prey.on_cell 
and state.prey.resources <= 0.) 
 
  231            state.prey.on_cell = 
false;
 
 
  240    std::shared_ptr<Cell>
 
  243        return nbs[std::uniform_int_distribution<std::size_t>(0, 
nbs.size() - 1)(*this->
_rng)];
 
 
  251                                  const std::shared_ptr<Cell>& 
nb_cell) {
 
  252        auto& state = 
cell->state;
 
  256        nb_state.predator.resources = state.predator.resources;
 
  258        state.predator.on_cell = 
false;
 
  259        state.predator.resources = 0.;
 
 
  267                              const std::shared_ptr<Cell>& 
nb_cell) {
 
  268        auto& state = 
cell->state;
 
  272        nb_state.prey.resources = state.prey.resources;
 
  274        state.prey.on_cell = 
false;
 
  275        state.prey.resources = 0.;
 
 
  293            if (
nb->state.plant.on_cell 
 
 
  333            if (    
nb->state.prey.on_cell
 
 
  371        auto& state = 
cell->state;
 
  374        unsigned int step = 0;
 
  376        if (state.predator.on_cell) {
 
  383        else if (state.prey.on_cell 
and not cell->state.plant.on_cell){
 
 
  398        auto& state = 
cell->state;
 
  400        if (state.prey.on_cell 
and state.predator.on_cell 
and  
  405                if (    
not nb->state.prey.on_cell
 
  413                std::uniform_int_distribution<> 
 
 
  432        auto& state = 
cell->state;
 
  435        if (state.predator.on_cell 
and state.prey.on_cell) {
 
  437            state.predator.resources =
 
  438                std::clamp(  state.predator.resources 
 
  443            state.prey.on_cell = 
false;
 
  444            state.prey.resources = 0.;
 
  448        else if (state.prey.on_cell 
and state.plant.on_cell) {
 
  450            state.prey.resources =
 
  451                std::clamp(  state.prey.resources 
 
  456                state.plant.on_cell = 
false;
 
  457                state.plant.regeneration_counter = 0;
 
 
  471        auto& state = 
cell->state;
 
  474        if (    state.predator.on_cell
 
  476            and (   state.predator.resources
 
  481                nb_cell->state.predator.on_cell = 
true;
 
  490        if (    state.prey.on_cell
 
  496                nb_cell->state.prey.on_cell = 
true;
 
  506        if (
not state.plant.on_cell) {
 
  511                if (   state.plant.regeneration_counter
 
  515                    state.plant.on_cell = 
true;
 
  518                    state.plant.regeneration_counter++;
 
  524                    state.plant.on_cell = 
true;
 
 
  545    template <
class ParentModel>
 
  547        const std::string& name,
 
  561            return f * this->_cm.
cells().size();
 
  581        this->
_log->info(
"The movement rule will be applied {} times each " 
  582                         "time step.", _num_moves);
 
  585        if (this->
_cfg[
"cell_states_from_file"]) {
 
  596        this->
_log->info(
"{} model fully set up.", this->
_name);
 
 
  607            this->
_log->info(
"Loading predator positions from file ...");
 
  614                [
this](
auto& 
cell, 
const int on_cell){
 
  615                    if (on_cell == 0 
or on_cell == 1) {
 
  617                        cell->state.predator.on_cell = on_cell;
 
  622                                _cfg[
"cell_manager"][
"cell_params"]);
 
  629                            std::uniform_int_distribution<> 
 
  634                            cell->state.predator.resources = 
 
  638                            cell->state.predator.resources = 0;
 
  642                    throw std::invalid_argument(
"While setting predator " 
  643                        "positions, encountered an invalid value: " 
  644                        + std::to_string(on_cell) + 
". Allowed: 0 or 1.");
 
  647            this->
_log->info(
"Predator positions loaded.");
 
  651            this->
_log->info(
"Loading prey positions from file ...");
 
  654                [
this](
auto& 
cell, 
const int on_cell){
 
  655                    if (on_cell == 0 
or on_cell == 1) {
 
  657                        cell->state.prey.on_cell = on_cell;
 
  662                                _cfg[
"cell_manager"][
"cell_params"]);
 
  667                            std::uniform_int_distribution<> 
 
  672                            cell->state.prey.resources = 
 
  676                            cell->state.prey.resources = 0;
 
  680                    throw std::invalid_argument(
"While setting prey " 
  681                        "positions, encountered an invalid value: " 
  682                        + std::to_string(on_cell) + 
". Allowed: 0 or 1.");
 
  685            this->
_log->info(
"Prey positions loaded.");
 
  689            this->
_log->info(
"Loading plant positions from file ...");
 
  692                [](
auto& 
cell, 
const int on_cell){
 
  693                    if (on_cell == 0 
or on_cell == 1) {
 
  694                        cell->state.plant.on_cell = on_cell;
 
  697                    throw std::invalid_argument(
"While setting plant " 
  698                        "positions, encountered an invalid value: " 
  699                        + std::to_string(on_cell) + 
". Allowed: 0 or 1.");
 
  702            this->
_log->info(
"Plant positions loaded.");
 
 
  740            double num_cells = this->_cm.
cells().size();
 
  742            for (
const auto& 
cell : this->_cm.
cells()) {
 
  743                auto state = 
cell->state;
 
  755        this->
_monitor.set_entry(
"prey_density", prey_density);
 
 
  771            [](
const auto& 
cell) {                
 
  772                return static_cast<char>(cell->state.predator.on_cell);
 
  778            [](
const auto& 
cell) {                
 
  779                return static_cast<char>(cell->state.prey.on_cell);
 
  785            [](
const auto& 
cell) {
 
  786                return static_cast<char>(cell->state.plant.on_cell);
 
  792            [](
const auto& 
cell) {
 
  793                return cell->state.predator.resources;
 
  799            [](
const auto& 
cell) {
 
  800                return cell->state.prey.resources;
 
 
 
 
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
 
typename std::function< CellState(const std::shared_ptr< Cell > &)> RuleFunc
The type of a rule function acting on cells of this cell manager.
Definition cell_manager.hh:84
 
auto nb_size() const
Return the (maximum) size of the currently selected neighborhood.
Definition cell_manager.hh:450
 
CellContainer< Cell > neighbors_of(const Cell &cell) const
Retrieve the given cell's neighbors.
Definition cell_manager.hh:458
 
const CellContainer< Cell > & cells() const
Return const reference to the managed CA cells.
Definition cell_manager.hh:219
 
Utopia::Cell< CellTraits > Cell
Type of the managed cells.
Definition cell_manager.hh:47
 
Base class interface for Models using the CRT Pattern.
Definition model.hh:112
 
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
 
Monitor _monitor
The monitor.
Definition model.hh:188
 
typename ModelTypes::DataSet DataSet
Data type that is used for storing data.
Definition model.hh:125
 
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
 
const std::shared_ptr< RNG > _rng
The RNG shared between models.
Definition model.hh:161
 
PredatorPreyPlant Model on grid cells.
Definition PredatorPreyPlant.hh:131
 
std::shared_ptr< Cell > get_random_neighbor(const CellContainer< Cell > &nbs) const
Returns a random neighbor.
Definition PredatorPreyPlant.hh:241
 
CellContainer< Cell > _repro_cell
A container to temporarily accumulate neighbour cells for reproduction.
Definition PredatorPreyPlant.hh:172
 
std::uniform_real_distribution< double > _prob_distr
Uniform real distribution [0, 1) for evaluating probabilities.
Definition PredatorPreyPlant.hh:179
 
const std::shared_ptr< DataSet > _dset_plant
Dataset of Plant resources.
Definition PredatorPreyPlant.hh:199
 
const std::shared_ptr< DataSet > _dset_resource_prey
Dataset of Prey resources on the grid.
Definition PredatorPreyPlant.hh:193
 
Rule _flee_prey
If a prey is on the cell, determine whether it may flee and where to.
Definition PredatorPreyPlant.hh:397
 
const std::shared_ptr< DataSet > _dset_predator
Dataset of Predator locations on the grid.
Definition PredatorPreyPlant.hh:190
 
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 PredatorPreyPlant.hh:250
 
auto move_prey(std::shared_ptr< Cell > cell)
Move the prey looking for resources.
Definition PredatorPreyPlant.hh:287
 
void perform_step()
Perform an iteration step.
Definition PredatorPreyPlant.hh:718
 
typename Base::DataSet DataSet
Data type for a dataset.
Definition PredatorPreyPlant.hh:137
 
CellManager _cm
The cell manager.
Definition PredatorPreyPlant.hh:155
 
CellContainer< Cell > _resource_cell
A container to temporarily accumulate neighbour cells with mature plants.
Definition PredatorPreyPlant.hh:175
 
PredatorPreyPlant(const std::string &name, ParentModel &parent_model, const DataIO::Config &custom_cfg={})
Construct the PredatorPreyPlant model.
Definition PredatorPreyPlant.hh:546
 
std::uniform_int_distribution _cm_dist
Distribution for randomly selecting a cell in your cellmanager.
Definition PredatorPreyPlant.hh:182
 
void monitor()
Monitor model information.
Definition PredatorPreyPlant.hh:734
 
const std::shared_ptr< DataSet > _dset_prey
Dataset of Prey locations on the grid.
Definition PredatorPreyPlant.hh:187
 
typename CellManager::RuleFunc Rule
Type of the update rules.
Definition PredatorPreyPlant.hh:146
 
Model< PredatorPreyPlant, ModelTypes > Base
The base model.
Definition PredatorPreyPlant.hh:134
 
void setup_cell_states_from_file(const Config &cs_cfg)
Sets predator, prey, and plant positions from loaded HDF5 data.
Definition PredatorPreyPlant.hh:603
 
CellContainer< Cell > _prey_cell
A container to temporarily accumulate the prey neighbour cells.
Definition PredatorPreyPlant.hh:166
 
auto move_predator(std::shared_ptr< Cell > cell)
Move the predator looking for preys.
Definition PredatorPreyPlant.hh:326
 
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
 
void move_entities(std::shared_ptr< Cell > cell)
Define the movement rule of an individual.
Definition PredatorPreyPlant.hh:370
 
std::size_t _num_moves
How many cells the movement rule should be applied to each time step.
Definition PredatorPreyPlant.hh:162
 
Rule _reproduce
Define the reproduction rule.
Definition PredatorPreyPlant.hh:470
 
const std::shared_ptr< DataSet > _dset_resource_predator
Dataset of Predator resources on the grid.
Definition PredatorPreyPlant.hh:196
 
void write_data()
Write data.
Definition PredatorPreyPlant.hh:768
 
Rule _cost_of_living
Cost of Living.
Definition PredatorPreyPlant.hh:209
 
SpeciesParams _params
All species-specific parameters.
Definition PredatorPreyPlant.hh:159
 
typename CellManager::Cell Cell
The type of a cell.
Definition PredatorPreyPlant.hh:143
 
Rule _eat
Define the eating rule.
Definition PredatorPreyPlant.hh:431
 
CellContainer< Cell > _empty_cell
A container to temporarily accumulate empty neighbour cells.
Definition PredatorPreyPlant.hh:169
 
YAML::Node Config
Type of a variadic dictionary-like data structure used throughout Utopia.
Definition types.hh:71
 
Container select_entities(const Manager &mngr, const DataIO::Config &sel_cfg)
Select entities according to parameters specified in a configuration.
Definition select.hh:213
 
ModelTypes<> ModelTypes
Typehelper to define data types of PredatorPreyPlant model.
Definition PredatorPreyPlant.hh:110
 
@ none
Plant level is ignored; prey are always able to eat.
 
@ stochastic
Once eaten, a plant regrows with probability regen_prob
 
@ deterministic
Once eaten, a plant requires regen_time time to regenerate.
 
DataIO::Config Config
Type of a variadic dictionary-like data structure used throughout Utopia.
Definition types.hh:80
 
EntityContainer< CellType > CellContainer
Type of the variably sized container for cells.
Definition types.hh:26
 
The entity traits struct gathers types to be used for specializing an entity.
Definition entity.hh:49
 
double resource_intake
Resource intake from eating.
Definition species.hh:33
 
double repro_cost
Cost of reproduction.
Definition species.hh:43
 
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
 
double repro_resource_requ
Minimal reproduction resources requirements.
Definition species.hh:36
 
double repro_prob
Reproduction probability.
Definition species.hh:46
 
Struct that holds all species states.
Definition species.hh:11
 
double resources
The internal resources reservoir.
Definition species.hh:16
 
bool on_cell
Whether the species is on the cell.
Definition species.hh:13
 
const unsigned int regen_time
The deterministic regeneration time.
Definition species.hh:44
 
const GrowthModel growth_model
The growth model of the plant.
Definition species.hh:41
 
const double regen_prob
The regeneration probability, evaluated each time step.
Definition species.hh:47
 
Struct that holds all plant characterising states.
Definition species.hh:12
 
bool on_cell
Whether a plant is on the cell.
Definition species.hh:14
 
double p_flee
Probability to flee from a predator if on the same cell.
Definition species.hh:116
 
unsigned int move_limit
Movement limit.
Definition species.hh:75
 
The parameter of all species.
Definition species.hh:143
 
PreyParams prey
Prey parameters.
Definition species.hh:145
 
PlantParams plant
Plant parameters.
Definition species.hh:151
 
PredatorParams predator
Predator parameters.
Definition species.hh:148
 
Cell state, combining states for predator, prey and plant species.
Definition PredatorPreyPlant.hh:23
 
PredatorPrey::SpeciesState prey
The state a prey on this cell has.
Definition PredatorPreyPlant.hh:28
 
PlantState plant
The state a plant on this cell has.
Definition PredatorPreyPlant.hh:31
 
State(const DataIO::Config &cfg, const std::shared_ptr< RNGType > &rng)
Construct a cell state with the use of a RNG.
Definition PredatorPreyPlant.hh:35
 
PredatorPrey::SpeciesState predator
The state a predator on this cell has.
Definition PredatorPreyPlant.hh:25