1 #ifndef UTOPIA_MODELS_ENVIRONMENT_ENVSTATEFUNCCOLLECTION_HH
2 #define UTOPIA_MODELS_ENVIRONMENT_ENVSTATEFUNCCOLLECTION_HH
7 namespace StateFunctionCollection {
18 template<
typename EnvModel,
class DistType,
19 class EnvStateFunc =
typename EnvModel::EnvStateFunc>
23 const std::string& param_name,
32 [&model, param_name, mode, dist{std::move(dist)}]
33 (
const auto& env_cell)
mutable {
34 auto& env_state = env_cell->state;
36 double current_value = 0.;
38 current_value = env_state.get_env(param_name);
40 const double rn = dist(*model.get_rng());
42 env_state.set_env(param_name, current_value + rn);
75 template<
typename EnvModel,
76 class EnvStateFunc =
typename EnvModel::EnvStateFunc>
77 std::pair<EnvStateFunc, Update>
esf_noise(
const EnvModel& model,
78 const std::string& param_name,
82 const auto distribution = get_as<std::string>(
"distribution", cfg);
86 if (distribution ==
"normal") {
87 const auto mean = get_as<double>(
"mean", cfg);
88 const auto stddev = get_as<double>(
"stddev", cfg);
89 std::normal_distribution<> dist(mean, stddev);
95 else if (distribution ==
"poisson") {
96 const auto mean = get_as<double>(
"mean", cfg);
97 std::poisson_distribution<> dist(mean);
103 else if (distribution ==
"exponential") {
104 const auto lambda = get_as<double>(
"lambda", cfg);
105 std::exponential_distribution<> dist(lambda);
111 else if (distribution ==
"uniform_int") {
112 auto interval = get_as<std::array<int, 2>>(
"interval", cfg);
113 std::uniform_int_distribution<> dist(interval[0], interval[1]);
119 else if (distribution ==
"uniform_real" or distribution ==
"uniform") {
120 auto interval = get_as<std::array<double, 2>>(
"interval", cfg);
121 std::uniform_real_distribution<> dist(interval[0], interval[1]);
128 throw std::invalid_argument(
"No method implemented to resolve "
129 "noise distribution '" + distribution +
"'! Valid options: "
130 "normal, poisson, uniform_int, uniform_real.");
149 template<
typename EnvModel,
typename Extent,
150 class EnvStateFunc =
typename EnvModel::EnvStateFunc>
151 std::pair<EnvStateFunc, Update>
esf_slope(
const EnvModel&,
152 const std::string& param_name,
154 const Extent& extent)
158 const auto values_north_south =
159 get_as<std::array<double, 2>>(
"values_north_south", cfg);
162 [param_name, mode, values_north_south, extent]
163 (
const auto& env_cell)
mutable
165 auto& env_state = env_cell->state;
168 const double pos = ( env_state.position[1] / extent[1]);
169 const double slope = values_north_south[0] - values_north_south[1];
170 const double value = values_north_south[1] + pos * slope;
172 double current_value = 0.;
174 current_value = env_state.get_env(param_name);
176 env_state.set_env(param_name, current_value + value);
199 template<
typename EnvModel,
200 class EnvStateFunc =
typename EnvModel::EnvStateFunc>
201 std::pair<EnvStateFunc, Update>
esf_steps(
const EnvModel&,
202 const std::string& param_name,
207 const auto latitudes =
208 get_as<std::vector<double>>(
"latitudes", cfg, {0.5});
209 const auto values_north_south =
210 get_as<std::vector<double>>(
"values_north_south", cfg);
212 if (latitudes.size() != values_north_south.size() - 1) {
213 throw std::invalid_argument(
"The list of 'latitudes' and"
214 " 'values_north_south' don't match in size. Sizes were "
217 ". Values_north_south must have one element more that"
222 [param_name, mode, latitudes, values_north_south]
223 (
const auto& env_cell)
mutable
225 auto& env_state = env_cell->state;
226 double value = values_north_south[0];
227 for (
unsigned int i = 0; i < latitudes.size(); ++i) {
228 if (env_state.position[1] > latitudes[i]) {
231 value = values_north_south[i+1];
234 double current_value = 0.;
236 current_value = env_state.get_env(param_name);
238 env_state.set_env(param_name, current_value + value);
258 template<
typename EnvModel,
259 class EnvStateFunc =
typename EnvModel::EnvStateFunc>
261 const std::string& param_name,
268 if (cfg.IsScalar()) {
271 value = cfg.as<
double>();
273 else if (cfg.IsMap()) {
275 value = get_as<double>(
"value", cfg);
278 throw std::invalid_argument(
"The configuration for environment "
279 "function 'uniform' must be a scalar or a mapping!");
283 [param_name, mode, value]
284 (
const auto& env_cell)
mutable
286 auto& env_state = env_cell->state;
288 double current_value = 0.;
290 current_value = env_state.get_env(param_name);
293 env_state.set_env(param_name, current_value + value);
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::pair< EnvStateFunc, Update > esf_uniform(const EnvModel &, const std::string ¶m_name, const Config &cfg)
Creates a rule function for spatially uniform parameter values.
Definition: env_state_func_collection.hh:260
std::pair< EnvStateFunc, Update > esf_steps(const EnvModel &, const std::string ¶m_name, const Config &cfg)
Creates a rule function for spatial steps in the parameter values.
Definition: env_state_func_collection.hh:201
ValMode extract_val_mode(const DataIO::Config &cfg, const std::string &context)
Given a configuration node, extract the value mode.
Definition: tools.hh:19
std::pair< EnvStateFunc, Update > esf_slope(const EnvModel &, const std::string ¶m_name, const Config &cfg, const Extent &extent)
Creates a rule function for spatially linearly parameter values.
Definition: env_state_func_collection.hh:151
ValMode
Value calculation mode.
Definition: tools.hh:10
std::pair< EnvStateFunc, Update > esf_noise(const EnvModel &model, const std::string ¶m_name, const Config &cfg)
Creates a rule function for noisy parameter values.
Definition: env_state_func_collection.hh:77
@ Set
Set a value, discarding the current state.
@ Add
Add the new value to the existing value.
@ sync
Synchronous update.
EnvStateFunc build_rng_env_state_func(EnvModel &model, DistType &&dist, const std::string ¶m_name, const ValMode &mode)
Create a rule function that uses a random number distribution.
Definition: env_state_func_collection.hh:21
DataIO::Config Config
Configuration node type alias.
Definition: env_state_func_collection.hh:10
Definition: env_param_func_collection.hh:8
unsigned int DistType
Type for distancens, i.e. intermediately long unsigned integers.
Definition: types.hh:37