示例#1
0
  /** @param args */
  public static void main(String[] args) throws IOException {
    // Parse the first argument as the input file and the second as the output file
    if (args.length != 2) {
      usage();
      System.exit(1);
    }
    CitySet cities = CitySet.LoadFromUrl(new File(args[0]).toURI().toString());
    // Take the first city
    // Find the closest city, add it, then iterate

    Set<Integer> usedCities = new HashSet<Integer>();
    Tour currentTour = new Tour(cities);

    List<City> cityList = new ArrayList<City>();
    for (City c : cities) {
      cityList.add(c);
    }
    double[][] distances = calculateAllDistances(cityList);
    int currentIndex = 0;
    usedCities.add(currentIndex);

    while (usedCities.size() < cities.size()) {
      currentTour.addCity(cityList.get(currentIndex).ID);
      int nextCity = findClosestCity(currentIndex, distances, usedCities);
      currentIndex = nextCity;
      usedCities.add(currentIndex);
    }
    currentTour.addCity(cityList.get(currentIndex).ID);

    currentTour.saveTour(new FileOutputStream(new File(args[1])));
  }
示例#2
0
  public static void main(String[] args) {
    String sampleFile = args[0];
    String solutionFile = args[1];

    // Parse the first argument as the city input file and the second as the
    // tour solution file
    if (args.length != 2) {
      usage();
      System.exit(-1);
    }
    CitySet cities = CitySet.LoadFromUrl(new File(sampleFile).toURI().toString());
    try {
      Tour t = Tour.parseTour(new FileInputStream(new File(solutionFile)), cities);
      // Print sentinel value if this is not a valid permutation
      if (!t.isValidPermutation()) {
        System.err.println("Not a valid permutation of cities");
        System.out.println(-1);
        return;
      }
      // In the error-free case, only print the value we care about
      System.out.println(t.evaluate());
    } catch (Exception ex) {
      System.err.println("Cannot evaluate solution: " + ex.getMessage());
      System.out.println(-1);
    }
  }