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