main_tsp.cpp

00001 
00002 /*
00003         This example finds a solution for the travelling salesman problem by using a SOM network with one dimension. 
00004 
00005         This program takes as parameters:
00006                 - the path to a data file;
00007                 - the neighbor radius (given as a percentage);
00008                 - the number of iterations;
00009                 - the type of learning;
00010         
00011         ./somcode <file path> <neighbor radius> <numIte> <type of learning>
00012 
00013         Example of how to execute this program: ./somcode ./data/cities.txt 10 1000 batch
00014 
00015         This is just an example. The values of the parameters must be adjusted to each situation.
00016         
00017 */
00018 
00019 #pragma warning(disable: 4786)
00020 
00021 #include <cstdlib>
00022 #include <iostream>
00023 #include <vector>
00024 
00025 //inclusion of the libraries from somcode needed to compile this example correctly
00026 
00027 #include "Som.h"
00028 #include "SOMDataCadastre.h"
00029 #include "MapcodeCadastre.h"
00030 
00031 #include "BatchFactory.h"
00032 #include <string>
00033 
00034 
00035 #include <iomanip.h>
00036 #include <fstream.h>
00037 #include <stdio.h>
00038 
00039 #include "RepositorySOMDataFile.h"
00040 
00041 #include "OneDTFactory.h"
00042 #include "OneDT.h"
00043 
00044 #include "Data.h"  
00045 #include "util.h"
00046 #include "defs.h"
00047 
00048 
00049 using std::cout;
00050 
00051 //creates the object constructors from abstract factory pattern
00052 
00053 static BatchFactory fact_batch("Batch");
00054 static OneDTFactory fact_twoD("Two");
00055 
00056 
00057 int main(int argc, char *argv[]) {
00058 
00059   try {         
00060           clock_t c0 = clock();
00061   
00062 
00063           std::vector<std::string> params;
00064           params.push_back(argv[1]);    
00065 
00066           //creates an object of the concrete class that implements the way to access data through file systems
00067           RepositorySOMDataFile repD;
00068           repD.load(params,0);
00069 
00070           //instantiates an object of the concrete class SOMDataCadastre, which is used to isolate data from storing details
00071           SOMDataCadastre cadD(repD);
00072 
00073           
00074           OneDT* tsp;
00075 
00076 
00077           cout << "Starting SOMCode-1.0 ...\n";
00078 
00079           //reads the file through the concrete class SOMDataCadastre
00080           SOMData* data = cadD.load( std::vector<std::string>() );
00081 
00082           clock_t c1 = clock();
00083 
00084           clock_t c2 = clock();
00085 
00086           SOM* mysom = new SOM();
00087           mysom->setData(data);
00088 
00089 
00100           int dimL, dimC, neig, iter; 
00101          
00102           dimL = data->getDataSize();   // neural network with number of neurons equal to the number
00103                                         // of entry cities
00104           dimC = 1;                     // due to it has only one dimension 
00105           neig = strtoint(argv[2]);
00106           iter = strtoint(argv[3]);
00107 
00108           mysom->getMapcode()->setNumVar( mysom->getData()->getDimension() );
00109           mysom->getMapcode()->setDimensions( 0, dimL );
00110           mysom->getMapcode()->setDimensions( 1, dimC );
00111 
00112           // kind of neighborhood for travelling salesman problem
00113           mysom->getMapcode()->setLattice( "toro" );   
00114           mysom->getMapcode()->setNeighborType( "bubble" );
00115           mysom->getMapcode()->CreateCodebook( dimL*dimC, mysom->getData()->getDimension() );   
00116 
00117           // in TSP , this must to be equal to (number_of_cities / 4)
00118           mysom->setInitNeighbor( dimL*(neig*0.01) );  
00119 
00120           // initialization , disposing the nodes on a frame around all the cities
00121           mysom->setInitType( WINDOW );  
00122 
00123           mysom->setNumIterations( iter );
00124 
00125           if ( strcmp(argv[4],"online") == 0 ){
00126              mysom->setLearningType( ONLINE );
00127              mysom->setLearnType( "Online" );
00128           }
00129 
00130           if ( strcmp(argv[4],"batch") == 0 ) {
00131             mysom->setLearningType( BATCH );
00132             mysom->setLearnType( "Batch" );
00133           }
00134 
00139          mysom->InitMapcode();  
00140 
00141          string SOM       = "Som" ;
00142          string PATH      = "Path" ;
00143          string INIT_ARRG = "InitialArrangement" ;
00144          string txt       = ".txt" ;
00145         
00146          // name of the generated files (initial arrangement, final coordenates of neurons and           // generated path) following this pattern: 
00147          // <generated file> N <n# of neurons> P <neighboorhood percentage> I <n# of iterations>
00148 
00149          string nick1 = INIT_ARRG + 'N' + inttostr(dimL) + 'P' + argv[2] + 'I' + argv[3] + txt ;
00150 
00151          tsp = new OneDT(mysom->getMapcode()->getTopolParams());
00152 
00153          // generate a file with the initial arrangement of the neurons (before the training)
00154          cout << "\nFile containing the initial arragement : \n\t" ;
00155          tsp->Save(mysom->getMapcode()->getCodebook() , nick1 , dimL); 
00156          cout << "\n" ;
00157 
00158          mysom->Learning();             
00159          mysom->Quality();
00160 
00161           
00166           clock_t c3 = clock();
00167           cout << "Quantization error = " << mysom->getQuantizationError() << std::endl;
00168           cout << "Topological error = " << mysom->getTopologicalError() << std::endl;
00169           cout << "Time elapsed Loading Data = " << (float) (c1 - c0)/CLOCKS_PER_SEC << std::endl;
00170           cout << "Time elapsed Computing Data = " << (float) (c3 - c2)/CLOCKS_PER_SEC << std::endl;
00171 
00172 
00173           mysom->Labeling();
00174 
00175           TLabel label = mysom->getMapcode()->getLabel();
00176 
00177         // cities' coordinates impression. 
00178         // uncomment if necessary
00179         /*
00180           int tsp_count = 1 ;
00181           for (int i = 0; i < label.size() ; i++) {
00182              cout << i+1 << ": " ;
00183              for (int j = 0; j < label[i].size(); j++){
00184                 cout << mysom->getCities()[strtoint(rem_id(label[i][j]))][1] ;
00185                 cout << "," << mysom->getCities()[strtoint(rem_id(label[i][j]))][2] << " " ;
00186                 tsp_count++ ;
00187                 }
00188              cout << "\n";
00189           }
00190          */
00191         
00192          string nick2, nick3 ;
00193          
00194          // name of the generated files (initial arrangement, final coordenates of neurons and           // generated path) following this pattern: 
00195          // <generated file> N <n# of neurons> P <neighboorhood percentage> I <n# of iterations>
00196          
00197          nick2 = PATH + 'N' + inttostr(dimL) + 'P' + argv[2] + 'I' + argv[3] + txt ;
00198          nick3 = SOM + 'N' + inttostr(dimL) + 'P' + argv[2] + 'I' + argv[3] + txt ;
00199 
00200 
00201          TMatrix path = tsp->Extract_Path(mysom->getMapcode()->getLabel() , mysom->getCities() , dimL) ;
00202 
00203          cout << "\nFile containing the generated path : \n\t" ;
00204          tsp->Save(path , nick2 , dimL);
00205          cout << "\nFile containing the neurons coordenates : \n\t" ;
00206          tsp->Save(mysom->getMapcode()->getCodebook() , nick3 , dimL);
00207 
00208 
00209   } catch(std::exception& ex) {
00210     cout << ex.what() << std::endl;
00211   }
00212       
00213   return EXIT_SUCCESS;
00214   
00215 };

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