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
statistictools.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 #ifndef _STATISTIC_TOOLS_H
25 #define _STATISTIC_TOOLS_H
26 
27 // begin forward declarations
28 class AbstractMeasure;
29 class StatisticMeasure;
30 class ComplexMeasure;
31 // end forward declarations
32 
33 #include <string>
34 
35 #include "inspectable.h"
36 #include "callbackable.h"
37 #include "measuremodes.h"
38 #include "analysationmodes.h"
40 
41 #define GET_TYPE_ANALYSATION(type) getAnalysation<type,defaultZero,defaultLower<type>,defaultHigher<type>,defaultDoubleDiv<type>,defaultDoubleMul<type>,defaultAdd<type>,defaultSub<type>,defaultMul<type>,defaultDiv<type> >
42 #define GET_DOUBLE_ANALYSATION GET_TYPE_ANALYSATION(double)
43 
44 /// TODO: add possibility to pass description of a measure
45 class StatisticTools : public Inspectable, public Callbackable {
46 
47 public:
48  StatisticTools(const std::string& name = "StatisticTools") : Inspectable(name), beginMeasureCounter(0) { }
49 
50  /**
51  * adds a variable to observe and measure the value
52  * @param observedValue the value to observe.
53  * @param measureName the name of the measured value
54  * @param mode the mode of measure
55  * @param stepSpan in most cases the stepSpan is important to get
56  * the measured value of a number of steps, like AVG:
57  * if stepSpan = 0, AVG is calculated over all steps
58  * if stepSpan = n, AVG is calculated over the LAST n steps
59  * The same counts for all the other MeasureModes.
60  * @param additionalParam is used for example for mode PEAK, the param is the limit value,
61  * all values minus limit are displayed, values below the limit are set to 0.
62  * In CONV mode (test the convergence), this value is the epsilon criteria.
63  * @return measured value as adress. So it is possible to measure this value again
64  */
65  virtual double& addMeasure(double& observedValue, const char* measureName, MeasureMode mode, long stepSpan, double additionalParam=0);
66 
67  /**
68  * Same as the method above, but instead of getting the calculated value back (the adress), you get
69  * the StatisticMeasure itself
70  */
71  virtual StatisticMeasure* getMeasure(double& observedValue,const char* measureName, MeasureMode mode, long stepSpan, double additionalParam=0);
72 
73  /** returns a measure that is already in the pool (or 0 if not found) */
74  virtual AbstractMeasure* getMeasure(const std::string & measureName) const;
75 
76  /**
77  * You can add another abstract measure you like. in some cases (e.g. complex
78  * measures) it is better to let the measure decide how it likes to be initialized
79  * @param measure the measure to add
80  * @return the address value of the measure
81  */
82  virtual double& addMeasure(AbstractMeasure* measure);
83 
84  /**
85  * You can add another abstract measure you like. in some cases (e.g. complex
86  * measures) it is better to let the measure decide how it likes to be initialized
87  * With this method you can add a list of AbstractMeasures.
88  * @param measureList the list of measures to add
89  * @return the address value of the first measure
90  */
91  virtual double& addMeasureList(std::list<AbstractMeasure*> measureList);
92 
93 
94  /**
95  * You can add another abstract measure you like. in some cases (e.g. complex
96  * measures) it is better to let the measure decide how it likes to be initialized
97  * With this method you can add a list of AbstractMeasures.
98  * @param measureList the list of measures to add
99  * @return the address value of the first measure
100  */
101  virtual double& addMeasureList(std::list<ComplexMeasure*> measureList);
102 
103 
104  /**
105  * You can add another abstract measure you like. in some cases (e.g. complex
106  * measures) it is better to let the measure decide how it likes to be initialized
107  * With this method you can add a list of AbstractMeasures.
108  * @param measureList the list of measures to add
109  * @return the address value of the first measure
110  */
111  virtual double& addMeasureList(std::list<StatisticMeasure*> measureList);
112 
113 
114 
115  /**
116  * starts the measure at a specific time. This is useful if there are
117  * values that have to be ignored at simulation start.
118  * @param step number of steps (normally simsteps) to wait for beginning the measures
119  */
120  virtual void beginMeasureAt(long step);
121 
122  /**
123  * Tells you wether the measures have already been started.
124  * @return true if measures have already been started, otherwise false
125  */
126  virtual bool measureStarted() { return (beginMeasureCounter==0?true:false); }
127 
128 
129  /**
130  * CALLBACKABLE INTERFACE
131  *
132  * this method is invoked when a callback is done from the class where this
133  * class is for callback registered
134  */
136 
137 
138 protected:
139  std::list<AbstractMeasure*> activeMeasures;
141 };
142 
143 
144 /**
145  * use this function if you want more than one value analysed
146  * class type must implement following operators:
147  *
148  * @param values (vector<type>) values for the analysation
149  * @return (TemplateValueAnalysation) the analysation context
150  */
151 template
152 <class type,
153 type zero(void),
154 bool lower(const type&, const type&),
155 bool higher(const type&, const type&),
156 type doubleDiv(const type&, const double&),
157 type doubleMul(const type&, const double&),
158 type add(const type&, const type&),
159 type sub(const type&, const type&),
160 type mul(const type&, const type&),
161 type div(const type&, const type&)>
162 ANALYSATION_CONTEXT* getAnalysation(std::vector<type> values) {
163  return new ANALYSATION_CONTEXT(values);
164 }
165 
166 /**
167  * class type must implement following operators:
168  *
169  * @param tvAnalysation (TemplateValueAnalysation) the analysation context
170  * @param mode (AnalysationMode) what value you want
171  * @param feature (unsigned int) special param. for mode
172  * @return (type) the value you want
173  */
174 template
175 <class type,
176 type zero(void),
177 bool lower(const type&, const type&),
178 bool higher(const type&, const type&),
179 type doubleDiv(const type&, const double&),
180 type doubleMul(const type&, const double&),
181 type add(const type&, const type&),
182 type sub(const type&, const type&),
183 type mul(const type&, const type&),
184 type div(const type&, const type&)>
185 type getAnalysation(ANALYSATION_CONTEXT* tvAnalysation, AnalysationMode mode, unsigned int feature = 0) {
186  switch(mode){
187  case AM_AVG:
188  return tvAnalysation->getAvg();
189  case AM_MIN:
190  return tvAnalysation->getMin();
191  case AM_MAX:
192  return tvAnalysation->getMax();
193  case AM_RANGE:
194  return tvAnalysation->getRange();
195  case AM_IQR:
196  return tvAnalysation->getIQR();
197  case AM_MED:
198  return tvAnalysation->getMedian();
199  case AM_WHISKER:
200  return tvAnalysation->getWhisker(1.5);
201  case AM_Q1:
202  return tvAnalysation->getQuartil1();
203  case AM_Q3:
204  return tvAnalysation->getQuartil3();
205  case AM_W1:
206  return tvAnalysation->getWhisker1(1.5);
207  case AM_W3:
208  return tvAnalysation->getWhisker3(1.5);
209  case AM_NUM_EXT:
210  return (type)tvAnalysation->getNumExtrems(1.5);
211  case AM_EXT:
212  return tvAnalysation->getExtrem(1.5,feature);
213  case AM_BEST:
214  return tvAnalysation->getBest();
215  default:
216  return zero();
217  }
218 }
219 
220 /**
221  * class type must implement following operators:
222  *
223  * @param values (vector<type>) values for the analysation
224  * @param mode (AnalysationMode) what value you want
225  * @param feature (unsigned int) special param. for mode
226  * @return (type) the value you want
227  */
228 template
229 <class type,
230 type zero(void),
231 bool lower(const type&, const type&),
232 bool higher(const type&, const type&),
233 type doubleDiv(const type&, const double&),
234 type doubleMul(const type&, const double&),
235 type add(const type&, const type&),
236 type sub(const type&, const type&),
237 type mul(const type&, const type&),
238 type div(const type&, const type&)>
239 type getAnalysation(std::vector<type> values, AnalysationMode mode, unsigned int feature = 0) {
240  ANALYSATION_CONTEXT* context = GET_TYPE_ANALYSATION(type)(values);
241  type result = GET_TYPE_ANALYSATION(type)(context,mode,feature);
242  delete context;
243  return result;
244 }
245 
246 #endif
virtual bool measureStarted()
Tells you wether the measures have already been started.
Definition: statistictools.h:126
long beginMeasureCounter
Definition: statistictools.h:140
Definition: complexmeasure.h:47
returns the count of extreme values
Definition: analysationmodes.h:61
StatisticTools(const std::string &name="StatisticTools")
Definition: statistictools.h:48
Interface class for a class which wants to be callback on a certain action.
Definition: callbackable.h:39
MeasureMode
usage of the statistictools with different measure modes (examples):
Definition: measuremodes.h:53
returns the quartile Q_(3/4) value
Definition: analysationmodes.h:51
virtual double & addMeasureList(std::list< AbstractMeasure * > measureList)
You can add another abstract measure you like.
Definition: statistictools.cpp:67
returns the quartile Q_(1/4) value
Definition: analysationmodes.h:49
returns the best value
Definition: analysationmodes.h:65
returns the whisker1 value
Definition: analysationmodes.h:55
std::list< AbstractMeasure * > activeMeasures
Definition: statistictools.h:139
returns the minimum value
Definition: analysationmodes.h:43
iparamkey name
Definition: inspectable.h:251
virtual double & addMeasure(double &observedValue, const char *measureName, MeasureMode mode, long stepSpan, double additionalParam=0)
adds a variable to observe and measure the value
Definition: statistictools.cpp:40
Class used by StatisticTools.
Definition: statisticmeasure.h:34
returns the average value
Definition: analysationmodes.h:39
returns the inter quartile range (IQR) value
Definition: analysationmodes.h:59
static const CallbackableType DEFAULT_CALLBACKABLE_TYPE
This is the default Callbackable type.
Definition: backcaller.h:51
returns the range of the values
Definition: analysationmodes.h:47
unsigned long CallbackableType
Definition: backcaller.h:45
virtual void doOnCallBack(BackCaller *source, BackCaller::CallbackableType type=BackCaller::DEFAULT_CALLBACKABLE_TYPE)
CALLBACKABLE INTERFACE.
Definition: statistictools.cpp:29
Interface for inspectable objects.
Definition: inspectable.h:48
virtual StatisticMeasure * getMeasure(double &observedValue, const char *measureName, MeasureMode mode, long stepSpan, double additionalParam=0)
Same as the method above, but instead of getting the calculated value back (the adress), you get the StatisticMeasure itself.
Definition: statistictools.cpp:45
returns the median value
Definition: analysationmodes.h:41
ANALYSATION_CONTEXT * getAnalysation(std::vector< type > values)
use this function if you want more than one value analysed class type must implement following operat...
Definition: statistictools.h:162
returns the maximum value
Definition: analysationmodes.h:45
#define ANALYSATION_CONTEXT
Definition: templatevalueanalysation.h:29
returns the whisker3 value
Definition: analysationmodes.h:57
TODO: add possibility to pass description of a measure.
Definition: statistictools.h:45
Class prototype which provides functions to handle callbackable classes.
Definition: backcaller.h:42
#define GET_TYPE_ANALYSATION(type)
Definition: statistictools.h:41
virtual void beginMeasureAt(long step)
starts the measure at a specific time.
Definition: statistictools.cpp:90
returns the whisker (1.5*IQR) value
Definition: analysationmodes.h:53
returns the special extreme value
Definition: analysationmodes.h:63
Class used by StatisticTools.
Definition: abstractmeasure.h:38
AnalysationMode
usage of the statistictools with different analysation modes modes (examples):
Definition: analysationmodes.h:37