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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#pragma once

#include <geode/stochastic/sampling/random_engine.hpp>
#include <geode/stochastic/spatial/object_set.hpp>
#include <geode/stochastic/spatial/object_sets.hpp>

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

StepResult< ObjectType > step(
ObjectSet< ObjectType >& state, RandomEngine& engine ) const
ObjectSets< ObjectType >& state, RandomEngine& engine ) const
{
Proposal< ObjectType > proposal =
proposal_kernel_->propose( state, engine );
Expand All @@ -79,7 +79,7 @@ namespace geode
return StepResult< ObjectType >{};
}

void walk( ObjectSet< ObjectType >& state,
void walk( ObjectSets< ObjectType >& state,
RandomEngine& engine,
index_t nb_steps ) const
{
Expand All @@ -90,7 +90,7 @@ namespace geode
}
}

ObjectSet< ObjectType > walk_copy( ObjectSet< ObjectType > initial,
ObjectSets< ObjectType > walk_copy( ObjectSets< ObjectType > initial,
RandomEngine& engine,
index_t nb_steps ) const
{
Expand Down Expand Up @@ -160,7 +160,7 @@ namespace geode
template < typename ApplyMove >
StepResult< ObjectType > accept_or_reject(
Proposal< ObjectType >& proposal,
ObjectSet< ObjectType >& state,
ObjectSets< ObjectType >& state,
RandomEngine& engine,
const double delta_log_energy,
ApplyMove&& apply_move ) const
Expand All @@ -183,7 +183,7 @@ namespace geode
}

StepResult< ObjectType > birth_step( Proposal< ObjectType >& proposal,
ObjectSet< ObjectType >& state,
ObjectSets< ObjectType >& state,
RandomEngine& engine ) const
{
const auto new_object = proposal.new_object();
Expand All @@ -193,12 +193,12 @@ namespace geode
[]( auto& state, auto& proposal ) {
state.add_object(
std::move( proposal.proposed_move.new_object.value() ),
proposal.subset_id );
proposal.set_id );
} );
};

StepResult< ObjectType > death_step( Proposal< ObjectType >& proposal,
ObjectSet< ObjectType >& state,
ObjectSets< ObjectType >& state,
RandomEngine& engine ) const
{
const auto old_object_id = proposal.old_object_id();
Expand All @@ -211,7 +211,7 @@ namespace geode
};

StepResult< ObjectType > change_step( Proposal< ObjectType >& proposal,
ObjectSet< ObjectType >& state,
ObjectSets< ObjectType >& state,
RandomEngine& engine ) const
{
const auto new_object = proposal.new_object();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/
#pragma once

#include <geode/stochastic/spatial/object_set.hpp>
#include <geode/stochastic/spatial/object_sets.hpp>

#include <geode/stochastic/sampling/mcmc/models/components/energy_term.hpp>

Expand All @@ -35,51 +35,51 @@ namespace geode
public:
explicit DensityTerm( std::string_view name,
double lambda,
absl::flat_hash_set< uuid > targeted_subset_ids )
absl::flat_hash_set< uuid > targeted_set_ids )
: EnergyTerm< ObjectType >(
name, lambda, std::move( targeted_subset_ids ) )
name, lambda, std::move( targeted_set_ids ) )
{
}

double total_log( const ObjectSet< ObjectType >& state ) const override
double total_log( const ObjectSets< ObjectType >& state ) const override
{
const auto n = this->statistic( state );
return this->contribution( n );
}

