Bratmobile
threshold.h
1 #include <cmath>
2 #include <vector>
3 #include <cstdio>
4 
5 class BodyFeatures;
6 
10 class Bundle{
11  float x=1;
12  float y=1;
13  float angle=1;
14  float width=1;
15  float length=1;
16 
17  public:
18  Bundle()=default;
19 
20  Bundle(float _x, float _y, float _a, float _w, float _l): x(_x), y(_y), angle(_a), width(_w), length(_l){}
21 
27  Bundle operator*(const Bundle & b)const;
28 
32  Bundle operator*(float)const;
33 
34  bool operator<(const Bundle & bf);
35 
36  Bundle operator+(const Bundle & b)const;
37 
38  Bundle operator-(const Bundle & b)const;
39 
40  float radius()const {
41  return sqrt(pow(x,2)+pow(y,2));
42  }
43 
44  float get_x()const {
45  return x;
46  }
47 
48  float get_y()const{
49  return y;
50  }
51 
52  float get_angle()const{
53  return angle;
54  }
55 
56  float get_width()const{
57  return width;
58  }
59 
60  float get_length()const{
61  return length;
62  }
63 
64 
65  float sum_squares()const{
66  return pow(x, 2) + pow(y, 2)+pow(angle, 2) +pow(width, 2) +pow(length, 2);
67  }
68 
69  std::vector <float> get_vector()const{
70  return {x, y, angle, width, length};
71  }
72 };
73 
74 template <typename T>
75 T linear_rectify(T value){
76  if (value<0){
77  value=0;
78  }
79  return value;
80 }
81 
82 
83 Bundle linear_rectify(const Bundle &);
84 
85 
91 class Threshold{
92  public:
93  static constexpr float FIXED_ENDPOSE=0.05;// maximum radius from candidate state's end pose
94  static constexpr float FIXED_ANGLE= M_PI/6; // maximum angle difference
95  static constexpr float FIXED_DISTPOS= 0.065;// maximum difference between disturbance positions
96  static constexpr float FIXED_AFFORDANCE =0; //maximum difference between affordances
97  static constexpr float FIXED_DIMENSIONS=0.03; //maximum differences in disturbance dimensions
98 
99  Threshold()=default;
100 
101  Threshold(float e, float a, float d, float aff, float d_dim):
102  endPosition(e), angle(a), dPosition(d), affordance(aff), D_dimensions(d_dim){}
103 
104  float for_robot_position()const{
105  return endPosition;
106  }
107 
108  float for_robot_angle()const{
109  return angle;
110  }
111 
112  float for_affordance()const{
113  return affordance;
114  }
115 
119  Bundle for_Di()const{
120  return Di;
121  }
122 
126  Bundle for_Dn()const{
127  return Dn;
128  }
129 
130  void set_Dn(const Bundle & b){
131  Dn=b;
132  }
133 
134  void set_Di(const Bundle & b){
135  Di=b;
136  }
141  return Bundle(dPosition, dPosition, angle, D_dimensions, D_dimensions);
142  }
143 
144 
145 
146  private:
147  float endPosition=FIXED_ENDPOSE;// maximum radius from candidate state's end pose
148  float angle= FIXED_ANGLE; // maximum angle difference
149  float dPosition= FIXED_DISTPOS;// maximum difference between disturbance positions
150  float affordance =FIXED_AFFORDANCE; //maximum difference between affordances
151  float D_dimensions=FIXED_DIMENSIONS; //maximum differences in disturbance dimensions
152  Bundle Di=Bundle(dPosition, dPosition, angle, D_dimensions, D_dimensions), Dn=Di;
153 };
154 
156  protected:
157  Bundle Di_weights, Dn_weights;
158  float mu=0.001; //learning rate
159  public:
160  ThresholdLearner()=default;
161 
162  void set_learning_rate(float f){
163  mu=f;
164  }
165 
166  Bundle get_Di_weights(){
167  return Di_weights;
168  }
169 
170  Bundle get_Dn_weights(){
171  return Dn_weights;
172  }
173 
174  void log(){
175  FILE *f= fopen("/tmp/threshold_weights.txt", "a+");
176  fprintf(f, "%f\t%f\t%f\t%f\t%f\t\t%f\t%f\t%f\t%f\t%f\n", Di_weights.get_x(),
177  Di_weights.get_y(),
178  Di_weights.get_angle(),
179  Di_weights.get_length(),
180  Di_weights.get_width(),
181  Dn_weights.get_x(),
182  Dn_weights.get_y(),
183  Dn_weights.get_angle(),
184  Dn_weights.get_length(),
185  Dn_weights.get_width()
186  );
187  fclose(f);
188  }
189 
190  void make_log(){
191  FILE *f= fopen("/tmp/threshold_weights", "w+");
192  fclose(f);
193  }
194 
200  virtual void Di_tune(const Bundle & error, const Bundle & x)=0;
201 
209  virtual Bundle update_bundle(const Bundle & error, const Bundle & x)=0;
210 
211  Threshold get_weighted(const Threshold & t){
212  Threshold result;
213  result.set_Di(t.for_Di()*Di_weights);
214  result.set_Dn(t.for_Dn()*Dn_weights);
215  return result;
216  }
217 
218 
219 };
Definition: threshold.h:10
Bundle operator*(const Bundle &b) const
Dot product between two bundles.
Definition: threshold.cpp:3
Definition: threshold.h:155
virtual void Di_tune(const Bundle &error, const Bundle &x)=0
Tune weights for Di.
virtual Bundle update_bundle(const Bundle &error, const Bundle &x)=0
Updates a threshold according to a custom learning rule.
Definition: threshold.h:91
Bundle for_Dn() const
Returns a bundle of thresholds for the initial disturbance.
Definition: threshold.h:126
Bundle for_Di() const
Returns a bundle of thresholds for the initial disturbance.
Definition: threshold.h:119
Bundle bundle_threshold()
Returns a bundle of parameters for disturbance matching.
Definition: threshold.h:140
Definition: disturbance.h:47