Skip to content

Commit 0f12e1c

Browse files
committed
getSuccessors(), clone(), swapped() implemented
1 parent 0e6cbaa commit 0f12e1c

File tree

4 files changed

+47
-38
lines changed

4 files changed

+47
-38
lines changed

.classpath

Lines changed: 0 additions & 7 deletions
This file was deleted.

.project

Lines changed: 0 additions & 17 deletions
This file was deleted.

AISearch.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

src/FullTour.java

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,55 @@
11
import java.util.ArrayList;
2+
import java.util.HashSet;
23

3-
public class FullTour extends Tour{
4-
4+
public class FullTour extends Tour {
5+
56
public FullTour(Graph g) {
67
super(g);
78
}
8-
9-
public FullTour(Graph g, ArrayList<Integer> cities){
9+
10+
public FullTour(Graph g, ArrayList<Integer> cities) {
1011
super(g, cities);
1112
}
1213

13-
public FullTour(PartialTour pt) throws Exception{
14+
public FullTour(PartialTour pt) throws Exception {
1415
super(pt.getGraph());
15-
if(pt.isComplete()){
16+
if (pt.isComplete()) {
1617
setCities(pt.getCities());
17-
}
18-
else{
18+
} else {
1919
throw new Exception("Incomplete partial tour cannot be passed as a full tour");
2020
}
2121
}
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";
2525
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+
}
2738
}
28-
stringForm = stringForm.substring(0, stringForm.length()-1); //delete the last comma
29-
return stringForm;
39+
return successors;
3040
}
3141

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+
}
3248

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+
}
3355
}

0 commit comments

Comments
 (0)