vrp/vrp_instance.cpp
/**  Read the instance from file : */
/** The input files follow the "Augerat" format. */
Read the instance from file :
The input files follow the "Augerat" format.
Read the instance from file : The input files follow the " Augerat " format .
bool VrpInstance::readDataFromFile(const std::string _fileName)
{
  bool everythingOK = true;
  std::ifstream infile(_fileName.c_str());
  if (!infile.is_open()) {
    if(!silence)
    std::cout    << "ERROR. file:vrp_instance.cpp, method:readDataFromFile, "
            << "error while opening file " << _fileName << std::endl;
    everythingOK = false;
    return everythingOK;
  }
  if(!silence){
    std::cout << "File " << _fileName << " is now open" << std::endl;
    std::cout << "Starting to read data..." << std::endl;
  }
  string str;
  char *pch;
  char* line;
  int nbNodes;
  while (true) {
      getline(infile, str);
      line = strdup(str.c_str());
      pch = strtok(line, " :");
      if (strcmp(pch, "DIMENSION") == 0) {
          pch = strtok(NULL, " :");
          nbNodes = atoi(pch);
          nbCustomers = nbNodes - 1;
      } else if (strcmp(pch, "CAPACITY") == 0) {
          pch = strtok(NULL, " :");
          truckCapacity = atoi(pch);
      } else if (strcmp(pch, "EDGE_WEIGHT_TYPE") == 0) {
          pch = strtok(NULL, " :");
          if (strcmp(pch, "EUC_2D") != 0) {
              throw std::runtime_error("***Only Edge Weight Type EUC_2D is supported");
          }
      } else if (strcmp(pch, "NODE_COORD_SECTION") == 0) {
          break;
      }
  }
  vector<int> customersX(nbCustomers);
  vector<int> customersY(nbCustomers);
  int depotX, depotY;
  for (int n = 1; n <= nbNodes; n++) {
      int id;
      infile >> id;
      if (id != n) {
          throw std::runtime_error("***Unexpected index");
      }
      if (n == 1) {
          infile >> depotX;
          infile >> depotY;
      } else {
          infile >> customersX[n-2];
          infile >> customersY[n-2];
      }
  }
  distanceMatrix = computeDistanceMatrix(nbCustomers, customersX, customersY);
  distanceWarehouses = computeDistanceToWarehouse(nbCustomers, depotX, depotY, customersX, customersY);
  getline(infile, str); 
  getline(infile, str);
  line = strdup(str.c_str());
  pch = strtok(line, " :");
  if (strcmp(pch, "DEMAND_SECTION") != 0) {
      throw std::runtime_error("***Expected keyword DEMAND_SECTION");
  }
  demands.resize(nbCustomers);
  for (int n = 1; n <= nbNodes; n++) {
      int id;
      infile >> id;
      if (id != n) {
          throw std::runtime_error("***Unexpected index");
      }
      int demand;
      infile >> demand;
      if (n == 1) {
          if (demand != 0) {
              throw std::runtime_error("***Demand for depot should be O");
          }
      } else {
          demands[n-2] = demand;
      }
  }
  getline(infile, str); 
  getline(infile, str);
  line = strdup(str.c_str());
  pch = strtok(line, " :");
  if (strcmp(pch, "DEPOT_SECTION") != 0) {
      throw std::runtime_error("***Expected keyword DEPOT_SECTION");
  }
  int warehouseId;
  infile >> warehouseId;
  if (warehouseId != 1) {
      throw std::runtime_error("***Warehouse id is supposed to be 1");
  }
  int endOfDepotSection;
  infile >> endOfDepotSection;
  if (endOfDepotSection != -1) {
      throw std::runtime_error("***Expecting only one warehouse, more than one found");
  }
  infile.close();
  if(!silence){
    cout << "Number of customers: "<< nbCustomers <<endl;
  	cout << "Truck capacity: "<< truckCapacity <<endl;
  	cout << "\n";
  	cout << "Distance matrix between customers"<< endl;
  	for(int i=0;i<distanceMatrix.size();i++){
        for(int j=0;j<distanceMatrix[i].size();j++){
            cout << distanceMatrix[i][j] << " ";
        }
    cout<<endl;
    }
    cout << "\n";
  	cout << "Distance between each customer and the warehouse "<< endl;
  	for(int i=0;i<distanceWarehouses.size();i++){
        cout << distanceWarehouses[i] << " ";
    cout<<endl;
    }
    cout << "\n";
  	cout << "Demand of each customer"<< endl;
  	for(int i=0;i<demands.size();i++){
        cout << demands[i] << " ";
    cout<<endl;
    }
    std::cout << "File is now closed." << std::endl;
  }
  return true;
}
bool VrpInstance :: readDataFromFile ( const std :: string _fileName ) { bool everythingOK = true ; std :: ifstream infile ( _fileName . c_str ( ) ) ; if ( ! infile . is_open ( ) ) { if ( ! silence ) std :: cout << " " << " " << _fileName << std :: endl ; everythingOK = false ; return everythingOK ; } if ( ! silence ) { std :: cout << " " << _fileName << " " << std :: endl ; std :: cout << " " << std :: endl ; } string str ; char * pch ; char * line ; int nbNodes ; while ( true ) { getline ( infile , str ) ; line = strdup ( str . c_str ( ) ) ; pch = strtok ( line , " " ) ; if ( strcmp ( pch , " " ) == 0 ) { pch = strtok ( NULL , " " ) ; nbNodes = atoi ( pch ) ; nbCustomers = nbNodes - 1 ; } else if ( strcmp ( pch , " " ) == 0 ) { pch = strtok ( NULL , " " ) ; truckCapacity = atoi ( pch ) ; } else if ( strcmp ( pch , " " ) == 0 ) { pch = strtok ( NULL , " " ) ; if ( strcmp ( pch , " " ) != 0 ) { throw std :: runtime_error ( " " ) ; } } else if ( strcmp ( pch , " " ) == 0 ) { break ; } } vector < int > customersX ( nbCustomers ) ; vector < int > customersY ( nbCustomers ) ; int depotX , depotY ; for ( int n = 1 ; n <= nbNodes ; n ++ ) { int id ; infile >> id ; if ( id != n ) { throw std :: runtime_error ( " " ) ; } if ( n == 1 ) { infile >> depotX ; infile >> depotY ; } else { infile >> customersX [ n - 2 ] ; infile >> customersY [ n - 2 ] ; } } distanceMatrix = computeDistanceMatrix ( nbCustomers , customersX , customersY ) ; distanceWarehouses = computeDistanceToWarehouse ( nbCustomers , depotX , depotY , customersX , customersY ) ; getline ( infile , str ) ; getline ( infile , str ) ; line = strdup ( str . c_str ( ) ) ; pch = strtok ( line , " " ) ; if ( strcmp ( pch , " " ) != 0 ) { throw std :: runtime_error ( " " ) ; } demands . resize ( nbCustomers ) ; for ( int n = 1 ; n <= nbNodes ; n ++ ) { int id ; infile >> id ; if ( id != n ) { throw std :: runtime_error ( " " ) ; } int demand ; infile >> demand ; if ( n == 1 ) { if ( demand != 0 ) { throw std :: runtime_error ( " " ) ; } } else { demands [ n - 2 ] = demand ; } } getline ( infile , str ) ; getline ( infile , str ) ; line = strdup ( str . c_str ( ) ) ; pch = strtok ( line , " " ) ; if ( strcmp ( pch , " " ) != 0 ) { throw std :: runtime_error ( " " ) ; } int warehouseId ; infile >> warehouseId ; if ( warehouseId != 1 ) { throw std :: runtime_error ( " " ) ; } int endOfDepotSection ; infile >> endOfDepotSection ; if ( endOfDepotSection != -1 ) { throw std :: runtime_error ( " " ) ; } infile . close ( ) ; if ( ! silence ) { cout << " " << nbCustomers << endl ; cout << " " << truckCapacity << endl ; cout << " \n " ; cout << " " << endl ; for ( int i = 0 ; i < distanceMatrix . size ( ) ; i ++ ) { for ( int j = 0 ; j < distanceMatrix [ i ] . size ( ) ; j ++ ) { cout << distanceMatrix [ i ] [ j ] << " " ; } cout << endl ; } cout << " \n " ; cout << " " << endl ; for ( int i = 0 ; i < distanceWarehouses . size ( ) ; i ++ ) { cout << distanceWarehouses [ i ] << " " ; cout << endl ; } cout << " \n " ; cout << " " << endl ; for ( int i = 0 ; i < demands . size ( ) ; i ++ ) { cout << demands [ i ] << " " ; cout << endl ; } std :: cout << " " << std :: endl ; } return true ; }
Read the instance from file :
The input files follow the "Augerat" format.
Read the instance from file : The input files follow the " Augerat " format .
// -2 because orginal customer indices are in 2..nbNodes // Compute distance matrix // End the last line // -2 because orginal customer indices are in 2..nbNodes // End the last line