Skip to content

Commit

Permalink
refactor: introduce StochasticParameterDistributionBase class
Browse files Browse the repository at this point in the history
  • Loading branch information
HansRobo committed Jan 21, 2025
1 parent 7e6ae02 commit 04cb6db
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2015 TIER IV, Inc. All rights reserved.
//
// 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 OPENSCENARIO_INTERPRETER__PARAMETER_DISTRIBUTION_HPP_
#define OPENSCENARIO_INTERPRETER__PARAMETER_DISTRIBUTION_HPP_

#include <openscenario_interpreter/object.hpp>

namespace openscenario_interpreter
{
struct StochasticParameterDistributionBase
{
virtual auto derive() -> Object = 0;
};
} // namespace openscenario_interpreter
#endif // OPENSCENARIO_INTERPRETER__PARAMETER_DISTRIBUTION_HPP_
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef OPENSCENARIO_INTERPRETER__SYNTAX__HISTOGRAM_HPP_
#define OPENSCENARIO_INTERPRETER__SYNTAX__HISTOGRAM_HPP_

#include <openscenario_interpreter/parameter_distribution.hpp>
#include <openscenario_interpreter/scope.hpp>
#include <openscenario_interpreter/syntax/histogram_bin.hpp>
#include <random>
Expand All @@ -34,7 +35,7 @@ inline namespace syntax
</xsd:sequence>
</xsd:complexType>
*/
struct Histogram : public ComplexType, private Scope
struct Histogram : public ComplexType, private Scope, public StochasticParameterDistributionBase
{
/**
* Note: HistogramBin must be stored in continuous range and ascending order, to `bins`
Expand All @@ -45,7 +46,7 @@ struct Histogram : public ComplexType, private Scope

explicit Histogram(const pugi::xml_node &, Scope & scope);

auto evaluate() -> Object;
auto derive() -> Object override;
};
} // namespace syntax
} // namespace openscenario_interpreter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef OPENSCENARIO_INTERPRETER__SYNTAX__NORMAL_DISTRIBUTION_HPP_
#define OPENSCENARIO_INTERPRETER__SYNTAX__NORMAL_DISTRIBUTION_HPP_

#include <openscenario_interpreter/parameter_distribution.hpp>
#include <openscenario_interpreter/scope.hpp>
#include <openscenario_interpreter/syntax/double.hpp>
#include <openscenario_interpreter/syntax/range.hpp>
Expand All @@ -38,7 +39,9 @@ inline namespace syntax
<xsd:attribute name="variance" type="Double" use="required"/>
</xsd:complexType>
*/
struct NormalDistribution : public ComplexType, private Scope
struct NormalDistribution : public ComplexType,
private Scope,
public StochasticParameterDistributionBase
{
const Range range;

Expand All @@ -50,7 +53,7 @@ struct NormalDistribution : public ComplexType, private Scope

explicit NormalDistribution(const pugi::xml_node &, Scope & scope);

auto evaluate() -> Object;
auto derive() -> Object override;
};
} // namespace syntax
} // namespace openscenario_interpreter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef OPENSCENARIO_INTERPRETER__SYNTAX__POISSON_DISTRIBUTION_HPP_
#define OPENSCENARIO_INTERPRETER__SYNTAX__POISSON_DISTRIBUTION_HPP_

#include <openscenario_interpreter/parameter_distribution.hpp>
#include <openscenario_interpreter/scope.hpp>
#include <openscenario_interpreter/syntax/double.hpp>
#include <openscenario_interpreter/syntax/range.hpp>
Expand All @@ -36,7 +37,9 @@ inline namespace syntax
<xsd:attribute name="expectedValue" type="Double" use="required"/>
</xsd:complexType>
*/
struct PoissonDistribution : public ComplexType, private Scope
struct PoissonDistribution : public ComplexType,
private Scope,
public StochasticParameterDistributionBase
{
const Range range;

Expand All @@ -46,7 +49,7 @@ struct PoissonDistribution : public ComplexType, private Scope

explicit PoissonDistribution(const pugi::xml_node &, Scope & scope);

auto evaluate() -> Object;
auto derive() -> Object override;
};
} // namespace syntax
} // namespace openscenario_interpreter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef OPENSCENARIO_INTERPRETER__SYNTAX__PROBABILITY_DISTRIBUTION_SET_HPP_
#define OPENSCENARIO_INTERPRETER__SYNTAX__PROBABILITY_DISTRIBUTION_SET_HPP_