double delta_log_add( const ObjectSet< ObjectType >& /*state*/,
double delta_log_add( const ObjectSets< ObjectType >& /*state*/,
const ObjectRef< ObjectType >& new_object ) const override
{
if( !this->is_targeted_subset( new_object.subset ) )
if( !this->is_targeted_set( new_object.set_id ) )
{
return 0.0;
}
return this->contribution( 1.0 );
}

double delta_log_remove( const ObjectSet< ObjectType >& /*state*/,
double delta_log_remove( const ObjectSets< ObjectType >& /*state*/,
const ObjectId& object_id ) const override
{
if( !this->is_targeted_subset( object_id.subset ) )
if( !this->is_targeted_set( object_id.set_id ) )
{
return 0.0;
}
return this->contribution( -1.0 );
}

double delta_log_change( const ObjectSet< ObjectType >& /*state*/,
double delta_log_change( const ObjectSets< ObjectType >& /*state*/,
const ObjectId& /*old_object_id*/,
const ObjectRef< ObjectType >& /*new_object*/ ) const override
{
return 0.0;
}

double statistic( const ObjectSet< ObjectType >& state ) const override
double statistic( const ObjectSets< ObjectType >& state ) const override
{
index_t count{ 0 };
for( const auto& subset_uuid : this->targeted_subset_ids() )
for( const auto& set_id : this->targeted_set_ids() )
{
count += state.nb_objects_in_subset( subset_uuid );
count += state.nb_objects_in_set( set_id );
}
return static_cast< double >( count );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
#include <geode/basic/identifier_builder.hpp>

#include <geode/stochastic/common.hpp>
#include <geode/stochastic/spatial/object_set.hpp>
#include <geode/stochastic/spatial/object_sets.hpp>

#include <optional>

Expand Down Expand Up @@ -88,14 +88,14 @@ namespace geode
// std::string name;
// std::string type;
// double parameter_value;
// std::optional< uuid > targeted_subset_id{};
// std::optional< uuid > targeted_set_id{};
// }
//
// struct StatisticalDescription
// {
// std::string label;
// double value;
// std::optional< uuid > targeted_subset_id{};
// std::optional< uuid > targeted_set_id{};
// };

template < typename ObjectType >
Expand All @@ -104,10 +104,9 @@ namespace geode
public:
explicit EnergyTerm( std::string_view name,
double param,
absl::flat_hash_set< uuid >&& targeted_subset_ids )
: Identifier{},
energy_scale_{ param },
targeted_subset_ids_{ std::move( targeted_subset_ids ) }
absl::flat_hash_set< uuid >&& targeted_set_ids )
: energy_scale_{ param },
targeted_set_ids_{ std::move( targeted_set_ids ) }
{
IdentifierBuilder builder( *this );
builder.set_name( name );
Expand All @@ -120,9 +119,9 @@ namespace geode
return energy_scale_.parameter();
}

const absl::flat_hash_set< uuid >& targeted_subset_ids() const
const absl::flat_hash_set< uuid >& targeted_set_ids() const
{
return targeted_subset_ids_;
return targeted_set_ids_;
}

/// Energy contribution for a given statistic multiplier
Expand All @@ -132,58 +131,57 @@ namespace geode
}

virtual double total_log(
const ObjectSet< ObjectType >& state ) const = 0;
const ObjectSets< ObjectType >& state ) const = 0;

virtual double delta_log_add( const ObjectSet< ObjectType >& state,
virtual double delta_log_add( const ObjectSets< ObjectType >& state,
const ObjectRef< ObjectType >& new_object ) const = 0;

virtual double delta_log_remove( const ObjectSet< ObjectType >& state,
virtual double delta_log_remove( const ObjectSets< ObjectType >& state,
const ObjectId& object_id ) const = 0;

virtual double delta_log_change( const ObjectSet< ObjectType >& state,
virtual double delta_log_change( const ObjectSets< ObjectType >& state,
const ObjectId& old_object_id,
const ObjectRef< ObjectType >& new_object ) const = 0;

virtual double statistic(
const ObjectSet< ObjectType >& state ) const = 0;
const ObjectSets< ObjectType >& state ) const = 0;

std::string string() const
{
auto message =
absl::StrCat( "Term : ", name(), "; uuid: ", id().string(),
" parameter value: ", energy_scale_.parameter(),
" applyied on ", targeted_subset_ids_.size(),
" applyied on ", targeted_set_ids_.size(),
" object subsets -->" );
for( const auto& subset_uuid : targeted_subset_ids_ )
for( const auto& set_id : targeted_set_ids_ )
{
absl::StrAppend( &message, "\t", subset_uuid.string() );
absl::StrAppend( &message, "\t", set_id.string() );
}
return message;
}

protected:
bool is_targeted_subset( const uuid& subset_id ) const
bool is_targeted_set( const uuid& set_id ) const
{
return targeted_subset_ids_.find( subset_id )
!= targeted_subset_ids_.end();
return targeted_set_ids_.find( set_id ) != targeted_set_ids_.end();
}

template < typename Func >
void for_each_targeted_object(
const ObjectSet< ObjectType >& state, Func&& do_apply ) const
const ObjectSets< ObjectType >& state, Func&& do_apply ) const
{
for( const auto& targeted_subset_id : targeted_subset_ids_ )
for( const auto& targeted_set_id : targeted_set_ids_ )
{
for( const auto id : geode::Range{
state.nb_objects_in_subset( targeted_subset_id ) } )
for( const auto id :
geode::Range{ state.nb_objects_in_set( targeted_set_id ) } )
{
do_apply( ObjectId{ id, targeted_subset_id } );
do_apply( ObjectId{ id, targeted_set_id } );
}
}
}

private:
detail::EnergyScale energy_scale_;
absl::flat_hash_set< uuid > targeted_subset_ids_;
absl::flat_hash_set< uuid > targeted_set_ids_;
};
} // namespace geode
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ namespace geode
{
const uuid id = term->id();
energy_terms_.emplace( id, term );
for( const uuid& subset_id : term->targeted_subset_ids() )
for( const uuid& set_id : term->targeted_set_ids() )
{
subset_to_terms_[subset_id].push_back( term );
set_to_terms_[set_id].push_back( term );
}
return id;
}
Expand All @@ -34,18 +34,18 @@ namespace geode

