Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.81 KB | None | 0 0
  1. /**
  2. * This program was designed to solve the traveling salesman problem with the
  3. * use of a stack and minimum scanning tree. The program will be given a list
  4. * of cities that are so far from each other and it must calculate the shortest
  5. * path to visit all cities once.
  6. * Author: Clint Foster
  7. * Date: 4/29/2017
  8. */
  9. package assignment6;
  10.  
  11. import java.io.File;
  12. import java.io.IOException;
  13. import java.util.Scanner;
  14. import java.util.Stack;
  15. import java.util.ArrayList;
  16. import java.text.NumberFormat;
  17. import java.text.DecimalFormat;
  18.  
  19. /**
  20. *
  21. * @author CFoster
  22. */
  23. public class Assignment6 {
  24. int CITI = 12;
  25. double tourCost = 0;
  26. /**
  27. * @param args the command line arguments
  28. */
  29. public static void main(String[] args) {
  30. Assignment6 test = new Assignment6();
  31. File input = new File("src/assignment6/tsp12.txt");
  32. System.out.println("The best tourCost for 12 cities is: " + test.findPath(test.populateMatrix(input)));
  33. System.out.println("\n");
  34.  
  35. Assignment6 test2 = new Assignment6();
  36. test2.setCityAmount(13);
  37. File input2 = new File("src/assignment6/tsp13.txt");
  38. System.out.println("The best tourCost for 13 cities is: " + test2.findPath(test2.populateMatrix(input2)));
  39. System.out.println("\n");
  40.  
  41. Assignment6 test3 = new Assignment6();
  42. test3.setCityAmount(14);
  43. File input3 = new File("src/assignment6/tsp14.txt");
  44. System.out.println("The best tourCost for 14 cities is: " + test3.findPath(test3.populateMatrix(input3)));
  45. System.out.println("\n");
  46.  
  47. Assignment6 test4 = new Assignment6();
  48. test4.setCityAmount(15);
  49. File input4 = new File("src/assignment6/tsp15.txt");
  50. System.out.println("The best tourCost for 15 cities is: " + test4.findPath(test4.populateMatrix(input4)));
  51. System.out.println("\n");
  52.  
  53. Assignment6 test5 = new Assignment6();
  54. test5.setCityAmount(16);
  55. File input5 = new File("src/assignment6/tsp16.txt");
  56. System.out.println("The best tourCost for 16cities is: " + test5.findPath(test5.populateMatrix(input5)));
  57. System.out.println("\n");
  58.  
  59. Assignment6 test6 = new Assignment6();
  60. test6.setCityAmount(19);
  61. File input6 = new File("src/assignment6/tsp19.txt");
  62. System.out.println("The best tourCost for 19 cities is: " + test6.findPath(test6.populateMatrix(input6)));
  63. System.out.println("\n");
  64.  
  65. Assignment6 test7 = new Assignment6();
  66. test7.setCityAmount(29);
  67. File input7 = new File("src/assignment6/tsp29.txt");
  68. System.out.println("The best tourCost for 29 cities is: " + test7.findPath(test7.populateMatrix(input7)));
  69. }
  70.  
  71. /**
  72. *
  73. * @param amount
  74. */
  75. public void setCityAmount(int amount){
  76. this.CITI = amount;
  77. }
  78.  
  79. /**
  80. * This method takes in an input file and creates a matrix of integer values.
  81. * It then outputs this matrix.
  82. * @param input The file to be used in calculating the matrix
  83. * @return The matrix of values
  84. */
  85. public int[][] populateMatrix(File input) {
  86. int [][]adjacency = new int[CITI][CITI];
  87. int value;
  88.  
  89. try {
  90. Scanner inf = new Scanner(input);
  91. for (int i = 0; i < CITI && inf.hasNext(); i++) {
  92. for (int j = i; j < CITI && inf.hasNext(); j++) {
  93. if (i == j) {
  94. adjacency[i][j] = 0;
  95. } else {
  96. value = inf.nextInt();
  97. adjacency[i][j] = value;
  98. adjacency[j][i] = value;
  99. }
  100. }
  101. }
  102. inf.close();
  103. } catch (IOException e) {
  104. }
  105.  
  106. return adjacency;
  107. }
  108.  
  109. /**
  110. * This method takes in the matrix of values and uses them to find the
  111. * shortest path. It also outputs that path and the time taken to find it.
  112. * @param adjacency The matrix of values needed for city to city travel
  113. * @return The total time used to travel the entire path
  114. */
  115. public double findPath(int[][] adjacency){
  116. Stack<Integer> pathStack = new Stack();
  117. ArrayList<Integer> visitedCities = new ArrayList<>(CITI);
  118. Boolean minFlag = true;
  119. int closestCity = 0;
  120. int currentCity = 0;
  121. int minDistance = 0;
  122. double tourCost = 0.0;
  123. long startTime = System.nanoTime();
  124. long endTime = System.nanoTime();
  125. float duration;
  126.  
  127. visitedCities.add(0);
  128. pathStack.push(0);
  129. minFlag = false;
  130. System.out.println("Starting City: " + pathStack.peek());
  131.  
  132. while(pathStack.empty() != true){
  133. currentCity = pathStack.peek();
  134. minDistance = Integer.MAX_VALUE;
  135. for(int i = 0; i < CITI; i++){
  136. if(adjacency[currentCity][i] != 0 && !visitedCities.contains(i)){
  137. if(adjacency[currentCity][i] < minDistance){
  138. minDistance = adjacency[currentCity][i];
  139. closestCity = i;
  140. minFlag = true;
  141. }
  142. }
  143. }
  144. if(minFlag){
  145. tourCost = tourCost + (double)adjacency[currentCity][closestCity];
  146. visitedCities.add(closestCity);
  147. pathStack.push(closestCity);
  148. System.out.println("Closest City: " + closestCity);
  149. minFlag = false;
  150. continue;
  151. }
  152. pathStack.pop();
  153. }
  154. endTime = System.nanoTime();
  155. NumberFormat nf = new DecimalFormat("#0.00");
  156. duration = (endTime - startTime)/1000000;
  157. System.out.println("The execution time for this file is: " + nf.format(duration) + " millisecond(s)");
  158. return tourCost;
  159. }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement