-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1081 rework ABM state transitions #1107
base: main
Are you sure you want to change the base?
Changes from all commits
3fcb877
3113609
cb2a563
59d10be
25e7905
7d92aa8
a4f16e8
64f2edf
8b995e2
e74c26c
e5afe7c
c7d6f12
5100fb8
85ab275
10bd31e
4a47c69
6f489f6
054b746
e9069a4
223e281
21c3753
1c36293
6f73cf4
19a48c7
eec7073
fb155bf
f95561e
b03f75d
af6111c
92a2034
32f3a00
dd13ea9
af643b9
e16b36d
9b4a51b
76fa145
87619a1
d072b6b
9efad16
1a06e61
a1772b1
be07ec6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* Copyright (C) 2020-2024 MEmilio | ||
* | ||
* Authors: Julia Bicker | ||
* | ||
* Contact: Martin J. Kuehn <[email protected]> | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#ifndef PARAMETER_DISTRIBUTION_WRAPPER_H | ||
#define PARAMETER_DISTRIBUTION_WRAPPER_H | ||
|
||
#include "memilio/io/io.h" | ||
#include "memilio/utils/compiler_diagnostics.h" | ||
#include "parameter_distributions.h" | ||
|
||
namespace mio | ||
{ | ||
|
||
class ParameterDistributionWrapper | ||
{ | ||
public: | ||
ParameterDistributionWrapper() | ||
: m_dist(nullptr) | ||
{ | ||
} | ||
|
||
ParameterDistributionWrapper(ParameterDistribution& dist) | ||
: m_dist(std::unique_ptr<ParameterDistribution>(dist.clone())) | ||
{ | ||
} | ||
|
||
ParameterDistributionWrapper(const ParameterDistributionWrapper& other) | ||
{ | ||
m_dist = (other.m_dist == nullptr) ? nullptr : std::unique_ptr<ParameterDistribution>(other.m_dist->clone()); | ||
} | ||
|
||
ParameterDistributionWrapper(ParameterDistributionWrapper&& other) | ||
{ | ||
m_dist = (other.m_dist == nullptr) ? nullptr : std::unique_ptr<ParameterDistribution>(other.m_dist->clone()); | ||
} | ||
|
||
ParameterDistributionWrapper& operator=(ParameterDistributionWrapper const& other) | ||
{ | ||
m_dist = (other.m_dist == nullptr) ? nullptr : std::unique_ptr<ParameterDistribution>(other.m_dist->clone()); | ||
return *this; | ||
} | ||
|
||
ParameterDistributionWrapper& operator=(ParameterDistributionWrapper&& other) | ||
{ | ||
m_dist = (other.m_dist == nullptr) ? nullptr : std::unique_ptr<ParameterDistribution>(other.m_dist->clone()); | ||
return *this; | ||
}; | ||
|
||
~ParameterDistributionWrapper() = default; | ||
|
||
std::vector<double> params() const | ||
{ | ||
if (m_dist == nullptr) { | ||
log_error("Distribution is not defined. Parameters cannot be deduced."); | ||
} | ||
return m_dist->params(); | ||
Comment on lines
+69
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Logging is not enough here, as a nullptr dereference is undefined behaviour. If we are lucky, this return will immediately cause a segfault and crash. |
||
} | ||
|
||
template <class RNG> | ||
double get(RNG& rng) | ||
{ | ||
if (m_dist == nullptr) { | ||
log_error("Distribution is not defined. Value cannot be sampled."); | ||
} | ||
return m_dist->get_sample(rng); | ||
} | ||
|
||
/** | ||
* serialize this. | ||
* @see mio::serialize | ||
*/ | ||
template <class IOContext> | ||
void serialize(IOContext& io) const | ||
{ | ||
m_dist->serialize(io); | ||
} | ||
|
||
private: | ||
std::unique_ptr<ParameterDistribution> m_dist; | ||
}; | ||
|
||
/** | ||
* deserialize a ParameterDistributionWrapper. | ||
* @see mio::deserialize | ||
*/ | ||
template <class IOContext> | ||
IOResult<ParameterDistributionWrapper> deserialize_internal(IOContext& io, Tag<ParameterDistributionWrapper>) | ||
{ | ||
|
||
auto obj = io.expect_object("ParameterDistribution"); | ||
auto type = obj.expect_element("Type", Tag<std::string>{}); | ||
if (type) { | ||
if (type.value() == "Uniform") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a way to avoid having to touch this each time a distribution is added? |
||
BOOST_OUTCOME_TRY(auto&& r, ParameterDistributionUniform::deserialize_elements(io, obj)); | ||
return ParameterDistributionWrapper(r); | ||
} | ||
else if (type.value() == "Normal") { | ||
BOOST_OUTCOME_TRY(auto&& r, ParameterDistributionNormal::deserialize_elements(io, obj)); | ||
return ParameterDistributionWrapper(r); | ||
} | ||
else if (type.value() == "LogNormal") { | ||
BOOST_OUTCOME_TRY(auto&& r, ParameterDistributionLogNormal::deserialize_elements(io, obj)); | ||
return ParameterDistributionWrapper(r); | ||
} | ||
else if (type.value() == "Exponential") { | ||
BOOST_OUTCOME_TRY(auto&& r, ParameterDistributionExponential::deserialize_elements(io, obj)); | ||
return ParameterDistributionWrapper(r); | ||
} | ||
else if (type.value() == "Constant") { | ||
BOOST_OUTCOME_TRY(auto&& r, ParameterDistributionConstant::deserialize_elements(io, obj)); | ||
return ParameterDistributionWrapper(r); | ||
} | ||
else { | ||
return failure(StatusCode::InvalidValue, "Type of ParameterDistribution in ParameterDistributionWrapper" + | ||
type.value() + " not valid."); | ||
} | ||
} | ||
return failure(type.error()); | ||
} | ||
|
||
} // namespace mio | ||
|
||
#endif //PARAMETER_DISTRIBUTION_WRAPPER_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, but shouldn't the move constructors move the distribution instead of copying it?