Robot Simulator of the Robotics Group for Self-Organization of Control  0.8.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
templatevalueanalysation.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2011 LpzRobots development team *
3  * Georg Martius <georg dot martius at web dot de> *
4  * Frank Guettler <guettler at informatik dot uni-leipzig dot de *
5  * Frank Hesse <frank at nld dot ds dot mpg dot de> *
6  * Ralf Der <ralfder at mis dot mpg dot de> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the *
20  * Free Software Foundation, Inc., *
21  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22  * *
23  ***************************************************************************/
24 
25 #ifndef TEMPLATEVALUEANALYSATION_H_
26 #define TEMPLATEVALUEANALYSATION_H_
27 
28 //macros
29 #define ANALYSATION_CONTEXT TemplateValueAnalysation<type,zero,lower,higher,doubleDiv,doubleMul,add,sub,mul,div>
30 #define DOUBLE_ANALYSATION_CONTEXT TemplateValueAnalysation<double,defaultZero>
31 
32 //includes
33 #include <vector>
34 #include <list>
35 
36 /**
37  * default function for "lower as" operation.
38  * @param a operator a
39  * @param b operator b
40  * @return (bool) true if a lower than b
41  */
42 template
43 <class type>
44 bool defaultLower(const type& a, const type& b) {
45  if(a<b)
46  return true;
47 
48  return false;
49 }
50 
51 /**
52  * default function for divide by a double value.
53  * @param a operator a
54  * @param b (double) the double value
55  * @return the result of the division
56  */
57 template
58 <class type>
59 type defaultDoubleDiv(const type& a, const double& b) {
60  return a/b;
61 }
62 
63 /**
64  * default function for mul. with a double value.
65  * @param a operator a
66  * @param b (double) the double value
67  * @return the result of the mul.
68  */
69 template
70 <class type>
71 type defaultDoubleMul(const type& a, const double& b) {
72  return a*b;
73 }
74 
75 /**
76  * default function for "higher than" operation.
77  * @param a operator a
78  * @param b operator b
79  * @return (bool) true if a higher than b
80  */
81 template
82 <class type>
83 bool defaultHigher(const type& a,const type& b) {
84  if(a>b)
85  return true;
86 
87  return false;
88 }
89 
90 /**
91  * default function for add two values
92  * @param a operator a
93  * @param b operator b
94  * @return the result of the add.
95  */
96 template
97 <class type>
98 type defaultAdd(const type& a, const type& b) {
99  return a+b;
100 }
101 
102 /**
103  * default function for sub. two values
104  * @param a operator a
105  * @param b operator b
106  * @return the result of the sub.
107  */
108 template
109 <class type>
110 type defaultSub(const type& a, const type& b) {
111  return a-b;
112 }
113 
114 /**
115  * default function for mul. of two values
116  * @param a operator a
117  * @param b operator b
118  * @return the result of the mul.
119  */
120 template
121 <class type>
122 type defaultMul(const type& a, const type& b) {
123  return a*b;
124 }
125 
126 /**
127  * default function for div. of two values
128  * @param a operator a
129  * @param b operator b
130  * @return the result of the div.
131  */
132 template
133 <class type>
134 type defaultDiv(const type& a, const type& b) {
135  return a/b;
136 }
137 
138 /**
139  * default function for zero double value
140  * @return (double) 0.0
141  */
142 double defaultZero();
143 
144 /**
145  * This template class give you some methods to calculate some statistical values like average, min, max, upper quartil,
146  * lower quartil and many more.
147  *
148  * All functions are implemented in the header because by generating the library it isn't known which type are later used.
149  * And a later compiling needs the implementation two!
150  */
151 template
152 <class type, //data type of the calculation
153 type zero(void), //gives the zero of this data type back
154 bool lower(const type&, const type&)=defaultLower<type>, //test if one element of the type lower than the other
155 bool higher(const type&, const type&)=defaultHigher<type>, //test if one element of the type higher than the other
156 type doubleDiv(const type&, const double&)=defaultDoubleDiv<type>, //divide a element of the type by a double value
157 type doubleMul(const type&, const double&)=defaultDoubleMul<type>, //mul. a element of the type by a double value
158 type add(const type&, const type&)=defaultAdd<type>, //add two elements of type
159 type sub(const type&, const type&)=defaultSub<type>, //sub two elements of type
160 type mul(const type&, const type&)=defaultMul<type>, //mul two elements of type
161 type div(const type&, const type&)=defaultDiv<type> > //div two elements of type
163 public:
164  /**
165  * constructor
166  * Needs a set of values for which the statistical values will calculate.
167  * @param values (vector<type>& the set)
168  */
169  TemplateValueAnalysation(std::vector<type>& values) : m_vector(values), m_list(), m_listCreated(false) {}
170 
171  /**
172  * default destructor
173  */
175 
176  /**
177  * this function calculate the average of the giving set
178  *
179  * It used zero, add and doubleDiv to calculate the average.
180  * @return the average
181  */
182  type getAvg() {
183  type avg=zero(); //by begin the average is zero
184  __gnu_cxx::__normal_iterator<type*,std::vector<type,std::allocator<type> > > iter;
185 
186  for(iter = m_vector.begin(); iter != m_vector.end(); iter++) {
187  avg = add(avg,(*iter)); //for all elements in the set add it to the average.
188  //So we become the sum. of all elements in the set.
189  }
190 
191  if(m_vector.size()!=0)
192  avg = doubleDiv(avg,m_vector.size()); //now devide the sum by the count of elements in the set.
193 
194  return avg; //return the result
195  }
196 
197  /**
198  * this function search the min. value in the set
199  *
200  * For this it use lower
201  * @return the minimum
202  */
203  type getMin() {
204  type min = m_vector[0]; //the lowest element is at begin the first element
205  __gnu_cxx::__normal_iterator<type*,std::vector<type,std::allocator<type> > > iter = m_vector.begin();
206 
207  for(iter++; iter != m_vector.end(); iter++) {
208  if(lower((*iter),min)) //if a element lower than min, so reset the min to the lower value.
209  min=(*iter);
210  }
211 
212  return min; //return the lowest element
213  }
214 
215  /**
216  * this function search the max. value in the set
217  *
218  * For this it use lower
219  * @return the minimum
220  */
221  type getMax() {
222  type max = m_vector[0]; //the highest element is at begin the first element
223  __gnu_cxx::__normal_iterator<type*,std::vector<type,std::allocator<type> > > iter = m_vector.begin();
224 
225  for(iter++; iter != m_vector.end(); iter++) {
226  if(lower(max,(*iter))) //if a element higher than max, so reset the max to the higher value.
227  max=(*iter);
228  }
229 
230  return max; //return the lowest element
231  }
232 
233  /**
234  * this function calculate the range of all values. For this he search the mibn and max and calculate from this values.
235  *
236  * use sub
237  * @return the range of the values in the set
238  */
239  type getRange() {
240  type dMax = getMax(); //become max
241  type dMin = getMin(); //become min
242 
243  return sub(dMax,dMin); //range=max-min
244  }
245 
246  /**
247  * this function search the median of the giving set of values.
248  *
249  * use add and doubleMul
250  * @return the median
251  */
252  type getMedian() {
253  type median;
254  int x;
255  int num = m_vector.size()/2;
256  std::_List_iterator<TYPE_SAVE> iter;
257 
258  if(!m_listCreated)
259  sort(); //sort the set. to define the middle
260 
261  iter = m_list.begin(); //go to the middle
262  for(x=0;x<num;x++) iter++;
263 
264  if(m_vector.size() % 2 == 0) { //if the real middle between two values add this values and calculate the arithmetical middle.
265  median = (*iter->pointer);
266  iter--;
267  median = add(median,(*iter->pointer));
268  median = doubleMul(median,0.5);
269  }
270 
271  else {
272  median = (*iter->pointer); //else gives the middle back
273  }
274 
275  return median;
276  }
277 
278  /**
279  * this function calculate the under quartil
280  *
281  * use add and doubleMul
282  * @return the under quartil
283  */
284  type getQuartil1() {
285  type q;
286  int x;
287  int num = m_vector.size()/4;
288  std::_List_iterator<TYPE_SAVE> iter;
289 
290  if(!m_listCreated)
291  sort(); //sort the set.
292 
293  iter = m_list.begin(); //go to the under quartil
294  for(x=0;x<num;x++) iter++;
295 
296  if(m_vector.size() % 4 == 0) { //if the real under quartil between two values add this values and calculate the arithmetical middle.
297  q = (*iter->pointer);
298  iter--;
299  q = add(q,(*iter->pointer));
300  q = doubleMul(q,0.5);
301  }
302  else { //else return the quartil
303  q = (*iter->pointer);
304  }
305 
306  return q;
307  }
308 
309  /**
310  * this function calculate the upper quartil.
311  *
312  * use add and doubleMul
313  * @return the upper quartil
314  */
315  type getQuartil3() {
316  type q;
317  int x;
318  int num = m_vector.size()*3/4;
319  std::_List_iterator<TYPE_SAVE> iter;
320 
321  if(!m_listCreated)
322  sort(); //sort the set.
323 
324  iter = m_list.begin();
325  for(x=0;x<num;x++) iter++; //go to the upper quartil
326 
327  if(m_vector.size() % 4 == 0) { //if the real upper quartil between two values add this values and calculate the arithmetical middle.
328  q = (*iter->pointer);
329  iter--;
330  q = add(q,(*iter->pointer));
331  q = doubleMul(q,0.5);
332  }
333  else { //else return the quartil
334  q = (*iter->pointer);
335  }
336 
337  return q;
338  }
339 
340  /**
341  * this function calculate the range between the upper and under quartil. This range is called inter-quartil-range.
342  *
343  * use sub
344  * @return the IQR
345  */
346  type getIQR() {
347  type q1 = getQuartil1(); //calculate the under quartil
348  type q3 = getQuartil3(); //calculate the upper quartil
349 
350  return sub(q3,q1); //IQR = Q3 - Q1
351  }
352 
353  /**
354  * this function calculate the whisker distance
355  *
356  * use doubleMul
357  * @param factor (double) the factor for this distance
358  * @return the result
359  */
360  type getWhisker(double factor) {
361  type dIqr = getIQR(); //calculate the IQR
362 
363  return doubleMul(dIqr,factor); //WHISKER distance is factor*IQR
364  }
365 
366  /**
367  * this function search the lowest value in the set which is from under quartil inside the whisker distance
368  *
369  * use sub, lower and zero
370  * @param factor (double) this factor is for calculating the whisker distance.
371  * @return the lowest value inside
372  */
373  type getWhisker1(double factor) {
374  type dW = getWhisker(factor); //calculate the whisker distance
375  type dQ1 = getQuartil1(); //calculate the under quartil
376  //TODO for optimization: getWhisker use getIQR and this calculate Q1 so it will be calculate two times!!!
377  type dBorder = sub(dQ1,dW); //where is the border for the lowest value
378  std::_List_iterator<TYPE_SAVE> iter = m_list.begin();
379 
380  while(lower((*iter->pointer),dBorder) && iter!=m_list.end()) { //search
381  iter++;
382  if(iter==m_list.end())
383  break;
384  }
385 
386  if(iter==m_list.end()) // ERROR
387  return zero();
388 
389  return (*iter->pointer);
390  }
391 
392  /**
393  * this function search the highest value in the set which is from the upper quartil inside the whisker distance
394  *
395  * use add and lower
396  * @param factor (double) this factor is for calculating the whisker distance.
397  * @return the highest value inside
398  */
399  type getWhisker3(double factor) {
400  type dW = getWhisker(factor); //calculate the whisker distance
401  type dQ3 = getQuartil3(); //calculate the upper quartil
402  //TODO for optimization: getWhisker use getIQR and this calculate Q3 so it will be calculate two times!!!
403  type dBorder = add(dQ3,dW); //where is the border for the lowest value
404  std::_List_iterator<TYPE_SAVE> iter = m_list.begin();
405 
406  while(lower((*iter->pointer),dBorder) && iter!=m_list.end()) { //search
407  iter++;
408  if(iter==m_list.end())
409  break;
410  }
411 
412  if(iter!=m_list.begin())
413  iter--;
414 
415  return (*iter->pointer);
416  }
417 
418  /**
419  * this function give you the number of elements in the giving set, which aren't in the whisker distance
420  *
421  * use lower and higher
422  * @param factor (double) this factor is for calculating the whisker distance
423  * @return (int) the number of extreme values in the set
424  */
425  unsigned int getNumExtrems(double factor) {
426  unsigned int result = 0;
427  type dW1 = getWhisker1(factor); //find W1
428  type dW3 = getWhisker3(factor); //find W3
429  std::_List_iterator<TYPE_SAVE> iter = m_list.begin();
430 
431  while(lower((*iter->pointer),dW1) && iter!=m_list.end()) { //count all elements which are lower than W1
432  iter++;
433  if(iter==m_list.end())
434  break;
435  result++;
436  }
437 
438  while(!higher((*iter->pointer),dW3) && iter!=m_list.end()) { //go from W1 to W3
439  iter++;
440  if(iter==m_list.end())
441  break;
442  }
443 
444  while(iter!=m_list.end()) { //count all element which are higher than W3
445  iter++;
446  result++;
447  }
448 
449  return result;
450  }
451 
452  /**
453  * this function gives one extreme value back
454  *
455  * use lower, higher and zero
456  * @param factor (double) the factor for the whisker distance
457  * @param i (unsigned int) the index of the searched extreme value
458  * @return the founded extreme value
459  */
460  type getExtrem(double factor, unsigned int i) {
461  type dW1 = getWhisker1(factor); //find W1
462  type dW3 = getWhisker3(factor); //find W3
463  std::_List_iterator<TYPE_SAVE> iter = m_list.begin();
464 
465  while(lower((*iter->pointer),dW1) && iter!=m_list.end()) { //search in lower area
466  i--;
467 
468  if(i==0)
469  return (*iter->pointer);
470 
471  iter++;
472  if(iter==m_list.end())
473  break;
474  }
475 
476  while(!higher((*iter->pointer),dW3) && iter!=m_list.end()) { //go from W1 to W3
477  iter++;
478  if(iter==m_list.end())
479  break;
480  }
481 
482  while(iter!=m_list.end()) { //search in upper area
483  i--;
484 
485  if(i==0)
486  return (*iter->pointer);
487 
488  iter++;
489  }
490 
491  return zero(); //not found //if not found return zero
492  }
493 
494  /**
495  * this function search the value which is next to zero.
496  *
497  * use zero, sub and lower
498  * @return the best value
499  */
500  type getBest(void) {
501  type z = zero(); //test value is zero
502  type* l;
503  type* h;
504 
505  if(!m_listCreated)
506  sort(); //sort the list
507 
508  std::_List_iterator<TYPE_SAVE> iter = m_list.begin();
509 
510  while(lower((*iter->pointer),z) && iter!=m_list.end()) { //search the element which is as first higher than zero
511  iter++;
512  if(iter==m_list.end())
513  break;
514  }
515 
516  if(iter!=m_list.end())
517  //return (*iter->pointer);
518  h = iter->pointer;
519 
520  else {
521  iter--;
522  //return (*iter->pointer);
523  h = iter->pointer;
524  }
525 
526  while(!lower((*iter->pointer),z) && iter!= m_list.begin()) { //search the element which is as first lower than zero
527  iter--;
528  if(iter==m_list.begin())
529  break;
530  }
531 
532  l = iter->pointer;
533 
534  if(lower(sub(*h,z),sub(z,*l))) //test which is next to zero
535  return *h;
536  else
537  return *l;
538  }
539 
540 protected:
541 
542  /**
543  * help structur for sorting the set.
544  * define the lower than operator
545  */
546  struct TYPE_SAVE {
547  TYPE_SAVE(type& a) : pointer(&a) {}
548 
549  type* pointer;
550 
551  bool operator<(const TYPE_SAVE& other) {
552  return lower((*pointer),(*other.pointer));
553  }
554  };
555 
556  /**
557  * this vector save the giving set.
558  */
559  std::vector<type>& m_vector;
560 
561  /**
562  * this list saves the sorted set
563  */
564  std::list<TYPE_SAVE> m_list;
565 
566  /**
567  * this variable remember if the sorted list is created
568  */
570 
571  /**
572  * this function create the sorted list
573  */
574  void sort(void) {
575  __gnu_cxx::__normal_iterator<type*,std::vector<type,std::allocator<type> > > iter;
576 
577  for(iter = m_vector.begin(); iter != m_vector.end(); iter++) { //fill the sorted list with elements of the help structur
578  m_list.push_back(TYPE_SAVE((*iter)));
579  }
580 
581  m_list.sort(); //sort the list
582  m_listCreated = true; //remember that it is created.
583  }
584 };
585 
586 #endif /* TEMPLATEVALUEANALYSATION_H_ */
std::vector< type > & m_vector
this vector save the giving set.
Definition: templatevalueanalysation.h:559
TemplateValueAnalysation(std::vector< type > &values)
constructor Needs a set of values for which the statistical values will calculate.
Definition: templatevalueanalysation.h:169
type getWhisker(double factor)
this function calculate the whisker distance
Definition: templatevalueanalysation.h:360
type defaultMul(const type &a, const type &b)
default function for mul.
Definition: templatevalueanalysation.h:122
type defaultDoubleMul(const type &a, const double &b)
default function for mul.
Definition: templatevalueanalysation.h:71
type getQuartil1()
this function calculate the under quartil
Definition: templatevalueanalysation.h:284
~TemplateValueAnalysation()
default destructor
Definition: templatevalueanalysation.h:174
type getBest(void)
this function search the value which is next to zero.
Definition: templatevalueanalysation.h:500
unsigned int getNumExtrems(double factor)
this function give you the number of elements in the giving set, which aren't in the whisker distance...
Definition: templatevalueanalysation.h:425
type getQuartil3()
this function calculate the upper quartil.
Definition: templatevalueanalysation.h:315
type getMedian()
this function search the median of the giving set of values.
Definition: templatevalueanalysation.h:252
bool operator<(const TYPE_SAVE &other)
Definition: templatevalueanalysation.h:551
void sort(void)
this function create the sorted list
Definition: templatevalueanalysation.h:574
This template class give you some methods to calculate some statistical values like average...
Definition: templatevalueanalysation.h:162
double defaultZero()
default function for zero double value
Definition: templatevalueanalysation.cpp:27
double max(const matrix::Matrix &v)
returns the largest element
Definition: controller_misc.cpp:318
type defaultAdd(const type &a, const type &b)
default function for add two values
Definition: templatevalueanalysation.h:98
type getExtrem(double factor, unsigned int i)
this function gives one extreme value back
Definition: templatevalueanalysation.h:460
type defaultDoubleDiv(const type &a, const double &b)
default function for divide by a double value.
Definition: templatevalueanalysation.h:59
type getAvg()
this function calculate the average of the giving set
Definition: templatevalueanalysation.h:182
help structur for sorting the set.
Definition: templatevalueanalysation.h:546
type getWhisker1(double factor)
this function search the lowest value in the set which is from under quartil inside the whisker dista...
Definition: templatevalueanalysation.h:373
type defaultSub(const type &a, const type &b)
default function for sub.
Definition: templatevalueanalysation.h:110
type getMin()
this function search the min.
Definition: templatevalueanalysation.h:203
bool m_listCreated
this variable remember if the sorted list is created
Definition: templatevalueanalysation.h:569
bool defaultHigher(const type &a, const type &b)
default function for "higher than" operation.
Definition: templatevalueanalysation.h:83
bool defaultLower(const type &a, const type &b)
default function for "lower as" operation.
Definition: templatevalueanalysation.h:44
type getRange()
this function calculate the range of all values.
Definition: templatevalueanalysation.h:239
double min(const matrix::Matrix &v)
returns the smallest element
Definition: controller_misc.cpp:307
std::list< TYPE_SAVE > m_list
this list saves the sorted set
Definition: templatevalueanalysation.h:564
type getIQR()
this function calculate the range between the upper and under quartil.
Definition: templatevalueanalysation.h:346
TYPE_SAVE(type &a)
Definition: templatevalueanalysation.h:547
type defaultDiv(const type &a, const type &b)
default function for div.
Definition: templatevalueanalysation.h:134
type * pointer
Definition: templatevalueanalysation.h:549
type getMax()
this function search the max.
Definition: templatevalueanalysation.h:221
type getWhisker3(double factor)
this function search the highest value in the set which is from the upper quartil inside the whisker ...
Definition: templatevalueanalysation.h:399