1#ifndef UTOPIA_MODELS_PREDATORPREY_HH
2#define UTOPIA_MODELS_PREDATORPREY_HH
33 template<
class RNGType>
40 std::uniform_real_distribution<double>
dist(0., 1.);
93 :
public Model<PredatorPrey, ModelTypes>
160 auto& state =
cell->state;
165 state.predator.resources =
166 std::clamp( state.predator.resources
169 state.prey.resources =
170 std::clamp( state.prey.resources
175 if (state.predator.on_cell
and state.predator.resources <= 0.) {
176 state.predator.on_cell =
false;
180 if (state.prey.on_cell
and state.prey.resources <= 0.) {
181 state.prey.on_cell =
false;
192 const std::shared_ptr<Cell>&
nb_cell) {
193 auto& state =
cell->state;
197 nb_state.predator.resources = state.predator.resources;
199 state.predator.on_cell =
false;
200 state.predator.resources = 0.;
208 const std::shared_ptr<Cell>&
nb_cell) {
209 auto& state =
cell->state;
213 nb_state.prey.resources = state.prey.resources;
215 state.prey.on_cell =
false;
216 state.prey.resources = 0.;
227 auto& state =
cell->state;
230 if ((state.predator.on_cell)
and (
not state.moved_predator)) {
256 std::uniform_int_distribution<>
262 nb_cell->state.moved_predator=
true;
267 else if (_empty_cell.size() > 0) {
270 std::uniform_int_distribution<>
277 nb_cell->state.moved_predator=
true;
289 auto& state =
cell->state;
291 if (state.prey.on_cell
and state.predator.on_cell) {
297 if ( (
not nb->state.prey.on_cell)
298 and (
not nb->state.predator.on_cell)) {
306 std::uniform_int_distribution<>
329 auto& state =
cell->state;
332 if (state.predator.on_cell
and state.prey.on_cell) {
335 state.predator.resources = std::clamp(
336 state.predator.resources
342 state.prey.on_cell =
false;
343 state.prey.resources = 0.;
347 else if (state.prey.on_cell ==
true) {
350 state.prey.resources = std::clamp(
365 auto& state =
cell->state;
368 if ( state.predator.on_cell
375 if (
not nb->state.predator.on_cell) {
398 if ( state.prey.on_cell
405 if (
not nb->state.prey.on_cell) {
419 nb_cell->state.prey.on_cell =
true;
434 auto& state =
cell->state;
435 state.moved_predator =
false;
451 template <
class ParentModel>
453 const std::string& name,
480 if (this->
_cfg[
"cell_states_from_file"]) {
481 const auto&
cs_cfg = this->
_cfg[
"cell_states_from_file"];
484 this->
_cfg[
"cell_manager"][
"cell_params"][
"predator"]);
486 this->
_cfg[
"cell_manager"][
"cell_params"][
"prey"]);
488 this->
_log->info(
"Loading predator positions from file ...");
496 if (on_cell == 0
or on_cell == 1) {
497 cell->state.predator.on_cell = on_cell;
499 cell->state.predator.resources =
503 cell->state.predator.resources = 0;
507 throw std::invalid_argument(
"While setting predator "
508 "positions, encountered an invalid value: "
509 + std::to_string(on_cell) +
". Allowed: 0 or 1.");
513 this->
_log->info(
"Predator positions loaded.");
517 this->
_log->info(
"Loading prey positions from file ...");
521 if (on_cell == 0
or on_cell == 1) {
522 cell->state.prey.on_cell = on_cell;
524 cell->state.prey.resources =
528 cell->state.prey.resources = 0;
532 throw std::invalid_argument(
"While setting prey "
533 "positions, encountered an invalid value: "
534 + std::to_string(on_cell) +
". Allowed: 0 or 1.");
538 this->
_log->info(
"Prey positions loaded.");
549 this->
_log->info(
"{} model fully set up.", this->
_name);
590 double num_cells = this->_cm.
cells().size();
592 for (
const auto&
cell : this->_cm.
cells()) {
593 auto state =
cell->state;
595 if (state.prey.on_cell) {
599 if (state.predator.on_cell) {
622 [](
const auto&
cell) {
623 return static_cast<char>(cell->state.predator.on_cell);
629 [](
const auto&
cell) {
630 return static_cast<char>(cell->state.prey.on_cell);
636 [](
const auto&
cell) {
637 return cell->state.predator.resources;
643 [](
const auto&
cell) {
644 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
PredatorPrey Model on grid cells.
Definition PredatorPrey.hh:94
void perform_step()
Perform an iteration step.
Definition PredatorPrey.hh:564
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
typename Base::DataSet DataSet
Data type for a dataset.
Definition PredatorPrey.hh:100
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
Rule _flee_prey
Define the movement rule of prey.
Definition PredatorPrey.hh:288
void monitor()
Monitor model information.
Definition PredatorPrey.hh:585
const std::shared_ptr< DataSet > _dset_predator
Dataset of Predator locations on the grid.
Definition PredatorPrey.hh:143
Rule _cost_of_living
Cost of Living.
Definition PredatorPrey.hh:158
std::uniform_real_distribution< double > _prob_distr
Definition PredatorPrey.hh:135
Rule _reset_predator_movement
Resets the movement flag of predators to "false" for next turn.
Definition PredatorPrey.hh:433
const std::shared_ptr< DataSet > _dset_prey
Dataset of Prey locations on the grid.
Definition PredatorPrey.hh:140
void write_data()
Write data.
Definition PredatorPrey.hh:619
const std::shared_ptr< DataSet > _dset_resource_predator
Dataset of Predator resources on the grid.
Definition PredatorPrey.hh:149
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
Rule _repro
Define the reproduction rule.
Definition PredatorPrey.hh:363
typename CellManager::Cell Cell
The type of a cell.
Definition PredatorPrey.hh:106
CellContainer< Cell > _repro_cell
A container to temporarily accumulate neighbour cells for reproduction.
Definition PredatorPrey.hh:132
PredatorPrey(const std::string &name, ParentModel &parent_model, const DataIO::Config &custom_cfg={})
Construct the PredatorPrey model.
Definition PredatorPrey.hh:452
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
Rule _eat
Define the eating rule.
Definition PredatorPrey.hh:327
CellManager _cm
The cell manager.
Definition PredatorPrey.hh:118
Rule _move_predator
Define the movement rule of predator.
Definition PredatorPrey.hh:225
CellContainer< Cell > _prey_cell
A container to temporarily accumulate the prey neighbour cells.
Definition PredatorPrey.hh:126
typename CellManager::RuleFunc Rule
Type of the update rules.
Definition PredatorPrey.hh:109
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 PredatorPrey model.
Definition PredatorPrey.hh:72
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 p_flee
Probability to flee from a predator if on the same cell.
Definition species.hh:87
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
The parameter of all species.
Definition species.hh:106
PredatorParams predator
Predator parameters.
Definition species.hh:111
PreyParams prey
Prey parameters.
Definition species.hh:108
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
Cell State struct.
Definition PredatorPrey.hh:22
State(const DataIO::Config &cfg, const std::shared_ptr< RNGType > &rng)
Construct a cell state with the use of a RNG.
Definition PredatorPrey.hh:34
bool moved_predator
Flag to indicate if the predator on this cell has already moved
Definition PredatorPrey.hh:30
SpeciesState prey
The state a prey on this cell has.
Definition PredatorPrey.hh:27
SpeciesState predator
The state a predator on this cell has.
Definition PredatorPrey.hh:24