controller_misc.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 * 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.12 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.11 2006/07/14 12:23:57 martius 00029 * selforg becomes HEAD 00030 * 00031 * Revision 1.9.6.4 2006/07/10 11:59:23 martius 00032 * Matrixlib now in selforg 00033 * no namespace std in header files 00034 * 00035 * Revision 1.9.6.3 2006/01/17 16:58:39 martius 00036 * loading and storing 00037 * 00038 * Revision 1.9.6.2 2005/12/15 17:04:39 martius 00039 * min, max and so on are template functions now 00040 * 00041 * Revision 1.9.6.1 2005/11/15 12:30:27 martius 00042 * new selforg structure and OdeAgent, OdeRobot ... 00043 * 00044 * Revision 1.9 2005/11/10 09:13:33 martius 00045 * readded defines like min, max sqr,... 00046 * 00047 * Revision 1.8 2005/10/27 14:17:17 martius 00048 * some functions are now found in mathutils.h (dir: ../ode_robots/utils) 00049 * 00050 * Revision 1.7 2005/10/21 11:50:33 martius 00051 * adapt functions 00052 * random number function for usage with Matrix::map 00053 * 00054 * Revision 1.6 2005/10/06 17:06:57 martius 00055 * switched to stl lists 00056 * 00057 * Revision 1.5 2005/09/22 10:33:07 martius 00058 * max and min more save 00059 * 00060 * Revision 1.4 2005/08/29 09:05:31 fhesse 00061 * removed weird bug in get4x4AndDiagonalSize, probably caused by some compiler optimization 00062 * 00063 * Revision 1.3 2005/08/06 20:47:54 martius 00064 * Commented 00065 * 00066 * Revision 1.2 2005/07/29 14:26:56 martius 00067 * fixed bug in length calculation of 4x4 diagonal routines 00068 * 00069 * Revision 1.1 2005/07/28 11:14:52 martius 00070 * helper functions for controller 00071 * support for internal parameters more easy 00072 * 00073 * 00074 ***************************************************************************/ 00075 #ifndef __CONTOLLER_MISC_H 00076 #define __CONTOLLER_MISC_H 00077 00078 #include <selforg/matrix.h> 00079 #include <assert.h> 00080 #include <stdlib.h> 00081 #include <math.h> 00082 00083 #include "stl_adds.h" 00084 #include "noisegenerator.h" 00085 #include "inspectable.h" 00086 00087 template<typename T> 00088 inline T sign(T v) { return v<(T)0?(T)-1:(T)1; } 00089 00090 template<typename T> 00091 inline T sqr(T v) { return v*v; } 00092 00093 template<typename T> 00094 inline T clip(T x, T lobound, T highbound) { 00095 return (x)<(lobound) ? (lobound) : ( (x) > (highbound) ? (highbound) : (x) ); 00096 } 00097 00098 00099 /// calculates 1/x 00100 double one_over(double x); 00101 00102 /// creates random number from -1 to 1 00103 double random_minusone_to_one(double); 00104 00105 /* stores at least left top 4x4 submatrix (row-wise) (if exists) and 00106 then the rest of the diagonal elements into a list 00107 @return list of values 00108 */ 00109 std::list<matrix::D> store4x4AndDiagonal(const matrix::Matrix& m); 00110 00111 /* stores at least left top 4x4 submatrix (row-wise) (if exists) and 00112 then the rest of the diagonal elements 00113 @param len Length of the provided buffer 00114 (should be min(getN(),4)*min(getM(),4)+ max(0,min(getM()-4,getN()-4))) 00115 @return number of actually written elements 00116 */ 00117 unsigned int store4x4AndDiagonal(const matrix::Matrix& m, matrix::D* buffer, unsigned int len); 00118 00119 /* returns the number of elements stored by store4x4AndDiagonal 00120 (should be min(getN(),4)*min(getM(),4)+ max(0,min(getM()-4,getN()-4))) 00121 */ 00122 unsigned int get4x4AndDiagonalSize(const matrix::Matrix& m); 00123 00124 /* writes the names of the fields stored by store4x4AndDiagonal into a list 00125 @param matrixName name of the matrix (prefix for all fields) 00126 @param keylist list for field names 00127 @param len Length of the provided buffer 00128 (should be min(getN(),4)*min(getM(),4)+ max(0,min(getM()-4,getN()-4))) 00129 @return number of actually written elements 00130 */ 00131 std::list<Inspectable::iparamkey> store4x4AndDiagonalFieldNames(const matrix::Matrix& m, 00132 const char* matrixName); 00133 00134 /* stores the names of the fields stored by store4x4AndDiagonal 00135 @param matrixName name of the matrix (prefix for all fields) 00136 @param keylist list for field names 00137 @param len Length of the provided buffer 00138 (should be min(getN(),4)*min(getM(),4)+ max(0,min(getM()-4,getN()-4))) 00139 @return number of actually written elements 00140 */ 00141 unsigned int store4x4AndDiagonalFieldNames(const matrix::Matrix& m, const char* matrixName, 00142 char** keylist, unsigned int len); 00143 00144 /* stores the names of the all matrix fieldnames produces by convertToBuffer into a list 00145 @return list of names 00146 */ 00147 std::list<Inspectable::iparamkey> storeMatrixFieldNames(const matrix::Matrix& m, const char* matrixName); 00148 00149 /* stores the names of the all vector (mx1 matrix) fieldnames produces by convertToBuffer into a list 00150 @return list of names 00151 */ 00152 std::list<Inspectable::iparamkey> storeVectorFieldNames(const matrix::Matrix& m, const char* vectorName); 00153 00154 /* stores the names of the all matrix fieldnames produces by convertToBuffer 00155 @param matrixName name of the matrix (prefix for all fields) 00156 @param keylist list for field names 00157 @param len Length of the provided buffer 00158 (should be getN()*getM() 00159 @return number of actually written elements 00160 */ 00161 unsigned int storeMatrixFieldNames(const matrix::Matrix& m, const char* matrixName, 00162 char** keylist, unsigned int len); 00163 00164 /* stores the names of the all vector (mx1 matrix) fieldnames produces by convertToBuffer 00165 @param vectorName name of the vector (prefix for all fields) 00166 @param keylist list for field names 00167 @param len Length of the provided buffer (should be getM()) 00168 @return number of actually written elements 00169 */ 00170 unsigned int storeVectorFieldNames(const matrix::Matrix& m, const char* vectorName, 00171 char** keylist, unsigned int len); 00172 00173 /** stores the Matrix into the given file stream (binary) 00174 */ 00175 bool storeMatrix(const matrix::Matrix& m, FILE* f); 00176 00177 /** reads a Matrix from the given file stream (binary) 00178 */ 00179 bool restoreMatrix(matrix::Matrix& m, FILE* f); 00180 00181 /// returns a Matrix with values generated by the given noise generator 00182 matrix::Matrix noiseMatrix(unsigned int m, unsigned int n, NoiseGenerator& ng, double p1, double p2); 00183 00184 /** calculates to linear matrix norm (sum of absolute values devided by number of values (m*n) ) 00185 */ 00186 double matrixNorm1(const matrix::Matrix& m); 00187 00188 00189 /// parameter adaptation algorithm. 00190 // @param p current parameter value 00191 // @param actual actual value of some size controlled by p 00192 // @param nominal nominal value of some size controlled by p 00193 // @param up_rate adaptation rate for increasing p (<< 1) 00194 // @param down_rate adaptation rate for decreasing p (<< 1) 00195 // @return new value of p (there is no clipping done) 00196 double adapt(double p, double actual, double nominal, double up_rate, double down_rate); 00197 00198 /// like adapt but that the adaption is just done if the actual value is outside the given interval (_min, _max) 00199 double adaptMinMax(double p, double actual, double _min, double _max, double up_rate, double down_rate); 00200 00201 #endif

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