Skip to content

Commit d724578

Browse files
create simulation context builder
1 parent e0a5a94 commit d724578

5 files changed

Lines changed: 186 additions & 126 deletions

File tree

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

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,19 @@
2626
#include <geode/stochastic/common.hpp>
2727

2828
#include <geode/stochastic/spatial/object_sets.hpp>
29+
#include <geode/stochastic/spatial/spatial_domain.hpp>
2930

3031
#include <geode/stochastic/inference/target_statistics.hpp>
3132

3233
#include <geode/stochastic/models/model.hpp>
3334

3435
#include <geode/stochastic/sampling/direct/object_set_sampler/object_set_sampler.hpp>
35-
#include <geode/stochastic/sampling/mcmc/metropolis_hasting_sampler.hpp>
36+
#include <geode/stochastic/sampling/direct/object_set_sampler/point_set_sampler.hpp>
3637

37-
#include <geode/stochastic/spatial/spatial_domain.hpp>
38+
#include <geode/stochastic/sampling/mcmc/proposal/classical_proposals.hpp>
39+
40+
#include <geode/stochastic/sampling/mcmc/metropolis_hasting_sampler.hpp>
41+
#include <geode/stochastic/sampling/mcmc/proposal/object_set_dynamic_config.hpp>
3842

3943
namespace geode
4044
{
@@ -65,4 +69,88 @@ namespace geode
6569
std::unique_ptr< geode::MetropolisHastings< ObjectType > > mh_sampler;
6670
};
6771

72+
template < typename ObjectType >
73+
struct SimulationContextConfig
74+
{
75+
SpatialDomainConfig< ObjectType::dim > domain;
76+
77+
std::vector< ObjectSetConfig > sets;
78+
std::vector< ObjectSetDynamicsConfig > proposals;
79+
80+
geode::ModelConfig model;
81+
};
82+
83+
template < typename ObjectType >
84+
[[nodiscard]] geode::SimulationContext< ObjectType >
85+
build_simulation_context(
86+
const SimulationContextConfig< ObjectType >& config )
87+
{
88+
geode::SimulationContext< ObjectType > context;
89+
90+
// -------------------------
91+
// Domain
92+
// -------------------------
93+
context.domain = geode::build_spatial_domain( config.domain );
94+
95+
// -------------------------
96+
// Sets
97+
// -------------------------
98+
99+
// auto proposal_kernel =
100+
// std::make_unique< geode::ProposalKernel< geode::Point2D >
101+
// >();
102+
// for( const auto& set_desc : set_descriptors_ )
103+
// {
104+
// const auto set_id = context.object_sets->add_set(
105+
// set_desc.name ); context.set_samplers.push_back(
106+
// std::make_unique< geode::UniformPointSetSampler< 2 >
107+
// >(
108+
// *context.domain ) );
109+
// geode::add_birth_death_change_moves(
110+
// context.set_samplers.back(),
111+
// *proposal_kernel, set_id, set_desc.birth_ratio,
112+
// set_desc.death_ratio, set_desc.change_ratio );
113+
// }
114+
// return proposal_kernel;
115+
116+
for( const auto& set_cfg : config.sets )
117+
{
118+
const auto set_id = context.object_sets->add_set( set_cfg.name );
119+
geode_unused( set_id );
120+
}
121+
122+
// -------------------------
123+
// Model
124+
// -------------------------
125+
context.model = geode::build_model< ObjectType >(
126+
config.model, *context.object_sets, *context.domain );
127+
128+
// -------------------------
129+
// Proposal
130+
// -------------------------
131+
auto proposal_kernel =
132+
std::make_unique< geode::ProposalKernel< ObjectType > >();
133+
for( const auto& set_proposal : config.proposals )
134+
{
135+
const auto set_id =
136+
context.object_sets->get_set_uuid( set_proposal.name );
137+
context.set_samplers.push_back(
138+
std::make_unique< geode::UniformPointSetSampler< 2 > >(
139+
*context.domain ) );
140+
141+
geode::add_birth_death_change_moves( context.set_samplers.back(),
142+
*proposal_kernel, set_id, set_proposal.birth_ratio,
143+
set_proposal.death_ratio, set_proposal.change_ratio );
144+
}
145+
146+
// -------------------------
147+
// MH sampler
148+
// -------------------------
149+
context.mh_sampler =
150+
std::make_unique< geode::MetropolisHastings< geode::Point2D > >(
151+
*context.model, std::move( proposal_kernel ) );
152+
153+
return context;
154+
}
155+
68156
} // namespace geode
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2019 - 2026 Geode-solutions
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*
22+
*/
23+
24+
#pragma once
25+
namespace geode
26+
{
27+
struct ObjectSetDynamicsConfig
28+
{
29+
std::string name;
30+
31+
double birth_ratio = 1.0;
32+
double death_ratio = 1.0;
33+
double change_ratio = 1.0;
34+
};
35+
} // namespace geode

include/geode/stochastic/spatial/object_sets.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ namespace geode
4646

4747
namespace geode
4848
{
49+
struct ObjectSetConfig
50+
{
51+
std::string name;
52+
53+
double birth_ratio = 1.0;
54+
double death_ratio = 1.0;
55+
double change_ratio = 1.0;
56+
};
57+
4958
template < typename Type >
5059
class ObjectSets
5160
{

src/geode/stochastic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ add_geode_library(
7676
"models/model.hpp"
7777
"sampling/mcmc/proposal/classical_proposals.hpp"
7878
"sampling/mcmc/proposal/moves.hpp"
79+
"sampling/mcmc/proposal/object_set_dynamic_config.hpp"
7980
"sampling/mcmc/proposal/proposal_kernel.hpp"
8081
"sampling/mcmc/metropolis_hasting_sampler.hpp"
8182
"sampling/mcmc/simulation_runner.hpp"

0 commit comments

Comments
 (0)