Skip to content

Commit 7b25483

Browse files
feat(string): add classes descriptions
1 parent bd65a7a commit 7b25483

13 files changed

Lines changed: 860 additions & 172 deletions

File tree

include/geode/stochastic/sampling/direct/object_set_sampler/object_set_sampler.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace geode
3232
class ObjectSetSampler
3333
{
3434
public:
35-
ObjectSetSampler( uuid subset_id ) : subset_id_{ subset_id } {}
35+
ObjectSetSampler( const uuid& subset_id ) : subset_id_( subset_id ) {}
3636
virtual ~ObjectSetSampler() = default;
3737

3838
virtual std::pair< Type, uuid > sample(
@@ -60,6 +60,6 @@ namespace geode
6060
virtual double log_pdf( const Type& obj ) const = 0;
6161

6262
protected:
63-
uuid subset_id_;
63+
const uuid& subset_id_;
6464
};
6565
} // namespace geode

include/geode/stochastic/sampling/direct/object_set_sampler/point_set_sampler.hpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ namespace geode
3636
{
3737
public:
3838
UniformPointSetSampler(
39-
const BoundingBox< dimension >& box, uuid subset_id )
40-
: ObjectSetSampler< Point< dimension > >{ subset_id }, box_( box )
39+
const BoundingBox< dimension >& box, const uuid& subset_id )
40+
: ObjectSetSampler< Point< dimension > >( subset_id ), box_( box )
4141
{
4242
auto volume = box_.n_volume();
4343
if( volume != 0. )
@@ -62,12 +62,21 @@ namespace geode
6262

6363
auto new_point =
6464
PointUniformSampler::sample< dimension >( engine, ball );
65-
while( !box_.contains( new_point ) )
65+
constexpr index_t max_try{ 100 };
66+
for( const auto try_id : geode::Range{ max_try } )
6667
{
68+
if( box_.contains( new_point ) )
69+
{
70+
return { new_point, this->subset_id_ };
71+
}
6772
new_point =
6873
PointUniformSampler::sample< dimension >( engine, ball );
6974
}
70-
return { new_point, this->subset_id_ };
75+
OPENGEODE_EXCEPTION( true == false,
76+
absl::StrCat(
77+
"[PointSampler] - Cannot find a point in the box: ",
78+
box_.string() ) );
79+
return { obj, this->subset_id_ };
7180
}
7281

7382
double log_pdf( const Point< dimension >& obj ) const override

