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

The GameOfLife Model. More...

#include <GameOfLife.hh>

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

Public Types

using Base = Model< GameOfLife, ModelTypes >
 The type of the Model base class of this derived class.
 
using DataGroup = typename Base::DataGroup
 Data type of the group to write model data to, holding datasets.
 
using DataSet = typename Base::DataSet
 Data type for a dataset.
 
using CellManager = Utopia::CellManager< CellTraits, GameOfLife >
 Type of the CellManager to use.
 
using NbLifeRule = std::unordered_set< unsigned short >
 Type of container to store the number of neighbors for the life rule.
 
using RuleFunc = typename CellManager::RuleFunc
 Extract the type of the rule function from the CellManager.
 
- Public Types inherited from Utopia::Model< GameOfLife, ModelTypes >
using Config = typename ModelTypes::Config
 Data type that holds the configuration.
 
using DataManager = DataIO::Default::DefaultDataManager< GameOfLife >
 The data manager to use, specialized with the derived model.
 
using DataGroup = typename ModelTypes::DataGroup
 Data type that is used for storing datasets.
 
using DataSet = typename ModelTypes::DataSet
 Data type that is used for storing data.
 
using RNG = typename ModelTypes::RNG
 Data type of the shared RNG.
 
using Space = typename ModelTypes::Space
 Data type of the space this model resides in.
 
using Time = typename ModelTypes::Time
 Data type for the model time.
 
using Monitor = typename ModelTypes::Monitor
 Data type for the monitor.
 
using MonitorManager = typename ModelTypes::MonitorManager
 Data type for the monitor manager.
 
using Level = typename ModelTypes::Level
 Data type for the hierarchical level.
 

Public Member Functions

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

Private Member Functions

NbLifeRule extract_birth_from_rule ()
 Extract the number of neighbors required for birth from the rule.
 
NbLifeRule extract_survive_from_rule ()
 Extract the number of neighbors required to survive from the rule.
 
double calculate_living_cell_density () const
 Calculate the mean of all cells' some_state.
 

Private Attributes

CellManager _cm
 The cell manager.
 
const std::string _rule
 The rule in Mirek's Cellebration notation.
 
const NbLifeRule _birth
 The number of neighbors required to get born.
 
const NbLifeRule _survive
 The number of neighbors required to survive.
 
std::shared_ptr< DataSet_dset_living
 A dataset for storing all cells living or dead status.
 
const RuleFunc _life_rule
 Implement the general life-like rule.
 

Additional Inherited Members

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

Detailed Description

The GameOfLife Model.

This model implement Conways Game of in the Utopia way.

Member Typedef Documentation

◆ Base

The type of the Model base class of this derived class.

◆ CellManager

Type of the CellManager to use.

◆ DataGroup

Data type of the group to write model data to, holding datasets.

◆ DataSet

Data type for a dataset.

◆ NbLifeRule

Type of container to store the number of neighbors for the life rule.

◆ RuleFunc

Extract the type of the rule function from the CellManager.

This is a function that receives a reference to a cell and returns the new cell state. For more details, check out Utopia::CellManager

Note
Whether the cell state gets applied directly or requires a synchronous update depends on the update mode specified in the cell traits.

Constructor & Destructor Documentation

◆ GameOfLife()

template<class ParentModel >
Utopia::Models::GameOfLife::GameOfLife::GameOfLife ( const std::string  name,
ParentModel parent 
)
inline

Construct the GameOfLife model.

Parameters
nameName of this model instance
parentThe parent model this model instance resides in
115 :
116 // Initialize first via base model
117 Base(name, parent),
118
119 // Now initialize the cell manager
120 _cm(*this),
121
122 // Initialize the rule and extract the number of neighbors required
123 // for birth and survival
124 _rule(get_as<std::string>("rule", this->_cfg)),
127
128 // Datasets
129 // For setting up datasets that store CellManager data, you can use the
130 // helper functions to take care of setting them up:
131 _dset_living(this->create_cm_dset("living", _cm))
132 {
133 // Select the cells that should be set to living
134 const auto living = this->_cm.select_cells(
135 get_as<DataIO::Config>("living", this->_cfg));
136 for (const auto& living_cell : living) {
137 living_cell->state.living = true;
138 }
139
140 // Initialization should be finished here.
141 this->_log->debug("{} model fully set up.", this->_name);
142 }
CellContainer< Cell > select_cells(Args &&... args) const
Select cells using the Utopia::select_entities interface.
Definition cell_manager.hh:342
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
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 NbLifeRule _birth
The number of neighbors required to get born.
Definition GameOfLife.hh:97
const NbLifeRule _survive
The number of neighbors required to survive.
Definition GameOfLife.hh:100
std::shared_ptr< DataSet > _dset_living
A dataset for storing all cells living or dead status.
Definition GameOfLife.hh:106
const std::string _rule
The rule in Mirek's Cellebration notation.
Definition GameOfLife.hh:94
NbLifeRule extract_birth_from_rule()
Extract the number of neighbors required for birth from the rule.
Definition GameOfLife.hh:147
Model< GameOfLife, ModelTypes > Base
The type of the Model base class of this derived class.
Definition GameOfLife.hh:60
CellManager _cm
The cell manager.
Definition GameOfLife.hh:91
NbLifeRule extract_survive_from_rule()
Extract the number of neighbors required to survive from the rule.
Definition GameOfLife.hh:163
Container select_entities(const Manager &mngr, const DataIO::Config &sel_cfg)
Select entities according to parameters specified in a configuration.
Definition select.hh:213

