Robot Simulator of the Robotics Group for Self-Organization of Control  0.8.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
controller_misc.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2005-2011 LpzRobots development team *
3  * Georg Martius <georg dot martius at web dot de> *
4  * Frank Guettler <guettler at informatik dot uni-leipzig dot de *
5  * Frank Hesse <frank at nld dot ds dot mpg dot de> *
6  * Ralf Der <ralfder at mis dot mpg dot de> *
7  * *
8  * This program is free software; you can redistribute it and/or modify *
9  * it under the terms of the GNU General Public License as published by *
10  * the Free Software Foundation; either version 2 of the License, or *
11  * (at your option) any later version. *
12  * *
13  * This program is distributed in the hope that it will be useful, *
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16  * GNU General Public License for more details. *
17  * *
18  * You should have received a copy of the GNU General Public License *
19  * along with this program; if not, write to the *
20  * Free Software Foundation, Inc., *
21  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
22  * *
23  ***************************************************************************/
24 #ifndef __CONTOLLER_MISC_H
25 #define __CONTOLLER_MISC_H
26 
27 #include <selforg/matrix.h>
28 #include <assert.h>
29 #include <stdlib.h>
30 #include <cmath>
31 
32 #include "stl_adds.h"
33 #include "noisegenerator.h"
34 #include "inspectable.h"
35 
36 
37 template<typename T>
38 inline T sign(T v) { return v<(T)0?(T)-1:(T)1; }
39 
40 template<typename T>
41 inline T sqr(T v) { return v*v; }
42 
43 template<typename T>
44 inline T clip(T x, T lobound, T highbound) {
45  return (x)<(lobound) ? (lobound) : ( (x) > (highbound) ? (highbound) : (x) );
46 }
47 
48 
49 /// calculates 1/x
50 double one_over(double x);
51 
52 /// returns c (useful for Matrix::mapP to fill matrix with constant value)
53 double constant(double c, double);
54 
55 /// returns x the power c (as a double)
56 double power(void* c, double x);
57 
58 /// power 3 for matrix::map
59 double power3(double x);
60 
61 /// creates random number from -1 to 1
62 double random_minusone_to_one(double);
63 
64 /// creates random number from -1 to 1
65 // using the RandGen* which is give as a void pointer (Matrix::mapP)
66 double random_minusone_to_one(void *, double);
67 
68 /// clipping function to the interval [-r, r] (for use with mapP)
69 double clip(double r,double);
70 
71 /// cutof function for mapP
72 double lowercutof(void* theta, double);
73 
74 /// returns -1 if probability is to low, otherwise 1 for mapP
75 double toBinaryWithProbability(void* r,double x);
76 
77 /// returns -1 if below threshold, otherwise 1 for map2
78 double toBinaryWithThreshold(double x, double threshold);
79 
80 /// plus function for mapP
81 double plus_(double b, double a);
82 
83 
84 /** stores at least left top 4x4 submatrix (row-wise) (if exists) and
85  then the rest of the diagonal elements into a list
86  @return list of values
87 */
88 std::list<matrix::D> store4x4AndDiagonal(const matrix::Matrix& m);
89 
90 /** stores at least left top 4x4 submatrix (row-wise) (if exists) and
91  then the rest of the diagonal elements
92  @param m matrix to be stored
93  @param len Length of the provided buffer
94  (should be min(getN(),4)*min(getM(),4)+ max(0,min(getM()-4,getN()-4)))
95  @return number of actually written elements
96 */
98 
99 /** returns the number of elements stored by store4x4AndDiagonal
100  (should be min(getN(),4)*min(getM(),4)+ max(0,min(getM()-4,getN()-4)))
101 */
103 
104 /** writes the names of the fields stored by store4x4AndDiagonal into a list
105  @param m matrix to be stored
106  @param matrixName name of the matrix (prefix for all fields)
107  @return list of keys
108 */
109 std::list<Inspectable::iparamkey> store4x4AndDiagonalFieldNames(const matrix::Matrix& m,
110  const std::string& matrixName);
111 
112 /** stores the names of the fields stored by store4x4AndDiagonal
113  @param m matrix to be stored
114  @param matrixName name of the matrix (prefix for all fields)
115  @param keylist list for field names
116  @param len Length of the provided buffer
117  (should be min(getN(),4)*min(getM(),4)+ max(0,min(getM()-4,getN()-4)))
118  @return number of actually written elements
119 */
121  const std::string& matrixName,
122  char** keylist, matrix::I len);
123 
124 /** stores the names of all matrix fieldnames
125  in the order produced by convertToBuffer (row-wise)
126  @return list of names
127 */
128 std::list<Inspectable::iparamkey> storeMatrixFieldNames(const matrix::Matrix& m, const std::string& matrixName);
129 
130 /** stores the names of all vector (mx1 or 1xn matrix) fieldnames
131  in the order produced by convertToBuffer (row-wise)
132  @return list of names
133 */
134 std::list<Inspectable::iparamkey> storeVectorFieldNames(const matrix::Matrix& m, const std::string& vectorName);
135 
136 /** stores the names of all matrix fieldnames
137  in the order produced by convertToBuffer (row-wise)
138  @param m matrix to be stored
139  @param matrixName name of the matrix (prefix for all fields)
140  @param keylist list for field names
141  @param len Length of the provided buffer
142  (should be getN()*getM()
143  @return number of actually written elements
144 */
145 matrix::I storeMatrixFieldNames(const matrix::Matrix& m, const char* matrixName,
146  char** keylist, matrix::I len);
147 
148 /** stores the names of all vector (mx1 or 1xn matrix) fieldnames
149  in the order produced by convertToBuffer (row-wise)
150  @param m vector to be stored
151  @param vectorName name of the vector (prefix for all fields)
152  @param keylist list for field names
153  @param len Length of the provided buffer (should be getM())
154  @return number of actually written elements
155 */
156 matrix::I storeVectorFieldNames(const matrix::Matrix& m, const char* vectorName,
157  char** keylist, matrix::I len);
158 
159 /** stores the Matrix into the given file stream (binary)
160  */
161 bool storeMatrix(const matrix::Matrix& m, FILE* f);
162 
163 /** reads a Matrix from the given file stream (binary)
164  */
165 bool restoreMatrix(matrix::Matrix& m, FILE* f);
166 
167 /// returns a Matrix with values generated by the given noise generator
169  double strength,double unused = 0);
170 
171 /** geneterates deterministically a new random generator from the given one.
172  If null then rand() is used for the seed;
173 */
174 RandGen* splitRandGen(RandGen* randGen);
175 
176 /** calculates to linear matrix norm (sum of absolute values divided by number of values (m*n) )
177 */
178 double matrixNorm1(const matrix::Matrix& m);
179 
180 /** calculates to square matrix norm (sum of square values divided by number of values (m*n) )
181 */
182 double matrixNorm2(const matrix::Matrix& m);
183 
184 /** Matrix/matrixNorm2 ) */
186 
187 
188 /** returns the k. largest element of the matrix
189  Attention: it will detroy the given matrix! (sorting)
190  Assumption: k>0 and k<=matrixsize
191 */
193 
194 /** returns the k. smallest element of the matrix
195  Attention: it will detroy the given matrix! (sorting)
196  Assumption: k>0 and k<=matrixsize
197 */
199 
200 /// considers the matrix as vector (mx1) and returns the index of the smallest element
202 
203 /// considers the matrix as vector (mx1) and returns the index of the largest element
205 
206 /// returns the smallest element
207 double min(const matrix::Matrix& v);
208 
209 /// minimum function for doubles without templates
210 double min(double a, double b);
211 
212 /// returns the largest element
213 double max(const matrix::Matrix& v);
214 
215 /// maximum function for doubles without templates
216 double max(double a, double b);
217 
218 
219 /// samples from the pdf (rowwise stored with sum = 1)
220 matrix::I sample(const matrix::Matrix& pdf);
221 
222 
223 /// parameter adaptation algorithm.
224 // @param p current parameter value
225 // @param actual actual value of some size controlled by p
226 // @param nominal nominal value of some size controlled by p
227 // @param up_rate adaptation rate for increasing p (<< 1)
228 // @param down_rate adaptation rate for decreasing p (<< 1)
229 // @return new value of p (there is no clipping done)
230 double adapt(double p, double actual, double nominal, double up_rate, double down_rate);
231 
232 /// like adapt but that the adaption is just done if the actual value is outside the given interval (_min, _max)
233 double adaptMinMax(double p, double actual, double _min, double _max, double up_rate, double down_rate);
234 
235  /**
236  * Helper function for converting an array with double values to a list.
237  * Is used for the method getInternalParams() interface inspectable.
238  */
239 std::list<Inspectable::iparamval> convertArrayToList(double* array,int arraySize);
240  /**
241  * Helper function for getting the array names of an array
242  * Is used for the method getInternalParamNames() interface inspectable.
243  */
244 std::list<Inspectable::iparamkey> getArrayNames(int arraySize,const char* name);
245 
246 #endif
Matrix type.
Definition: matrix.h:65
T clip(T x, T lobound, T highbound)
Definition: controller_misc.h:44
matrix::I sample(const matrix::Matrix &pdf)
samples from the pdf (rowwise stored with sum = 1)
Definition: controller_misc.cpp:328
double max(const matrix::Matrix &v)
returns the largest element
Definition: controller_misc.cpp:318
matrix::Matrix matrixNormalized(const matrix::Matrix &m)
Matrix/matrixNorm2 )
Definition: controller_misc.cpp:253
double getKthSmallestElement(matrix::Matrix &matrix, matrix::I k)
returns the k.
Definition: controller_misc.cpp:269
double plus_(double b, double a)
plus function for mapP
Definition: controller_misc.cpp:58
double toBinaryWithProbability(void *r, double x)
returns -1 if probability is to low, otherwise 1 for mapP
Definition: controller_misc.cpp:47
random generator with 48bit integer arithmentic
Definition: randomgenerator.h:34
double random_minusone_to_one(double)
creates random number from -1 to 1
Definition: controller_misc.cpp:27
std::list< Inspectable::iparamval > convertArrayToList(double *array, int arraySize)
Helper function for converting an array with double values to a list.
Definition: controller_misc.cpp:371
T sign(T v)
Definition: controller_misc.h:38
matrix::I argmax(const matrix::Matrix &v)
considers the matrix as vector (mx1) and returns the index of the largest element ...
Definition: controller_misc.cpp:293
std::list< Inspectable::iparamkey > storeVectorFieldNames(const matrix::Matrix &m, const std::string &vectorName)
stores the names of all vector (mx1 or 1xn matrix) fieldnames in the order produced by convertToBuffe...
unsigned int I
type for matrix indices
Definition: matrix.h:36
matrix::I argmin(const matrix::Matrix &v)
considers the matrix as vector (mx1) and returns the index of the smallest element ...
Definition: controller_misc.cpp:279
std::list< Inspectable::iparamkey > storeMatrixFieldNames(const matrix::Matrix &m, const std::string &matrixName)
stores the names of all matrix fieldnames in the order produced by convertToBuffer (row-wise) ...
double one_over(double x)
calculates 1/x
Definition: controller_misc.cpp:9
std::list< Inspectable::iparamkey > getArrayNames(int arraySize, const char *name)
Helper function for getting the array names of an array Is used for the method getInternalParamNames(...
Definition: controller_misc.cpp:383
double power(void *c, double x)
returns x the power c (as a double)
Definition: controller_misc.cpp:17
double toBinaryWithThreshold(double x, double threshold)
returns -1 if below threshold, otherwise 1 for map2
Definition: controller_misc.cpp:53
double matrixNorm2(const matrix::Matrix &m)
calculates to square matrix norm (sum of square values divided by number of values (m*n) ) ...
Definition: controller_misc.cpp:249
std::list< Inspectable::iparamkey > store4x4AndDiagonalFieldNames(const matrix::Matrix &m, const std::string &matrixName)
writes the names of the fields stored by store4x4AndDiagonal into a list
Definition: controller_misc.cpp:116
matrix::Matrix noiseMatrix(matrix::I m, matrix::I n, NoiseGenerator &ng, double strength, double unused=0)
returns a Matrix with values generated by the given noise generator
Definition: controller_misc.cpp:223
bool storeMatrix(const matrix::Matrix &m, FILE *f)
stores the Matrix into the given file stream (binary)
double min(const matrix::Matrix &v)
returns the smallest element
Definition: controller_misc.cpp:307
double power3(double x)
power 3 for matrix::map
Definition: controller_misc.cpp:21
const int T
integer constant for use with exp function and (^) operator to transpose the matrix ...
Definition: matrix.cpp:21
T sqr(T v)
Definition: controller_misc.h:41
std::list< matrix::D > store4x4AndDiagonal(const matrix::Matrix &m)
stores at least left top 4x4 submatrix (row-wise) (if exists) and then the rest of the diagonal eleme...
Definition: controller_misc.cpp:67
double getKthLargestElement(matrix::Matrix &matrix, matrix::I k)
returns the k.
Definition: controller_misc.cpp:260
double lowercutof(void *theta, double)
cutof function for mapP
Definition: controller_misc.cpp:43
double constant(double c, double)
returns c (useful for Matrix::mapP to fill matrix with constant value)
Definition: controller_misc.cpp:13
bool restoreMatrix(matrix::Matrix &m, FILE *f)
reads a Matrix from the given file stream (binary)
double matrixNorm1(const matrix::Matrix &m)
calculates to linear matrix norm (sum of absolute values divided by number of values (m*n) ) ...
Definition: controller_misc.cpp:245
RandGen * splitRandGen(RandGen *randGen)
geneterates deterministically a new random generator from the given one.
Definition: controller_misc.cpp:233
double adaptMinMax(double p, double actual, double _min, double _max, double up_rate, double down_rate)
like adapt but that the adaption is just done if the actual value is outside the given interval (_min...
Definition: controller_misc.cpp:355
double adapt(double p, double actual, double nominal, double up_rate, double down_rate)
parameter adaptation algorithm.
Definition: controller_misc.cpp:347
matrix::I get4x4AndDiagonalSize(const matrix::Matrix &m)
returns the number of elements stored by store4x4AndDiagonal (should be min(getN(),4)*min(getM(),4)+ max(0,min(getM()-4,getN()-4)))
Definition: controller_misc.cpp:107
double D
type for matrix elements
Definition: matrix.h:38
int c
Definition: hexapod.cpp:56
Interface and basic class for noise generator.
Definition: noisegenerator.h:37