23 #ifndef PT_SAMPLE_SCENES_H
24 #define PT_SAMPLE_SCENES_H
36 #include <boost/foreach.hpp>
37 #include <boost/optional.hpp>
42 #include <ergo/record.h>
73 bool infer_head =
true
75 : N_height(0.0, hsdv),
78 m_infer_head(infer_head)
105 posterior_p_(&posterior),
108 num_leapfrog_steps_(5),
109 pos_opt_step_size_(0.00001),
110 pos_grad_step_size_(0.01),
111 estimate_grad_step_size_(true),
115 infer_head_(infer_head)
125 posterior_p_ = &post;
140 num_leapfrog_steps_ = lf_steps;
149 pos_grad_step_size_ = pss;
150 estimate_grad_step_size_ =
false;
164 class SampleIterator,
165 class ProposalIterator,
169 const Scene& initial_scene,
170 boost::optional<StepIterator> step_out,
171 boost::optional<SampleIterator> sample_out,
172 boost::optional<ProposalIterator> proposed_out,
173 boost::optional<PdIterator> pd_out
189 Mh_step make_mh_size_step(
bool infer_head)
const;
192 template<
class TrajStep,
class TRecIter,
class SRecIter>
210 size_t num_iterations_;
211 size_t num_leapfrog_steps_;
212 double pos_opt_step_size_;
213 double pos_grad_step_size_;
214 bool estimate_grad_step_size_;
225 class SampleIterator,
226 class ProposalIterator,
228 void Sample_scenes::operator()
230 const Scene& initial_scene,
231 boost::optional<StepIterator> step_out,
232 boost::optional<SampleIterator> sample_out,
233 boost::optional<ProposalIterator> proposed_out,
234 boost::optional<PdIterator> pd_out
238 typedef std::vector<Hmc_step::record_t> Hmc_rec_vector;
239 typedef std::vector<Mh_step::record_t> Mh_rec_vector;
241 if(num_iterations_ == 0)
return;
244 Scene_adapter scene_adapter(posterior_p_->vis_off(), infer_head_);
245 size_t scene_size = scene_adapter.
size(&initial_scene);
247 if(scene_size == 0)
return;
250 Scene cur_scene = initial_scene;
251 double cur_lt = (*posterior_p_)(cur_scene);
252 Scene best_scene = initial_scene;
255 Hmc_step traj_step = make_hmc_traj_step(scene_adapter, best_scene);
256 Mh_step size_step = make_mh_size_step(infer_head_);
265 hrv.push_back(ergo::make_best_sample_recorder(&best_scene).replace());
266 mrv.push_back(ergo::make_best_sample_recorder(&best_scene).replace());
271 hrv.push_back(ergo::make_hmc_detail_recorder(*step_out));
272 mrv.push_back(ergo::make_mh_detail_recorder(*step_out));
278 hrv.push_back(ergo::make_sample_recorder(*sample_out));
279 mrv.push_back(ergo::make_sample_recorder(*sample_out));
285 hrv.push_back(ergo::make_proposed_recorder(*proposed_out));
286 mrv.push_back(ergo::make_proposed_recorder(*proposed_out));
287 traj_step.store_proposed();
288 size_step.store_proposed();
296 traj_step.store_proposed();
297 size_step.store_proposed();
313 BOOST_FOREACH(
const Target& itarget, initial_scene.association)
315 const Target& btarget = *tg_p++;
332 template<
class TrajStep,
class TRecIter,
class SRecIter>
333 void Sample_scenes::run_steps
341 const Mh_step& size_step,
348 int D =
dims(scene,
true);
351 size_t num_iterations =
std::max(5, D / 10);
352 std::cout <<
"\t\t\t\tscene size: " << std::setw(4) << D <<
" iters: " << num_iterations << std::endl;
354 double prev_best_lt = lt;
355 for(
size_t i = 1;
i <= num_iterations;
i++)
358 traj_step(scene, lt);
373 for(TRecIter rec_p = tfirst; rec_p != tlast; ++rec_p)
375 (*rec_p)(traj_step, scene, lt);
381 size_step(scene, lt);
382 for(SRecIter rec_p = sfirst; rec_p != slast; ++rec_p)
384 (*rec_p)(size_step, scene, lt);
387 if(lt > best_lt) best_lt = lt;
389 if((
i + 1) % 10 == 0)
391 if(best_lt > prev_best_lt)
392 prev_best_lt = best_lt;
Definition: mcmcda_proposer.h:57
void set_gradient_step_size(double pss)
Set the gradient approximation step sizes.
Definition: pt_sample_scenes.h:147
void set_num_leapfrog_steps(size_t lf_steps)
Set the number of leapfrog steps.
Definition: pt_sample_scenes.h:138
Adapts a Scene into a VectorModel for HMC sampling.
Definition: pt_scene_adapter.h:183
Int_matrix::Value_type max(const Int_matrix &mat)
Return the maximum value in this matrix.
Definition: l_int_matrix.h:1397
Definition of various standard probability distributions.
Parent::const_iterator const_iterator
Definition: mcmcda_association.h:122
bool all_new_targets(const Scene &scene)
Helper fcn; returns true if the scene has only new targets.
Definition: pt_sample_scenes.cpp:46
Definition: pt_sample_scenes.h:99
const Scene_posterior & scene_posterior() const
Get posterior distribution.
Definition: pt_sample_scenes.h:120
Trajectory & trajectory() const
Get this target's current 3D positions.
Definition: pt_target.h:85
Class that represents a full scene in the PT universe.
Definition: pt_scene.h:40
void swap(Scene &s1, Scene &s2)
Swap two scenes.
Definition: pt_scene.h:69
Face_2d_trajectory & face_trajectory() const
Get this target's current 2D body trajectory.
Definition: pt_target.h:97
void set_scene_posterior(const Scene_posterior &post)
Set posterior distribution.
Definition: pt_sample_scenes.h:123
Definition: pt_sample_scenes.h:99
void set_hmc_step_size(double pss)
Set the HMC step sizes.
Definition: pt_sample_scenes.h:144
Posterior_detail_recorder< OutputIterator > make_pd_recorder(OutputIterator it, const Scene_posterior &post)
Convenience function to create a Posterior_detail_recorder.
Definition: pt_recorder.h:76
size_t dims(const Scene &scene, bool respect_changed=true, bool infer_head=true)
Computes the number of variables in this scene.
Definition: pt_scene.cpp:106
void set_size_proposal_sigmas(double hsdv, double wsdv, double gsdv)
Set the MH proposal sigmas.
Definition: pt_sample_scenes.h:154
Propose_person_size(double hsdv, double wsdv, double gsdv, bool infer_head=true)
Definition: pt_sample_scenes.h:69
Sample_method
Definition: pt_sample_scenes.h:99
Class that represents a target moving through space.
Definition: pt_target.h:50
void set_num_iterations(size_t num_its)
Set the number of iterations and the fraction of HMC steps (vis-a-vis MH steps).
Definition: pt_sample_scenes.h:135
Use HMC to draw samples from the scene posterior using HMC.
Definition: pt_sample_scenes.h:96
Body_2d_trajectory & body_trajectory() const
Get this target's current 2D body trajectory.
Definition: pt_target.h:91
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
Posterior distribution of a scene.
Definition: pt_scene_posterior.h:53
ergo::mh_step< Scene > Mh_step
Definition: pt_sample_scenes.h:101
void swap(kjb::Gsl_Multimin_fdf &m1, kjb::Gsl_Multimin_fdf &m2)
Swap two wrapped multimin objects.
Definition: gsl_multimin.h:693
boost::math::normal Normal_distribution
Definition: prob_distribution.h:68
size_t size(const Scene *s) const
Get the number of elements in this scene.
Definition: pt_scene_adapter.h:233
void set_sample_method(Sample_method method)
Set the sampling method.
Definition: pt_sample_scenes.h:129
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
Sample_scenes(const Scene_posterior &posterior, bool infer_head)
Definition: pt_sample_scenes.h:104
D
Definition: APPgetLargeConnectedEdges.m:106
Support for error handling exception classes in libKJB.
Object thrown when a program lacks required resources or libraries.
Definition: l_exception.h:539
Proposal distribution/mechanism for the size of targets.
Definition: pt_sample_scenes.h:65
ergo::hmc_step< Scene > Hmc_step
Definition: pt_sample_scenes.h:100