Member Function Documentation

◆ calculate_living_cell_density()

double Utopia::Models::GameOfLife::GameOfLife::calculate_living_cell_density ( ) const
inlineprivate

Calculate the mean of all cells' some_state.

186 {
187 double sum = 0.;
188 for (const auto& cell : _cm.cells()) {
189 sum += cell->state.living;
190 }
191 return sum / _cm.cells().size();
192 }
const CellContainer< Cell > & cells() const
Return const reference to the managed CA cells.
Definition cell_manager.hh:219

◆ extract_birth_from_rule()

NbLifeRule Utopia::Models::GameOfLife::GameOfLife::extract_birth_from_rule ( )
inlineprivate

Extract the number of neighbors required for birth from the rule.

148 {
149 const std::string delimiter = "/";
150 const std::string birth_s = _rule.substr(0, _rule.find(delimiter));
151
153 for (auto b_char : birth_s) {
154 // Subtracting '0' is needed to get from the ASCII number stored as
155 // char to the corresponding unsigned short value.
156 auto b = static_cast<unsigned short>(b_char - '0');
157 birth.emplace(b);
158 }
159 return birth;
160 }
std::unordered_set< unsigned short > NbLifeRule
Type of container to store the number of neighbors for the life rule.
Definition GameOfLife.hh:73

◆ extract_survive_from_rule()

NbLifeRule Utopia::Models::GameOfLife::GameOfLife::extract_survive_from_rule ( )
inlineprivate

Extract the number of neighbors required to survive from the rule.

164 {
165 // Get the substring specifying `survive` which is the substring that
166 // starts after the delimiter and ends with the end of the rule string.
167 const std::string delimiter = "/";
168 const auto pos_delimiter = _rule.find(delimiter);
169 const std::string survive_s =
170 _rule.substr(pos_delimiter + 1, _rule.size() - pos_delimiter);
171
173 for (auto b_char : survive_s) {
174 // Subtracting '0' is needed to get from the ASCII number stored as
175 // char to the corresponding unsigned short value.
176 auto b = static_cast<unsigned short>(b_char - '0');
177 survive.emplace(b);
178 }
179 return survive;
180 }

◆ monitor()

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

Monitor model information.

Here, functions and values can be supplied to the monitor that are then available to the frontend. The monitor() function is only called if a certain emit interval has passed; thus, the performance hit is small. With this information, you can then define stop conditions on frontend side, that can stop a simulation once a certain set of conditions is fulfilled.

251 {
252 this->_monitor.set_entry("living_cell_density",
254 }
Monitor _monitor
The monitor.
Definition model.hh:188
double calculate_living_cell_density() const
Calculate the mean of all cells' some_state.
Definition GameOfLife.hh:185

◆ perform_step()

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

Iterate a single step.

236 {
237 // Apply the rules to all cells, first the interaction, then the update
239 }
const RuleFunc _life_rule
Implement the general life-like rule.
Definition GameOfLife.hh:196

◆ write_data()

void Utopia::Models::GameOfLife::GameOfLife::write_data ( )
inline

Write data.

This function is called to write out data. The configuration determines the times at which it is invoked. See Utopia::DataIO::Dataset::write

262 {
263 // Write out the some_state of all cells
264 _dset_living->write(_cm.cells().begin(),
265 _cm.cells().end(),
266 [](const auto& cell) {
267 return static_cast<char>(cell->state.living);
268 });
269 }

Member Data Documentation

◆ _birth

const NbLifeRule Utopia::Models::GameOfLife::GameOfLife::_birth
private

The number of neighbors required to get born.

◆ _cm

CellManager Utopia::Models::GameOfLife::GameOfLife::_cm
private

The cell manager.

◆ _dset_living

std::shared_ptr<DataSet> Utopia::Models::GameOfLife::GameOfLife::_dset_living
private

A dataset for storing all cells living or dead status.

◆ _life_rule

const RuleFunc Utopia::Models::GameOfLife::GameOfLife::_life_rule
private

Implement the general life-like rule.

196 {
197 // Get the current state of the cell
198 auto state = cell->state;
199
200 // Calculate the number of living neighbors
201 auto num_living_nbs {0u};
202 for (auto nb : this->_cm.neighbors_of(cell)) {
203 if (nb->state.living) {
205 }
206 }
207
208 const bool survive =
209 std::find(_survive.begin(), _survive.end(), num_living_nbs) !=
210 _survive.end();
211 // Die if the number of living neighbors is not in the survival
212 // container
213 if (not survive) {
214 state.living = false;
215 }
216
217 // Give birth if the number of living neighbors is in the birth
218 // container
219 const bool birth =
220 std::find(_birth.begin(), _birth.end(), num_living_nbs) !=
221 _birth.end();
222 if (birth) {
223 state.living = true;
224 }
225
226 // Return the new cell state
227 return state;
228 };

◆ _rule

const std::string Utopia::Models::GameOfLife::GameOfLife::_rule
private

The rule in Mirek's Cellebration notation.

◆ _survive

const NbLifeRule Utopia::Models::GameOfLife::GameOfLife::_survive
private

The number of neighbors required to survive.


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