1 #ifndef UTOPIA_MODELS_ENVIRONMENT_ENVPARAMFUNCCOLLECTION_HH
2 #define UTOPIA_MODELS_ENVIRONMENT_ENVPARAMFUNCCOLLECTION_HH
9 namespace ParameterFunctionCollection {
21 template<
typename EnvModel,
class DistType,
22 class EnvParamFunc =
typename EnvModel::EnvParamFunc>
26 const std::string& param_name,
35 [&model, param_name, mode, dist{std::move(dist)}] ()
mutable {
36 double current_value = 0.;
38 current_value = model.get_parameter(param_name);
40 const double rn = dist(*model.get_rng());
42 return current_value + rn;
71 template<
typename EnvModel,
72 class EnvParamFunc =
typename EnvModel::EnvParamFunc>
73 EnvParamFunc
epf_increment(
const EnvModel& model,
const std::string param_name,
76 const auto value = get_as<double>(
"value", cfg);
77 EnvParamFunc epf = [&model, param_name, value] ()
mutable
79 return model.get_parameter(param_name) + value;
104 template<
typename EnvModel,
105 class EnvParamFunc =
typename EnvModel::EnvParamFunc>
106 EnvParamFunc
epf_random(
const EnvModel& model,
const std::string& param_name,
110 const auto distribution = get_as<std::string>(
"distribution", cfg);
114 if (distribution ==
"normal") {
115 const auto mean = get_as<double>(
"mean", cfg);
116 const auto stddev = get_as<double>(
"stddev", cfg);
117 std::normal_distribution<> dist(mean, stddev);
122 else if (distribution ==
"poisson") {
123 const auto mean = get_as<double>(
"mean", cfg);
124 std::poisson_distribution<> dist(mean);
129 else if (distribution ==
"exponential") {
130 const auto lambda = get_as<double>(
"lambda", cfg);
131 std::exponential_distribution<> dist(lambda);
136 else if (distribution ==
"uniform_int") {
137 auto interval = get_as<std::array<int, 2>>(
"interval", cfg);
138 std::uniform_int_distribution<> dist(interval[0], interval[1]);
143 else if (distribution ==
"uniform_real" or distribution ==
"uniform") {
144 auto interval = get_as<std::array<double, 2>>(
"interval", cfg);
145 std::uniform_real_distribution<> dist(interval[0], interval[1]);
151 throw std::invalid_argument(
"No method implemented to resolve "
152 "noise distribution '" + distribution +
"'! Valid options: "
153 "normal, poisson, uniform_int, uniform_real.");
179 template<
typename EnvModel,
180 class EnvParamFunc =
typename EnvModel::EnvParamFunc>
183 using Time =
typename EnvModel::Time;
185 double min_value = get_as<double>(
"offset", cfg, 0.);
186 double max_value = get_as<double>(
"amplitude", cfg) + min_value;
187 Time period = get_as<Time>(
"period", cfg);
188 Time time_in_max = get_as<Time>(
"time_in_max", cfg, period / 2.);
189 double phase = get_as<double>(
"phase", cfg, 0) * period;
192 if (time_in_max > period) {
193 throw std::invalid_argument(
"The `time_in_max` argument cannot be "
194 "larger than the `period` argument in rectangular "
195 "environment parameter function!");
197 if (phase < 0 or phase > period) {
198 throw std::invalid_argument(
"The `phase` argument was not in interval "
203 Time time_start = extract_time_start<Time>(cfg);
206 EnvParamFunc epf = [&model, max_value, min_value,
207 period, time_in_max, phase, time_start] ()
mutable
209 auto time = (model.get_time()+1 - time_start) % period;
210 if (time >= phase and time < time_in_max + phase) {
234 template<
typename EnvModel,
235 class EnvParamFunc =
typename EnvModel::EnvParamFunc>
238 const auto value = get_as<double>(
"value", cfg);
239 EnvParamFunc epf = [value] ()
mutable
268 template<
typename EnvModel,
269 class EnvParamFunc =
typename EnvModel::EnvParamFunc>
272 using Time =
typename EnvModel::Time;
274 const auto period = get_as<double>(
"period", cfg);
275 const auto amplitude = get_as<double>(
"amplitude", cfg);
278 phase = get_as<double>(
"phase", cfg);
282 offset = get_as<double>(
"offset", cfg);
286 Time time_start = extract_time_start<Time>(cfg);
288 EnvParamFunc epf = [&model, period, amplitude,
289 phase, offset, time_start] ()
mutable
291 double x = (model.get_time() + 1 - time_start)/period * 2*M_PI;
292 return offset + amplitude * sin(x + phase * M_PI);
YAML::Node Config
Type of a variadic dictionary-like data structure used throughout Utopia.
Definition: types.hh:71
EnvParamFunc epf_sinusoidal(const EnvModel &model, const Config &cfg)
Creates a rule function for sinusoidal parameter values.
Definition: env_param_func_collection.hh:270
EnvParamFunc epf_random(const EnvModel &model, const std::string ¶m_name, const Config &cfg)
Creates a rule function for random parameter values.
Definition: env_param_func_collection.hh:106
EnvParamFunc epf_increment(const EnvModel &model, const std::string param_name, const Config &cfg)
Creates a rule function for incrementing parameter values.
Definition: env_param_func_collection.hh:73
ValMode extract_val_mode(const DataIO::Config &cfg, const std::string &context)
Given a configuration node, extract the value mode.
Definition: tools.hh:19
EnvParamFunc epf_rectangular(const EnvModel &model, const Config &cfg)
Creates a rule function for rectangular function like parameter values.
Definition: env_param_func_collection.hh:181
EnvParamFunc epf_set(const EnvModel &, const Config &cfg)
Creates a rule function for setting a parameter value.
Definition: env_param_func_collection.hh:236
ValMode
Value calculation mode.
Definition: tools.hh:10
@ Add
Add the new value to the existing value.
EnvParamFunc build_rng_env_param_func(const EnvModel &model, DistType &&dist, const std::string ¶m_name, const ValMode &mode)
Create a rule function that uses a random number distribution.
Definition: env_param_func_collection.hh:24
DataIO::Config Config
Configuration node type alias.
Definition: env_param_func_collection.hh:13
Definition: env_param_func_collection.hh:8
unsigned int DistType
Type for distancens, i.e. intermediately long unsigned integers.
Definition: types.hh:37