Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions include/geode/stochastic/sampling/direct/double_sampler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,20 @@ namespace geode
Gaussian,
TruncatedGaussian >;

struct DistributionDescription
{
std::string name{ "default_distribution" };
DistributionType distribution_type;

std::optional< double > min_value;
std::optional< double > max_value;
std::optional< double > mean;
std::optional< double > standard_deviation;
};

static Distribution create_distribution(
const DistributionDescription& desc );

static double sample( RandomEngine& engine, const Distribution& dist );
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ namespace geode
}

private:
const BoundingBox< dimension >& box_;
BoundingBox< dimension > box_;
};

} // namespace geode
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright (c) 2019 - 2025 Geode-solutions
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

#pragma once

#include <geode/geometry/basic_objects/segment.hpp>
#include <geode/geometry/basic_objects/sphere.hpp>

#include <geode/stochastic/sampling/direct/object_set_sampler/object_set_sampler.hpp>
#include <geode/stochastic/sampling/direct/point_uniform_sampler.hpp>
#include <geode/stochastic/sampling/direct/segment_uniform_sampler.hpp>

namespace geode
{
class UniformSegmentSetSampler : public ObjectSetSampler< OwnerSegment2D >
{
public:
UniformSegmentSetSampler( const BoundingBox< 2 >& box,
const DoubleSampler::Distribution& length,
const DoubleSampler::Distribution& azimuth )
: ObjectSetSampler< OwnerSegment2D >{},
box_{ box },
length_{ length },
azimuth_{ azimuth }
{
auto volume = box_.n_volume();
OPENGEODE_EXCEPTION( volume != 0.,
"[SegmentSetSampler] - Undefined Bounding Box (volume == 0)." );
this->log_pdf_ = -std::log( volume );
}

OwnerSegment2D sample( RandomEngine& engine ) const override
{
auto seg = SegmentUniformSampler::sample(
engine, box_, length_, azimuth_ );
return seg;
}

OwnerSegment2D change(
const OwnerSegment2D& obj, RandomEngine& engine ) const override
{
double ratio = 0.1;
const auto& extremities = obj.vertices();
const auto current =
static_cast< local_index_t >( engine.sample_bernoulli( 0.5 ) );

geode::Sphere< 2 > ball{ extremities[current],
ratio * obj.length() };

auto new_point = PointUniformSampler::sample< 2 >( engine, ball );
constexpr index_t max_try{ 100 };
for( const auto try_id : geode::Range{ max_try } )
{
if( box_.contains( new_point ) )
{
OwnerSegment2D new_segment{ obj };
new_segment.set_point( current, new_point );
return new_segment;
}
new_point = PointUniformSampler::sample< 2 >( engine, ball );
}
throw OpenGeodeException( absl::StrCat(
"[PointSampler] - Cannot find a point in the box: ",
box_.string() ) );
return obj;
}

private:
bool is_valid_object( const OwnerSegment2D& obj ) const override
{
const auto& extremities = obj.vertices();

return box_.contains( extremities[0] )
&& box_.contains( extremities[1] );
}

private:
BoundingBox2D box_;
DoubleSampler::Distribution length_;
DoubleSampler::Distribution azimuth_;
};

} // namespace geode
48 changes: 48 additions & 0 deletions include/geode/stochastic/sampling/distributions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,16 @@

#include <optional>

#include <geode/basic/named_type.hpp>

namespace geode
{
struct DistributionTag
{
};

using DistributionType = NamedType< std::string, DistributionTag >;

template < typename Type >
struct UniformClosed
{
Expand All @@ -37,6 +45,16 @@ namespace geode

Type min_value{ static_cast< Type >( 0 ) };
Type max_value{ static_cast< Type >( 1 ) };

[[nodiscard]] static DistributionType distribution_type_static()
{
return DistributionType{ "UniformClosed" };
}

[[nodiscard]] DistributionType distribution_type() const
{
return distribution_type_static();
}
};

template < typename Type >
Expand All @@ -47,6 +65,16 @@ namespace geode

Type min_value{ static_cast< Type >( 0 ) };
Type max_value{ static_cast< Type >( 1 ) };

[[nodiscard]] static DistributionType distribution_type_static()
{
return DistributionType{ "UniformClosedOpen" };
}

[[nodiscard]] DistributionType distribution_type() const
{
return distribution_type_static();
}
};

struct opengeode_stochastic_stochastic_api Gaussian
Expand All @@ -56,6 +84,16 @@ namespace geode

double mean{ 0. };
double standard_deviation{ 1. };

[[nodiscard]] static DistributionType distribution_type_static()
{
return DistributionType{ "Gaussian" };
}

[[nodiscard]] DistributionType distribution_type() const
{
return distribution_type_static();
}
};

struct opengeode_stochastic_stochastic_api TruncatedGaussian
Expand All @@ -69,6 +107,16 @@ namespace geode

std::optional< double > min_value;
std::optional< double > max_value;

[[nodiscard]] static DistributionType distribution_type_static()
{
return DistributionType{ "TruncatedGaussian" };
}

[[nodiscard]] DistributionType distribution_type() const
{
return distribution_type_static();
}
};

} // namespace geode
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2019 - 2025 Geode-solutions
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

#pragma once
#include <geode/basic/range.hpp>
#include <geode/stochastic/common.hpp>

namespace geode
{

class StatisticsMonitor
{
public:
StatisticsMonitor( StatisticsMonitor&& ) = default;
StatisticsMonitor( const StatisticsMonitor& ) = default;
StatisticsMonitor& operator=( StatisticsMonitor&& ) noexcept = default;
StatisticsMonitor& operator=(
const StatisticsMonitor& ) noexcept = default;

StatisticsMonitor( const index_t nb_energy_terms )
{
sum_.resize( nb_energy_terms, 0.0 );
sum_squares_.resize( nb_energy_terms, 0.0 );
means_.resize( nb_energy_terms, 0.0 );
variances_.resize( nb_energy_terms, 0.0 );
}

void add_realization( const std::vector< double >& values )
{
OPENGEODE_EXCEPTION( values.size() == sum_.size(),
"[StatisticsMonitor] - Mismatch between realization size and "
"expected number of statistics." );
++count_;
for( size_t i = 0; i < values.size(); ++i )
{
double delta = values[i] - means_[i];
means_[i] += delta / count_;
if( count_ > 1 )
variances_[i] = ( ( count_ - 2 ) * variances_[i]
+ delta * ( values[i] - means_[i] ) )
/ ( count_ - 1 );
}
}

const index_t statiscal_count() const
{
return count_;
}

const std::vector< double >& means() const
{
return means_;
}

const std::vector< double >& variances() const
{
return variances_;
}

private:
std::vector< double > sum_;
std::vector< double > sum_squares_;
std::vector< double > means_;
std::vector< double > variances_;
index_t count_{ 0 };
};

} // namespace geode
Loading