KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sample_annealing.h
Go to the documentation of this file.
1 /* $Id: sample_annealing.h 12839 2012-08-08 23:51:05Z ksimek $ */
2 /* {{{=========================================================================== *
3  |
4  | Copyright (c) 1994-2011 by Kobus Barnard (author)
5  |
6  | Personal and educational use of this code is granted, provided that this
7  | header is kept intact, and that the authorship is not misrepresented, that
8  | its use is acknowledged in publications, and relevant papers are cited.
9  |
10  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
11  |
12  | Please note that the code in this file has not necessarily been adequately
13  | tested. Naturally, there is no guarantee of performance, support, or fitness
14  | for any particular task. Nonetheless, I am interested in hearing about
15  | problems that you encounter.
16  |
17  | Author: Kyle Simek
18  * =========================================================================== }}}*/
19 
20 // vim: tabstop=4 shiftwidth=4 foldmethod=marker
22 #include <sample_cpp/sample_step.h>
24 
25 template <class Model>
27 {
29  typedef boost::function1<void, double> Set_temperature_callback;
30 
31  // Copying this sampler will cause aliasing of the underlying steps, and
32  // there's no obvious way to implemnt an array of arbitrary
33  // concepts without this occurring. For now, lets be safe and disable copying
35  Annealing_sampler& operator=(const Annealing_sampler& s) { return *this; }
36 
37 public:
38  using Base::add_step;
39 
45  const Model& initial_state,
46  double initial_log_target) :
47  Base(initial_state, initial_log_target),
48  temperature_(1.0)
49  {}
50 
51  template <class StepType>
52  void add_annealing_step(const StepType& step, double prob, const std::string& name = "")
53  {
55 
56  boost::shared_ptr<StepType> tmp(new StepType(step));
57  add_annealing_step(tmp, prob, name);
58  }
59 
60 
61  template <class StepType>
62  void add_annealing_step(StepType* step, double prob, const std::string& name = "")
63  {
65 
66  add_annealing_step(boost::shared_ptr<StepType>(step, Base::null_deleter()), prob, name);
67  }
68 
69  template <class StepType>
70  void add_annealing_step(const boost::shared_ptr<StepType>& step, double prob, const std::string& name = "")
71  {
73 
74  step->set_temperature(temperature_);
75 
76  m_temperature_setters.push_back(boost::bind(&StepType::set_temperature, step.get(), _1));
77 
78  Base::add_step(step, prob, name);
79  }
80 
82  void add_temperature_changed_callback(const boost::function1<void, double>& cb)
83  {
84  m_temperature_setters.push_back(cb);
85  }
86 
87 // template <class StepType>
88 // void add_step(const typename boost::reference_wrapper<StepType>& step_ref, double prob, const std::string& name)
89 // {
90 // BOOST_CONCEPT_ASSERT((Annealable<StepType>));
91 //
92 // // probably unnecessary:
93 // m_any_steps.push_back(step_ref);
94 //
95 // Base::add_step(step_ref, prob, name);
96 //
97 // Set_temperature_callback set_temp;
98 // set_temp = boost::bind(&StepType::set_temperature, step_ref, _1);
99 //
100 // m_temperature_setters.push_back(set_temp);
101 // m_temperature_setters.back()(temperature_);
102 // }
103 
104  void set_temperature(double temperature)
105  {
106  if(temperature_ == temperature) return;
107 
108  temperature_ = temperature;
109  for(size_t i = 0; i < m_temperature_setters.size(); i++)
110  {
111  m_temperature_setters[i](temperature_);
112  }
113  }
114 private:
115  std::vector<Set_temperature_callback> m_temperature_setters;
116  double temperature_;
117 };
118 
119 
120 template <class Proposer, class Model>
122 {
123 public:
125 
126  Annealing_proposer_wrapper(const Proposer& p) :
127  p_(p)
128  {}
129 
130  Mh_proposal_result operator()(const Model& in, Model& out) const
131  {
132  return p_(in, out);
133  }
134 
135  // ignore temperature
136  void set_temperature(double t) {}
137 private:
138  const Proposer p_;
139 };
140 
150 template<typename Model, typename Proposer = typename Mh_model_proposer<Model>::Type >
151 class Annealing_mh_step : public Basic_mh_step<Model, Proposer>
152 {
153  BOOST_CONCEPT_ASSERT((Annealable<Proposer>));
154 
156  typedef typename Base::Target_distribution Target_distribution;
157 
158 public:
159  Annealing_mh_step(const Target_distribution& log_target, const Proposer& proposer) :
160  Base(log_target, proposer)
161  {
162  set_temperature(1.0);
163  }
164 
169  Base(step)
170  {
171  set_temperature(1.0);
172  }
173 
178  {
179  Base::operator=(step);
180  return *this;
181  }
182 
183 
185  void set_temperature(double T)
186  {
188  Base::m_proposer.set_temperature(T);
189  }
190 };
191 
Annealing_proposer_wrapper(const Proposer &p)
Definition: sample_annealing.h:126
Definition: sample_annealing.h:151
Definition: sample_annealing.h:121
Annealing_mh_step & operator=(const Annealing_mh_step< Model, Proposer > &step)
Assignment.
Definition: sample_annealing.h:177
void add_step(const StepType &step, double prob, const std::string &name="")
Add a new step with associated probability. This does NOT make sure probabilities add up to 1...
Definition: sample_sampler.h:364
Definition: sample_concept.h:60
Proposer Proposer
Definition: sample_step.h:225
Indicates the result of an MH proposal. It is simply a pair of probabilities, forward and backward...
Definition: sample_base.h:334
Annealing_mh_step(const Annealing_mh_step< Model, Proposer > &step)
Copy-constructor.
Definition: sample_annealing.h:168
Annealing_mh_step(const Target_distribution &log_target, const Proposer &proposer)
Definition: sample_annealing.h:159
Definition: sample_step.h:219
void set_temperature(double temperature)
Definition: sample_annealing.h:104
void set_temperature(double T)
Definition: sample_step.h:333
Mh_proposal_result operator()(const Model &in, Model &out) const
Definition: sample_annealing.h:130
Annealing_sampler(const Model &initial_state, double initial_log_target)
Definition: sample_annealing.h:44
void add_temperature_changed_callback(const boost::function1< void, double > &cb)
Add an arbitrary function to be called when temperature changes.
Definition: sample_annealing.h:82
Definition: sample_sampler.h:281
void set_temperature(double t)
Definition: sample_annealing.h:136
void add_annealing_step(StepType *step, double prob, const std::string &name="")
Definition: sample_annealing.h:62
Parent::Target_distribution Target_distribution
Definition: sample_step.h:224
void add_annealing_step(const boost::shared_ptr< StepType > &step, double prob, const std::string &name="")
Definition: sample_annealing.h:70
Proposer m_proposer
Definition: sample_step.h:355
Definition: sample_concept.h:161
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
Definition: sample_sampler.h:284
void add_annealing_step(const StepType &step, double prob, const std::string &name="")
Definition: sample_annealing.h:52
void set_temperature(double T)
Definition: sample_annealing.h:185
BOOST_CONCEPT_ASSERT((BaseModel< Model >))
BOOST_CONCEPT_ASSERT((ModelProposer< Proposer, Model >))
Definition: sample_annealing.h:26