main_segmentation.cpp

00001 
00002 /*
00003 *        This example takes a saved net's map as parameter, segments the neural net and then prints the results
00004 *
00005 *       This program takes as parameters
00006 *               - the path to a data file;
00007 *               - the path to a file which contains the net's map;
00008 *
00009 *       ./somcode <file path> <file path>
00010 *
00011 *       Example of how to execute this program:  ./somcode ./data/IrinInputTrain.txt ./map10x15.txt
00012 *
00013 */
00014 
00015 #pragma warning(disable: 4786)
00016 
00017 #include <cstdlib>
00018 #include <iostream>
00019 #include <vector>
00020 #include <string>
00021 
00022 //inclusion of the libraries from somcode needed to compile this example correctly
00023 
00024 
00025 #include "RepositorySOMDataXML.h"
00026 #include "RepositoryMapcodeXML.h"
00027 #include "RepositorySOMDataFile.h"
00028 #include "RepositoryMapcodeFile.h"
00029 
00030 
00031 
00032 #include "Som.h"
00033 #include "SOMDataCadastre.h"
00034 #include "MapcodeCadastre.h"
00035 
00036 #include "GraphSegmentation.h"
00037 #include "util.h"
00038 #include "CDbw.h"
00039 #include "DaviesBouldin.h"
00040 
00041 #include "BatchFactory.h"
00042 #include "TwoDFactory.h"
00043 #include "OnlineFactory.h"
00044 
00045 
00046 using std::cout;
00047 
00048 //creates the object constructors from abstract factory pattern
00049 
00050 
00051 static OnlineFactory fact_online("Online");
00052 static BatchFactory fact_batch("Batch");
00053 static TwoDFactory fact_twoD("Two");
00054 
00055 std::map<string, int>  makeTblEntry_Cluster(SOM* som,int* clusters,int dimL,int dimC);
00056 
00057 int main(int argc, char *argv[]) {  
00058 
00059   try {
00060       
00061           clock_t c0 = clock();
00062 
00063           //abstract class used as interface to the different ways to access data used for training.
00064           ISOMDataRepository *repD; 
00065 
00066           //concrete class used to isolate data from storing details.
00067           SOMDataCadastre *cadD;
00068 
00069           //attribution of the parameter which contains the name of the file that will be read.
00070           std::vector<std::string> params;
00071           params.push_back(argv[1]);    
00072           
00073           //creates an object of the concrete class that implements the way to access data through file systems
00074           repD = new RepositorySOMDataFile();
00075           repD->load(params,0);
00076 
00077           //instantiates an object of the concrete class SOMDataCadastre, which is used to isolate data from storing details
00078           cadD = new SOMDataCadastre(*repD);
00079 
00080           cout << "Starting SOMCode-1.0 ...\n\n";
00081  
00082           //reads the file through the concrete class SOMDataCadastre
00083           SOMData* data = cadD->load( std::vector<std::string>() );
00084             
00085            
00086           //Abstract class used as interface to the different ways to access data related to a net's map
00087           IMapcodeRepository *repM;
00088 
00089           //concrete class used to isolate data from storing details
00090           MapcodeCadastre *cadM;
00091           
00092           //creates an object of the concrete class that implements the way to access data through file systems
00093           repM = new RepositoryMapcodeFile();
00094           repM->load(argv[2]);
00095 
00096           //instantiates an object of the concrete class MapcodeCadastre, which is used to isolate map data from storing details
00097           cadM = new MapcodeCadastre(*repM);
00098 
00099           //reads a file through the concrete class MapcodeCadastre
00100           Mapcode *map = cadM->load(argv[2]);
00101                  
00102           clock_t c1 = clock();
00103   
00104           
00105 
00106           clock_t c2 = clock();
00107             
00108 
00109             
00110           
00111           //instantiates the object 'net', of type NetParams, which represents the net. 
00112           NetParams* net = new NetParams();
00113            
00114 
00129           net->setData(data);
00130           net->setNumIterations( 2000 );
00131           
00132           net->setInitType(10); 
00133           //here the value 10, which doesn't correspond to any kind of existent initialization,
00134           //is set in order to not intialize the map, so that the data read from file is not modified
00135           
00136           
00137           net->setInitLearningRate( 0.5 );
00138           net->setInitNeighbor( 15 );
00139           net->setLearningType( BATCH );
00140           net->setLearnType( "Batch" );
00141           net->setMapcode(map);
00142   
00143           //instantiates the object mysom, of type SOM, which represents the SOM neural network
00144           SOM* mysom = new SOM(net,map);  
00145           
00146 
00147           cout << "\nNumber of neurons = " << mysom->getMapcode()->getMapSize();
00148           cout << "\nDimension of vectors = " <<  data->getDimension();
00149          
00150           /*
00151                Calls the SOM methods:
00152                - InitMapcode -> initializes the map;
00153                - Labeling -> labels the code vectors of the map;
00154           */
00155 
00156 
00157           mysom->InitMapcode(); 
00158           //the method used to initialize the map is called, but the map initialization isn't done once the kind of initialization set doesn't exist.
00159           //in this example, this method is called only to create the distance matrix.
00160           
00161           
00162           mysom->getLearningAlgorithm()->MakeBMU(data,map);
00163           //the method used to create the BMU matrix is usually called in the training method Learning()
00164           //because the training method is not called in this example it is needed to call the MakeBMU method directly
00165                 
00166 
00167           mysom->Labeling(); 
00168        
00169           clock_t c3 = clock();
00170            
00171           //instantiates an object of the class for segmentation which implements the costa neto algorithm
00172           GraphSegmentation *cluster = new GraphSegmentation();
00173 
00174           //creates the vector to store the clusters generated by the segmentation
00175           TIntVector clusters = create_intvector(1, mysom->getMapcode()->getMapSize());
00176 
00177 
00178           //segments the net. It takes as parameters: the net to be segmented and the minimum number of neurons that each group must have.
00179           clusters = cluster->Segment( mysom, 3 );
00180           
00181 
00182           cout << "\n\n\nSegmented map: \n\n";
00183           printClusters(clusters,mysom->getMapcode()->getDimensions(0),mysom->getMapcode()->getDimensions(1));
00184           
00185           //CDbw index calculation
00186           CDbw *cdbw = new CDbw(mysom, clusters, cluster->getNumClusters());
00187           cout << "\nCDbw = " << cdbw->Index();
00188 
00189 
00190           //Davies Bouldin index calculation
00191           DaviesBouldin *db = new DaviesBouldin(mysom, clusters, cluster->getNumClusters());
00192           cout << "\nDavies Bouldin = " << db->Index(1,2) << "\n";
00193 
00194 
00195            /*
00196             The table relating each entry vector to its cluster may be too big depending on the amount of entry vectors, so the code to its creation and impression is commented in order to be used as the user needs it.
00197           */
00198 
00199           /* //begining of the code to create the table
00200           
00201           //method used to create the table relating each entry vector to its cluster
00202           std::map<string, int> tbl = makeTblEntry_Cluster(mysom,clusters,mysom->getMapcode()->getDimensions(0),mysom->getMapcode()->getDimensions(1));
00203 
00204           //vector that contains the labels of the entry vectors
00205           TvecLabel entryLabel = mysom->getData()->getId();
00206 
00207           cout << "\n\n\nTable relating each entry vector to its cluster...\n\n";
00208           for(int i = 0; i < entryLabel.size(); i++)
00209           cout << "Entry = " << i+1 << "\tCluster = " << tbl[ entryLabel[i] ] << "\n";
00210 
00211           //end of the code to create the table */  
00212 
00213       delete cdbw;
00214       delete data;
00215       delete mysom;
00216 
00217   } catch(std::exception& ex) {
00218     cout << ex.what() << std::endl;
00219   }
00220       
00221   return EXIT_SUCCESS;
00222   
00223 };
00224 
00225 
00226 
00227 //implementation of the method used to create the table relating each entry vector to its cluster
00228 std::map<string, int>  makeTblEntry_Cluster(SOM* som,int* clusters,int dimL,int dimC){          
00229           TLabel label = som->getMapcode()->getLabel();
00230           
00231           std::map<string, int> tbl;
00232           
00233           for(int i = 0; i < dimL*dimC; i++){
00234             for(int j = 0; j < label[i].size(); j++){
00235               string value = inttostr( clusters[i+1] );
00236               string id = label[i][j];
00237               tbl[ label[i][j] ] = clusters[i+1];
00238             }
00239           }  
00240 
00241           return tbl;
00242 }

Generated on Tue Aug 7 16:03:33 2007 for SOMCode by  doxygen 1.5.3