00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
00027 
00028 
00029 
00030 
00031 
00032 
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 
00063 Machine* MachineFactory(int nrpar,...);
00064 
00065 
00066 Machine* MachineFactory(char* filename);
00067 
00068 
00069 
00070 
00071 
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 
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 
00107     virtual int InDim(void)
00108     {
00109         return inDim;
00110     }
00111 
00112 
00113     int OutDim(void)
00114     {
00115         return outDim;
00116     }
00117 
00118 
00119     virtual void Infer(void)
00120     {}
00121     ;
00122 
00123 
00124     virtual bool Tick(void)
00125     {
00126         Infer();
00127         return true;
00128     }
00129 
00130 
00131     virtual void Identify(void)
00132     {
00133         if (training!=NULL)
00134         {
00135             int iter;
00136             double err=0;
00137             
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             
00151             if (iter<maxIter)
00152                 cout << iter << " " << N << " " << err << "  " << setprecision(8) << Criterion << endl;
00153 
00154             
00155             if (savePattern!=NULL && iter<=maxIter)
00156             {
00157                 char filename[256];
00158                 sprintf(filename,savePattern,iter);
00159                 SaveToFile(filename);
00160             }
00161         }
00162     }
00163 
00164 
00165     virtual void Prune(void)
00166     { }
00167 
00168 
00169     virtual double FindBestParameter(char* name)
00170     {
00171         return 0.0;
00172     }
00173 
00174 
00175 
00176 
00177     inline void SetTrainingData(TrainingData* Training)
00178     {
00179         training=Training;
00180     }
00181 
00182 
00183 
00184 
00185     void SetPruningData(TrainingData* Pruning)
00186     {
00187         pruning=Pruning;
00188     }
00189 
00190 
00191 
00192 
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 
00208     virtual void GenerateStructure(string Type)
00209     {}
00210 
00211 
00212 
00213 
00214     inline void SaveToFile(char* filename)
00215     {
00216         ofstream ostr(filename);
00217         SaveToStream(ostr);
00218     }
00219 
00220 
00221 
00222 
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         
00243         output << "</Machine>" << endl;
00244 
00245     }
00246 
00247 #endif
00248 
00249 
00250 
00251 
00252     virtual string TypeName(void)
00253     {
00254         return string("Machine");
00255     };
00256 
00257 
00258     virtual bool SetOptionOnComponents(char* optName,double value)
00259     {
00260         return false;
00261     };
00262 
00263 
00264 
00265 
00266 
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