KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
psi_sample_util.h
Go to the documentation of this file.
1 /* $Id: psi_sample_util.h 10707 2011-09-29 20:05:56Z predoehl $ */
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
21 
22 #ifndef PSI_SAMPLE_UTIL_V1_H
23 #define PSI_SAMPLE_UTIL_V1_H
24 
26 #include <limits>
27 
28 namespace kjb
29 {
30 namespace psi
31 {
32 
39 {
41 // progress(num_iterations),
42  i(0)
43  {}
44 
45  template <class Model> // don't care what model type
46  void operator()(const Model&, const Step_log& log)
47  {
48  ++i;
49 // ++progress;
50  std::cout << "it " << i << std::endl;
51  std::cout << "lp " << log[0].lt << std::endl;
52  std::cout << "accept " << log[0].accept << std::endl;
53  }
54 // boost::progress_display progress;
55  int i;
56 };
57 
58 // the next three functions two functions set up a generic interface
59 // for the sampler to manipulate it.
60 inline void update_model_parameter(Model& m, int dimension, double delta)
61 {
62  double value = m.get(dimension);
63  value += delta;
64  m.set(dimension, value);
65 }
66 
67 inline size_t get_model_dimension(const Model& m)
68 {
69  return m.size();
70 }
71 
72 inline double get_model_parameter(const Model& m, size_t i)
73 {
74  return m.get(i);
75 }
76 
78 {
79  const size_t size = m.size();
80  const double inf = std::numeric_limits<double>::infinity();
81 
82  return kjb::Vector((int) size, inf);
83 }
84 
86 {
87  KJB(UNTESTED_CODE());
88  static const double inf = std::numeric_limits<double>::infinity();
89  const size_t size = m.size();
90 
91  return kjb::Vector((int) size, -inf);
92 
93 #if 0
94  for(int i = 0; i < size(); i++)
95  {
96  switch(m.get_units[i])
97  {
98  case DURATION_UNIT:
99  return 0;
100  default:
101  return -inf;
102  }
103  }
104 #endif
105 }
106 
107 
108 
109 // This tells us how much to step in each dimension while sampling.
110 // These will be multiplied by the gradient to get the step size.
112 {
113 public:
115  spacial_step_size_(),
116  angle_step_size_(),
117  velocity_step_size_(),
118  vangle_step_size_(),
119  duration_step_size_(),
120  length_step_size_(),
121  mass_step_size_()
122  {}
123 
125  double spacial_step,
126  double angle_step,
127  double velocity_step,
128  double angular_velocity_step,
129  double duration_step,
130  double mass_step,
131  double length_step,
132  double scale = 1) :
133  spacial_step_size_(spacial_step * scale),
134  angle_step_size_(angle_step * scale),
135  velocity_step_size_(velocity_step * scale),
136  vangle_step_size_(angular_velocity_step * scale),
137  duration_step_size_(duration_step * scale),
138  length_step_size_(length_step * scale),
139  mass_step_size_(mass_step * scale)
140  {}
141 
143  {
144  kjb::Vector result(m.size());
145  for(size_t i = 0; i < m.size(); i++)
146  {
147  Unit_type unit = m.get_units(i);
148  switch(unit)
149  {
150  case SPACIAL_UNIT:
151  result[i] = spacial_step_size_;
152  break;
153  case ANGLE_UNIT:
154  result[i] = angle_step_size_;
155  break;
156  case VSPACIAL_UNIT:
157  result[i] = velocity_step_size_;
158  break;
159  case VANGLE_UNIT: // angular_velocity
160  result[i] = vangle_step_size_;
161  break;
162  case TIME_UNIT:
163  result[i] = duration_step_size_;
164  break;
165  case MASS_UNIT:
166  result[i] = mass_step_size_;
167  break;
168  case LENGTH_UNIT:
169  result[i] = length_step_size_;
170  case DISCRETE_UNIT: // ???? maybe not a good idea to put it here
171  case UNKNOWN_UNIT:
172  case OTHER_UNIT:
173  case ASPACIAL_UNIT:
174  case AANGLE_UNIT:
175  default:
176  KJB_THROW_2(kjb::Runtime_error, "Unknown parameter type");
177  }
178 
179  }
180 
181  return result;
182  }
183 
184 private:
185  double spacial_step_size_;
186  double angle_step_size_;
187  double velocity_step_size_;
188  double vangle_step_size_;
189  double duration_step_size_;
190  double length_step_size_;
191  double mass_step_size_;
192 
193 
194 };
195 
196 
197 } // namespace psi
198 } // namespace kjb
199 #endif
Definition: psi_sample_util.h:38
kjb::Vector get_model_lower_bounds(const Model &m)
Definition: psi_sample_util.h:85
Definition: psi_units.h:40
double get(size_t dimension) const
Definition: psi_model.cpp:120
Definition: psi_units.h:37
Psi_step_size()
Definition: psi_sample_util.h:114
Definition: psi_units.h:38
Definition: psi_units.h:30
size_t get_model_dimension(const Model &m)
Definition: psi_sample_util.h:67
double get_model_parameter(const Model &m, size_t i)
Definition: psi_sample_util.h:72
Unit_type
Definition: psi_units.h:30
#define KJB(x)
Definition: l_util.h:9
Definition: sample_base.h:160
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
Unit_type get_units(size_t dimension) const
Definition: psi_model.cpp:80
Definition: psi_sample_util.h:111
size_t size() const
Definition: psi_model.cpp:211
Definition: psi_units.h:32
void set(size_t dimension, double value)
Definition: psi_model.cpp:165
void update_model_parameter(Model &m, int dimension, double delta)
Definition: psi_sample_util.h:60
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
Definition: psi_units.h:35
Progress_recorder()
Definition: psi_sample_util.h:40
Definition: psi_units.h:41
Definition: psi_units.h:36
int i
Definition: psi_sample_util.h:55
Definition: psi_units.h:39
Definition: sample_recorder.h:945
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
kjb::Vector operator()(const Model &m) const
Definition: psi_sample_util.h:142
for m
Definition: APPgetLargeConnectedEdges.m:64
Psi_step_size(double spacial_step, double angle_step, double velocity_step, double angular_velocity_step, double duration_step, double mass_step, double length_step, double scale=1)
Definition: psi_sample_util.h:124
Definition: psi_units.h:31
void scale(kjb::Axis_aligned_rectangle_2d &box, const kjb::Vector &s)
Definition: gr_2D_bounding_box.cpp:108
Definition: psi_units.h:34
kjb::Vector get_model_upper_bounds(const Model &m)
Definition: psi_sample_util.h:77
void operator()(const Model &, const Step_log &log)
Definition: psi_sample_util.h:46
Definition: psi_model.h:62
Definition: psi_units.h:33
Object thrown when computation fails somehow during execution.
Definition: l_exception.h:321