Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.31 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.  
  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. ArrayList<Double> newPoint = findNewNeighbours(bestPoint, stepSize);
  39. double newPointEval = problem.Eval(newPoint);
  40.  
  41. for (int nb = 0; nb < neighbours - 1; nb++) {
  42. ArrayList<Double> newNeighbour = findNewNeighbours(bestPoint, stepSize);
  43. if (problem.Eval(newNeighbour) > newPointEval)
  44. newPoint = newNeighbour;
  45. }
  46.  
  47. if (bestPointEval < newPointEval) {
  48. bestPointEval = newPointEval;
  49. bestPoint = newPoint;
  50. } else {
  51. shouldContinue = false;
  52. }
  53. }
  54.  
  55. // System.out.println("-------------Global");
  56. // System.out.println(bestGlobal);
  57. // if(bestGlobal.size() != 0)
  58. // System.out.println(problem.Eval(bestGlobal));
  59. // System.out.println("-------------Iteration");
  60. // System.out.println(bestPoint);
  61. // System.out.println(bestPointEval);
  62.  
  63.  
  64. if (bestGlobal.size() == 0 || problem.Eval(bestGlobal) < bestPointEval) {
  65. bestGlobal = bestPoint;
  66. }
  67.  
  68.  
  69. //System.out.println(problem.Eval(randomInitPoints));
  70. }
  71. System.out.print("(best eval: " + problem.Eval(bestGlobal) + "");
  72. return bestGlobal;
  73. }
  74.  
  75. private ArrayList<Double> createInitPoints() {
  76. ArrayList<Double> initPoint = new ArrayList<>();
  77. for (int dim = 0; dim < problem.getDimensions(); dim++) {
  78. initPoint.add(problem.getMinValues().get(dim) + Math.random() * (problem.getMaxValues().get(dim) - problem.getMinValues().get(dim)));
  79. }
  80. return initPoint;
  81. }
  82.  
  83. public static void main(String[] args) {
  84. //Initial setup
  85. SimpleHillClimbing climber = new SimpleHillClimbing(new P1());
  86. int iterations = 10;
  87. int neighbours = 1;
  88. double stepsize = 0.01;
  89.  
  90. //----------------------------------P2------------------------------------------------------//
  91. System.out.println("--> P1");
  92.  
  93. System.out.println("-> Iteration Increase");
  94. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  95. iterations = 100;
  96. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  97. System.out.println();
  98. iterations = 1;
  99.  
  100. System.out.println("-> Neighbour Increase");
  101. neighbours = 10;
  102. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  103. neighbours = 100;
  104. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  105. System.out.println();
  106. neighbours = 1;
  107.  
  108. System.out.println("-> Iteration Increase");
  109. stepsize = 0.1;
  110. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  111. stepsize = 0.01;
  112. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  113. System.out.println();
  114.  
  115. //----------------------------------P2------------------------------------------------------//
  116. climber = new SimpleHillClimbing(new P2());
  117. System.out.println("--> P2");
  118.  
  119. System.out.println("-> Iteration Increase");
  120. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  121. iterations = 100;
  122. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  123. System.out.println();
  124. iterations = 1;
  125.  
  126. System.out.println("-> Neighbour Increase");
  127. neighbours = 10;
  128. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  129. neighbours = 100;
  130. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  131. System.out.println();
  132. neighbours = 1;
  133.  
  134. System.out.println("-> Iteration Increase");
  135. stepsize = 0.1;
  136. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  137. stepsize = 0.01;
  138. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  139. System.out.println();
  140.  
  141. //----------------------------------P2------------------------------------------------------//
  142. climber = new SimpleHillClimbing(new RevAckley());
  143. System.out.println("--> RevAckley");
  144.  
  145. System.out.println("-> Iteration Increase");
  146. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  147. iterations = 100;
  148. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  149. iterations = 200;
  150. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  151. System.out.println();
  152. iterations = 1;
  153.  
  154. System.out.println("-> Neighbour Increase");
  155. neighbours = 10;
  156. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  157. neighbours = 100;
  158. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  159. neighbours = 200;
  160. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  161. System.out.println();
  162. neighbours = 1;
  163.  
  164. System.out.println("-> Iteration Increase");
  165. stepsize = 0.1;
  166. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  167. stepsize = 0.01;
  168. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  169. stepsize = 0.001;
  170. System.out.println("> Iterations: " + iterations + ", Neighbours: " + neighbours + ", Stepsize: " + stepsize + " Global optima: " + climber.findOptima(iterations, neighbours, stepsize));
  171. System.out.println();
  172. }
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement