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