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.);
43 const auto p_prey = get_as<double>(
"p_prey", cfg);
44 const auto p_predator = get_as<double>(
"p_predator", cfg);
48 if (dist(*
rng) < p_prey){
55 if (dist(*
rng) < p_predator){
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;
194 auto& nb_state = nb_cell->state;
196 nb_state.predator.on_cell =
true;
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;
210 auto& nb_state = nb_cell->state;
212 nb_state.prey.on_cell =
true;
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)) {
239 auto& nb_state = nb->state;
241 if ((nb_state.prey.on_cell) and (not nb_state.predator.on_cell)) {
246 else if (not nb_state.predator.on_cell) {
256 std::uniform_int_distribution<>
262 nb_cell->state.moved_predator=
true;
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) {
383 std::uniform_int_distribution<> dist(0,
_repro_cell.size()-1);
385 auto& nb_state = nb_cell->state;
389 nb_state.predator.on_cell =
true;
398 if ( state.prey.on_cell
405 if (not nb->state.prey.on_cell) {
412 std::uniform_int_distribution<> dist(0,
_repro_cell.size() - 1);
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,
454 ParentModel& parent_model,
458 Base(name, parent_model, custom_cfg),
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 ...");
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;
499 cell->state.predator.resources =
500 predator_init_resources;
503 cell->state.predator.resources = 0;
507 throw std::invalid_argument(
"While setting predator "
508 "positions, encountered an invalid value: "
513 this->
_log->info(
"Predator positions loaded.");
516 if (get_as<bool>(
"load_prey", cs_cfg)) {
517 this->
_log->info(
"Loading prey positions from file ...");
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;
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: "
538 this->
_log->info(
"Prey positions loaded.");
549 this->
_log->info(
"{} model fully set up.", this->
_name);
565 apply_rule<Update::async, Shuffle::off>
568 apply_rule<Update::async, Shuffle::on>
571 apply_rule<Update::async, Shuffle::on>
574 apply_rule<Update::async, Shuffle::off>
577 apply_rule<Update::async, Shuffle::on>
580 apply_rule<Update::sync>
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();
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) {
603 return std::pair{predator_sum / num_cells, prey_sum / num_cells};
606 this->
_monitor.set_entry(
"predator_density", pred_density);
607 this->
_monitor.set_entry(
"prey_density", prey_density);
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
const CellContainer< Cell > & cells() const
Return const reference to the managed CA cells.
Definition: cell_manager.hh:219
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
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
Monitor _monitor
The monitor.
Definition: model.hh:188
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
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
std::string to_string(const Config &node)
Given a config node, returns a string representation of it.
Definition: cfg_utils.hh:110
std::mt19937 rng
– Type definitions ----------------------------------------------------—
Definition: test_revision.cc:17
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