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