invertmotornstep.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: invertmotornstep.h,v $ 00020 * Revision 1.22 2006/12/21 11:44:17 martius 00021 * commenting style for doxygen //< -> ///< 00022 * FOREACH and FOREACHC are macros for collection iteration 00023 * 00024 * Revision 1.21 2006/12/01 16:18:26 martius 00025 * S uses first and second derivative. Not really tested yet 00026 * 00027 * Revision 1.20 2006/11/29 16:22:43 martius 00028 * name is a variable of configurable and is used as such 00029 * 00030 * Revision 1.19 2006/10/23 10:47:30 martius 00031 * model as function 00032 * 00033 * Revision 1.18 2006/08/02 09:31:16 martius 00034 * calcErrorFactor moved to invertmotorcontroller 00035 * 00036 * Revision 1.17 2006/07/20 17:14:34 martius 00037 * removed std namespace from matrix.h 00038 * storable interface 00039 * abstract model and invertablemodel as superclasses for networks 00040 * 00041 * Revision 1.16 2006/07/18 15:17:16 martius 00042 * buffer operations like delayed values and smoothed values moved to invertmotorcontroller 00043 * 00044 * Revision 1.15 2006/07/18 14:47:45 martius 00045 * new regularisation of g' and g''/g' and squashP moved to invertmotorcontroller 00046 * 00047 * Revision 1.14 2006/07/14 12:23:58 martius 00048 * selforg becomes HEAD 00049 * 00050 * Revision 1.12.6.12 2006/07/10 13:05:16 martius 00051 * NON-COMMERICAL LICENSE added to controllers 00052 * 00053 * Revision 1.12.6.11 2006/07/10 11:59:23 martius 00054 * Matrixlib now in selforg 00055 * no namespace std in header files 00056 * 00057 * Revision 1.12.6.10 2006/02/24 14:46:44 martius 00058 * removed gR 00059 * 00060 * Revision 1.12.6.9 2006/02/08 15:16:24 martius 00061 * *** empty log message *** 00062 * 00063 * Revision 1.12.6.8 2006/01/31 15:30:34 martius 00064 * model does not learn if too large error. 00065 * 00066 * Revision 1.12.6.7 2006/01/18 16:49:34 martius 00067 * new Update algo (simpler and hopefully better) 00068 * 00069 * Revision 1.12.6.6 2006/01/17 16:58:40 martius 00070 * loading and storing 00071 * 00072 * Revision 1.12.6.5 2006/01/13 12:24:39 martius 00073 * motor teaching included 00074 * 00075 * Revision 1.12.6.4 2005/12/15 17:04:39 martius 00076 * min, max and so on are template functions now 00077 * 00078 * Revision 1.12.6.3 2005/11/21 17:02:46 martius 00079 * calcCandHUpdates as separat function 00080 * better formula for g' (q') using Mittelwertsatz 00081 * works great! 00082 * 00083 * Revision 1.12.6.2 2005/11/16 11:24:26 martius 00084 * moved to selforg 00085 * 00086 * Revision 1.12.6.1 2005/11/14 14:54:02 martius 00087 * cpp file is seperate now 00088 * 00089 * Revision 1.12 2005/10/27 15:46:38 martius 00090 * inspectable interface is expanded to structural information for network visualiser 00091 * 00092 * Revision 1.11 2005/10/24 11:10:27 martius 00093 * disabled exact formula for shift 00094 * 00095 * Revision 1.10 2005/10/21 16:02:49 martius 00096 * added additional model matrix S (switch in contructor) 00097 * 00098 * Revision 1.9 2005/10/21 11:55:13 martius 00099 * id is now toId 00100 * relativeE can be switched on 00101 * uses an symmetric but exact formula for shift calcuation through neuron 00102 * However g'/g'' stays -2g because implementation was making trouble 00103 * 00104 * Revision 1.8 2005/10/06 17:08:08 martius 00105 * switched to stl lists 00106 * Bias noise 00107 * correct error_factor for controller learning 00108 * 00109 * Revision 1.7 2005/09/27 10:59:47 martius 00110 * error_factor (from logaE or rootE) is member now 00111 * 00112 * Revision 1.6 2005/09/22 07:22:59 martius 00113 * proper destruction 00114 * 00115 * Revision 1.5 2005/09/21 07:31:10 martius 00116 * v does not go through g 00117 * 00118 * Revision 1.4 2005/07/28 11:16:08 martius 00119 * there is a switch for exporting just some of the internal parameters 00120 * 00121 * Revision 1.3 2005/07/27 15:44:23 martius 00122 * learning rate adaptation 00123 * 00124 * Revision 1.2 2005/07/21 15:07:59 martius 00125 * working version 00126 * squashing normalised 00127 * 00128 * Revision 1.1 2005/07/19 09:35:35 martius 00129 * invert motor space controller that iterates 00130 * over multiple time steps in past 00131 * 00132 * 00133 ***************************************************************************/ 00134 #ifndef __INVERTMOTORNSTEP_H 00135 #define __INVERTMOTORNSTEP_H 00136 00137 #include "invertmotorcontroller.h" 00138 00139 #include <assert.h> 00140 #include <math.h> 00141 00142 #include <selforg/matrix.h> 00143 #include <selforg/noisegenerator.h> 00144 00145 typedef struct InvertMotorNStepConf { 00146 int buffersize; //< buffersize size of the time-buffer for x,y,eta 00147 double cInit; //< cInit size of the C matrix to initialised with. 00148 double cNonDiag; ///< cNonDiag is the size of the nondiagonal elements in respect to the diagonal (cInit) ones 00149 bool useS; ///< useS decides whether to use the S matrix in addition to the A matrix 00150 bool someInternalParams; ///< someInternalParams if true only some internal parameters are exported, all otherwise 00151 bool useTeaching; ///< if true, the controller honors the teaching signal 00152 } InvertMotorNStepConf; 00153 00154 /** 00155 * class for robot controller that uses the georg's matrixlib for 00156 * direct matrix inversion for n channels 00157 * (simple one layer networks) 00158 * 00159 * Implements standart parameters: eps, rho, mu, stepnumber4avg, stepnumber4delay 00160 */ 00161 class InvertMotorNStep : public InvertMotorController { 00162 00163 public: 00164 InvertMotorNStep(const InvertMotorNStepConf& conf = getDefaultConf()); 00165 virtual void init(int sensornumber, int motornumber); 00166 00167 virtual ~InvertMotorNStep(); 00168 00169 /// returns the number of sensors the controller was initialised with or 0 if not initialised 00170 virtual int getSensorNumber() const { return number_sensors; } 00171 /// returns the mumber of motors the controller was initialised with or 0 if not initialised 00172 virtual int getMotorNumber() const { return number_motors; } 00173 00174 /// performs one step (includes learning). 00175 /// Calulates motor commands from sensor inputs. 00176 virtual void step(const sensor* , int number_sensors, motor* , int number_motors); 00177 00178 /// performs one step without learning. Calulates motor commands from sensor inputs. 00179 virtual void stepNoLearning(const sensor* , int number_sensors, 00180 motor* , int number_motors); 00181 00182 /**** STOREABLE ****/ 00183 /** stores the controller values to a given file. */ 00184 virtual bool store(FILE* f) const; 00185 /** loads the controller values from a given file. */ 00186 virtual bool restore(FILE* f); 00187 00188 /**** INSPECTABLE ****/ 00189 virtual std::list<iparamkey> getInternalParamNames() const; 00190 virtual std::list<iparamval> getInternalParams() const; 00191 virtual std::list<ILayer> getStructuralLayers() const; 00192 virtual std::list<IConnection> getStructuralConnections() const; 00193 00194 /**** TEACHING ****/ 00195 virtual void setTeachingMode(bool onOff); 00196 virtual bool getTeachingMode(); 00197 virtual void setMotorTeachingSignal(const motor* teaching, int len); 00198 void calcCandHUpdatesTeaching(matrix::Matrix& C_update, matrix::Matrix& H_update, int y_delay); 00199 00200 00201 static InvertMotorNStepConf getDefaultConf(){ 00202 InvertMotorNStepConf c; 00203 c.buffersize = 50; 00204 c.cInit = 1.0; 00205 c.cNonDiag = 0; 00206 c.useS = false; 00207 c.someInternalParams = true; 00208 c.useTeaching = false; 00209 return c; 00210 } 00211 00212 void getLastMotors(motor* motors, int len); 00213 00214 protected: 00215 unsigned short number_sensors; 00216 unsigned short number_motors; 00217 00218 matrix::Matrix A; ///< Model Matrix (motors to sensors) 00219 matrix::Matrix S; ///< additional Model Matrix (sensors derivatives to sensors) 00220 matrix::Matrix C; ///< Controller Matrix 00221 matrix::Matrix H; ///< Controller Bias 00222 matrix::Matrix B; ///< Model Bias 00223 NoiseGenerator* BNoiseGen; ///< Noisegenerator for noisy bias 00224 matrix::Matrix R; ///< C*A 00225 matrix::Matrix SmallID; ///< small identity matrix in the dimension of R 00226 matrix::Matrix xsi; ///< current output error 00227 double xsi_norm; ///< norm of matrix 00228 double xsi_norm_avg; ///< average norm of xsi (used to define whether Modell learns) 00229 double pain; ///< if the modelling error (xsi) is too high we have a pain signal 00230 matrix::Matrix* x_buffer; 00231 matrix::Matrix* y_buffer; 00232 matrix::Matrix* eta_buffer; 00233 matrix::Matrix zero_eta; // zero initialised eta 00234 matrix::Matrix x_smooth; 00235 // matrix::Matrix z; ///< membrane potential 00236 00237 matrix::Matrix y_teaching; ///< teaching motor signal 00238 00239 InvertMotorNStepConf conf; 00240 00241 /// puts the sensors in the ringbuffer, generate controller values and put them in the 00242 // ringbuffer as well 00243 virtual void fillBuffersAndControl(const sensor* x_, int number_sensors, 00244 motor* y_, int number_motors); 00245 00246 /// calculates the first shift into the motor space useing delayed motor values. 00247 // @param delay 0 for no delay and n>0 for n timesteps delay in the time loop 00248 virtual void calcEtaAndBufferIt(int delay); 00249 /// calculates xsi for the current time step using the delayed y values 00250 // and x delayed by one 00251 // @param delay 0 for no delay and n>0 for n timesteps delay in the time loop 00252 virtual void calcXsi(int delay); 00253 00254 /// learn H,C with motors y and corresponding sensors x 00255 virtual void learnController(); 00256 00257 /// calculates the Update for C and H 00258 // @param y_delay timesteps to delay the y-values. (usually 0) 00259 // Please note that the delayed values are NOT used for the error calculation 00260 // (this is done in calcXsi()) 00261 virtual void calcCandHUpdates(matrix::Matrix& C_update, matrix::Matrix& H_update, int y_delay); 00262 00263 /// updates the matrix C and H 00264 virtual void updateCandH(const matrix::Matrix& C_update, const matrix::Matrix& H_update, double squashSize); 00265 00266 /// learn A, (and S) using motors y and corresponding sensors x 00267 // @param delay 0 for no delay and n>0 for n timesteps delay in the time loop 00268 virtual void learnModel(int delay); 00269 00270 /// calculates the predicted sensor values 00271 virtual matrix::Matrix model(const matrix::Matrix* x_buffer, int delay, const matrix::Matrix& y); 00272 00273 /// returns controller output for given sensor values 00274 virtual matrix::Matrix calculateControllerValues(const matrix::Matrix& x_smooth); 00275 00276 /** Calculates first and second derivative and returns both in on matrix (above). 00277 We use simple discrete approximations: 00278 \f[ f'(x) = (f(x) - f(x-1)) / 2 \f] 00279 \f[ f''(x) = f(x) - 2f(x-1) + f(x-2) \f] 00280 where we have to go into the past because we do not have f(x+1). The scaling can be neglegted. 00281 */ 00282 matrix::Matrix calcDerivatives(const matrix::Matrix* buffer, int delay); 00283 00284 00285 }; 00286 00287 #endif

Generated on Tue Jan 16 02:14:36 2007 for Robotsystem of the Robot Group Leipzig by doxygen 1.3.8