invertnchannelcontroller.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  *    Frank Hesse    <frank at nld dot ds dot mpg dot de>                  *
00005  *    Ralf Der       <ralfder at mis dot mpg dot 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  ***************************************************************************/
00020 #ifndef __INVERTNCHANNELCONTROLLER_H
00021 #define __INVERTNCHANNELCONTROLLER_H
00022 
00023 #include "invertcontroller.h"
00024 #include "controller_misc.h"
00025 
00026 #include <assert.h>
00027 #include <cmath>
00028 
00029 #include <selforg/matrix.h>
00030 
00031 /**
00032  * class for robot controller that uses the georg's matrixlib for 
00033  *  direct matrix inversion for n channels 
00034  * (simple one layer networks)
00035  * 
00036  * Implements standart parameters: eps, rho, mu, stepnumber4avg, stepnumber4delay
00037  */
00038 class InvertNChannelController : public InvertController {
00039 
00040 public:
00041   InvertNChannelController(int _buffersize, bool _update_only_1=false);
00042   virtual void init(int sensornumber, int motornumber, RandGen* randGen = 0);
00043 
00044   virtual ~InvertNChannelController();
00045 
00046   /// returns the name of the object (with version number)
00047   virtual paramkey getName() const {return name; } 
00048   /// returns the number of sensors the controller was initialised with or 0 if not initialised
00049   virtual int getSensorNumber() const { return number_channels; }
00050   /// returns the mumber of motors the controller was initialised with or 0 if not initialised
00051   virtual int getMotorNumber() const  { return number_channels; }
00052 
00053   /// performs one step (includes learning). 
00054   /// Calulates motor commands from sensor inputs.
00055   virtual void step(const sensor* , int number_sensors, motor* , int number_motors);
00056 
00057 
00058   /// performs one step without learning. Calulates motor commands from sensor inputs.
00059   virtual void stepNoLearning(const sensor* , int number_sensors, 
00060                               motor* , int number_motors);
00061 
00062 
00063   /***** STOREABLE ****/
00064   /** stores the controller values to a given file. */
00065   virtual bool store(FILE* f) const;
00066   /** loads the controller values from a given file. */
00067   virtual bool restore(FILE* f);  
00068 
00069   // inspectable interface
00070   virtual std::list<ILayer> getStructuralLayers() const;
00071   virtual std::list<IConnection> getStructuralConnections() const;
00072   
00073 
00074 protected:
00075   unsigned short number_channels;
00076   unsigned short buffersize;
00077   bool update_only_1;
00078 
00079   matrix::Matrix A; // Model Matrix
00080   matrix::Matrix C; // Controller Matrix
00081   matrix::Matrix h; // Controller Bias
00082   matrix::Matrix L; // Jacobi Matrix
00083   matrix::Matrix* x_buffer;
00084   matrix::Matrix* y_buffer;
00085   int t;
00086   paramkey name;
00087   
00088 
00089 /*   virtual void iteration(double *column, */
00090 /*                       double dommy[NUMBER_CHANNELS][NUMBER_CHANNELS], */
00091 /*                       double *improvment); */
00092 
00093   virtual double calculateE(const matrix::Matrix& x_delay, const matrix::Matrix& y_delay);      
00094 
00095   /// learn values h,C
00096   virtual void learn(const matrix::Matrix& x_delay, const matrix::Matrix& y_delay);
00097 
00098   virtual void learnmodel( const matrix::Matrix& y_delay);
00099 
00100   /// calculate delayed values
00101   virtual matrix::Matrix calculateDelayedValues(const matrix::Matrix* buffer, 
00102                                         unsigned int number_steps_of_delay_);
00103   virtual matrix::Matrix calculateSmoothValues(const matrix::Matrix* buffer, 
00104                                        unsigned int number_steps_for_averaging_);
00105 
00106   matrix::Matrix calculateControllerValues(const matrix::Matrix& x_smooth);
00107     
00108   // put new value in ring buffer
00109   void putInBuffer(matrix::Matrix* buffer, const matrix::Matrix& vec);
00110 
00111   /// neuron transfer function
00112   static double g(double z)
00113   {
00114     return tanh(z);
00115   };
00116 
00117   ///
00118   static double g_s(double z)
00119   {
00120     double k=tanh(z);
00121     return 1.0 - k*k;
00122     //    return 1.0 - tanh(z)*tanh(z);
00123   };
00124 
00125 
00126 
00127   /// squashing function, to protect against to large weight updates
00128   static double squash(double z)
00129   {
00130     return clip(z,-0.1,0.1);
00131     //    return z < -0.1 ? -0.1 : ( z > 0.1 ? 0.1 : z );
00132     //return 0.1 * tanh(10.0 * z);
00133   };
00134 };
00135 
00136 #endif
00137 
00138 
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