auto term = it->second;

for( const uuid& subset_id : term->targeted_subset_ids() )
for( const uuid& set_id : term->targeted_set_ids() )
{
auto vec_it = subset_to_terms_.find( subset_id );
if( vec_it == subset_to_terms_.end() )
auto vec_it = set_to_terms_.find( set_id );
if( vec_it == set_to_terms_.end() )
continue;

auto& vec = vec_it->second;
vec.erase(
std::remove( vec.begin(), vec.end(), term ), vec.end() );

if( vec.empty() )
subset_to_terms_.erase( vec_it );
set_to_terms_.erase( vec_it );
}

energy_terms_.erase( it );
Expand All @@ -55,7 +55,7 @@ namespace geode
void clear()
{
energy_terms_.clear();
subset_to_terms_.clear();
set_to_terms_.clear();
}

[[nodiscard]] index_t size() const
Expand All @@ -82,11 +82,11 @@ namespace geode

[[nodiscard]] const std::vector<
std::shared_ptr< EnergyTerm< ObjectType > > >&
terms_for_subset( const uuid& subset_id ) const
terms_for_set( const uuid& set_id ) const
{
const auto it = subset_to_terms_.find( subset_id );
OPENGEODE_EXCEPTION( it != subset_to_terms_.end(),
"[EnergyTermCollection] - Object Subset (", subset_id.string(),
const auto it = set_to_terms_.find( set_id );
OPENGEODE_EXCEPTION( it != set_to_terms_.end(),
"[EnergyTermCollection] - Object Subset (", set_id.string(),
") does not have any energy term." );
return it->second;
}
Expand All @@ -103,14 +103,12 @@ namespace geode
}

private:
// strong ownership
absl::flat_hash_map< uuid, std::shared_ptr< EnergyTerm< ObjectType > > >
energy_terms_;

// subset index (shared ownership)
absl::flat_hash_map< uuid,
std::vector< std::shared_ptr< EnergyTerm< ObjectType > > > >
subset_to_terms_;
set_to_terms_;
};

} // namespace geode
Loading