Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1.     private static void simulatedAnnealing(int noIteration, ProblemData problemData) throws IOException {
  2.        
  3.         Date start = new Date();
  4.         ArrayList<Solution> bestSolutions = new ArrayList<>();
  5.         double cost = 0;
  6.         for (int k = 0; k < 10; k++) {
  7.            
  8.             /*
  9.              * Parameters section
  10.              */
  11.             double T = 10000;
  12.             double alpha = 0.9999;
  13.             double minT = 0.00001;
  14.            
  15.             Solution c = UtilFunctions.createRandomSolution(problemData);
  16.             UtilFunctions.writeFile("SimulatedAnnealing-" + problemData.getMapCapacity() + ".txt",
  17.                     "Starting random point:" + "\n" + c);
  18.  
  19.             ArrayList<Solution> solutions = new ArrayList<>();
  20.             solutions.add(c);
  21.             System.out.println(String.format("Initial Solution: %s", c));
  22.             Random rnd = new Random();
  23.            
  24.             while (T > minT) {
  25.                 int i = 1;
  26.                 do {
  27.                     Solution x = UtilFunctions.getVecin(c, problemData);
  28.                     double delta = x.getTotalCost() - c.getTotalCost();
  29.                     if (delta < 0) {
  30.                         c = new Solution(i, x.getCitiesVisited(), x.getTotalCost());
  31.                         solutions.add(c);
  32.                     } else if (rnd.nextDouble() < Math.exp(-delta / T)) {
  33.                         c = new Solution(i, x.getCitiesVisited(), x.getTotalCost());
  34.                     }
  35.                     i++;
  36.                 } while (i < noIteration);
  37.                 T = alpha * T;
  38.             }
  39.             solutions.sort(new Comparator<Solution>() {
  40.                 @Override
  41.                 public int compare(Solution sol0, Solution sol1) {
  42.                     if (sol0.getTotalCost() > sol1.getTotalCost())
  43.                         return 1;
  44.                     else if (sol0.getTotalCost() < sol1.getTotalCost())
  45.                         return -1;
  46.  
  47.                     return 0;
  48.                 }
  49.  
  50.             });
  51.  
  52.             cost += solutions.get(0).getTotalCost();
  53.             bestSolutions.add(solutions.get(0));
  54.  
  55.             System.out.println(String.format("Final Solution: %s", solutions.get(0)));
  56.  
  57.             UtilFunctions.writeFile("SimulatedAnnealing-" + problemData.getMapCapacity() + ".txt",
  58.                     "Best ending point:" + "\n" + c);
  59.             UtilFunctions.writeFile("SimulatedAnnealing-" + problemData.getMapCapacity() + ".txt", "\n \n");
  60.            
  61.            
  62.         }
  63.  
  64.         bestSolutions.sort(new Comparator<Solution>() {
  65.  
  66.             @Override
  67.             public int compare(Solution sol0, Solution sol1) {
  68.                 if (sol0.getTotalCost() > sol1.getTotalCost())
  69.                     return 1;
  70.                 else if (sol0.getTotalCost() < sol1.getTotalCost())
  71.                     return -1;
  72.  
  73.                 return 0;
  74.             }
  75.  
  76.         });
  77.         UtilFunctions.writeFile("SimulatedAnnealing-" + problemData.getMapCapacity() + ".txt",
  78.                 "Best Solution Found: " + bestSolutions.get(0));
  79.         UtilFunctions.writeFile("SimulatedAnnealing-" + problemData.getMapCapacity() + ".txt",
  80.                 "Average cost(distance): " + Math.round((cost / 10)/100.0)*100.0);
  81.         Date end = new Date();
  82.         UtilFunctions.writeFile("SimulatedAnnealing-" + problemData.getMapCapacity() + ".txt",
  83.                 "Timp " + (end.getTime() - start.getTime()));
  84.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement