Skip to content

Commit 00e2bda

Browse files
tidy
1 parent c963096 commit 00e2bda

17 files changed

Lines changed: 539 additions & 525 deletions

File tree

include/geode/stochastic/sampling/mcmc/helpers/simulation_printer.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,12 @@ namespace geode
4444

4545
bool print_realisations{ true };
4646
std::string realisations_prefix{ "pattern_" };
47+
// NOLINTNEXTLINE(*-magic-numbers)
4748
index_t realisations_print_frequency{ 100 };
4849

4950
std::string output_folder{ std::filesystem::current_path().string() };
5051

51-
std::string string() const
52+
[[nodiscard]] std::string string() const
5253
{
5354
auto message = absl::StrCat(
5455
"SimulationPrinterConfigurator - print to folder: ",
@@ -80,8 +81,8 @@ namespace geode
8081
{
8182
public:
8283
SimulationPrinter( const Model< ObjectType >& model,
83-
const SimulationPrinterConfigurator& config )
84-
: model_( model ), config_( config )
84+
SimulationPrinterConfigurator config )
85+
: model_( model ), config_( std::move( config ) )
8586
{
8687
}
8788

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace geode
4040
struct StepResult
4141
{
4242
MH_DECISION decision{ MH_DECISION::undecided };
43-
MoveType move_type{ MoveType::Invalid };
43+
MOVE_TYPE move_type{ MOVE_TYPE::invalid };
4444
double log_accept{ -std::numeric_limits< double >::infinity() };
4545
double delta_log_energy{ 0.0 };
4646
};
@@ -65,15 +65,15 @@ namespace geode
6565
Proposal< ObjectType > proposal =
6666
proposal_kernel_->propose( state, engine );
6767
const auto& move_type = proposal.proposed_move.type;
68-
if( move_type == MoveType::Birth )
68+
if( move_type == MOVE_TYPE::birth )
6969
{
7070
return birth_step( proposal, state, engine );
7171
}
72-
if( move_type == MoveType::Death )
72+
if( move_type == MOVE_TYPE::death )
7373
{
7474
return death_step( proposal, state, engine );
7575
}
76-
if( move_type == MoveType::Change )
76+
if( move_type == MOVE_TYPE::change )
7777
{
7878
return change_step( proposal, state, engine );
7979
}
@@ -201,7 +201,7 @@ namespace geode
201201
[]( auto& cur_state, auto& accepted_proposal ) {
202202
cur_state.add_object(
203203
std::move( accepted_proposal.proposed_move.new_object
204-
.value() ),
204+
.value() ),
205205
accepted_proposal.set_id, false );
206206
} );
207207
};
@@ -233,7 +233,7 @@ namespace geode
233233
cur_state.update_free_object(
234234
accepted_proposal.old_object_id(),
235235
std::move( accepted_proposal.proposed_move.new_object
236-
.value() ) );
236+
.value() ) );
237237
} );
238238
};
239239

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

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,27 +28,27 @@
2828

