Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.95 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. public class SimpleHillClimbing {
  4. private Problem problem;
  5.  
  6. private SimpleHillClimbing(Problem problem) {
  7. this.problem = problem;
  8. }
  9.  
  10. //Simple transformation
  11. private ArrayList<Double> findNewNeighbours(ArrayList<Double> parameters, double stepSize) {
  12. ArrayList<Double> newNeighbours = new ArrayList<>();
  13.  
  14. for (int i = 0; i < parameters.size(); i++) {
  15. int randomInt = (int) (2 * Math.random());
  16. if (randomInt == 1)
  17. newNeighbours.add(parameters.get(i) + stepSize);
  18. else
  19. newNeighbours.add(parameters.get(i) - stepSize);
  20.  
  21. if (newNeighbours.get(i) > problem.getMaxValues().get(i))
  22. newNeighbours.set(i, problem.getMaxValues().get(i));
  23. else if (newNeighbours.get(i) < problem.getMinValues().get(i))
  24. newNeighbours.set(i, problem.getMinValues().get(i));
  25. }
  26. return newNeighbours;
  27. }
  28.  
  29. private ArrayList<Double> findOptima(int iterations, int neighbours, double stepSize) {
  30. ArrayList<Double> bestGlobal = new ArrayList<>();
  31. int count = 0;
  32. for (int i = 0; i < iterations; i++) {
  33. ArrayList<Double> bestPoint = createInitPoints();
  34. double bestPointEval = problem.Eval(bestPoint);
  35.  
  36. boolean shouldContinue = true;
  37. while (shouldContinue) {
  38. count++;
  39. ArrayList<Double> newPoint = findNewNeighbours(bestPoint, stepSize);
  40. double newPointEval = problem.Eval(newPoint);
  41.  
  42. for (int nb = 0; nb < neighbours - 1; nb++) {
  43. ArrayList<Double> newNeighbour = findNewNeighbours(bestPoint, stepSize);
  44. if (problem.Eval(newNeighbour) > newPointEval)
  45. newPoint = newNeighbour;
  46. }
  47.  
  48. if (bestPointEval < newPointEval) {
  49. bestPointEval = newPointEval;
  50. bestPoint = newPoint;
  51. } else {
  52. shouldContinue = false;
  53. }
  54.  
  55. }
  56.  
  57. if (bestGlobal.size() == 0 || problem.Eval(bestGlobal) < bestPointEval) {
  58. bestGlobal = bestPoint;
  59. }
  60.  
  61. }
  62. System.out.println("!--Best eval: " + problem.Eval(bestGlobal) + " Count: " + count + "--!");
  63. return bestGlobal;
  64. }
  65.  
  66. private ArrayList<Double> createInitPoints() {
  67. ArrayList<Double> initPoint = new ArrayList<>();
  68. for (int dim = 0; dim < problem.getDimensions(); dim++) {
  69. initPoint.add(problem.getMinValues().get(dim) + Math.random() * (problem.getMaxValues().get(dim) - problem.getMinValues().get(dim)));
  70. }
  71. return initPoint;
  72. }
  73.  
  74. public static void main(String[] args) {
  75. //Initial setup
  76. SimpleHillClimbing climber = new SimpleHillClimbing(new P1());
  77. int iterations = 10;
  78. int neighbours = 1;
  79. double stepsize = 0.01;
  80.  
  81. //----------------------------------P2------------------------------------------------------//
  82. System.out.println("--> P1");
  83.  
  84. System.out.println("-> Iteration Increase");
  85. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  86. iterations = 100;
  87. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  88. System.out.println();
  89. iterations = 1;
  90.  
  91. System.out.println("-> Neighbour Increase");
  92. neighbours = 10;
  93. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  94. neighbours = 100;
  95. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  96. System.out.println();
  97. neighbours = 1;
  98.  
  99. System.out.println("-> Iteration Increase");
  100. stepsize = 0.1;
  101. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  102. stepsize = 0.01;
  103. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  104. System.out.println();
  105.  
  106. //----------------------------------P2------------------------------------------------------//
  107. climber = new SimpleHillClimbing(new P2());
  108. System.out.println("--> P2");
  109.  
  110. System.out.println("-> Iteration Increase");
  111. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  112. iterations = 100;
  113. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  114. System.out.println();
  115. iterations = 1;
  116.  
  117. System.out.println("-> Neighbour Increase");
  118. neighbours = 10;
  119. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  120. neighbours = 100;
  121. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  122. System.out.println();
  123. neighbours = 1;
  124.  
  125. System.out.println("-> Iteration Increase");
  126. stepsize = 0.1;
  127. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  128. stepsize = 0.01;
  129. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  130. System.out.println();
  131.  
  132. //----------------------------------P2------------------------------------------------------//
  133. climber = new SimpleHillClimbing(new RevAckley());
  134. System.out.println("--> RevAckley");
  135.  
  136. System.out.println("-> Iteration Increase");
  137. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  138. iterations = 100;
  139. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  140. iterations = 200;
  141. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  142. System.out.println();
  143. iterations = 1;
  144.  
  145. System.out.println("-> Neighbour Increase");
  146. neighbours = 10;
  147. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  148. neighbours = 100;
  149. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  150. neighbours = 200;
  151. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  152. System.out.println();
  153. neighbours = 1;
  154.  
  155. System.out.println("-> Iteration Increase");
  156. stepsize = 0.1;
  157. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  158. stepsize = 0.01;
  159. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  160. stepsize = 0.001;
  161. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  162. System.out.println();
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement