statistictools.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005 by Robot Group Leipzig                             *
00003  *    martius@informatik.uni-leipzig.de                                    *
00004  *    fhesse@informatik.uni-leipzig.de                                     *
00005  *    der@informatik.uni-leipzig.de                                        *
00006  *    frankguettler@gmx.de                                                 *
00007  *    joergweide84@aol.com (robot12)                                       *
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  *   This program is distributed in the hope that it will be useful,       *
00015  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00016  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00017  *   GNU General Public License for more details.                          *
00018  *                                                                         *
00019  *   You should have received a copy of the GNU General Public License     *
00020  *   along with this program; if not, write to the                         *
00021  *   Free Software Foundation, Inc.,                                       *
00022  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00023  ***************************************************************************
00024  *                                                                         *
00025  *  DESCRIPTION                                                            *
00026  *                                                                         *
00027  *   $Log: statistictools.h,v $
00028  *   Revision 1.7  2009/08/10 07:39:54  guettler
00029  *   implements new Callbackable interface
00030  *
00031  *   Revision 1.6  2009/07/28 09:09:28  robot12
00032  *   add a clean up to getAnalysation
00033  *
00034  *   Revision 1.5  2009/07/21 08:47:33  robot12
00035  *   add some comments
00036  *
00037  *   Revision 1.4  2009/06/29 15:24:23  robot12
00038  *   patch a warning
00039  *
00040  *   Revision 1.3  2009/06/29 12:43:37  robot12
00041  *   3 new functions to work whit the new part data analysation.
00042  *
00043  *   Revision 1.2  2009/05/19 11:39:45  robot12
00044  *   add to statistictools some template analysation methodes (needed for boxplots)
00045  *
00046  *   Revision 1.1  2009/03/27 06:16:57  guettler
00047  *   support for gcc 4.3 compatibility (has to be checked), StatisticTools moves from utils to statistictools
00048  *
00049  *   Revision 1.10  2008/04/29 09:56:21  guettler
00050  *   -debug printouts removed
00051  *
00052  *   Revision 1.9  2008/04/29 08:51:54  guettler
00053  *   -cosmetic changes of StatisticTools
00054  *   -StatisticTools now uses new function addInspectableValue of the
00055  *   interface Inspectable, not overloading getInternalParams and
00056  *   getInternalParamNames anymore
00057  *
00058  *   Revision 1.8  2008/01/17 09:59:27  der
00059  *   complexmeasure: preparations made for predictive information,
00060  *   fixed a minor bug
00061  *   statisticmeasure, statistictools: added support for adding
00062  *   std::list<AbstractMeasure*> to StatisticTools, some minor
00063  *   improvements
00064  *
00065  *   Revision 1.7  2007/12/06 10:18:10  der
00066  *   AbstractMeasure is now a abstract type for Measures,
00067  *   StatisticTools now supports AbstractMeasures,
00068  *   StatisticalMeasure, ComplexMeasure  now derived from
00069  *   AbstractMeasure,
00070  *   ComplexMeasure provides support for calculation e.g. entropy,
00071  *   uses Discretisizer,
00072  *   Discretisizer is a stand-alone class for support of discretisizing values
00073  *   TrackableMeasure derived from ComplexMeasure and provides support for calculating complex measures for Trackable objects
00074  *
00075  *   Revision 1.6  2007/10/01 13:27:47  robot3
00076  *   documentation
00077  *
00078  *   Revision 1.5  2007/09/28 10:08:49  robot3
00079  *   fixed memory bugs, statistics are from now on aligned right
00080  *
00081  *   Revision 1.4  2007/09/28 08:48:21  robot3
00082  *   corrected some minor bugs, files are still in develop status
00083  *
00084  *   Revision 1.3  2007/09/27 10:49:39  robot3
00085  *   removed some minor bugs,
00086  *   added CONVergence test
00087  *   changed little things for support of the new WSM
00088  *
00089  *   Revision 1.2  2007/05/08 10:18:15  der
00090  *   added a function for starting the measure after a given time.
00091  *   made some tests
00092  *
00093  *   Revision 1.1  2007/05/07 21:01:32  robot3
00094  *   statistictools is a class for easy visualization of measurements of observed values
00095  *   it is possible to add the observed value itself with mode ID
00096  *
00097  *                                                                         *
00098  ***************************************************************************/
00099 #ifndef _STATISTIC_TOOLS_H
00100 #define _STATISTIC_TOOLS_H
00101 
00102 // begin forward declarations
00103 class AbstractMeasure;
00104 class StatisticMeasure;
00105 class ComplexMeasure;
00106 // end forward declarations
00107 
00108 #include "inspectable.h"
00109 #include "callbackable.h"
00110 #include "measuremodes.h"
00111 #include "analysationmodes.h"
00112 #include "templatevalueanalysation.h"
00113 
00114 #define GET_TYPE_ANALYSATION(type) getAnalysation<type,defaultZero,defaultLower<type>,defaultHigher<type>,defaultDoubleDiv<type>,defaultDoubleMul<type>,defaultAdd<type>,defaultSub<type>,defaultMul<type>,defaultDiv<type> >
00115 #define GET_DOUBLE_ANALYSATION GET_TYPE_ANALYSATION(double)
00116 
00117 class StatisticTools : public Inspectable, public Callbackable {
00118 
00119 public:
00120   StatisticTools() : beginMeasureCounter(0) { }
00121 
00122         /**
00123          * adds a variable to observe and measure the value
00124          * @param observedValue    the value to observe.
00125          * @param measureName      the name of the measured value
00126          * @param mode             the mode of measure
00127          * @param stepSpan         in most cases the stepSpan is important to get
00128          * the measured value of a number of steps, like AVG:
00129          * if stepSpan = 0, AVG is calculated over all steps
00130          * if stepSpan = n, AVG is calculated over the LAST n steps
00131          * The same counts for all the other MeasureModes.
00132          * @param additionalParam  is used for example for mode PEAK, the param is the limit value,
00133          * all values minus limit are displayed, values below the limit are set to 0.
00134          * In CONV mode (test the convergence), this value is the epsilon criteria.
00135          * @return measured value as adress. So it is possible to measure this value again
00136          */
00137   virtual double& addMeasure(double& observedValue, const char* measureName, MeasureMode mode, long stepSpan, double additionalParam=0);
00138 
00139   /**
00140    * Same as the method above, but instead of getting the calculated value back (the adress), you get
00141    * the StatisticMeasure itself
00142    */
00143   virtual StatisticMeasure* getMeasure(double& observedValue,const char* measureName, MeasureMode mode, long stepSpan, double additionalParam=0);
00144 
00145 
00146   /**
00147    * You can add another abstract measure you like. in some cases (e.g. complex
00148    * measures) it is better to let the measure decide how it likes to be initialized
00149    * @param measure the measure to add
00150    * @return the address value of the measure
00151    */
00152   virtual double& addMeasure(AbstractMeasure* measure);
00153 
00154   /**
00155    * You can add another abstract measure you like. in some cases (e.g. complex
00156    * measures) it is better to let the measure decide how it likes to be initialized
00157    * With this method you can add a list of AbstractMeasures.
00158    * @param measureList the list of measures to add
00159    * @return the address value of the first measure
00160    */
00161   virtual double& addMeasureList(std::list<AbstractMeasure*> measureList);
00162 
00163 
00164     /**
00165    * You can add another abstract measure you like. in some cases (e.g. complex
00166    * measures) it is better to let the measure decide how it likes to be initialized
00167    * With this method you can add a list of AbstractMeasures.
00168    * @param measureList the list of measures to add
00169    * @return the address value of the first measure
00170    */
00171   virtual double& addMeasureList(std::list<ComplexMeasure*> measureList);
00172 
00173 
00174     /**
00175    * You can add another abstract measure you like. in some cases (e.g. complex
00176    * measures) it is better to let the measure decide how it likes to be initialized
00177    * With this method you can add a list of AbstractMeasures.
00178    * @param measureList the list of measures to add
00179    * @return the address value of the first measure
00180    */
00181   virtual double& addMeasureList(std::list<StatisticMeasure*> measureList);
00182 
00183 
00184 
00185         /**
00186          * starts the measure at a specific time. This is useful if there are
00187          * values that have to be ignored at simulation start.
00188          * @param step number of steps (normally simsteps) to wait for beginning the measures
00189          */
00190         virtual void beginMeasureAt(long step);
00191 
00192   /**
00193    * Tells you wether the measures have already been started.
00194    * @return true if measures have already been started, otherwise false
00195    */
00196   virtual bool measureStarted() { return (beginMeasureCounter==0?true:false); }
00197 
00198 
00199         /**
00200          * CALLBACKABLE INTERFACE
00201          *
00202          *      this method is invoked when a callback is done from the class where this
00203          * class is for callback registered
00204          */
00205         virtual void doOnCallBack(BackCaller* source, BackCaller::CallbackableType type = BackCaller::DEFAULT_CALLBACKABLE_TYPE);
00206 
00207 
00208 protected:
00209         std::list<AbstractMeasure*> activeMeasures;
00210         long beginMeasureCounter;
00211 };
00212 
00213 
00214 /**
00215  * use this function if you want more than one value analysed
00216  * class type must implement following operators:
00217  *
00218  * @param values (vector<type>) values for the analysation
00219  * @return (TemplateValueAnalysation) the analysation context
00220  */
00221 template
00222 <class type,
00223 type zero(void),
00224 bool lower(const type&, const type&),
00225 bool higher(const type&, const type&),
00226 type doubleDiv(const type&, const double&),
00227 type doubleMul(const type&, const double&),
00228 type add(const type&, const type&),
00229 type sub(const type&, const type&),
00230 type mul(const type&, const type&),
00231 type div(const type&, const type&)>
00232 ANALYSATION_CONTEXT* getAnalysation(std::vector<type> values) {
00233         return new ANALYSATION_CONTEXT(values);
00234 }
00235 
00236 /**
00237  * class type must implement following operators:
00238  *
00239  * @param tvAnalysation (TemplateValueAnalysation) the analysation context
00240  * @param mode (AnalysationMode) what value you want
00241  * @param feature (unsigned int) special param. for mode
00242  * @return (type) the value you want
00243  */
00244 template
00245 <class type,
00246 type zero(void),
00247 bool lower(const type&, const type&),
00248 bool higher(const type&, const type&),
00249 type doubleDiv(const type&, const double&),
00250 type doubleMul(const type&, const double&),
00251 type add(const type&, const type&),
00252 type sub(const type&, const type&),
00253 type mul(const type&, const type&),
00254 type div(const type&, const type&)>
00255 type getAnalysation(ANALYSATION_CONTEXT* tvAnalysation, AnalysationMode mode, unsigned int feature = 0) {
00256         switch(mode){
00257         case AM_AVG:
00258                 return tvAnalysation->getAvg();
00259         case AM_MIN:
00260                 return tvAnalysation->getMin();
00261         case AM_MAX:
00262                 return tvAnalysation->getMax();
00263         case AM_RANGE:
00264                 return tvAnalysation->getRange();
00265         case AM_IQR:
00266                 return tvAnalysation->getIQR();
00267         case AM_MED:
00268                 return tvAnalysation->getMedian();
00269         case AM_WHISKER:
00270                 return tvAnalysation->getWhisker(1.5);
00271         case AM_Q1:
00272                 return tvAnalysation->getQuartil1();
00273         case AM_Q3:
00274                 return tvAnalysation->getQuartil3();
00275         case AM_W1:
00276                 return tvAnalysation->getWhisker1(1.5);
00277         case AM_W3:
00278                 return tvAnalysation->getWhisker3(1.5);
00279         case AM_NUM_EXT:
00280                 return (type)tvAnalysation->getNumExtrems(1.5);
00281         case AM_EXT:
00282                 return tvAnalysation->getExtrem(1.5,feature);
00283         case AM_BEST:
00284                 return tvAnalysation->getBest();
00285         default:
00286                 return zero();
00287         }
00288 }
00289 
00290 /**
00291  * class type must implement following operators:
00292  *
00293  * @param values (vector<type>) values for the analysation
00294  * @param mode (AnalysationMode) what value you want
00295  * @param feature (unsigned int) special param. for mode
00296  * @return (type) the value you want
00297  */
00298 template
00299 <class type,
00300 type zero(void),
00301 bool lower(const type&, const type&),
00302 bool higher(const type&, const type&),
00303 type doubleDiv(const type&, const double&),
00304 type doubleMul(const type&, const double&),
00305 type add(const type&, const type&),
00306 type sub(const type&, const type&),
00307 type mul(const type&, const type&),
00308 type div(const type&, const type&)>
00309 type getAnalysation(std::vector<type> values, AnalysationMode mode, unsigned int feature = 0) {
00310         ANALYSATION_CONTEXT* context = GET_TYPE_ANALYSATION(type)(values);
00311         type result = GET_TYPE_ANALYSATION(type)(context,mode,feature);
00312         delete context;
00313         return result;
00314 }
00315 
00316 #endif

Generated on Fri Oct 30 16:29:01 2009 for Robot Simulator of the Robotics Group for Self-Organization of Control by  doxygen 1.4.7