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