public Tour createRandomTour() { Tour tour = new Tour(); List<City> newPath = new ArrayList<>(); int tourSize = allCities.size(); int index; Set<Integer> seen = new HashSet<>(); City city; newPath.add(allCities.get(0)); // System.out.println("** " + allCities.get(0).getName()); for (int i = 1; i < tourSize; i++) { index = (int) (Math.random() * tourSize); while (index == 0 || seen.contains(index)) { // System.out.println("in " + index); index = (int) (Math.random() * tourSize); } seen.add(index); city = allCities.get(index); // System.out.println("** " + index + " " + allCities.get(index).getName()); newPath.add(city); } // System.out.println(""); if (allCities.size() > 1) { // System.out.println("** last :" + allCities.get(0).getName()); newPath.add(allCities.get(0)); } tour.setPath(newPath); return tour; }
private void createEdges() { City c1, c2; for (int i = 0; i < allCities.size(); i++) { for (int j = 0; j < allCities.size(); j++) { if (i == j) continue; c1 = allCities.get(i); c2 = allCities.get(j); c1.addCityToMap(c2); c2.addCityToMap(c1); // System.out.println("c1 " + c1.getName() + " , c2 " + c2.getName() + " dist : " + // c1.getdMap().get(c2)); } } sortEdges(allCities); }