homeokinbase.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2009 by Robot Group Leipzig                             *
00003  *    martius@informatik.uni-leipzig.de                                    *
00004  *    der@informatik.uni-leipzig.de                                        *
00005  *                                                                         *
00006  *   ANY COMMERCIAL USE FORBIDDEN!                                         *
00007  *   LICENSE:                                                              *
00008  *   This work is licensed under the Creative Commons                      *
00009  *   Attribution-NonCommercial-ShareAlike 2.5 License. To view a copy of   *
00010  *   this license, visit http://creativecommons.org/licenses/by-nc-sa/2.5/ *
00011  *   or send a letter to Creative Commons, 543 Howard Street, 5th Floor,   *
00012  *   San Francisco, California, 94105, USA.                                *
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.                  *
00017  *                                                                         *
00018  *   $Log: homeokinbase.h,v $
00019  *   Revision 1.1  2009/08/05 23:05:56  martius
00020  *   new base class for controllers. This a smaller version of
00021  *    the invertmotorcontroller class
00022  *
00023  *                                                                         *
00024  ***************************************************************************/
00025 
00026 #ifndef __HOMEOKINBASE_H
00027 #define __HOMEOKINBASE_H
00028 
00029 #include "abstractcontroller.h"
00030 #include "controller_misc.h"
00031 #include <stdlib.h>
00032 #include <string.h>
00033 
00034 
00035 /**
00036  * Abstract class (interface) for robot controller that use are based on the homeokinetic
00037  * prinziple
00038  * 
00039  * Implements standard buffers and 
00040  *  configureable interface for some useful parameters like  epsC, epsA, s4avg ...
00041  */
00042 class HomeokinBase : public AbstractController {
00043 public:
00044   HomeokinBase( unsigned short buffersize ,
00045                  const std::string& name, const std::string& revision)
00046     : AbstractController(name, revision){
00047     this->buffersize = buffersize;
00048     addParameterDef("epsC", &epsC, 0.1); 
00049     addParameterDef("epsA",&epsA,  0.1); 
00050     addParameterDef("s4delay",&s4delay,1); 
00051     addParameterDef("s4avg",&s4avg,1); 
00052     addParameterDef("factorB",&factorB,0.2); 
00053     addParameterDef("squashsize",&squashSize,0.01);
00054     addParameterDef("rootE",&rootE,0); 
00055     addParameterDef("logaE",&logaE,0); 
00056     t=0;
00057     initialised = false;
00058   }
00059 
00060 
00061 protected:
00062   paramval epsC; ///< learning rate factor for controller learning
00063   paramval epsA; ///< learning rate factor for model learning
00064   paramval factorB; ///< additional learning rate factor for model bias 
00065   paramint s4delay; ///< number of timesteps of delay in the SML 
00066   paramint s4avg; ///< number of timesteps used for smoothing the controller output values 
00067   paramint logaE;  ///< logarithmic error is used for learning 1: controller 2: model 3: both
00068   paramint rootE;  ///< root error is used for learning 1: controller 2: model 3: both
00069 
00070 
00071   paramval squashSize; ///< size of the box, where the parameter updates are clipped to
00072 
00073   int t;
00074   unsigned short buffersize;
00075   bool initialised;
00076 
00077 protected:
00078   /// put new value in ring buffer
00079   void putInBuffer(matrix::Matrix* buffer, const matrix::Matrix& vec, int delay=0){
00080     buffer[(t-delay)%buffersize] = vec;
00081   }
00082 
00083   /// calculate delayed values
00084   virtual matrix::Matrix calculateDelayedValues(const matrix::Matrix* buffer, 
00085                                                   int number_steps_of_delay_){
00086     // number_steps_of_delay must not be smaller than buffersize
00087     assert ((unsigned)number_steps_of_delay_ < buffersize);  
00088     return buffer[(t - number_steps_of_delay_) % buffersize];
00089   };
00090   
00091   /// calculate time-smoothed values 
00092   virtual matrix::Matrix calculateSmoothValues(const matrix::Matrix* buffer, 
00093                                                  int number_steps_for_averaging_){
00094     // number_steps_for_averaging_ must not be larger than buffersize
00095     assert ((int)number_steps_for_averaging_ <= buffersize);
00096     
00097     matrix::Matrix result(buffer[t % buffersize]);
00098     for (int k = 1; k < number_steps_for_averaging_; k++) {
00099       result += buffer[(t - k + buffersize) % buffersize];
00100     }
00101     result *= 1/((double) (number_steps_for_averaging_)); // scalar multiplication
00102     return result;
00103   };
00104 
00105   /// calculates the error_factor for either logarithmic (E=ln(e^T*e)) or square (E=sqrt(e^t*e)) error
00106   virtual double calcErrorFactor(const matrix::Matrix& e, bool loga, bool root) {
00107     double error_factor = 1;
00108     if (loga){   // using logarithmic error E=ln(v^T*v)
00109       error_factor= 1/(e.multTM().val(0,0)+0.000001)*0.01; // factor 1/100 for normalising (empirically)
00110     }
00111     if (root){  // using root error E=(v^T*v)^(1/2) 
00112       error_factor= 1/sqrt(e.multTM().val(0,0)+0.000001)*0.1; // factor 1/10 for normalising (empirically)
00113     }  
00114     return error_factor;
00115   }
00116   
00117   
00118   /// neuron transfer function
00119   static double g(double z)
00120   {
00121     return tanh(z);
00122   };
00123 
00124 
00125  
00126 };
00127 
00128 #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