Reinforcement Learning Toolbox 2.0
last updated:
General
Documentation
Manual
Tutorial
Class Reference
Master Thesis
Examples
Related Papers
Downloads
Links
News
mailto:webmaster
Main Page     Class Hierarchy   Compound List   File List   Compound Members   File Members

cutility.h

Go to the documentation of this file.
00001 // Copyright (C) 2003
00002 // Gerhard Neumann (gneumann@gmx.net)
00003 // Stephan Neumann (sneumann@gmx.net) 
00004 //                
00005 // This file is part of RL Toolbox.
00006 // http://www.igi.tugraz.at/ril_toolbox
00007 //
00008 // All rights reserved.
00009 // 
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions
00012 // are met:
00013 // 1. Redistributions of source code must retain the above copyright
00014 //    notice, this list of conditions and the following disclaimer.
00015 // 2. Redistributions in binary form must reproduce the above copyright
00016 //    notice, this list of conditions and the following disclaimer in the
00017 //    documentation and/or other materials provided with the distribution.
00018 // 3. The name of the author may not be used to endorse or promote products
00019 //    derived from this software without specific prior written permission.
00020 // 
00021 // THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00022 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00023 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00024 // IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00025 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00026 // NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00027 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00028 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00029 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00030 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00031 
00032 #ifndef C_UTILITY_H
00033 #define C_UTILITY_H
00034 
00035 #include <map>
00036 #include <assert.h>
00037 
00038 #include <newmat/newmat.h>
00039 
00040 
00041 //class Matrix;
00042 //class ColumnVector;
00043 
00044 int my_round(double value);
00045 
00046 double my_exp(double value);
00047 
00048 void getPseudoInverse(Matrix *J, Matrix *pinv, double lambda);
00049 
00050 /*
00051 class ColumnVector 
00052 {
00053 protected:
00054        double *data;
00055        unsigned int dimensions;
00056 
00057 public:
00058        ColumnVector(unsigned int dimension, double *data = NULL);
00059        virtual ~ColumnVector();
00060 
00061        void addVector(ColumnVector *vector);
00062        void addScalar(double scalar);
00063 
00064        void dotVector(ColumnVector *vector);
00065 
00066        void multMatrix(Matrix *matrix, ColumnVector *output);
00067        void multMatrix(Matrix *matrix);
00068 
00069        void multVector(ColumnVector *vector, Matrix *output);
00070        
00071        void multScalar(double scalar);
00072 
00073        double getLength();
00074        void normalizeVector();
00075        
00076        double scalarProduct(ColumnVector *vector);
00077        virtual double getDistance(ColumnVector *vector);
00078 
00079        double element(unsigned int index);
00080        void setElement(unsigned int index, double value);
00081 
00082        unsigned int getNumDimensions();
00083 //      bool isColumnVector();
00084 
00085        double *getData();
00086 
00087        void setVector(ColumnVector *vector);
00088        void initVector(double init);
00089 
00090        void saveASCII(FILE *stream);
00091        void loadASCII(FILE *stream);
00092 };
00093 
00094 class Matrix
00095 {
00096 protected:
00097        double *data;
00098        unsigned int columns;
00099        unsigned int rows;
00100 
00101 public:
00102        Matrix(unsigned int rows, unsigned int columns, double *data = NULL);
00103        ~Matrix();
00104 
00105        void addVector(ColumnVector *vector);
00106        void addScalar(double scalar);
00107 
00108        void multMatrix(Matrix *matrix, Matrix *output);
00109 
00110        void multVector(ColumnVector *vector, ColumnVector *output);
00111 
00112        void multScalar(double scalar);
00113 
00114        double element(unsigned int row, unsigned int column);
00115        void setElement(unsigned int row, unsigned int column, double value);
00116 
00117        double *getRow(unsigned int row);
00118 
00119        unsigned int nrows();
00120        unsigned int ncols();
00121 
00122        double *getData();
00123 
00124        void setMatrix(Matrix *matrix);
00125        void initMatrix(double init);
00126 
00127        void saveASCII(FILE *stream);
00128 };*/
00129 
00131 
00137 template <typename T1> class CMyArray 
00138 {
00139 protected:
00140         T1 *data;
00141 
00142         int *dim;
00143         int numDim;
00144 
00145         int size;
00146 
00147         CMyArray() {};
00148 
00149         void initialize(int numDim, int dim[])
00150         {
00151                 this->numDim = numDim;
00152                 this->dim = new int[numDim];
00153 
00154                 memcpy(this->dim, dim, numDim * sizeof(int));
00155 
00156                 size = 1;
00157 
00158                 for (int i = 0; i < numDim; i++)
00159                 {
00160                         size = size * dim[i];
00161                 }
00162 
00163                 data = new T1 [size];
00164         }
00165 
00166 public:
00167         CMyArray(int numDim, int dim[])
00168         {
00169                 CMyArray<T1>::initialize(numDim, dim);
00170         }
00171 
00172         ~CMyArray()
00173         {
00174                 delete dim;
00175                 delete data;
00176         }
00177 
00178         T1 get(int indices[])
00179         {
00180                 int index = 0, size = 1;
00181 
00182                 for (int i = 0; i < numDim; i++)
00183                 {
00184                         assert(indices[i] < dim[i] && indices[i] >=0);
00185 
00186                         index += indices[i] * size;
00187                         size = size * dim[i];
00188                 }
00189                 return data[index];
00190         }
00191 
00192         void set(int indices[], T1 d)
00193         {
00194                 int index = 0, size = 1;
00195 
00196                 for (int i = 0; i < numDim; i++)
00197                 {
00198                         assert(indices[i] < dim[i] && indices[i] >=0);
00199 
00200                         index += indices[i] * size;
00201                         size = size * dim[i];
00202                 }
00203                 data[index] = d;
00204         
00205         }
00206 
00207         void init(T1 initVal)
00208         {
00209                 for (int i = 0; i < size; i++)
00210                 {
00211                         data[i] = initVal;
00212                 }
00213         }
00214 
00215         int getSize()
00216         {
00217                 return size;
00218         }
00219 
00220         void set1D(int index1d, T1 d)
00221         {
00222                 data[index1d] = d;
00223         }
00224 
00225         T1 get1D(int index1d)
00226         {
00227                 return data[index1d];
00228         }
00229 };
00230 
00232 class CDistributions
00233 {
00234 public:
00236 
00239         static void getGibbsDistribution(double beta, double *values, unsigned int numValues);
00240 
00241         static void getS1L0Distribution(double *values, unsigned int numValues);
00242 
00243         static double getNormalDistributionSample(double mu, double sigma);
00244 
00245         static int getSampledIndex(double *distribution, int numValues);
00246 };
00247 
00248 
00249 
00251 template<typename T1> class CMyArray2D : public CMyArray<T1>
00252 {
00253 public:
00254         CMyArray2D(int xDim, int yDim) 
00255         {
00256                 int *l_dim = new int[2];
00257                 l_dim[0] = xDim;
00258                 l_dim[1] = yDim;
00259 
00260                 CMyArray<T1>::initialize(2, l_dim);
00261 
00262                 delete l_dim;
00263         }
00264 
00265         ~CMyArray2D()
00266         {
00267         }
00268 
00269         T1 get(int xIndex, int yIndex)
00270         {
00271                 int indices[2];
00272                 indices[0] = xIndex;
00273                 indices[1] = yIndex;
00274 
00275                 return CMyArray<T1>::get(indices);
00276         }
00277 
00278         void set(int xIndex, int yIndex, T1 d)
00279         {
00280                 int indices[2];
00281                 indices[0] = xIndex;
00282                 indices[1] = yIndex;
00283 
00284                 CMyArray<T1>::set(indices, d);
00285         }
00286 };
00287 
00289 template<typename T1> class CMyArray3D : public CMyArray<T1>
00290 {
00291 public:
00292         CMyArray3D(int xDim, int yDim, int zDim) : CMyArray<T1>()
00293         {
00294                 int *ldim = new int[3];
00295                 ldim[0] = xDim;
00296                 ldim[1] = yDim;
00297                 ldim[2] = zDim;
00298 
00299                 CMyArray<T1>::initialize(3, ldim);
00300         }
00301 
00302         ~CMyArray3D()
00303         {
00304         }
00305 
00306         T1 get(int xIndex, int yIndex, int zIndex)
00307         {
00308                 int indices[3];
00309                 indices[0] = xIndex;
00310                 indices[1] = yIndex;
00311                 indices[2] = zIndex;
00312 
00313                 return CMyArray<T1>::get(indices);
00314         }
00315 
00316         void set(int xIndex, int yIndex, int zIndex, T1 d)
00317         {
00318                 int indices[3];
00319                 indices[0] = xIndex;
00320                 indices[1] = yIndex;
00321                 indices[2] = zIndex;
00322 
00323                 CMyArray<T1>::set(indices, d);
00324         }
00325 };
00326 
00327 // Maps feature indices to feature factors
00328 class CFeatureMap : public std::map<int, double>
00329 {
00330 protected:
00331         double stdValue;
00332 
00333 public:
00334         CFeatureMap(double stdValue = 0.0);
00335 
00336         double getValue(unsigned int featureIndex);
00337 };
00338 /*
00340 class CFeatureSparse
00341 {
00342 protected:
00343        CMyArray<CFeatureList *> *sparse;
00344        double stdValue;
00345        char *sparseName;
00346 
00347        int numDim;
00348        int *dim;
00349 
00350        void initSparse(int numDim, int dim[]);
00351 
00352        CFeatureSparse();
00353 
00354 public:
00355        CFeatureSparse(FILE *file, int numDim = 0, int *dim = NULL);
00356        CFeatureSparse(int numDim, int dim[]);
00357        virtual ~CFeatureSparse();
00358 
00359        virtual double getFactor(int indeces[], unsigned int featureIndex);
00360        void setFactor(double factor, int indeces[], unsigned int featureIndex);
00361        void addFactor(double factor, int indeces[], unsigned int featureIndex);
00362        void loadASCII(FILE *stream);
00363 
00364        CFeature *getCFeature(int indeces[], unsigned int featureIndex);
00365        CFeature *getCFeature1D(int index1D, unsigned int featureIndex);
00366        virtual CFeatureList *getFeatureList(int indeces[]);
00367        
00368        void saveASCII(FILE *stream);
00369 };
00370 
00372 //so there a a 2 dimensional array of feature lists.
00373 
00374 class CFeatureSparse2D : public CFeatureSparse
00375 {
00376 public:
00377        CFeatureSparse2D(FILE *file);
00378        CFeatureSparse2D(int dim1, int dim2);
00379        virtual ~CFeatureSparse2D();
00380 
00381        virtual double getFactor(int ind1, int ind2, unsigned int featureIndex);
00382        virtual void setFactor(double factor, int ind1, int ind2, unsigned int featureIndex);
00383        virtual void addFactor(double factor, int ind1, int ind2, unsigned int featureIndex);
00384 
00385        virtual CFeatureList *getFeatureList(int ind1, int ind2);
00386 
00387        CFeature* getCFeature(int ind1, int ind2, unsigned int featureIndex);
00388 
00389        
00390 };*/
00391 
00392 #endif