|
1 | 1 | import java.util.ArrayList; |
| 2 | +import java.util.HashSet; |
2 | 3 |
|
3 | | -public class FullTour extends Tour{ |
4 | | - |
| 4 | +public class FullTour extends Tour { |
| 5 | + |
5 | 6 | public FullTour(Graph g) { |
6 | 7 | super(g); |
7 | 8 | } |
8 | | - |
9 | | - public FullTour(Graph g, ArrayList<Integer> cities){ |
| 9 | + |
| 10 | + public FullTour(Graph g, ArrayList<Integer> cities) { |
10 | 11 | super(g, cities); |
11 | 12 | } |
12 | 13 |
|
13 | | - public FullTour(PartialTour pt) throws Exception{ |
| 14 | + public FullTour(PartialTour pt) throws Exception { |
14 | 15 | super(pt.getGraph()); |
15 | | - if(pt.isComplete()){ |
| 16 | + if (pt.isComplete()) { |
16 | 17 | setCities(pt.getCities()); |
17 | | - } |
18 | | - else{ |
| 18 | + } else { |
19 | 19 | throw new Exception("Incomplete partial tour cannot be passed as a full tour"); |
20 | 20 | } |
21 | 21 | } |
22 | | - |
23 | | - public String toString(){ |
24 | | - String stringForm = "NAME = " + getGraph().getFilename().substring(3, getGraph().getFilename().length()-4) + ",\nTOURSIZE = " + getCities().size() + ",\nLENGTH = " + this.computeWeight() + ",\n"; |
| 22 | + |
| 23 | + public String toString() { |
| 24 | + String stringForm = "NAME = " + getGraph().getFilename().substring(3, getGraph().getFilename().length() - 4) + ",\nTOURSIZE = " + getCities().size() + ",\nLENGTH = " + this.computeWeight() + ",\n"; |
25 | 25 | for (Integer i : getCities()) { |
26 | | - stringForm += (i+1) + ","; //+1 to account for city indexing starting at 1, not 0 |
| 26 | + stringForm += (i + 1) + ","; //+1 to account for city indexing starting at 1, not 0 |
| 27 | + } |
| 28 | + stringForm = stringForm.substring(0, stringForm.length() - 1); //delete the last comma |
| 29 | + return stringForm; |
| 30 | + } |
| 31 | + |
| 32 | + public HashSet<FullTour> getSuccessors() { |
| 33 | + HashSet<FullTour> successors = new HashSet<>(); |
| 34 | + for (int i = 0; i < this.size(); i++) { |
| 35 | + for (int j = 0; j < this.size(); j++) { |
| 36 | + successors.add(swapped(i,j)); |
| 37 | + } |
27 | 38 | } |
28 | | - stringForm = stringForm.substring(0, stringForm.length()-1); //delete the last comma |
29 | | - return stringForm; |
| 39 | + return successors; |
30 | 40 | } |
31 | 41 |
|
| 42 | + public FullTour clone() { |
| 43 | + ArrayList<Integer> cloneCities = new ArrayList<>(); |
| 44 | + //may cause issues as Integer is Object-type |
| 45 | + cloneCities.addAll(this.getCities()); |
| 46 | + return new FullTour(this.getGraph(), cloneCities); |
| 47 | + } |
32 | 48 |
|
| 49 | + public FullTour swapped(int cityIndex1, int cityIndex2) { |
| 50 | + FullTour swappedTour = this.clone(); |
| 51 | + swappedTour.getCities().set(cityIndex2, this.getCities().get(cityIndex1)); |
| 52 | + swappedTour.getCities().set(cityIndex1, this.getCities().get(cityIndex2)); |
| 53 | + return swappedTour; |
| 54 | + } |
33 | 55 | } |
0 commit comments