homeokinbase.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2011 by                                            *
00003  *    Georg Martius  <georg dot martius at web dot de>                     *
00004  *    Ralf Der       <ralfder at mis dot mpg dot 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  ***************************************************************************/
00019 
00020 #ifndef __HOMEOKINBASE_H
00021 #define __HOMEOKINBASE_H
00022 
00023 #include "abstractcontroller.h"
00024 #include "controller_misc.h"
00025 #include <stdlib.h>
00026 #include <string.h>
00027 
00028 
00029 /**
00030  * Abstract class (interface) for robot controller that use are based on the homeokinetic
00031  * prinziple
00032  * 
00033  * Implements standard buffers and 
00034  *  configureable interface for some useful parameters like  epsC, epsA, s4avg ...
00035  */
00036 class HomeokinBase : public AbstractController {
00037 public:
00038   HomeokinBase( unsigned short buffersize ,
00039                  const std::string& name, const std::string& revision)
00040     : AbstractController(name, revision){
00041     this->buffersize = buffersize;
00042     addParameterDef("epsC", &epsC, 0.1); 
00043     addParameterDef("epsA",&epsA,  0.1); 
00044     addParameterDef("s4delay",&s4delay,1); 
00045     addParameterDef("s4avg",&s4avg,1); 
00046     addParameterDef("factorB",&factorB,0.2); 
00047     addParameterDef("squashsize",&squashSize,0.01);
00048     addParameterDef("rootE",&rootE,0); 
00049     addParameterDef("logaE",&logaE,0); 
00050     t=0;
00051     initialised = false;
00052   }
00053 
00054 
00055 protected:
00056   paramval epsC; ///< learning rate factor for controller learning
00057   paramval epsA; ///< learning rate factor for model learning
00058   paramval factorB; ///< additional learning rate factor for model bias 
00059   paramint s4delay; ///< number of timesteps of delay in the SML 
00060   paramint s4avg; ///< number of timesteps used for smoothing the controller output values 
00061   paramint logaE;  ///< logarithmic error is used for learning 1: controller 2: model 3: both
00062   paramint rootE;  ///< root error is used for learning 1: controller 2: model 3: both
00063 
00064 
00065   paramval squashSize; ///< size of the box, where the parameter updates are clipped to
00066 
00067   int t;
00068   unsigned short buffersize;
00069   bool initialised;
00070 
00071 protected:
00072   /// put new value in ring buffer
00073   void putInBuffer(matrix::Matrix* buffer, const matrix::Matrix& vec, int delay=0){
00074     buffer[(t-delay)%buffersize] = vec;
00075   }
00076 
00077   /// calculate delayed values
00078   virtual matrix::Matrix calculateDelayedValues(const matrix::Matrix* buffer, 
00079                                                   int number_steps_of_delay_){
00080     // number_steps_of_delay must not be smaller than buffersize
00081     assert ((unsigned)number_steps_of_delay_ < buffersize);  
00082     return buffer[(t - number_steps_of_delay_) % buffersize];
00083   };
00084   
00085   /// calculate time-smoothed values 
00086   virtual matrix::Matrix calculateSmoothValues(const matrix::Matrix* buffer, 
00087                                                  int number_steps_for_averaging_){
00088     // number_steps_for_averaging_ must not be larger than buffersize
00089     assert ((int)number_steps_for_averaging_ <= buffersize);
00090     
00091     matrix::Matrix result(buffer[t % buffersize]);
00092     for (int k = 1; k < number_steps_for_averaging_; k++) {
00093       result += buffer[(t - k + buffersize) % buffersize];
00094     }
00095     result *= 1/((double) (number_steps_for_averaging_)); // scalar multiplication
00096     return result;
00097   };
00098 
00099   /// calculates the error_factor for either logarithmic (E=ln(e^T*e)) or square (E=sqrt(e^t*e)) error
00100   virtual double calcErrorFactor(const matrix::Matrix& e, bool loga, bool root) {
00101     double error_factor = 1;
00102     if (loga){   // using logarithmic error E=ln(v^T*v)
00103       error_factor= 1/(e.multTM().val(0,0)+0.000001)*0.01; // factor 1/100 for normalising (empirically)
00104     }
00105     if (root){  // using root error E=(v^T*v)^(1/2) 
00106       error_factor= 1/sqrt(e.multTM().val(0,0)+0.000001)*0.1; // factor 1/10 for normalising (empirically)
00107     }  
00108     return error_factor;
00109   }
00110   
00111   
00112   /// neuron transfer function
00113   static double g(double z)
00114   {
00115     return tanh(z);
00116   };
00117 
00118 
00119  
00120 };
00121 
00122 #endif
Generated on Thu Jun 28 14:45:36 2012 for Robot Simulator of the Robotics Group for Self-Organization of Control by  doxygen 1.6.3