Skip to content

Commit 2b3236d

Browse files
committed
implemented batch tests for algorithms
1 parent eb64936 commit 2b3236d

File tree

4 files changed

+94
-29
lines changed

4 files changed

+94
-29
lines changed

AISearch.iml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,27 @@
77
</content>
88
<orderEntry type="inheritedJdk" />
99
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="module-library">
11+
<library name="JUnit5.2">
12+
<CLASSES>
13+
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.3.1/junit-jupiter-api-5.3.1.jar!/" />
14+
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.0.0/apiguardian-api-1.0.0.jar!/" />
15+
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.1.1/opentest4j-1.1.1.jar!/" />
16+
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.3.1/junit-platform-commons-1.3.1.jar!/" />
17+
</CLASSES>
18+
<JAVADOC />
19+
<SOURCES />
20+
</library>
21+
</orderEntry>
22+
<orderEntry type="module-library">
23+
<library name="JUnit4">
24+
<CLASSES>
25+
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
26+
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
27+
</CLASSES>
28+
<JAVADOC />
29+
<SOURCES />
30+
</library>
31+
</orderEntry>
1032
</component>
1133
</module>

src/SimAnnealer.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,25 @@
11
public class SimAnnealer {
22

3-
public static FullTour SimAnneal(FullTour initial){
4-
3+
public static FullTour SimAnneal(FullTour initial) {
54
return null;
65
}
7-
6+
7+
public static FullTour HillClimb(FullTour initial) {
8+
FullTour current = initial;
9+
FullTour optimal = initial;
10+
int optimalWeight = initial.computeWeight();
11+
long time = System.nanoTime();
12+
System.out.println(time);
13+
do {
14+
for (FullTour ft : current.getSuccessors()) {
15+
if(ft.computeWeight() < optimalWeight){
16+
optimal = ft;
17+
optimalWeight = ft.computeWeight();
18+
}
19+
}
20+
} while (!current.equals(optimal) && System.nanoTime() < time + 5000000); //timeout after 5 seconds
21+
return optimal;
22+
}
23+
824
}
925

src/StartPointGenerator.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.lang.reflect.Array;
2+
import java.util.ArrayList;
3+
4+
public class StartPointGenerator {
5+
6+
// done
7+
public static FullTour Greedy(Graph g) throws Exception {
8+
PartialTour pt = new PartialTour(g);
9+
pt.append(0);
10+
while (pt.greedyAppend()) {
11+
// System.out.println(pt);
12+
}
13+
return new FullTour(pt);
14+
}
15+
16+
17+
}

src/TSP.java

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,55 @@
11
import java.io.File;
2-
import java.io.FileNotFoundException;
32
import java.io.PrintWriter;
4-
import java.io.UnsupportedEncodingException;
53
import java.util.ArrayList;
64
import java.util.Arrays;
75
import java.util.Collection;
86

97
public class TSP {
10-
8+
119
public static void main(String[] args) throws Exception {
12-
greedyFile();
13-
}
14-
15-
// done
16-
public static FullTour Greedy(Graph g) throws Exception {
17-
PartialTour pt = new PartialTour(g);
18-
pt.append(0);
19-
while (pt.greedyAppend()) {
20-
// System.out.println(pt);
10+
ArrayList<FullTour> greedyTours = new ArrayList<>();
11+
ArrayList<FullTour> annealingTours = new ArrayList<>();
12+
ArrayList<FullTour> hillClimbingTours = new ArrayList<>();
13+
for(Graph graph : readGraphs()){
14+
System.out.println("Currently processing graph of size "+graph.getSize());
15+
//Greedy
16+
greedyTours.add(StartPointGenerator.Greedy(graph));
17+
// Hill Climbing
18+
hillClimbingTours.add(SimAnnealer.HillClimb(StartPointGenerator.Greedy(graph)));
19+
//Annealing
20+
//hillClimbingTours.add(SimAnnealer.SimAnneal(StartPointGenerator.Greedy(graph)));
2121
}
22-
return new FullTour(pt);
22+
System.out.println("writeToFile called");
23+
24+
writeToFile("Greedy", greedyTours);
25+
writeToFile("HillClimb", hillClimbingTours);
26+
writeToFile("Annealing", annealingTours);
2327
}
24-
25-
public static FullTour simulatedAnnealing(Graph g) {
26-
return null;
27-
28+
29+
//TODO: change ...file58.txt to ...file058.txt
30+
public static void writeToFile(String pathName, ArrayList<FullTour> tours) throws Exception {
31+
new File(pathName).mkdirs();
32+
for (FullTour tour : tours) {
33+
String outputFileName = "tourNEWAISearchfile" + tour.size() + ".txt";
34+
PrintWriter writer = new PrintWriter(pathName + "/" + outputFileName, "UTF-8");
35+
writer.println(tour);
36+
System.out.println("Successfully printed tour" + outputFileName);
37+
writer.close();
38+
}
2839
}
29-
30-
public static void greedyFile() throws Exception {
40+
41+
42+
public static ArrayList<Graph> readGraphs() throws Exception {
3143
Collection<String> fileNumbers = Arrays.asList("012", "017", "021", "026", "042", "048", "058", "175", "180",
3244
"535");
45+
ArrayList<Graph> graphs = new ArrayList<>();
3346
for (String ext : fileNumbers) {
3447
String fileName = "NEWAISearchfile" + ext + ".txt";
35-
System.out.println(fileName);
48+
System.out.println("Reading graph from: " + fileName);
3649
Graph g = new Graph(fileName);
37-
new File("Greedy").mkdirs();
38-
String outputFileName = "tourNEWAISearchfile" + ext + ".txt";
39-
PrintWriter writer = new PrintWriter("Greedy/" + outputFileName, "UTF-8");
40-
writer.println(Greedy(g));
41-
writer.close();
50+
graphs.add(g);
4251
}
43-
52+
return graphs;
4453
}
54+
4555
}

0 commit comments

Comments
 (0)