Advertisement
Guest User

algorithms

a guest
Aug 16th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. package travelling_salesman_package;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5.  
  6. import given_functions.CS2004;
  7.  
  8. public class Algorithms {
  9. //Random mutation
  10. public static TSPObject RandomRestartHillClimber (int iter, int restart, double[][] distances, boolean print) {
  11. ArrayList<TSPObject> Solutions = new ArrayList<>();
  12. for (int i=1; i<=restart; i++){
  13. Solutions.add(RandomMutation (iter,distances,print));
  14. }
  15.  
  16. return bestSol(Solutions);
  17. }
  18.  
  19. public static TSPObject bestSol(ArrayList<TSPObject> solution) {
  20. ArrayList<Double> fitness = new ArrayList<>();
  21.  
  22. for(int i = 0; i < solution.size(); i++) {
  23. fitness.add(solution.get(i).getFitness());
  24. }
  25.  
  26. int min = fitness.indexOf(Collections.min(fitness));
  27.  
  28. return solution.get(min);
  29.  
  30.  
  31. }
  32.  
  33.  
  34.  
  35.  
  36. //Stochastic Hill Climber
  37. public static TSPObject StochasticHillClimber(int iter, double[][] distances, boolean print)
  38. {
  39. int n = distances.length;
  40. TSPObject solution = new TSPObject(n);//creates random solution
  41. for (int i = 1; i <= iter; i++) //For loop iterated the amount of times needed
  42. {
  43. TSPObject OldSolution = new TSPObject(solution.getTour(),n); //solution is copied into OldSolution
  44. double f1 = OldSolution.fitnessFunction(distances); //first fitness is evaluated within the loop
  45.  
  46. solution.smallChange();
  47.  
  48. TSPObject NewSolution = new TSPObject(solution.getTour(),n);
  49.  
  50. double f2 = NewSolution.fitnessFunction(distances); //The new fitness is evaluated with a different variable
  51.  
  52. //If the newest fitness is worse than the old one, keep the old solution
  53.  
  54.  
  55.  
  56. //if a random number is greater than the probability acceptance then the probability is
  57. //not good enough so we keep the old fitness
  58.  
  59.  
  60. //If the probability acceptance is 0.6 and random is 0.7 it would go back to the old solution so accepting the old solution is better
  61.  
  62.  
  63. if(CS2004.UR(0, 1) > PRAccept(f2,f1,95)) {
  64. solution=OldSolution;
  65. }
  66.  
  67. if(print) {
  68. System.out.println("Iteration " + i + ": " +" Fitness: " + f1);
  69. }
  70. }
  71.  
  72. return(solution);//returns current solution
  73. }
  74.  
  75.  
  76.  
  77.  
  78.  
  79. private static double PRAccept(double newFitness, double oldFitness, int t) {
  80.  
  81. return 1.0/(1.0+Math.exp((newFitness-oldFitness)/t));
  82. }
  83.  
  84.  
  85. //Random Restart Hill Climber
  86. protected static TSPObject RandomMutation (int iter, double[][] distances, boolean print)
  87. {
  88. int n = distances.length;
  89. TSPObject solution = new TSPObject(n);//creates random solution
  90. for (int i = 1; i <= iter; i++) //For loop iterated the amount of times needed
  91. {
  92.  
  93.  
  94. TSPObject OldSolution = new TSPObject(solution.getTour(),n); //copies solution into OldSolution
  95. double f1 = OldSolution.fitnessFunction(distances); //First fitness is evaluated within the loop
  96.  
  97. solution.smallChange(); //Small change is made in solution
  98.  
  99. TSPObject NewSolution = new TSPObject(solution.getTour(),n);
  100.  
  101. double f2 = NewSolution.fitnessFunction(distances); //The new fitness is evaluated with a different variable
  102.  
  103. //If the newest fitness is worse than the old one, keep the old solution
  104. if (f2 > f1)
  105. {
  106. solution=OldSolution;
  107. }
  108. if(print) {
  109. System.out.println("Iteration " + i + ": " +" Fitness: " + f1);
  110. }
  111. }
  112.  
  113. return(solution);//returns current solution
  114. }
  115.  
  116.  
  117.  
  118. //Simulated Annealing
  119. protected static TSPObject SimulatedAnnealing(int iter, double[][] distances, boolean print, double coolingrate, double temperature)
  120. {
  121. int n = distances.length;
  122. TSPObject solution = new TSPObject(n);//create random solution
  123.  
  124. double currentTemp = temperature;
  125. for (int i = 1; i <= iter; i++) //loops for the number of iterations specified
  126. {
  127.  
  128.  
  129. TSPObject OldSolution = new TSPObject(solution.getTour(),n); //solution is copied into OldSolution
  130. double f1 = OldSolution.fitnessFunction(distances); //evaluates the first fitness within the loop
  131.  
  132. solution.smallChange(); //small change is performed in solution
  133.  
  134. TSPObject NewSolution = new TSPObject(solution.getTour(),n);
  135.  
  136. double f2 = NewSolution.fitnessFunction(distances); //fitness is evaluated to new different variable
  137.  
  138. //keep old solution if the new fitness is worse
  139. if (f2 > f1)
  140. {
  141. double p = PR(f2,f1,currentTemp);
  142.  
  143. if(p<CS2004.UR(0, 1)) {
  144. solution=OldSolution;
  145. }
  146. else
  147. solution=NewSolution;
  148. }
  149. if(print) {
  150. System.out.println("Iteration " + i + ": " +" Fitness: " + f1);
  151. }
  152.  
  153. currentTemp *= coolRate(temperature, i);
  154. }
  155.  
  156. return(solution);//returns current solution
  157. }
  158.  
  159. private static double coolRate(double initialTemp, int iter) {
  160. return Math.exp((Math.log(Math.pow(10,-100))-Math.log(initialTemp))/iter); //figure out the landar
  161. }
  162.  
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169. //PR- probability
  170. private static double PR(double newFitness, double oldFitness, double currentTemperature) {
  171. //New Fitness
  172. //Old Fitness
  173. double changeOfFitness = newFitness - oldFitness;
  174.  
  175. return Math.exp(-changeOfFitness/currentTemperature);
  176. //Current Temperature
  177.  
  178. }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement