Main Page | Namespace List | Class Hierarchy | Data Structures | File List | Namespace Members | Data Fields | Globals | Related Pages

machine.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (c) 2003, Cornell University
00004 All rights reserved.
00005 
00006 Redistribution and use in source and binary forms, with or without
00007 modification, are permitted provided that the following conditions are met:
00008 
00009    - Redistributions of source code must retain the above copyright notice,
00010        this list of conditions and the following disclaimer.
00011    - Redistributions in binary form must reproduce the above copyright
00012        notice, this list of conditions and the following disclaimer in the
00013        documentation and/or other materials provided with the distribution.
00014    - Neither the name of Cornell University nor the names of its
00015        contributors may be used to endorse or promote products derived from
00016        this software without specific prior written permission.
00017 
00018 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00022 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
00028 THE POSSIBILITY OF SUCH DAMAGE.
00029 
00030 */
00031 
00032 // -*- C++ -*-
00033 
00034 #if !defined(_CLUS_MACHINE_H)
00035 #define _CLUS_MACHINE_H
00036 
00037 #include "general.h"
00038 #include "vec.h"
00039 #include "normaliz.h"
00040 #include <stdlib.h>
00041 #include "traingen.h"
00042 #include "filter.h"
00043 #include <fstream>
00044 #include <string>
00045 #include <iostream>
00046 #include <iomanip>
00047 #include <stdio.h>
00048 
00049 #ifdef CLUS_USE_XML
00050 #include "xml.h"
00051 #endif
00052 
00053 extern Vector<double> DummyVector;
00054 
00055 using namespace TNT;
00056 
00057 namespace CLUS
00058 {
00059 
00060 class Machine;
00061 
00062 /// this function constructs a machine when given a list of its parameters
00063 Machine* MachineFactory(int nrpar,...);
00064 
00065 /// this function constructs a machine when given it's definition file
00066 Machine* MachineFactory(char* filename);
00067 
00068 /** Every machine has an input vector, an output one and a real output one
00069     should provide a constructor from file.
00070     A constructor of the form Cons(int nrpar, int* pararray) and one of the form
00071     Cons(FILE* filename, int inDim, int outDim) 
00072  */
00073 class Machine : public Filter
00074 {
00075 protected:
00076     Subscript            maxIter;
00077     double               convergenceLim, Criterion;
00078     Vector<Scale>        scale;
00079     TrainingData*        training;
00080     TrainingData*        pruning;
00081     char*                savePattern;
00082     char*                saveInferPattern;
00083     int                  inDim, outDim;
00084 
00085     /// number of subparts
00086     int                  N; 
00087 
00088     virtual double IdentifyStep(int iter)
00089     {
00090         return 0.0;
00091     }
00092 public:
00093     Machine():Filter(),scale()
00094     {}
00095     ;
00096 
00097     Machine(Subscript InDim, Subscript OutDim, Vector<double>* Input=NULL):
00098             Filter(Input, OutDim), scale(InDim+OutDim), inDim(InDim), outDim(OutDim)
00099     {
00100         maxIter=30;
00101         convergenceLim=1.0e-4;
00102         savePattern=NULL;
00103         saveInferPattern=NULL;
00104     }
00105 
00106     /// Get the number of input dimensions
00107     virtual int InDim(void)
00108     {
00109         return inDim;
00110     }
00111 
00112     /// Get the number of output dimensions
00113     int OutDim(void)
00114     {
00115         return outDim;
00116     }
00117 
00118     /// Do the inference
00119     virtual void Infer(void)
00120     {}
00121     ;
00122 
00123     /// Process more data for the epoch
00124     virtual bool Tick(void)
00125     {
00126         Infer();
00127         return true;
00128     }
00129 
00130     /// Use the training data to learn a new structure
00131     virtual void Identify(void)
00132     {
00133         if (training!=NULL)
00134         {
00135             int iter;
00136             double err=0;
00137             //   cerr << "MaxIter=" << maxIter << ", ConvLim=" << convergenceLim << endl;
00138 
00139             for (iter=0; iter<maxIter && (err=IdentifyStep(iter))>convergenceLim; iter++)
00140             {
00141                 cout << iter << " " << N << " " << err << "  " << setprecision(8) << Criterion << endl;
00142                 if (savePattern!=NULL)
00143                 {
00144                     char filename[256];
00145                     sprintf(filename,savePattern,iter);
00146                     SaveToFile(filename);
00147                 }
00148             }
00149 
00150             // if it stopped because of convergence limit print the information
00151             if (iter<maxIter)
00152                 cout << iter << " " << N << " " << err << "  " << setprecision(8) << Criterion << endl;
00153 
00154             // save the last machine
00155             if (savePattern!=NULL && iter<=maxIter)
00156             {
00157                 char filename[256];
00158                 sprintf(filename,savePattern,iter);
00159                 SaveToFile(filename);
00160             }
00161         }
00162     }
00163 
00164     /// Prune the structure
00165     virtual void Prune(void)
00166     { }
00167 
00168     /// @deprecated
00169     virtual double FindBestParameter(char* name)
00170     {
00171         return 0.0;
00172     }
00173 
00174     /// Set the training data for this machine
00175     ///
00176     /// @param Training
00177     inline void SetTrainingData(TrainingData* Training)
00178     {
00179         training=Training;
00180     }
00181 
00182     /// Set the pruning data for this machine
00183     ///
00184     /// @param Pruning
00185     void SetPruningData(TrainingData* Pruning)
00186     {
00187         pruning=Pruning;
00188     }
00189 
00190     /// Scale the training data
00191     ///
00192     /// @param type          interval or distribution
00193     inline void ScaleData(Scale::NormType type)
00194     {
00195         if (training!=NULL)
00196             training->Normalize(scale, type, inDim);
00197         else
00198             throw ErrMsg("No training data and scaling atempted");
00199 
00200     }
00201 
00202     const Vector<Scale>& GetScaleFactors(void) const
00203     {
00204         return scale;
00205     }
00206 
00207     /// @deprecated
00208     virtual void GenerateStructure(string Type)
00209     {}
00210 
00211     /// Output the structure data to a file
00212     ///
00213     /// @param filename      name of save file
00214     inline void SaveToFile(char* filename)
00215     {
00216         ofstream ostr(filename);
00217         SaveToStream(ostr);
00218     }
00219 
00220     /// Output the structure data to a stream
00221     ///
00222     /// @param out           stream for output
00223     virtual void SaveToStream(ostream &out)
00224     {}
00225 
00226 #ifdef CLUS_USE_XML
00227 
00228     void SaveXmlToFile(char* filename)
00229     {
00230         ofstream ostr(filename);
00231         PrintToXmlStream(ostr);
00232     }
00233 
00234     virtual void PrintToXmlStream(ostream &output)
00235     {
00236         output << "<Machine";
00237         PrintAttribute(output, "maxIter", maxIter);
00238         PrintAttribute(output, "convergenceLim", convergenceLim);
00239         PrintAttribute(output, "inDim", inDim);
00240         PrintAttribute(output, "outDim", outDim);
00241         output << ">" << endl;
00242         // save scale;
00243         output << "</Machine>" << endl;
00244 
00245     }
00246 
00247 #endif
00248 
00249     /// Get the type of this object
00250     ///
00251     /// @return              name of object type as a string
00252     virtual string TypeName(void)
00253     {
00254         return string("Machine");
00255     };
00256 
00257     /// @deprecated
00258     virtual bool SetOptionOnComponents(char* optName,double value)
00259     {
00260         return false;
00261     };
00262 
00263     /// Set an option for the machine
00264     ///
00265     /// @param name          name of option to be set
00266     /// @param val           value of option
00267     virtual int SetOption(char* name,char* val)
00268     {
00269         if (strcmp(name,"MaxIter")==0)
00270             maxIter=atoi(val);
00271         else if (strcmp(name,"ConvergenceLimit")==0)
00272             convergenceLim=atof(val);
00273         else if (strcmp(name,"SavePattern")==0)
00274         {
00275             if (savePattern!=NULL)
00276                 free(savePattern);
00277             if (val!=NULL)
00278             {
00279                 savePattern=(char*)malloc(strlen(val)+1);
00280                 strcpy(savePattern,val);
00281             }
00282             else
00283                 savePattern=NULL;
00284         }
00285         else if (strcmp(name,"SaveInferPattern")==0)
00286         {
00287             if (saveInferPattern!=NULL)
00288                 free(saveInferPattern);
00289             if (val!=NULL)
00290             {
00291                 saveInferPattern=(char*)malloc(strlen(val)+1);
00292                 strcpy(saveInferPattern,val);
00293             }
00294             else
00295                 saveInferPattern=NULL;
00296         }
00297         else
00298             return 0;
00299         return 1;
00300     }
00301 };
00302 
00303 }
00304 
00305 
00306 #endif    /* _MACHINE_H */

Generated on Mon Jul 21 16:57:24 2003 for SECRET by doxygen 1.3.2