statistictools.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2011 LpzRobots development team                    *
00003  *    Georg Martius  <georg dot martius at web dot de>                     *
00004  *    Frank Guettler <guettler at informatik dot uni-leipzig dot de        *
00005  *    Frank Hesse    <frank at nld dot ds dot mpg dot de>                  *
00006  *    Ralf Der       <ralfder at mis dot mpg dot de>                       *
00007  *                                                                         *
00008  *   This program is free software; you can redistribute it and/or modify  *
00009  *   it under the terms of the GNU General Public License as published by  *
00010  *   the Free Software Foundation; either version 2 of the License, or     *
00011  *   (at your option) any later version.                                   *
00012  *                                                                         *
00013  *   This program is distributed in the hope that it will be useful,       *
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00016  *   GNU General Public License for more details.                          *
00017  *                                                                         *
00018  *   You should have received a copy of the GNU General Public License     *
00019  *   along with this program; if not, write to the                         *
00020  *   Free Software Foundation, Inc.,                                       *
00021  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00022  *                                                                         *
00023  ***************************************************************************/
00024 #ifndef _STATISTIC_TOOLS_H
00025 #define _STATISTIC_TOOLS_H
00026 
00027 // begin forward declarations
00028 class AbstractMeasure;
00029 class StatisticMeasure;
00030 class ComplexMeasure;
00031 // end forward declarations
00032 
00033 #include "inspectable.h"
00034 #include "callbackable.h"
00035 #include "measuremodes.h"
00036 #include "analysationmodes.h"
00037 #include "templatevalueanalysation.h"
00038 
00039 #define GET_TYPE_ANALYSATION(type) getAnalysation<type,defaultZero,defaultLower<type>,defaultHigher<type>,defaultDoubleDiv<type>,defaultDoubleMul<type>,defaultAdd<type>,defaultSub<type>,defaultMul<type>,defaultDiv<type> >
00040 #define GET_DOUBLE_ANALYSATION GET_TYPE_ANALYSATION(double)
00041 
00042 /// TODO: add possibility to pass description of a measure
00043 class StatisticTools : public Inspectable, public Callbackable {
00044 
00045 public:
00046   StatisticTools(const std::string& name = "StatisticTools") : Inspectable(name), beginMeasureCounter(0) { }
00047 
00048         /**
00049          * adds a variable to observe and measure the value
00050          * @param observedValue    the value to observe.
00051          * @param measureName      the name of the measured value
00052          * @param mode             the mode of measure
00053          * @param stepSpan         in most cases the stepSpan is important to get
00054          * the measured value of a number of steps, like AVG:
00055          * if stepSpan = 0, AVG is calculated over all steps
00056          * if stepSpan = n, AVG is calculated over the LAST n steps
00057          * The same counts for all the other MeasureModes.
00058          * @param additionalParam  is used for example for mode PEAK, the param is the limit value,
00059          * all values minus limit are displayed, values below the limit are set to 0.
00060          * In CONV mode (test the convergence), this value is the epsilon criteria.
00061          * @return measured value as adress. So it is possible to measure this value again
00062          */
00063   virtual double& addMeasure(double& observedValue, const char* measureName, MeasureMode mode, long stepSpan, double additionalParam=0);
00064 
00065   /**
00066    * Same as the method above, but instead of getting the calculated value back (the adress), you get
00067    * the StatisticMeasure itself
00068    */
00069   virtual StatisticMeasure* getMeasure(double& observedValue,const char* measureName, MeasureMode mode, long stepSpan, double additionalParam=0);
00070 
00071 
00072   /**
00073    * You can add another abstract measure you like. in some cases (e.g. complex
00074    * measures) it is better to let the measure decide how it likes to be initialized
00075    * @param measure the measure to add
00076    * @return the address value of the measure
00077    */
00078   virtual double& addMeasure(AbstractMeasure* measure);
00079 
00080   /**
00081    * You can add another abstract measure you like. in some cases (e.g. complex
00082    * measures) it is better to let the measure decide how it likes to be initialized
00083    * With this method you can add a list of AbstractMeasures.
00084    * @param measureList the list of measures to add
00085    * @return the address value of the first measure
00086    */
00087   virtual double& addMeasureList(std::list<AbstractMeasure*> measureList);
00088 
00089 
00090     /**
00091    * You can add another abstract measure you like. in some cases (e.g. complex
00092    * measures) it is better to let the measure decide how it likes to be initialized
00093    * With this method you can add a list of AbstractMeasures.
00094    * @param measureList the list of measures to add
00095    * @return the address value of the first measure
00096    */
00097   virtual double& addMeasureList(std::list<ComplexMeasure*> measureList);
00098 
00099 
00100     /**
00101    * You can add another abstract measure you like. in some cases (e.g. complex
00102    * measures) it is better to let the measure decide how it likes to be initialized
00103    * With this method you can add a list of AbstractMeasures.
00104    * @param measureList the list of measures to add
00105    * @return the address value of the first measure
00106    */
00107   virtual double& addMeasureList(std::list<StatisticMeasure*> measureList);
00108 
00109 
00110 
00111         /**
00112          * starts the measure at a specific time. This is useful if there are
00113          * values that have to be ignored at simulation start.
00114          * @param step number of steps (normally simsteps) to wait for beginning the measures
00115          */
00116         virtual void beginMeasureAt(long step);
00117 
00118   /**
00119    * Tells you wether the measures have already been started.
00120    * @return true if measures have already been started, otherwise false
00121    */
00122   virtual bool measureStarted() { return (beginMeasureCounter==0?true:false); }
00123 
00124 
00125         /**
00126          * CALLBACKABLE INTERFACE
00127          *
00128          *      this method is invoked when a callback is done from the class where this
00129          * class is for callback registered
00130          */
00131         virtual void doOnCallBack(BackCaller* source, BackCaller::CallbackableType type = BackCaller::DEFAULT_CALLBACKABLE_TYPE);
00132 
00133 
00134 protected:
00135         std::list<AbstractMeasure*> activeMeasures;
00136         long beginMeasureCounter;
00137 };
00138 
00139 
00140 /**
00141  * use this function if you want more than one value analysed
00142  * class type must implement following operators:
00143  *
00144  * @param values (vector<type>) values for the analysation
00145  * @return (TemplateValueAnalysation) the analysation context
00146  */
00147 template
00148 <class type,
00149 type zero(void),
00150 bool lower(const type&, const type&),
00151 bool higher(const type&, const type&),
00152 type doubleDiv(const type&, const double&),
00153 type doubleMul(const type&, const double&),
00154 type add(const type&, const type&),
00155 type sub(const type&, const type&),
00156 type mul(const type&, const type&),
00157 type div(const type&, const type&)>
00158 ANALYSATION_CONTEXT* getAnalysation(std::vector<type> values) {
00159         return new ANALYSATION_CONTEXT(values);
00160 }
00161 
00162 /**
00163  * class type must implement following operators:
00164  *
00165  * @param tvAnalysation (TemplateValueAnalysation) the analysation context
00166  * @param mode (AnalysationMode) what value you want
00167  * @param feature (unsigned int) special param. for mode
00168  * @return (type) the value you want
00169  */
00170 template
00171 <class type,
00172 type zero(void),
00173 bool lower(const type&, const type&),
00174 bool higher(const type&, const type&),
00175 type doubleDiv(const type&, const double&),
00176 type doubleMul(const type&, const double&),
00177 type add(const type&, const type&),
00178 type sub(const type&, const type&),
00179 type mul(const type&, const type&),
00180 type div(const type&, const type&)>
00181 type getAnalysation(ANALYSATION_CONTEXT* tvAnalysation, AnalysationMode mode, unsigned int feature = 0) {
00182         switch(mode){
00183         case AM_AVG:
00184                 return tvAnalysation->getAvg();
00185         case AM_MIN:
00186                 return tvAnalysation->getMin();
00187         case AM_MAX:
00188                 return tvAnalysation->getMax();
00189         case AM_RANGE:
00190                 return tvAnalysation->getRange();
00191         case AM_IQR:
00192                 return tvAnalysation->getIQR();
00193         case AM_MED:
00194                 return tvAnalysation->getMedian();
00195         case AM_WHISKER:
00196                 return tvAnalysation->getWhisker(1.5);
00197         case AM_Q1:
00198                 return tvAnalysation->getQuartil1();
00199         case AM_Q3:
00200                 return tvAnalysation->getQuartil3();
00201         case AM_W1:
00202                 return tvAnalysation->getWhisker1(1.5);
00203         case AM_W3:
00204                 return tvAnalysation->getWhisker3(1.5);
00205         case AM_NUM_EXT:
00206                 return (type)tvAnalysation->getNumExtrems(1.5);
00207         case AM_EXT:
00208                 return tvAnalysation->getExtrem(1.5,feature);
00209         case AM_BEST:
00210                 return tvAnalysation->getBest();
00211         default:
00212                 return zero();
00213         }
00214 }
00215 
00216 /**
00217  * class type must implement following operators:
00218  *
00219  * @param values (vector<type>) values for the analysation
00220  * @param mode (AnalysationMode) what value you want
00221  * @param feature (unsigned int) special param. for mode
00222  * @return (type) the value you want
00223  */
00224 template
00225 <class type,
00226 type zero(void),
00227 bool lower(const type&, const type&),
00228 bool higher(const type&, const type&),
00229 type doubleDiv(const type&, const double&),
00230 type doubleMul(const type&, const double&),
00231 type add(const type&, const type&),
00232 type sub(const type&, const type&),
00233 type mul(const type&, const type&),
00234 type div(const type&, const type&)>
00235 type getAnalysation(std::vector<type> values, AnalysationMode mode, unsigned int feature = 0) {
00236         ANALYSATION_CONTEXT* context = GET_TYPE_ANALYSATION(type)(values);
00237         type result = GET_TYPE_ANALYSATION(type)(context,mode,feature);
00238         delete context;
00239         return result;
00240 }
00241 
00242 #endif
Generated on Thu Jun 28 14:45:37 2012 for Robot Simulator of the Robotics Group for Self-Organization of Control by  doxygen 1.6.3