|
1 | 1 | import java.io.File; |
2 | | -import java.io.FileNotFoundException; |
3 | 2 | import java.io.PrintWriter; |
4 | | -import java.io.UnsupportedEncodingException; |
5 | 3 | import java.util.ArrayList; |
6 | 4 | import java.util.Arrays; |
7 | 5 | import java.util.Collection; |
8 | 6 |
|
9 | 7 | public class TSP { |
10 | | - |
| 8 | + |
11 | 9 | 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))); |
21 | 21 | } |
22 | | - return new FullTour(pt); |
| 22 | + System.out.println("writeToFile called"); |
| 23 | + |
| 24 | + writeToFile("Greedy", greedyTours); |
| 25 | + writeToFile("HillClimb", hillClimbingTours); |
| 26 | + writeToFile("Annealing", annealingTours); |
23 | 27 | } |
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 | + } |
28 | 39 | } |
29 | | - |
30 | | - public static void greedyFile() throws Exception { |
| 40 | + |
| 41 | + |
| 42 | + public static ArrayList<Graph> readGraphs() throws Exception { |
31 | 43 | Collection<String> fileNumbers = Arrays.asList("012", "017", "021", "026", "042", "048", "058", "175", "180", |
32 | 44 | "535"); |
| 45 | + ArrayList<Graph> graphs = new ArrayList<>(); |
33 | 46 | for (String ext : fileNumbers) { |
34 | 47 | String fileName = "NEWAISearchfile" + ext + ".txt"; |
35 | | - System.out.println(fileName); |
| 48 | + System.out.println("Reading graph from: " + fileName); |
36 | 49 | 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); |
42 | 51 | } |
43 | | - |
| 52 | + return graphs; |
44 | 53 | } |
| 54 | + |
45 | 55 | } |
0 commit comments