#include <openscenario_interpreter/parameter_distribution.hpp>
#include <openscenario_interpreter/syntax/probability_distribution_set_element.hpp>
#include <random>

Expand All @@ -33,15 +34,17 @@ inline namespace syntax
</xsd:sequence>
</xsd:complexType>
*/
struct ProbabilityDistributionSet : public ComplexType, private Scope
struct ProbabilityDistributionSet : public ComplexType,
private Scope,
public StochasticParameterDistributionBase
{
const std::vector<ProbabilityDistributionSetElement> elements;

std::discrete_distribution<std::size_t> distribute;

explicit ProbabilityDistributionSet(const pugi::xml_node &, Scope & scope);

auto evaluate() -> Object;
auto derive() -> Object override;
};
} // namespace syntax
} // namespace openscenario_interpreter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#ifndef OPENSCENARIO_INTERPRETER__SYNTAX__UNIFORM_DISTRIBUTION_HPP_
#define OPENSCENARIO_INTERPRETER__SYNTAX__UNIFORM_DISTRIBUTION_HPP_

#include <openscenario_interpreter/parameter_distribution.hpp>
#include <openscenario_interpreter/scope.hpp>
#include <openscenario_interpreter/syntax/range.hpp>
#include <random>
Expand All @@ -34,15 +35,17 @@ inline namespace syntax
</xsd:sequence>
</xsd:complexType>
*/
struct UniformDistribution : public ComplexType, private Scope
struct UniformDistribution : public ComplexType,
private Scope,
public StochasticParameterDistributionBase
{
const Range range;

std::uniform_real_distribution<Double::value_type> distribute;

explicit UniformDistribution(const pugi::xml_node &, Scope & scope);

auto evaluate() -> Object;
auto derive() -> Object override;
};
} // namespace syntax
} // namespace openscenario_interpreter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ Histogram::Histogram(const pugi::xml_node & node, openscenario_interpreter::Scop
{
}

auto Histogram::evaluate() -> Object { return make<Double>(distribute(random_engine)); }
auto Histogram::derive() -> Object { return make<Double>(distribute(random_engine)); }
} // namespace syntax
} // namespace openscenario_interpreter
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ NormalDistribution::NormalDistribution(
{
}

auto NormalDistribution::evaluate() -> Object { return make<Double>(distribute(random_engine)); }
auto NormalDistribution::derive() -> Object { return make<Double>(distribute(random_engine)); }
} // namespace syntax
} // namespace openscenario_interpreter
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ PoissonDistribution::PoissonDistribution(
{
}

auto PoissonDistribution::evaluate() -> Object { return make<Double>(distribute(random_engine)); }
auto PoissonDistribution::derive() -> Object { return make<Double>(distribute(random_engine)); }
} // namespace syntax
} // namespace openscenario_interpreter
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ ProbabilityDistributionSet::ProbabilityDistributionSet(
{
}

auto ProbabilityDistributionSet::evaluate() -> Object
auto ProbabilityDistributionSet::derive() -> Object
{
std::size_t index = distribute(random_engine);
return elements.at(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ UniformDistribution::UniformDistribution(
{
}

auto UniformDistribution::evaluate() -> Object { return make<Double>(distribute(random_engine)); }
auto UniformDistribution::derive() -> Object { return make<Double>(distribute(random_engine)); }
} // namespace syntax
} // namespace openscenario_interpreter

0 comments on commit 04cb6db

Please sign in to comment.