예제 #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])));
  }