opt_route.cpp

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 // "opt_route.cpp"
00004 
00005 // (c) OPAC Team, LIFL, January 2006
00006 
00007 /* This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011    
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016    
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with this library; if not, write to the Free Software
00019    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00020    
00021    Contact: cahon@lifl.fr
00022 */
00023 
00024 #include "opt_route.h"
00025 
00026 #define MAX_TRASH_LENGTH 1000
00027 #define MAX_FIELD_LENGTH 1000
00028 #define MAX_LINE_LENGTH 1000
00029 
00030 static void getNextField (FILE * __f, char * __buff) {
00031   
00032   char trash [MAX_TRASH_LENGTH];  
00033 
00034   fscanf (__f, "%[ \t:\n]", trash); /* Discarding sep. */ 
00035   fscanf (__f, "%[^:\n]", __buff); /* Reading the field */
00036   fgetc (__f);
00037 }
00038 
00039 static void getLine (FILE * __f, char * __buff) {
00040 
00041   char trash [MAX_TRASH_LENGTH];  
00042 
00043   fscanf (__f, "%[ \t:\n]", trash); /* Discarding sep. */ 
00044   fscanf (__f, "%[^\n]", __buff); /* Reading the line */
00045 }
00046 
00047 static void loadBestRoute (FILE * __f) {
00048 
00049   opt_route.clear ();
00050   
00051   for (unsigned i = 0; i < numNodes; i ++) {
00052     Node node;
00053     fscanf (__f, "%u", & node);
00054     opt_route.push_back (node - 1);
00055   }
00056   int d; /* -1 ! */
00057   fscanf (__f, "%d", & d);
00058 }
00059 
00060 void loadOptimumRoute (const char * __filename) {
00061 
00062   FILE * f = fopen (__filename, "r");
00063 
00064   if (f) {
00065      
00066      printf ("Loading '%s'.\n", __filename);
00067      
00068      char field [MAX_FIELD_LENGTH];
00069      
00070      getNextField (f, field); /* Name */
00071      assert (strstr (field, "NAME"));
00072      getNextField (f, field); 
00073      //printf ("NAME: %s.\n", field);
00074 
00075           getNextField (f, field); /* Comment */
00076      assert (strstr (field, "COMMENT"));
00077      getLine (f, field);
00078      //     printf ("COMMENT: %s.\n", field);
00079      
00080      getNextField (f, field); /* Type */
00081      assert (strstr (field, "TYPE"));
00082      getNextField (f, field); 
00083      //printf ("TYPE: %s.\n", field);
00084 
00085      getNextField (f, field); /* Dimension */
00086      assert (strstr (field, "DIMENSION"));
00087      getNextField (f, field); 
00088      //     printf ("DIMENSION: %s.\n", field);
00089      numNodes = atoi (field);
00090 
00091      getNextField (f, field); /* Tour section */
00092      assert (strstr (field, "TOUR_SECTION"));
00093      loadBestRoute (f);
00094      
00095      getNextField (f, field); /* End of file */
00096      assert (strstr (field, "EOF"));
00097      //printf ("EOF.\n");
00098      
00099      printf ("The length of the best route is %u.\n", length (opt_route));
00100   }
00101    else {
00102      
00103      fprintf (stderr, "Can't open '%s'.\n", __filename); 
00104      exit (1);
00105    }
00106 }
00107 
00108 void loadOptimumRoute (eoParser & __parser) {
00109   
00110   /* Getting the path of the instance */
00111   
00112   eoValueParam <std :: string> param ("", "optimumTour", "Optimum tour") ;
00113   __parser.processParam (param) ;
00114   if (strlen (param.value ().c_str ()))
00115     loadOptimumRoute (param.value ().c_str ());
00116   else
00117     opt_route.fitness (0);
00118 }
00119 
00120 Route opt_route; /* Optimum route */
00121 
00122 

Generated on Fri Dec 22 16:54:58 2006 for ParadisEO by  doxygen 1.4.7