2929
namespace geode
3030
{
31-
enum class MoveType
31+
enum class MOVE_TYPE : local_index_t
3232
{
33-
Invalid,
34-
Birth,
35-
Death,
36-
Change
33+
invalid,
34+
birth,
35+
death,
36+
change
3737
};
3838

3939
struct ProposalProbabilities
4040
{
4141
double log_forward_prob{ 0. };
4242
double log_backward_prob{ 0. };
43-
std::string string() const
43+
[[nodiscard]] std::string string() const
4444
{
4545
return absl::StrCat(
4646
"\t Proposal Probabilities: (log) forward probability: ",
4747
log_forward_prob,
4848
" (log) backward probability: ", log_backward_prob );
4949
}
5050

51-
double transition_probability() const
51+
[[nodiscard]] double transition_probability() const
5252
{
5353
OpenGeodeStochasticStochasticException::check_exception(
5454
std::isfinite( log_forward_prob )
@@ -63,23 +63,23 @@ namespace geode
6363
template < typename ObjectType >
6464
struct MoveResult
6565
{
66-
MoveType type{ MoveType::Invalid };
66+
MOVE_TYPE type{ MOVE_TYPE::invalid };
6767
std::optional< ObjectType > new_object;
6868
std::optional< index_t > old_object_id;
6969

7070
ProposalProbabilities proposal_probabilities;
7171

72-
std::string type_string() const
72+
[[nodiscard]] std::string type_string() const
7373
{
7474
switch( type )
7575
{
76-
case MoveType::Invalid:
76+
case MOVE_TYPE::invalid:
7777
return "Invalid";
78-
case MoveType::Birth:
78+
case MOVE_TYPE::birth:
7979
return "Birth";
80-
case MoveType::Death:
80+
case MOVE_TYPE::death:
8181
return "Death";
82-
case MoveType::Change:
82+
case MOVE_TYPE::change:
8383
return "Change";
8484
default:
8585
throw OpenGeodeStochasticStochasticException{ nullptr,
@@ -89,7 +89,7 @@ namespace geode
8989
return "Unknown";
9090
}
9191
}
92-
std::string string() const
92+
[[nodiscard]] std::string string() const
9393
{
9494
auto message = absl::StrCat( "Move result type: ", type_string() );
9595
if( new_object )
@@ -111,7 +111,11 @@ namespace geode
111111
template < typename ObjectType >
112112
class Move
113113
{
114+
OPENGEODE_DISABLE_COPY_AND_MOVE( Move );
115+
114116
public:
117+
Move() noexcept = default;
118+
115119
Move( const ObjectSetSampler< ObjectType >& sampler,
116120
double proportion_weight )
117121
: sampler_( sampler ), proportion_weight_{ proportion_weight }
@@ -129,7 +133,7 @@ namespace geode
129133
const ObjectSet< ObjectType >& set,
130134
RandomEngine& engine ) const = 0;
131135

132-
double proportion_weight() const
136+
[[nodiscard]] double proportion_weight() const
133137
{
134138
return proportion_weight_;
135139
}
@@ -139,7 +143,7 @@ namespace geode
139143
geode_unused( probability );
140144
}
141145

142-
virtual std::string string() const = 0;
146+
[[nodiscard]] virtual std::string string() const = 0;
143147

144148
protected:
145149
std::optional< geode::index_t > draw_a_free_sample_id(
@@ -157,6 +161,12 @@ namespace geode
157161
}
158162

159163
protected:
164+
const ObjectSetSampler< ObjectType >& sampler() const
165+
{
166+
return sampler_;
167+
}
168+
169+
private:
160170
const ObjectSetSampler< ObjectType >& sampler_;
161171
double proportion_weight_{ 1.0 };
162172
};
@@ -195,10 +205,11 @@ namespace geode
195205
log_p_birth_ = std::log( probability * birth_ratio_ );
196206
log_p_death_ = std::log( probability * ( 1.0 - birth_ratio_ ) );
197207
}
198-
std::string string() const override
208+
209+
[[nodiscard]] std::string string() const override
199210
{
200211
return absl::StrCat( "Birth and Death Move (proportion weight: ",
201-
this->proportion_weight_, " -- birth ratio:", birth_ratio_,
212+
this->proportion_weight(), " -- birth ratio:", birth_ratio_,
202213
" log_p_birth: ", log_p_birth_, " log_p_death_: ", log_p_death_,
203214
")" );
204215
}
@@ -208,15 +219,15 @@ namespace geode
208219
const ObjectSet< ObjectType >& set, RandomEngine& engine ) const
209220
{
210221
MoveResult< ObjectType > birth;
211-
birth.type = MoveType::Birth;
212-
birth.new_object = this->sampler_.sample( engine );
222+
birth.type = MOVE_TYPE::birth;
223+
birth.new_object = this->sampler().sample( engine );
213224
if( !birth.new_object.has_value() )
214225
{
215226
return birth;
216227
}
217228
auto& new_obj = birth.new_object.value();
218229
birth.proposal_probabilities.log_forward_prob =
219-
log_p_birth_ + this->sampler_.log_pdf( new_obj );
230+
log_p_birth_ + this->sampler().log_pdf( new_obj );
220231
birth.proposal_probabilities.log_backward_prob =
221232
log_p_death_ - std::log( set.nb_objects() + 1.0 );
222233
return birth;
@@ -232,20 +243,22 @@ namespace geode
232243
return death;
233244
}
234245
const auto cur_object_id = death.old_object_id.value();
235-
death.type = MoveType::Death;
246+
death.type = MOVE_TYPE::death;
236247
death.proposal_probabilities.log_forward_prob =
237248
log_p_death_ - std::log( set.nb_free_objects() );
238249
death.proposal_probabilities.log_backward_prob =
239250
log_p_birth_
240-
+ this->sampler_.log_pdf(
251+
+ this->sampler().log_pdf(
241252
set.get_free_object( cur_object_id ) );
242253
return death;
243254
}
244255

245256
private:
257+
// NOLINTNEXTBEGIN(*-magic-numbers)
246258
double birth_ratio_{ 0.5 };
247259
double log_p_birth_{ 0. };
248260
double log_p_death_{ 0. };
261+
// NOLINTNEXTEND(*-magic-numbers)
249262
};
250263

251264
template < typename ObjectType >
@@ -268,21 +281,22 @@ namespace geode
268281
{
269282
return change;
270283
}
271-
change.type = MoveType::Change;
284+
change.type = MOVE_TYPE::change;
272285
const auto& object_to_change =
273286
set.get_free_object( change.old_object_id.value() );
274287
change.new_object =
275-
this->sampler_.change( object_to_change, engine );
288+
this->sampler().change( object_to_change, engine );
276289
change.proposal_probabilities.log_forward_prob =
277-
this->sampler_.log_pdf( change.new_object.value() );
290+
this->sampler().log_pdf( change.new_object.value() );
278291
change.proposal_probabilities.log_backward_prob =
279-
this->sampler_.log_pdf( object_to_change );
292+
this->sampler().log_pdf( object_to_change );
280293
return change;
281294
}
282-
std::string string() const override
295+
296+
[[nodiscard]] std::string string() const override
283297
{
284298
return absl::StrCat( "Change Move (proportion weight: ",
285-
this->proportion_weight_, ")" );
299+
this->proportion_weight(), ")" );
286300
}
287301
};
288302
} // namespace geode

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace geode
5757
set_id };
5858
};
5959