include/geode/stochastic/sampling/mcmc/metropolis_hasting_sampler.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ namespace geode
8383
{
8484
Proposal< Type > proposal =
8585
proposal_kernel_->propose( state, engine );
86-
8786
if( proposal.type == Proposal< Type >::Move::Birth )
8887
{
8988
return birth_step( proposal, state, engine );

include/geode/stochastic/sampling/mcmc/models/components/energy_term.hpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,22 @@ namespace geode
7676

7777
namespace geode
7878
{
79+
// struct EnergyTermDescription
80+
// {
81+
// geode::uuid id;
82+
// std::string name;
83+
// std::string type;
84+
// double parameter_value;
85+
// std::optional< uuid > targeted_subset_id{};
86+
// }
87+
//
88+
// struct StatisticalDescription
89+
// {
90+
// std::string label;
91+
// double value;
92+
// std::optional< uuid > targeted_subset_id{};
93+
// };
94+
7995
template < typename ObjectType >
8096
class EnergyTerm
8197
{
@@ -138,6 +154,19 @@ namespace geode
138154
virtual double statistic(
139155
const ObjectSet< ObjectType >& state ) const = 0;
140156

157+
std::string string() const
158+
{
159+
auto message =
160+
absl::StrCat( "Term : ", name(), "; uuid: ", id().string(),
161+
" parameter value: ", energy_scale_.parameter() );
162+
if( targeted_subset_id_ )
163+
{
164+
absl::StrAppend( &message, " targetted subset: ",
165+
targeted_subset_id_.value().string() );
166+
}
167+
return message;
168+
}
169+
141170
protected:
142171
bool is_targeted_subset( const uuid& subset_id ) const
143172
{

include/geode/stochastic/sampling/mcmc/models/gibbs_energy.hpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,22 @@ namespace geode
3131
template < typename ObjectType >
3232
class GibbsEnergy
3333
{
34-
OPENGEODE_DISABLE_COPY_AND_MOVE( GibbsEnergy );
34+
OPENGEODE_DISABLE_COPY( GibbsEnergy );
3535

3636
public:
3737
GibbsEnergy() = default;
38+
GibbsEnergy( GibbsEnergy&& other )
39+
: energy_terms_( std::move( other.energy_terms_ ) )
40+
{
41+
}
42+
3843
~GibbsEnergy() = default;
3944

40-
void add_energy_term( std::unique_ptr< EnergyTerm< ObjectType > > term )
45+
uuid add_energy_term( std::unique_ptr< EnergyTerm< ObjectType > > term )
4146
{
47+
auto id = term->id();
4248
energy_terms_.emplace( term->id(), std::move( term ) );
49+
return id;
4350
}
4451
void clear_energy_terms()
4552
{
@@ -116,6 +123,12 @@ namespace geode
116123
return log_energy;
117124
}
118125

126+
double energy_term_statistic(
127+
const ObjectSet< ObjectType >& state, const uuid& energy_term_id )
128+
{
129+
return energy_terms_.at( energy_term_id )->statistic( state );
130+
}
131+
119132
std::vector< double > ordered_energy_term_statistics(
120133
const ObjectSet< ObjectType >& state ) const
121134
{
@@ -137,6 +150,20 @@ namespace geode
137150
return vector_string( stats );
138151
}
139152

153+
std::string string() const
154+
{
155+
auto message = absl::StrCat(
156+
"Gibbs Energy: ", energy_terms_.size(), " terms:" );
157+
// we should find a way to iterate in a ordered way.
158+
for( const auto& energy_term : energy_terms_ )
159+
{
160+
absl::StrAppend( &message,
161+
"\n\t --> uuid: ", energy_term.first.string(), " - ",
162+
energy_term.second->string() );
163+
}
164+
return message;
165+
}
166+
140167
private:
141168
std::string vector_string( const std::vector< double >& vector ) const
142169
{

include/geode/stochastic/sampling/mcmc/proposal/moves.hpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#pragma once
2525
#include <geode/stochastic/sampling/direct/object_set_sampler/object_set_sampler.hpp>
26+
#include <geode/stochastic/spatial/object_helpers.hpp>
2627
#include <geode/stochastic/spatial/object_set.hpp>
2728

2829
namespace geode
@@ -44,6 +45,26 @@ namespace geode
4445
std::optional< ObjectId > old_object_id; // for death/change
4546
double log_forward_prob{ 0. };
4647
double log_backward_prob{ 0. };
48+
std::string string() const
49+
{
50+
auto message = absl::StrCat( "Poposal: Type ", type );
51+
if( new_object )
52+
{
53+
absl::StrAppend( &message, "\n\t - New object: ",
54+
geode::object_bounding_box( new_object->first ).string(),
55+
" subset id ", new_object->second.string() );
56+
}
57+
if( old_object_id )
58+
{
59+
absl::StrAppend( &message,
60+
"\n\t - Old object: ", old_object_id->index, " subset id ",
61+
old_object_id->subset.string() );
62+
}
63+
absl::StrAppend( &message,
64+
"\n\t - log_forward_prob: ", log_forward_prob,
65+
" log_backward_prob ", log_backward_prob );
66+
return message;
67+
}
4768
};
4869

4970
// Move does not hold the sampler... should it?
@@ -55,6 +76,10 @@ namespace geode
5576
const ObjectSetSampler< ObjectType >& sampler, double probability )
5677
: sampler_( sampler ), p_move_{ probability }
5778
{
79+
OPENGEODE_EXCEPTION( p_move_ > 0.,
80+
"[Move] - the weight factor for a move should be in higher "
81+
"than 0. (here = ",
82+
p_move_, ")" );
5883
}
5984
virtual ~Move() = default;
6085

@@ -67,6 +92,11 @@ namespace geode
6792
return p_move_;
6893
}
6994

95+
std::string string() const
96+
{
97+
return absl::StrCat( "Move proba: ", p_move_ );
98+
}
99+
70100
protected:
71101
const ObjectSetSampler< ObjectType >& sampler_;
72102
double p_move_{ 1.0 };
@@ -94,10 +124,13 @@ namespace geode
94124
const ObjectSet< ObjectType >& current,
95125
RandomEngine& engine ) const override
96126
{
127+
DEBUG( "Birth Death move" );
97128
if( engine.sample_bernoulli( birth_ratio_ ) )
98129
{
130+
DEBUG( "Birth move" );
99131
return propose_birth_move( current, engine );
100132
}
133+
DEBUG( " Death move" );
101134
return propose_death_move( current, engine );
102135
}
103136

include/geode/stochastic/sampling/mcmc/proposal/proposal_kernel.hpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,27 @@ namespace geode
6464
compute_cumulative_sum_probs();
6565
}
6666

67+
std::string string() const
68+
{
69+
auto message = absl::StrCat(
70+
"Proposal Kernel:", "\n\t - number of moves: ", moves_.size() );
71+
for( const auto& move : moves_ )
72+
{
73+
absl::StrAppend( &message, " \n\t --> ", move->string() );
74+
}
75+
absl::StrAppend( &message, "\n\t - cumsum :" );
76+
for( const auto cumsum : cumulative_probs_ )
77+
{
78+
absl::StrAppend( &message, " ", cumsum );
79+
}
80+
return message;
81+
}
82+
6783
private:
6884
void compute_cumulative_sum_probs()
6985
{
7086
const auto n = moves_.size();
87+
cumulative_probs_.clear();
7188
cumulative_probs_.resize( n );
7289
if( n == 1 )
7390
{

include/geode/stochastic/spatial/object_set.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ namespace geode
6767
index_t nb_objects() const;
6868

6969
void add_subset( const uuid& subset_id );
70+
uuid add_subset();
7071
ObjectId add_object( Type&& object, const uuid& subset_id );
7172
void update_object( const ObjectId& object_id, Type&& object );
7273
void remove_object( const ObjectId& object_id );
@@ -79,6 +80,8 @@ namespace geode
7980
std::vector< ObjectId > neighbors(
8081
const Type& object, double searching_distance ) const;
8182

83+
std::string string() const;
84+
8285
private:
8386
std::vector< Type >& get_subset( const uuid& subset_id );
8487

src/geode/stochastic/spatial/object_set.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,16 @@ namespace geode
9898
OPENGEODE_EXCEPTION( inserted, "[ObjectSet]- group (",
9999
subset_id.string(), ") already exists." );
100100
}
101+
template < typename Type >
102+
uuid ObjectSet< Type >::add_subset()
103+
{
104+
uuid subset_id{};
105+
auto [it, inserted] =
106+
groups_.emplace( subset_id, std::vector< Type >{} );
107+
OPENGEODE_EXCEPTION( inserted, "[ObjectSet]- group (",
108+
subset_id.string(), ") already exists." );
109+
return subset_id;
110+
}
101111

102112
template < typename Type >
103113
ObjectId ObjectSet< Type >::add_object(
@@ -197,6 +207,20 @@ namespace geode
197207
static_cast< const ObjectSet* >( this )->get_subset( subset_id ) );
198208
}
199209

210+
template < typename Type >
211+
std::string ObjectSet< Type >::string() const
212+
{
213+
auto message =
214+
absl::StrCat( "ObjectSet with ", nb_subsets(), " subsets:" );
215+
for( const auto& [subset_id, objs] : groups_ )
216+
{
217+
absl::StrAppend( &message,
218+
"\n\t --> subset uuid: ", subset_id.string(),
219+
"; number of objects: ", objs.size() );
220+
}
221+
return message;
222+
}
223+
200224
template class opengeode_stochastic_stochastic_api ObjectSet< Point2D >;
201225
template class opengeode_stochastic_stochastic_api ObjectSet< Point3D >;
202226
template class opengeode_stochastic_stochastic_api

tests/stochastic/CMakeLists.txt

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,15 @@ add_geode_test(
106106
)
107107

108108
add_geode_test(
109-
SOURCE "sampling/mcmc/test-mh-poisson-statistics-and-convergence.cpp"
110-
DEPENDENCIES
111-
OpenGeode::basic
112-
OpenGeode::geometry
113-
${PROJECT_NAME}::stochastic
114-
UNSTABLE
109+
SOURCE "sampling/mcmc/test-mh-poisson.cpp"
110+
DEPENDENCIES
111+
OpenGeode::basic
112+
OpenGeode::geometry
113+
${PROJECT_NAME}::stochastic
115114
)
116115

117116
add_geode_test(
118-
SOURCE "sampling/mcmc/test-mh-strauss-statistics-and-convergence.cpp"
117+
SOURCE "sampling/mcmc/test-mh-strauss.cpp"
119118
DEPENDENCIES
120119
OpenGeode::basic
121120
OpenGeode::geometry

0 commit comments

Comments
 (0)