|
| 1 | +/* |
| 2 | +* Copyright (C) 2020-2025 MEmilio |
| 3 | +* |
| 4 | +* Authors: Julia Bicker |
| 5 | +* |
| 6 | +* Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de> |
| 7 | +* |
| 8 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +* you may not use this file except in compliance with the License. |
| 10 | +* You may obtain a copy of the License at |
| 11 | +* |
| 12 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +* |
| 14 | +* Unless required by applicable law or agreed to in writing, software |
| 15 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +* See the License for the specific language governing permissions and |
| 18 | +* limitations under the License. |
| 19 | +*/ |
| 20 | +#ifndef ABSTRACT_PARAMETER_DISTRIBUTION_H |
| 21 | +#define ABSTRACT_PARAMETER_DISTRIBUTION_H |
| 22 | + |
| 23 | +#include "memilio/io/io.h" |
| 24 | +#include "memilio/utils/compiler_diagnostics.h" |
| 25 | +#include "memilio/utils/logging.h" |
| 26 | +#include "memilio/utils/random_number_generator.h" |
| 27 | +#include "parameter_distributions.h" |
| 28 | +#include <memory> |
| 29 | +#include <string> |
| 30 | + |
| 31 | +namespace mio |
| 32 | +{ |
| 33 | + |
| 34 | +/** |
| 35 | + * @brief This class represents an arbitrary ParameterDistribution. |
| 36 | + * @see mio::ParameterDistribution |
| 37 | + * This class can for instance be used for model parameters that should have an arbitrary distribution. |
| 38 | + */ |
| 39 | +class AbstractParameterDistribution |
| 40 | +{ |
| 41 | + |
| 42 | +public: |
| 43 | + /** |
| 44 | + * The implementation handed to the constructor should have get_sample function |
| 45 | + * overloaded with mio::RandomNumberGenerator and mio::abm::PersonalRandomNumberGenerator as input arguments |
| 46 | + */ |
| 47 | + template <class Impl> |
| 48 | + AbstractParameterDistribution(Impl&& dist) |
| 49 | + : m_dist(std::make_shared<Impl>(std::move(dist))) |
| 50 | + , sample_impl1([](void* d, RandomNumberGenerator& rng) { |
| 51 | + return static_cast<Impl*>(d)->get_sample(rng); |
| 52 | + }) |
| 53 | + , sample_impl2([](void* d, abm::PersonalRandomNumberGenerator& rng) { |
| 54 | + return static_cast<Impl*>(d)->get_sample(rng); |
| 55 | + }) |
| 56 | + { |
| 57 | + } |
| 58 | + |
| 59 | + AbstractParameterDistribution(AbstractParameterDistribution& other) |
| 60 | + : m_dist(other.m_dist) |
| 61 | + , sample_impl1(other.sample_impl1) |
| 62 | + , sample_impl2(other.sample_impl2) |
| 63 | + { |
| 64 | + } |
| 65 | + |
| 66 | + AbstractParameterDistribution(AbstractParameterDistribution&& other) |
| 67 | + : m_dist(other.m_dist) |
| 68 | + , sample_impl1(other.sample_impl1) |
| 69 | + , sample_impl2(other.sample_impl2) |
| 70 | + { |
| 71 | + } |
| 72 | + |
| 73 | + AbstractParameterDistribution(const AbstractParameterDistribution& other) |
| 74 | + : m_dist(other.m_dist) |
| 75 | + , sample_impl1(other.sample_impl1) |
| 76 | + , sample_impl2(other.sample_impl2) |
| 77 | + { |
| 78 | + } |
| 79 | + |
| 80 | + AbstractParameterDistribution() |
| 81 | + : m_dist(nullptr) |
| 82 | + , sample_impl1([](void* /*dist*/, RandomNumberGenerator& /*rng*/) { |
| 83 | + log_critical("AbstractParameterDistribution does not hold a distribution."); |
| 84 | + if (true) { |
| 85 | + exit(static_cast<int>(StatusCode::UnknownError)); |
| 86 | + } |
| 87 | + else { |
| 88 | + return -1.; |
| 89 | + } |
| 90 | + }) |
| 91 | + , sample_impl2([](void* /*dist*/, abm::PersonalRandomNumberGenerator& /*rng*/) { |
| 92 | + log_critical("AbstractParameterDistribution does not hold a distribution."); |
| 93 | + if (true) { |
| 94 | + exit(static_cast<int>(StatusCode::UnknownError)); |
| 95 | + } |
| 96 | + else { |
| 97 | + return -1.; |
| 98 | + } |
| 99 | + }) |
| 100 | + { |
| 101 | + } |
| 102 | + |
| 103 | + AbstractParameterDistribution& operator=(AbstractParameterDistribution&& other) = default; |
| 104 | + |
| 105 | + AbstractParameterDistribution& operator=(const AbstractParameterDistribution& other) = default; |
| 106 | + |
| 107 | + bool operator<(const AbstractParameterDistribution& other) const |
| 108 | + { |
| 109 | + return static_cast<ParameterDistribution*>(m_dist.get()) |
| 110 | + ->smaller_impl(*static_cast<ParameterDistribution*>(other.m_dist.get())); |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * @brief Returns a value sampled with the given distribution. |
| 115 | + * @param[in] rng RandomNumberGenerator used for sampling. |
| 116 | + */ |
| 117 | + double get(RandomNumberGenerator& rng) const |
| 118 | + { |
| 119 | + return sample_impl1(m_dist.get(), rng); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * @brief Returns a value sampled with the given distribution. |
| 124 | + * @param[in] rng abm::PersonalRandomNumberGenerator used for sampling. |
| 125 | + */ |
| 126 | + double get(abm::PersonalRandomNumberGenerator& rng) const |
| 127 | + { |
| 128 | + return sample_impl2(m_dist.get(), rng); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * @brief Get the parameters of the given distribution. |
| 133 | + */ |
| 134 | + std::vector<double> params() const |
| 135 | + { |
| 136 | + return static_cast<ParameterDistribution*>(m_dist.get())->params(); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * serialize an AbstractParameterDistribution. |
| 141 | + * @see mio::serialize |
| 142 | + */ |
| 143 | + template <class IOContext> |
| 144 | + void serialize(IOContext& io) const |
| 145 | + { |
| 146 | + static_cast<ParameterDistribution*>(m_dist.get())->serialize(io); |
| 147 | + } |
| 148 | + |
| 149 | +private: |
| 150 | + std::shared_ptr<void> m_dist; ///< Underlying distribtuion. |
| 151 | + double (*sample_impl1)( |
| 152 | + void*, |
| 153 | + RandomNumberGenerator&); ///< Sample function of the distribution which gets a RandomNumberGenerator as rng. |
| 154 | + double (*sample_impl2)( |
| 155 | + void*, |
| 156 | + abm:: |
| 157 | + PersonalRandomNumberGenerator&); ///< Sample function of the distribution which gets a abm::PersonalRandomNumberGenerator as rng. |
| 158 | +}; |
| 159 | + |
| 160 | +/** |
| 161 | + * deserialize a AbstractParameterDistribution. |
| 162 | + * @see mio::deserialize |
| 163 | + */ |
| 164 | +template <class IOContext> |
| 165 | +IOResult<AbstractParameterDistribution> deserialize_internal(IOContext& io, Tag<AbstractParameterDistribution>) |
| 166 | +{ |
| 167 | + auto obj = io.expect_object("ParameterDistribution"); |
| 168 | + auto type = obj.expect_element("Type", Tag<std::string>{}); |
| 169 | + if (type) { |
| 170 | + if (type.value() == "Uniform") { |
| 171 | + BOOST_OUTCOME_TRY(auto&& r, ParameterDistributionUniform::deserialize_elements(io, obj)); |
| 172 | + return mio::success(AbstractParameterDistribution(std::move(r))); |
| 173 | + } |
| 174 | + else if (type.value() == "Normal") { |
| 175 | + BOOST_OUTCOME_TRY(auto&& r, ParameterDistributionNormal::deserialize_elements(io, obj)); |
| 176 | + return mio::success(AbstractParameterDistribution(std::move(r))); |
| 177 | + } |
| 178 | + else if (type.value() == "LogNormal") { |
| 179 | + BOOST_OUTCOME_TRY(auto&& r, ParameterDistributionLogNormal::deserialize_elements(io, obj)); |
| 180 | + return mio::success(AbstractParameterDistribution(std::move(r))); |
| 181 | + } |
| 182 | + else if (type.value() == "Exponential") { |
| 183 | + BOOST_OUTCOME_TRY(auto&& r, ParameterDistributionExponential::deserialize_elements(io, obj)); |
| 184 | + return mio::success(AbstractParameterDistribution(std::move(r))); |
| 185 | + } |
| 186 | + else if (type.value() == "Constant") { |
| 187 | + BOOST_OUTCOME_TRY(auto&& r, ParameterDistributionConstant::deserialize_elements(io, obj)); |
| 188 | + return mio::success(AbstractParameterDistribution(std::move(r))); |
| 189 | + } |
| 190 | + else { |
| 191 | + return failure(StatusCode::InvalidValue, "Type of ParameterDistribution in AbstractParameterDistribution" + |
| 192 | + type.value() + " not valid."); |
| 193 | + } |
| 194 | + } |
| 195 | + return failure(type.error()); |
| 196 | +} |
| 197 | + |
| 198 | +} // namespace mio |
| 199 | + |
| 200 | +#endif //ABSTRACT_PARAMETER_DISTRIBUTION_H |
0 commit comments