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 
94  Threshold()=default;
95 
96  Threshold(float e, float a, float d, float aff, float d_dim):
97  endPosition(e), angle(a), dPosition(d), affordance(aff), D_dimensions(d_dim){}
98 
99  float for_robot_position(){
100  return endPosition;
101  }
102 
103  float for_robot_angle(){
104  return angle;
105  }
106 
107  float for_affordance(){
108  return affordance;
109  }
110 
114  Bundle for_Di()const{
115  return Di;
116  }
117 
121  Bundle for_Dn()const{
122  return Dn;
123  }
124 
125  void set_Dn(const Bundle & b){
126  Dn=b;
127  }
128 
129  void set_Di(const Bundle & b){
130  Di=b;
131  }
136  return Bundle(dPosition, dPosition, angle, D_dimensions, D_dimensions);
137  }
138 
139 
140  private:
141  float endPosition=0.05;// maximum radius from candidate state's end pose
142  float angle= M_PI/6; // maximum angle difference
143  float dPosition= 0.065;// maximum difference between disturbance positions
144  float affordance =0; //maximum difference between affordances
145  float D_dimensions=0.03; //maximum differences in disturbance dimensions
146  Bundle Di=Bundle(dPosition, dPosition, angle, D_dimensions, D_dimensions), Dn=Di;
147 };
148 
150  protected:
151  Bundle Di_weights, Dn_weights;
152  float mu=0.001; //learning rate
153  public:
154  ThresholdLearner()=default;
155 
156  void set_learning_rate(float f){
157  mu=f;
158  }
159 
160  Bundle get_Di_weights(){
161  return Di_weights;
162  }
163 
164  Bundle get_Dn_weights(){
165  return Dn_weights;
166  }
167 
168  void log(){
169  FILE *f= fopen("/tmp/threshold_weights.txt", "a+");
170  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(),
171  Di_weights.get_y(),
172  Di_weights.get_angle(),
173  Di_weights.get_length(),
174  Di_weights.get_width(),
175  Dn_weights.get_x(),
176  Dn_weights.get_y(),
177  Dn_weights.get_angle(),
178  Dn_weights.get_length(),
179  Dn_weights.get_width()
180  );
181  fclose(f);
182  }
183 
184  void make_log(){
185  FILE *f= fopen("/tmp/threshold_weights", "w+");
186  fclose(f);
187  }
188 
194  virtual void Di_tune(const Bundle & error, const Bundle & x)=0;
195 
203  virtual Bundle update_bundle(const Bundle & error, const Bundle & x)=0;
204 
205  Threshold get_weighted(const Threshold & t){
206  Threshold result;
207  result.set_Di(t.for_Di()*Di_weights);
208  result.set_Dn(t.for_Dn()*Dn_weights);
209  return result;
210  }
211 
212 
213 };
Definition: disturbance.h:45
Definition: threshold.h:10
Bundle operator*(const Bundle &b) const
Dot product between two bundles.
Definition: threshold.cpp:3
Definition: threshold.h:149
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:121
Bundle for_Di() const
Returns a bundle of thresholds for the initial disturbance.
Definition: threshold.h:114
Bundle bundle_threshold()
Returns a bundle of parameters for disturbance matching.
Definition: threshold.h:135