invertmotorcontroller.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  *                                                                         *
00007  *   ANY COMMERCIAL USE FORBIDDEN!                                         *
00008  *   LICENSE:                                                              *
00009  *   This work is licensed under the Creative Commons                      *
00010  *   Attribution-NonCommercial-ShareAlike 2.5 License. To view a copy of   *
00011  *   this license, visit http://creativecommons.org/licenses/by-nc-sa/2.5/ *
00012  *   or send a letter to Creative Commons, 543 Howard Street, 5th Floor,   *
00013  *   San Francisco, California, 94105, USA.                                *
00014  *                                                                         *
00015  *   This program is distributed in the hope that it will be useful,       *
00016  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00017  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                  *
00018  *                                                                         *
00019  *   $Log: invertmotorcontroller.h,v $
00020  *   Revision 1.31  2007/12/11 14:44:28  martius
00021  *   possible segfault bug in buffer function
00022  *
00023  *   Revision 1.30  2007/04/20 12:26:35  martius
00024  *   *** empty log message ***
00025  *
00026  *   Revision 1.29  2007/02/23 09:40:45  der
00027  *   regularisation used from regularisation.h
00028  *
00029  *   Revision 1.28  2007/01/24 14:39:10  martius
00030  *   use new configurable addParameter feature
00031  *
00032  *   Revision 1.27  2006/12/21 11:44:17  martius
00033  *   commenting style for doxygen //< -> ///<
00034  *   FOREACH and FOREACHC are macros for collection iteration
00035  *
00036  *   Revision 1.26  2006/12/11 18:14:14  martius
00037  *   delete for BNoise
00038  *   delete of buffers
00039  *
00040  *   Revision 1.25  2006/11/29 16:22:43  martius
00041  *   name is a variable of configurable and is used as such
00042  *
00043  *   Revision 1.24  2006/10/20 13:32:35  der
00044  *   fantasy
00045  *
00046  *   Revision 1.23  2006/08/02 09:30:16  martius
00047  *   virtualized new calc functions
00048  *   calcErrorFactor added
00049  *
00050  *   Revision 1.22  2006/07/20 17:14:34  martius
00051  *   removed std namespace from matrix.h
00052  *   storable interface
00053  *   abstract model and invertablemodel as superclasses for networks
00054  *
00055  *   Revision 1.21  2006/07/18 15:17:16  martius
00056  *   buffer operations like delayed values and smoothed values moved to invertmotorcontroller
00057  *
00058  *   Revision 1.20  2006/07/18 14:47:15  martius
00059  *   new regularisation of g' and g''/g'
00060  *   g''/g' uses the same clipped z which used for g'
00061  *   sqashing function with variable squash size
00062  *
00063  *   Revision 1.19  2006/07/14 12:23:58  martius
00064  *   selforg becomes HEAD
00065  *
00066  *   Revision 1.17.6.10  2006/07/10 13:05:16  martius
00067  *   NON-COMMERICAL LICENSE added to controllers
00068  *
00069  *                                                                         *
00070  ***************************************************************************/
00071 
00072 #ifndef __INVERTMOTORCONTROLLER_H
00073 #define __INVERTMOTORCONTROLLER_H
00074 
00075 #include "abstractcontroller.h"
00076 #include "controller_misc.h"
00077 #include <stdlib.h>
00078 #include <string.h>
00079 
00080 
00081 /**
00082  * Abstract class (interface) for robot controller that use direct matrix inversion and
00083  * a time loop in motor domain
00084  * 
00085  * Implements standart configureable interface for some useful parameters like  epsC, epsA, s4avg ...
00086  */
00087 class InvertMotorController : public AbstractController {
00088 public:
00089   InvertMotorController( unsigned short buffersize ,
00090                          const std::string& name, const std::string& revision)
00091     : AbstractController(name, revision){
00092     this->buffersize = buffersize;
00093     addParameterDef("epsC", &epsC, 0.1); 
00094     addParameterDef("epsA",&epsA,  0.1); 
00095     addParameterDef("dampA",&dampA,0 ); 
00096     addParameterDef("adaptrate",&adaptRate,0.000);
00097     addParameterDef("nomupdate",&nomUpdate,0.005);
00098     addParameterDef("desens",&desens,0);          
00099     addParameterDef("s4delay",&s4delay,1); 
00100     addParameterDef("s4avg",&s4avg,1); 
00101     addParameterDef("steps",&steps,1); 
00102     addParameterDef("zetaupdate",&zetaupdate,0); 
00103     addParameterDef("squashsize",&squashSize,0.01);
00104     addParameterDef("factorB",&factorB,0.2); 
00105     addParameterDef("noiseB",&noiseB,0.001);
00106     addParameterDef("noiseY",&noiseY,0);
00107     addParameterDef("teacher",&teacher,5);
00108     rootE=0;
00109     logaE=0;
00110     relativeE=0;
00111     t=0;
00112     initialised = false;
00113   }
00114 
00115   /***** CONFIGURABLE ******/
00116 
00117   virtual paramval getParam(const paramkey& key) const{
00118     if(key == "logaE") return logaE; 
00119     else if(key == "rootE") return rootE; 
00120     else if(key == "relativeE") return relativeE; 
00121     else  return AbstractController::getParam(key);
00122   }
00123 
00124   virtual bool setParam(const paramkey& key, paramval val){
00125     if(key == "logaE") logaE=(short)val;
00126     else if(key == "rootE") rootE=(short)val;
00127     else if(key == "relativeE") relativeE=(short)val;
00128     else return AbstractController::setParam(key, val);
00129     //    else fprintf(stderr, "parameter %s unknown\n", key);
00130     return true;
00131   }
00132 
00133   virtual paramlist getParamList() const{
00134     paramlist list;
00135     list.push_back(std::pair<paramkey, paramval> ("logaE", logaE));
00136     list.push_back(std::pair<paramkey, paramval> ("rootE", rootE));
00137     list.push_back(std::pair<paramkey, paramval> ("relativeE", relativeE));
00138     return list;
00139   }
00140 
00141 protected:
00142   paramval epsC; ///< learning rate factor for controller learning
00143   paramval desens;
00144   paramval s4delay; ///< number of timesteps of delay in the SML 
00145   paramval s4avg; ///< number of timesteps used for smoothing the controller output values 
00146   paramval steps; ///< number of timesteps is used for controller learning
00147   paramval epsA; ///< learning rate factor for model learning
00148   paramval factorB; ///< additional learning rate factor for model bias 
00149   paramval zetaupdate;
00150   paramval dampA; ///< damping term for model (0: no damping, 0.1 high damping)
00151   short logaE;  ///< logarithmic error is used for learning 1: controller 2: model 3: both
00152   short rootE;  ///< root error is used for learning 1: controller 2: model 3: both
00153   short relativeE; ///< if not 0: a relative error signal is used (xsi is normalised in respect to |y|)
00154 
00155   paramval squashSize; ///< size of the box, where the parameter updates are clipped to
00156   paramval adaptRate;  ///< adaptation rate for learning rate adaptation
00157   paramval nomUpdate;  ///< nominal update of controller in respect to matrix norm
00158   paramval noiseB;     ///< size of the additional noise for model bias B
00159   paramval noiseY;     ///< size of the additional noise for motor values
00160   paramval teacher;    ///< factor for teaching signal
00161 
00162   int t;
00163   unsigned short buffersize;
00164   bool initialised;
00165 
00166 protected:
00167   // put new value in ring buffer
00168   void putInBuffer(matrix::Matrix* buffer, const matrix::Matrix& vec, int delay=0){
00169     buffer[(t-delay)%buffersize] = vec;
00170   }
00171 
00172   /// calculate delayed values
00173   virtual matrix::Matrix calculateDelayedValues(const matrix::Matrix* buffer, 
00174                                                   int number_steps_of_delay_){
00175     // number_steps_of_delay must not be smaller than buffersize
00176     assert ((unsigned)number_steps_of_delay_ < buffersize);  
00177     return buffer[(t - number_steps_of_delay_) % buffersize];
00178   };
00179   
00180   /// calculate time-smoothed values 
00181   virtual matrix::Matrix calculateSmoothValues(const matrix::Matrix* buffer, 
00182                                                  int number_steps_for_averaging_){
00183     // number_steps_for_averaging_ must not be larger than buffersize
00184     assert ((int)number_steps_for_averaging_ <= buffersize);
00185     
00186     matrix::Matrix result(buffer[t % buffersize]);
00187     for (int k = 1; k < number_steps_for_averaging_; k++) {
00188       result += buffer[(t - k + buffersize) % buffersize];
00189     }
00190     result *= 1/((double) (number_steps_for_averaging_)); // scalar multiplication
00191     return result;
00192   };
00193 
00194   /// calculates the error_factor for either logarithmic (E=ln(e^T*e)) or square (E=sqrt(e^t*e)) error
00195   virtual double calcErrorFactor(const matrix::Matrix& e, bool loga, bool root) {
00196     double error_factor = 1;
00197     if (loga){   // using logarithmic error E=ln(v^T*v)
00198       error_factor= 1/(e.multTM().val(0,0)+0.000001)*0.01; // factor 1/100 for normalising (empirically)
00199     }
00200     if (root){  // using root error E=(v^T*v)^(1/2) 
00201       error_factor= 1/sqrt(e.multTM().val(0,0)+0.000001)*0.1; // factor 1/10 for normalising (empirically)
00202     }  
00203     return error_factor;
00204   }
00205   
00206   
00207   /// neuron transfer function
00208   static double g(double z)
00209   {
00210     return tanh(z);
00211   };
00212 
00213 
00214  
00215 };
00216 
00217 #endif

Generated on Tue Sep 16 22:00:22 2008 for Robotsystem of the Robot Group Leipzig by  doxygen 1.4.7