BioDynaMo  v1.05.159-3be850bb
log_range_param.h
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------
2 //
3 // Copyright (C) 2021 CERN & University of Surrey for the benefit of the
4 // BioDynaMo collaboration. All Rights Reserved.
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // See the LICENSE file distributed with this work for details.
10 // See the NOTICE file distributed with this work for additional information
11 // regarding copyright ownership.
12 //
13 // -----------------------------------------------------------------------------
14 
15 #ifndef CORE_MULTI_SIMULATION_OPTIMIZATION_PARAM_TYPE_LOG_RANGE_PARAM_H_
16 #define CORE_MULTI_SIMULATION_OPTIMIZATION_PARAM_TYPE_LOG_RANGE_PARAM_H_
17 
18 #include <cmath>
19 #include <string>
20 
22 #include "core/util/log.h"
23 
24 namespace bdm {
25 
28  LogRangeParam() = default;
29  LogRangeParam(const std::string& n, real_t base, real_t min, real_t max,
30  real_t stride)
32  base(base),
33  lower_bound(min),
34  upper_bound(max),
35  stride(stride) {
36  Validate();
37  };
38 
39  void Validate() const override {
40  if (lower_bound > upper_bound) {
41  Log::Fatal("LogRangeParam", "Tried to initialize parameter '", param_name,
42  "' with a lower_bound value higher than upper_bound: ",
43  lower_bound, " > ", upper_bound);
44  }
45  }
46 
47  OptimizationParamType* GetCopy() const override {
48  return new LogRangeParam(*this);
49  }
50 
51  // Get the nth value
52  real_t GetValue(int n) const override {
53  real_t exp = lower_bound + n * stride;
54  return exp > upper_bound ? std::pow(base, upper_bound)
55  : std::pow(base, exp);
56  }
57 
58  // Returns the number of discrete values that this range contains (including
59  // the `lower_bound` and `upper_bound` values)
60  uint32_t GetNumElements() const override {
61  return std::round(((upper_bound - lower_bound) + stride) / stride);
62  }
63 
64  // The base value
65  real_t base = 10;
66  // The minimum value
68  // THe maximum value
70  // The stride
73 };
74 
75 } // namespace bdm
76 
77 #endif // CORE_MULTI_SIMULATION_OPTIMIZATION_PARAM_TYPE_LOG_RANGE_PARAM_H_
static void Fatal(const std::string &location, const Args &... parts)
Prints fatal error message.
Definition: log.h:115
Definition: agent.cc:39
double real_t
Definition: real_t.h:21
A uniform range of logarithmically scaled values (e.g. 1, 10, 100, 1000)
uint32_t GetNumElements() const override
BDM_CLASS_DEF_OVERRIDE(LogRangeParam, 1)
LogRangeParam(const std::string &n, real_t base, real_t min, real_t max, real_t stride)
real_t GetValue(int n) const override
void Validate() const override
OptimizationParamType * GetCopy() const override
LogRangeParam()=default
An interface for creating different types of optimization parameters.