- Main Page
- Classes
- Files

- Search for |

# opt_route.cpp

    00001 // "opt_route.cpp"
    00002
    00003 // (c) OPAC Team, LIFL, January 2006
    00004
    00005 /*
    00006    Contact: paradiseo-help@lists.gforge.inria.fr
    00007 */
    00008
    00009 #include "opt_route.h"
    00010
    00011 #define MAX_TRASH_LENGTH 1000
    00012 #define MAX_FIELD_LENGTH 1000
    00013 #define MAX_LINE_LENGTH 1000
    00014
    00015 static void getNextField (FILE * __f, char * __buff) {
    00016
    00017   char trash [MAX_TRASH_LENGTH];
    00018
    00019   fscanf (__f, "%[ \t:\n]", trash); /* Discarding sep. */
    00020   fscanf (__f, "%[^:\n]", __buff); /* Reading the field */
    00021   fgetc (__f);
    00022 }
    00023
    00024 static void getLine (FILE * __f, char * __buff) {
    00025
    00026   char trash [MAX_TRASH_LENGTH];
    00027
    00028   fscanf (__f, "%[ \t:\n]", trash); /* Discarding sep. */
    00029   fscanf (__f, "%[^\n]", __buff); /* Reading the line */
    00030 }
    00031
    00032 static void loadBestRoute (FILE * __f) {
    00033
    00034   opt_route.clear ();
    00035
    00036   for (unsigned i = 0; i < numNodes; i ++) {
    00037     Node node;
    00038     fscanf (__f, "%u", & node);
    00039     opt_route.push_back (node - 1);
    00040   }
    00041   int d; /* -1 ! */
    00042   fscanf (__f, "%d", & d);
    00043 }
    00044
    00045 void loadOptimumRoute (const char * __filename) {
    00046
    00047   FILE * f = fopen (__filename, "r");
    00048
    00049   if (f) {
    00050
    00051      printf ("Loading '%s'.\n", __filename);
    00052
    00053      char field [MAX_FIELD_LENGTH];
    00054
    00055      getNextField (f, field); /* Name */
    00056      assert (strstr (field, "NAME"));
    00057      getNextField (f, field);
    00058      //printf ("NAME: %s.\n", field);
    00059
    00060           getNextField (f, field); /* Comment */
    00061      assert (strstr (field, "COMMENT"));
    00062      getLine (f, field);
    00063      //     printf ("COMMENT: %s.\n", field);
    00064
    00065      getNextField (f, field); /* Type */
    00066      assert (strstr (field, "TYPE"));
    00067      getNextField (f, field);
    00068      //printf ("TYPE: %s.\n", field);
    00069
    00070      getNextField (f, field); /* Dimension */
    00071      assert (strstr (field, "DIMENSION"));
    00072      getNextField (f, field);
    00073      //     printf ("DIMENSION: %s.\n", field);
    00074      numNodes = atoi (field);
    00075
    00076      getNextField (f, field); /* Tour section */
    00077      assert (strstr (field, "TOUR_SECTION"));
    00078      loadBestRoute (f);
    00079
    00080      getNextField (f, field); /* End of file */
    00081      assert (strstr (field, "EOF"));
    00082      //printf ("EOF.\n");
    00083
    00084      printf ("The length of the best route is %u.\n", length (opt_route));
    00085   }
    00086    else {
    00087
    00088      fprintf (stderr, "Can't open '%s'.\n", __filename);
    00089      exit (1);
    00090    }
    00091 }
    00092
    00093 void loadOptimumRoute (eoParser & __parser) {
    00094
    00095   /* Getting the path of the instance */
    00096
    00097   eoValueParam <std :: string> param ("", "optimumTour", "Optimum tour") ;
    00098   __parser.processParam (param) ;
    00099   if (strlen (param.value ().c_str ()))
    00100     loadOptimumRoute (param.value ().c_str ());
    00101   else
    00102     opt_route.fitness (0);
    00103 }
    00104
    00105 Route opt_route; /* Optimum route */
    00106
    00107