60-
std::string string() const
60+
[[nodiscard]] std::string string() const
6161
{
6262
return absl::StrCat( "Move proposal on subset: ", set_id, " -- ",
6363
proposed_move.string() );
@@ -67,7 +67,10 @@ namespace geode
6767
template < typename ObjectType >
6868
class ProposalKernel
6969
{
70+
OPENGEODE_DISABLE_COPY_AND_MOVE( ProposalKernel );
71+
7072
public:
73+
ProposalKernel() = default;
7174
virtual ~ProposalKernel() = default;
7275

7376
Proposal< ObjectType > propose( const ObjectSets< ObjectType >& current,
@@ -100,7 +103,7 @@ namespace geode
100103
initialize_probabilities();
101104
}
102105

103-
std::string string() const
106+
[[nodiscard]] std::string string() const
104107
{
105108
auto message = absl::StrCat( "Proposal Kernel:",
106109
"\n\t - number of moves: ", set_moves_.size() );
@@ -119,7 +122,7 @@ namespace geode
119122
}
120123

121124
private:
122-
std::vector< double > compute_probabilities() const
125+
[[nodiscard]] std::vector< double > compute_probabilities() const
123126
{
124127
std::vector< double > probabilities( set_moves_.size(), 0. );
125128

@@ -141,8 +144,8 @@ namespace geode
141144

142145
// Normalize
143146
std::transform( probabilities.begin(), probabilities.end(),
144-
probabilities.begin(), [total]( double p ) {
145-
return p / total;
147+
probabilities.begin(), [total]( double cur_proba ) {
148+
return cur_proba / total;
146149
} );
147150
return probabilities;
148151
}
@@ -158,7 +161,9 @@ namespace geode
158161
cumulative_probs_.begin() );
159162

160163
if( !cumulative_probs_.empty() )
161-
cumulative_probs_.back() = 1.0; // ensure exact 1.0
164+
{
165+
cumulative_probs_.back() = 1.0;
166+
}
162167
}
163168
void initialize_move_probabilities(
164169
const std::vector< double >& probabilities )

include/geode/stochastic/spatial/object_neighborhood.hpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,24 @@
2222
*/
2323

2424
#pragma once
25+
26+
#include <geode/stochastic/common.hpp>
27+
2528
#include <geode/stochastic/spatial/details/RTree.hpp>
2629
#include <optional>
2730

2831
namespace geode
2932
{
3033
FORWARD_DECLARATION_DIMENSION_CLASS( BoundingBox );
31-
}
34+
} // namespace geode
3235

3336
namespace geode
3437
{
3538
struct ObjectId
3639
{
37-
index_t index;
38-
bool fixed;
39-
uuid set_id;
40+
index_t index{ NO_ID };
41+
bool fixed{ false };
42+
uuid set_id{};
4043
bool operator==( const ObjectId& other ) const noexcept
4144
{
4245
return index == other.index && fixed == other.fixed
@@ -57,20 +60,23 @@ namespace geode
5760
template < index_t dimension >
5861
class ObjectNeighborhood
5962
{
63+
OPENGEODE_DISABLE_COPY_AND_MOVE( ObjectNeighborhood );
64+
6065
public:
6166
ObjectNeighborhood();
6267
~ObjectNeighborhood() = default;
6368

64-
void add( const BoundingBox< dimension >& box, const ObjectId& id );
69+
void add( const BoundingBox< dimension >& box, const ObjectId& obj_id );
6570
void update( const BoundingBox< dimension >& old_box,
6671
const BoundingBox< dimension >& new_box,
67-
const ObjectId& id );
72+
const ObjectId& obj_id );
6873
void update( const BoundingBox< dimension >& box,
6974
const ObjectId& old_id,
7075
const ObjectId& new_id );
71-
void remove( const BoundingBox< dimension >& box, const ObjectId& id );
76+
void remove(
77+
const BoundingBox< dimension >& box, const ObjectId& obj_id );
7278

73-
std::vector< ObjectId > get_all_neighbor_ids(
79+
[[nodiscard]] std::vector< ObjectId > get_all_neighbor_ids(
7480
const BoundingBox< dimension >& box,
7581
const std::vector< uuid >& targeted_set_ids,
7682
std::optional< ObjectId > exclude_self_id ) const;

0 commit comments

Comments
 (0)