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 * This program is free software; you can redistribute it and/or modify * 00008 * it under the terms of the GNU General Public License as published by * 00009 * the Free Software Foundation; either version 2 of the License, or * 00010 * (at your option) any later version. * 00011 * * 00012 * This program is distributed in the hope that it will be useful, * 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00015 * GNU General Public License for more details. * 00016 * * 00017 * You should have received a copy of the GNU General Public License * 00018 * along with this program; if not, write to the * 00019 * Free Software Foundation, Inc., * 00020 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00021 * * 00022 * $Log: controller_misc.h,v $ 00023 * Revision 1.9 2008/05/30 11:58:27 martius 00024 * use cmath instead of math.h 00025 * 00026 * Revision 1.8 2008/05/07 16:45:52 martius 00027 * code cosmetics and documentation 00028 * 00029 * Revision 1.7 2008/04/30 14:54:48 guettler 00030 * support for new matrix indices integrated 00031 * 00032 * Revision 1.6 2008/04/29 15:31:44 guettler 00033 * modified toBinary map functions 00034 * 00035 * Revision 1.5 2008/04/29 10:29:39 guettler 00036 * added toBinaryWithProbability for mapP 00037 * 00038 * Revision 1.4 2008/04/28 15:32:46 guettler 00039 * doxygen corrected 00040 * 00041 * Revision 1.3 2008/04/25 10:31:18 guettler 00042 * added toBinaryWithThreshold function for map2 00043 * 00044 * Revision 1.2 2008/04/17 14:54:45 martius 00045 * randomGen added, which is a random generator with long period and an 00046 * internal state. Each Agent has an instance and passed it to the controller 00047 * and the wiring. This is good for 00048 * a) repeatability on agent basis, 00049 * b) parallel execution as done in ode_robots 00050 * 00051 * Revision 1.1 2008/04/16 12:44:16 martius 00052 * moved to utils 00053 * 00054 * Revision 1.24 2008/03/01 01:47:34 martius 00055 * plus_ and lowercutof 00056 * 00057 * Revision 1.23 2007/12/13 16:43:24 martius 00058 * noiseMatrix adapted 00059 * 00060 * Revision 1.22 2007/12/11 14:43:27 martius 00061 * clipping for matrix mapP 00062 * 00063 * Revision 1.21 2007/08/24 12:02:22 martius 00064 * getKSmallestElement 00065 * 00066 * Revision 1.20 2007/07/16 08:51:57 martius 00067 * argmax 00068 * 00069 * Revision 1.19 2007/06/21 16:27:43 martius 00070 * min and max for matrices 00071 * 00072 * Revision 1.18 2007/06/08 15:44:57 martius 00073 * constant function 00074 * 00075 * Revision 1.17 2007/05/22 16:01:45 martius 00076 * argmin 00077 * 00078 * Revision 1.16 2007/03/22 08:07:47 robot3 00079 * added two methods for generating names and a list from arrays for the interface 00080 * inspectable: getInternalParams() and getInternalParamNames() 00081 * 00082 * Revision 1.15 2007/03/22 08:05:03 robot3 00083 * this is an adapter class which implements all needed things. This class is 00084 * used for example by the DiscreteControllerAdapter. 00085 * 00086 * Revision 1.14 2007/02/20 15:41:06 martius 00087 * big model stuff, elman and co 00088 * 00089 * Revision 1.13 2007/02/02 11:16:18 martius 00090 * getKthLargestElementc 00091 * 00092 * Revision 1.12 2006/07/20 17:14:34 martius 00093 * removed std namespace from matrix.h 00094 * storable interface 00095 * abstract model and invertablemodel as superclasses for networks 00096 * 00097 * Revision 1.11 2006/07/14 12:23:57 martius 00098 * selforg becomes HEAD 00099 * 00100 * Revision 1.9.6.4 2006/07/10 11:59:23 martius 00101 * Matrixlib now in selforg 00102 * no namespace std in header files 00103 * 00104 * Revision 1.9.6.3 2006/01/17 16:58:39 martius 00105 * loading and storing 00106 * 00107 * Revision 1.9.6.2 2005/12/15 17:04:39 martius 00108 * min, max and so on are template functions now 00109 * 00110 * Revision 1.9.6.1 2005/11/15 12:30:27 martius 00111 * new selforg structure and OdeAgent, OdeRobot ... 00112 * 00113 * Revision 1.9 2005/11/10 09:13:33 martius 00114 * readded defines like min, max sqr,... 00115 * 00116 * Revision 1.8 2005/10/27 14:17:17 martius 00117 * some functions are now found in mathutils.h (dir: ../ode_robots/utils) 00118 * 00119 * Revision 1.7 2005/10/21 11:50:33 martius 00120 * adapt functions 00121 * random number function for usage with Matrix::map 00122 * 00123 * Revision 1.6 2005/10/06 17:06:57 martius 00124 * switched to stl lists 00125 * 00126 * Revision 1.5 2005/09/22 10:33:07 martius 00127 * max and min more save 00128 * 00129 * Revision 1.4 2005/08/29 09:05:31 fhesse 00130 * removed weird bug in get4x4AndDiagonalSize, probably caused by some compiler optimization 00131 * 00132 * Revision 1.3 2005/08/06 20:47:54 martius 00133 * Commented 00134 * 00135 * Revision 1.2 2005/07/29 14:26:56 martius 00136 * fixed bug in length calculation of 4x4 diagonal routines 00137 * 00138 * Revision 1.1 2005/07/28 11:14:52 martius 00139 * helper functions for controller 00140 * support for internal parameters more easy 00141 * 00142 * 00143 ***************************************************************************/ 00144 #ifndef __CONTOLLER_MISC_H 00145 #define __CONTOLLER_MISC_H 00146 00147 #include <selforg/matrix.h> 00148 #include <assert.h> 00149 #include <stdlib.h> 00150 #include <cmath> 00151 00152 #include "stl_adds.h" 00153 #include "noisegenerator.h" 00154 #include "inspectable.h" 00155 00156 00157 template<typename T> 00158 inline T sign(T v) { return v<(T)0?(T)-1:(T)1; } 00159 00160 template<typename T> 00161 inline T sqr(T v) { return v*v; } 00162 00163 template<typename T> 00164 inline T clip(T x, T lobound, T highbound) { 00165 return (x)<(lobound) ? (lobound) : ( (x) > (highbound) ? (highbound) : (x) ); 00166 } 00167 00168 00169 /// calculates 1/x 00170 double one_over(double x); 00171 00172 /// returns c as a double (useful for Matrix::mapP to fill matrix with constant value) 00173 double constant(void* c, double); 00174 00175 /// creates random number from -1 to 1 00176 double random_minusone_to_one(double); 00177 00178 /// creates random number from -1 to 1 00179 // using the RandGen* which is give as a void pointer (Matrix::mapP) 00180 double random_minusone_to_one(void *, double); 00181 00182 /// clipping function for mapP 00183 double clip(void* r,double); 00184 00185 /// cutof function for mapP 00186 double lowercutof(void* theta, double); 00187 00188 /// returns -1 if probability is to low, otherwise 1 for mapP 00189 double toBinaryWithProbability(void* r,double x); 00190 00191 /// returns -1 if below threshold, otherwise 1 for map2 00192 double toBinaryWithThreshold(double x, double threshold); 00193 00194 /// plus function for mapP 00195 double plus_(void* b, double a); 00196 00197 00198 /** stores at least left top 4x4 submatrix (row-wise) (if exists) and 00199 then the rest of the diagonal elements into a list 00200 @return list of values 00201 */ 00202 std::list<matrix::D> store4x4AndDiagonal(const matrix::Matrix& m); 00203 00204 /** stores at least left top 4x4 submatrix (row-wise) (if exists) and 00205 then the rest of the diagonal elements 00206 @param m matrix to be stored 00207 @param len Length of the provided buffer 00208 (should be min(getN(),4)*min(getM(),4)+ max(0,min(getM()-4,getN()-4))) 00209 @return number of actually written elements 00210 */ 00211 matrix::I store4x4AndDiagonal(const matrix::Matrix& m, matrix::D* buffer, matrix::I len); 00212 00213 /** returns the number of elements stored by store4x4AndDiagonal 00214 (should be min(getN(),4)*min(getM(),4)+ max(0,min(getM()-4,getN()-4))) 00215 */ 00216 matrix::I get4x4AndDiagonalSize(const matrix::Matrix& m); 00217 00218 /** writes the names of the fields stored by store4x4AndDiagonal into a list 00219 @param m matrix to be stored 00220 @param matrixName name of the matrix (prefix for all fields) 00221 @param keylist list for field names 00222 @param len Length of the provided buffer 00223 (should be min(getN(),4)*min(getM(),4)+ max(0,min(getM()-4,getN()-4))) 00224 @return number of actually written elements 00225 */ 00226 std::list<Inspectable::iparamkey> store4x4AndDiagonalFieldNames(const matrix::Matrix& m, 00227 const std::string& matrixName); 00228 00229 /** stores the names of the fields stored by store4x4AndDiagonal 00230 @param m matrix to be stored 00231 @param matrixName name of the matrix (prefix for all fields) 00232 @param keylist list for field names 00233 @param len Length of the provided buffer 00234 (should be min(getN(),4)*min(getM(),4)+ max(0,min(getM()-4,getN()-4))) 00235 @return number of actually written elements 00236 */ 00237 matrix::I store4x4AndDiagonalFieldNames(const matrix::Matrix& m, 00238 const std::string& matrixName, 00239 char** keylist, matrix::I len); 00240 00241 /** stores the names of the all matrix fieldnames produces by convertToBuffer into a list 00242 @return list of names 00243 */ 00244 std::list<Inspectable::iparamkey> storeMatrixFieldNames(const matrix::Matrix& m, const std::string& matrixName); 00245 00246 /** stores the names of the all vector (mx1 matrix) fieldnames produces by convertToBuffer into a list 00247 @return list of names 00248 */ 00249 std::list<Inspectable::iparamkey> storeVectorFieldNames(const matrix::Matrix& m, const std::string& vectorName); 00250 00251 /** stores the names of the all matrix fieldnames produces by convertToBuffer 00252 @param m matrix to be stored 00253 @param matrixName name of the matrix (prefix for all fields) 00254 @param keylist list for field names 00255 @param len Length of the provided buffer 00256 (should be getN()*getM() 00257 @return number of actually written elements 00258 */ 00259 matrix::I storeMatrixFieldNames(const matrix::Matrix& m, const char* matrixName, 00260 char** keylist, matrix::I len); 00261 00262 /** stores the names of the all vector (mx1 matrix) fieldnames produces by convertToBuffer 00263 @param m vector to be stored 00264 @param vectorName name of the vector (prefix for all fields) 00265 @param keylist list for field names 00266 @param len Length of the provided buffer (should be getM()) 00267 @return number of actually written elements 00268 */ 00269 matrix::I storeVectorFieldNames(const matrix::Matrix& m, const char* vectorName, 00270 char** keylist, matrix::I len); 00271 00272 /** stores the Matrix into the given file stream (binary) 00273 */ 00274 bool storeMatrix(const matrix::Matrix& m, FILE* f); 00275 00276 /** reads a Matrix from the given file stream (binary) 00277 */ 00278 bool restoreMatrix(matrix::Matrix& m, FILE* f); 00279 00280 /// returns a Matrix with values generated by the given noise generator 00281 matrix::Matrix noiseMatrix(matrix::I m, matrix::I n, NoiseGenerator& ng, 00282 double strength,double unused = 0); 00283 00284 /** calculates to linear matrix norm (sum of absolute values divided by number of values (m*n) ) 00285 */ 00286 double matrixNorm1(const matrix::Matrix& m); 00287 00288 /** calculates to square matrix norm (sum of square values divided by number of values (m*n) ) 00289 */ 00290 double matrixNorm2(const matrix::Matrix& m); 00291 00292 00293 /** returns the k. largest element of the matrix 00294 Attention: it will detroy the given matrix! (sorting) 00295 Assumption: k>0 and k<=matrixsize 00296 */ 00297 double getKthLargestElement(matrix::Matrix& matrix, matrix::I k); 00298 00299 /** returns the k. smallest element of the matrix 00300 Attention: it will detroy the given matrix! (sorting) 00301 Assumption: k>0 and k<=matrixsize 00302 */ 00303 double getKthSmallestElement(matrix::Matrix& matrix, matrix::I k); 00304 00305 /// considers the matrix as vector (mx1) and returns the index of the smallest element 00306 matrix::I argmin(const matrix::Matrix& v); 00307 00308 /// considers the matrix as vector (mx1) and returns the index of the largest element 00309 matrix::I argmax(const matrix::Matrix& v); 00310 00311 /// returns the smallest element 00312 double min(const matrix::Matrix& v); 00313 00314 /// minimum function for doubles without templates 00315 double min(double a, double b); 00316 00317 /// returns the largest element 00318 double max(const matrix::Matrix& v); 00319 00320 /// maximum function for doubles without templates 00321 double max(double a, double b); 00322 00323 00324 /// samples from the pdf (rowwise stored with sum = 1) 00325 matrix::I sample(const matrix::Matrix& pdf); 00326 00327 00328 /// parameter adaptation algorithm. 00329 // @param p current parameter value 00330 // @param actual actual value of some size controlled by p 00331 // @param nominal nominal value of some size controlled by p 00332 // @param up_rate adaptation rate for increasing p (<< 1) 00333 // @param down_rate adaptation rate for decreasing p (<< 1) 00334 // @return new value of p (there is no clipping done) 00335 double adapt(double p, double actual, double nominal, double up_rate, double down_rate); 00336 00337 /// like adapt but that the adaption is just done if the actual value is outside the given interval (_min, _max) 00338 double adaptMinMax(double p, double actual, double _min, double _max, double up_rate, double down_rate); 00339 00340 /** 00341 * Helper function for converting an array with double values to a list. 00342 * Is used for the method getInternalParams() interface inspectable. 00343 */ 00344 std::list<Inspectable::iparamval> convertArrayToList(double* array,int arraySize); 00345 /** 00346 * Helper function for getting the array names of an array 00347 * Is used for the method getInternalParamNames() interface inspectable. 00348 */ 00349 std::list<Inspectable::iparamkey> getArrayNames(int arraySize,const char* name); 00350 00351 #endif