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.);
44 const auto p_plant = get_as<double>(
"p_plant", cfg);
45 const auto p_prey = get_as<double>(
"p_prey", cfg);
46 const auto p_predator = get_as<double>(
"p_predator", cfg);
48 auto cfg_prey = get_as<DataIO::Config>(
"prey", cfg);
49 auto cfg_predator = get_as<DataIO::Config>(
"predator", cfg);
53 const auto min_init_resources_prey = get_as<int>(
"min_init_resources",
55 const auto max_init_resources_prey = get_as<int>(
"max_init_resources",
57 const auto min_init_resources_predator =
58 get_as<int>(
"min_init_resources", cfg_predator);
59 const auto max_init_resources_predator =
60 get_as<int>(
"max_init_resources", cfg_predator);
61 if(max_init_resources_predator <
62 min_init_resources_predator){
63 throw::std::invalid_argument(
"The upper limit for the initial "
64 "predator resources needs to be higher than the lower limit.");
66 if(max_init_resources_prey <
67 min_init_resources_prey){
68 throw::std::invalid_argument(
"The upper limit for the initial "
69 "prey resources needs to be higher than the lower limit.");
76 if (dist(*
rng) < p_predator) {
78 std::uniform_int_distribution<> init_res_dist_pred(
79 min_init_resources_predator,
80 max_init_resources_predator
87 if (dist(*
rng) < p_prey) {
89 std::uniform_int_distribution<> init_res_dist_prey(
90 min_init_resources_prey,
91 max_init_resources_prey
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;
253 auto& nb_state = nb_cell->state;
255 nb_state.predator.on_cell =
true;
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;
269 auto& nb_state = nb_cell->state;
271 nb_state.prey.on_cell =
true;
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
294 and not nb->state.prey.on_cell
295 and not nb->state.predator.on_cell)
301 std::uniform_int_distribution<> dist(0,
_resource_cell.size() - 1);
310 if (not nb_cell->state.prey.on_cell) {
333 if ( nb->state.prey.on_cell
334 and not nb->state.predator.on_cell)
341 std::uniform_int_distribution<> dist_prey(0 ,
_prey_cell.size() - 1);
351 if (not nb_cell->state.predator.on_cell){
371 auto& state = cell->state;
374 unsigned int step = 0;
376 if (state.predator.on_cell) {
378 and not cell->state.prey.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
406 and not nb->state.predator.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
480 if (not nb_cell->state.predator.on_cell) {
481 nb_cell->state.predator.on_cell =
true;
490 if ( state.prey.on_cell
495 if (not nb_cell->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,
548 ParentModel& parent_model,
552 Base(name, parent_model, custom_cfg),
560 const auto f = get_as<double>(
"num_moves_fraction", this->
_cfg);
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);
604 const auto hdf5_file = get_as<std::string>(
"hdf5_file", cs_cfg);
606 if (get_as<bool>(
"load_predator", cs_cfg)) {
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;
620 const auto& predator_cfg =
621 get_as<Config>(
"predator",
622 _cfg[
"cell_manager"][
"cell_params"]);
623 int min_init_resources_predator =
624 get_as<int>(
"min_init_resources",
626 int max_init_resources_predator =
627 get_as<int>(
"max_init_resources",
629 std::uniform_int_distribution<>
630 init_res_dist_predator(
631 min_init_resources_predator,
632 max_init_resources_predator
634 cell->state.predator.resources =
635 init_res_dist_predator(*this->
_rng);
638 cell->state.predator.resources = 0;
642 throw std::invalid_argument(
"While setting predator "
643 "positions, encountered an invalid value: "
647 this->
_log->info(
"Predator positions loaded.");
650 if (get_as<bool>(
"load_prey", cs_cfg)) {
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;
660 const auto& prey_cfg =
661 get_as<Config>(
"prey",
662 _cfg[
"cell_manager"][
"cell_params"]);
663 int min_init_resources_prey =
664 get_as<int>(
"min_init_resources", prey_cfg);
665 int max_init_resources_prey =
666 get_as<int>(
"max_init_resources", prey_cfg);
667 std::uniform_int_distribution<>
669 min_init_resources_prey,
670 max_init_resources_prey
672 cell->state.prey.resources =
673 init_res_dist_prey(*this->
_rng);
676 cell->state.prey.resources = 0;
680 throw std::invalid_argument(
"While setting prey "
681 "positions, encountered an invalid value: "
685 this->
_log->info(
"Prey positions loaded.");
688 if (get_as<bool>(
"load_plant", cs_cfg)) {
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: "
702 this->
_log->info(
"Plant positions loaded.");
723 for (std::size_t i = 0; i <
_num_moves; ++i) {
728 apply_rule<Update::async, Shuffle::off>(
_eat,
_cm.
cells());
736 auto [pred_density, prey_density, plant_density] = [
this](){
737 double predator_sum = 0.;
738 double prey_sum = 0.;
739 double plant_sum = 0.;
740 double num_cells = this->_cm.
cells().size();
742 for (
const auto& cell : this->_cm.
cells()) {
743 auto state = cell->state;
745 if (state.prey.on_cell) prey_sum++;
746 if (state.predator.on_cell) predator_sum++;
747 if (state.plant.on_cell) plant_sum++;
749 return std::tuple{predator_sum / num_cells,
750 prey_sum / num_cells,
751 plant_sum / num_cells};
754 this->
_monitor.set_entry(
"predator_density", pred_density);
755 this->
_monitor.set_entry(
"prey_density", prey_density);
756 this->
_monitor.set_entry(
"plant_density", plant_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
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
typename ModelTypes::Config Config
Data type that holds the configuration.
Definition: model.hh:116
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
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
std::shared_ptr< Cell > get_random_neighbor(const CellContainer< Cell > &nbs) const
Returns a random neighbor.
Definition: PredatorPreyPlant.hh:241
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
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 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.
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