BioDynaMo  v1.05.119-a4ff3934
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_RANGE_PARAM_H_
16 #define CORE_MULTI_SIMULATION_OPTIMIZATION_PARAM_TYPE_RANGE_PARAM_H_
17 
18 #include <string>
19 
21 #include "core/util/log.h"
22 
23 namespace bdm {
24 
28  RangeParam() = default;
29  RangeParam(const std::string& name, real_t min, real_t max, real_t stride)
30  : OptimizationParamType(name),
31  lower_bound(min),
32  upper_bound(max),
33  stride(stride) {
34  Validate();
35  };
36 
37  void Validate() const override {
38  if (lower_bound > upper_bound) {
39  Log::Fatal("RangeParam", "Tried to initialize parameter '", param_name,
40  "' with a lower_bound value higher than upper_bound: ",
41  lower_bound, " > ", upper_bound);
42  }
43  }
44 
45  OptimizationParamType* GetCopy() const override {
46  return new RangeParam(*this);
47  }
48 
49  // Get the nth value
50  real_t GetValue(int n) const override {
51  real_t curr = lower_bound + n * stride;
52  return curr > upper_bound ? upper_bound : curr;
53  }
54 
55  // Returns the number of discrete values that this range contains (including
56  // the `lower_bound` and `upper_bound` values)
57  uint32_t GetNumElements() const override {
58  return std::round(((upper_bound - lower_bound) + stride) / stride);
59  }
60 
61  // The minimum value
63  // THe maximum value
65  // The stride
68 };
69 
70 } // namespace bdm
71 
72 #endif // CORE_MULTI_SIMULATION_OPTIMIZATION_PARAM_TYPE_RANGE_PARAM_H_
bdm::RangeParam::GetNumElements
uint32_t GetNumElements() const override
Definition: range_param.h:57
bdm::RangeParam::RangeParam
RangeParam(const std::string &name, real_t min, real_t max, real_t stride)
Definition: range_param.h:29
optimization_param_type.h
bdm
Definition: agent.cc:39
bdm::real_t
double real_t
Definition: real_t.h:21
bdm::OptimizationParamType
An interface for creating different types of optimization parameters.
Definition: optimization_param_type.h:27
bdm::OptimizationParamType::param_name
std::string param_name
Definition: optimization_param_type.h:52
bdm::RangeParam::GetCopy
OptimizationParamType * GetCopy() const override
Definition: range_param.h:45
bdm::RangeParam::lower_bound
real_t lower_bound
Definition: range_param.h:62
bdm::RangeParam::RangeParam
RangeParam()=default
bdm::RangeParam::Validate
void Validate() const override
Definition: range_param.h:37
bdm::RangeParam::stride
real_t stride
Definition: range_param.h:66
bdm::Log::Fatal
static void Fatal(const std::string &location, const Args &... parts)
Prints fatal error message.
Definition: log.h:115
log.h
bdm::RangeParam::BDM_CLASS_DEF_OVERRIDE
BDM_CLASS_DEF_OVERRIDE(RangeParam, 1)
bdm::RangeParam
Definition: range_param.h:27
bdm::RangeParam::GetValue
real_t GetValue(int n) const override
Definition: range_param.h:50
bdm::RangeParam::upper_bound
real_t upper_bound
Definition: range_param.h:64