AcoAlgorithm2.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. package exampleAlgorithms;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.ListIterator;
  5. import javax.swing.JFrame;
  6. import javax.swing.JPanel;
  7. import api.AlgorithmFramework;
  8. import ui.model.DecoratedState;
  9. import ui.view.Console;
  10. public class AcoAlgorithm2 extends AlgorithmFramework{
  11. //Parameter for Algo with default Values:
  12. /**
  13. * Should be even.
  14. */
  15. private int popsize = 20;
  16. private int maxGenerations = 200;
  17. /**
  18. * The vaporization factor;
  19. */
  20. private double p = 0.3;
  21. private double convergenceFactorReset = 0.99;
  22. private boolean moreInformation = false;
  23. public static void main(String[] args)
  24. {
  25. JFrame newFrame = new JFrame("exampleWindow");
  26. AcoAlgorithm2 instance = new AcoAlgorithm2();
  27. newFrame.setContentPane(instance.getPanel());
  28. newFrame.pack();
  29. newFrame.setVisible(true);
  30. newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  31. }
  32. @Override
  33. protected double evaluateState(DecoratedState actualstate) {
  34. return PSOAlgorithm.getFitnessValueForState(actualstate);
  35. }
  36. @Override
  37. protected int getProgressBarMaxCount() {
  38. return rounds * popsize * maxGenerations;
  39. }
  40. /**
  41. * Algorithm 20 !! Fitness is better when smaller.:
  42. * PseudoCode:
  43. * Best <- actual;
  44. * pheromones = initPheromons();
  45. * for(maxGeneration times){
  46. * population = createSolutionsBiasedBy(pheromones);
  47. * for(each Individual i from population){
  48. * fitness <- evaluatePosition(i);
  49. * if(fitness < best.fitnessValue) Best <- i;
  50. * }
  51. * vaporizeIntensifiePheromons(pheromones);
  52. * }
  53. * @return
  54. */
  55. @Override
  56. protected Individual executeAlgo() {
  57. Individual best = new Individual();
  58. best.position = extractPositionAndAccess();
  59. if(moreInformation )console.println("Bit-Array_length: " + best.position.size());
  60. best.fitness = evaluatePosition(best.position);
  61. List<Double> runList = new ArrayList<Double>();
  62. runList.add(best.fitness);
  63. console.print("Start with: " + best.fitness);
  64. if(moreInformation)console.println("");
  65. int problemSize = best.position.size();
  66. if(problemSize == 0) return best;
  67. List<Double> pheromones = initPheromones(problemSize);
  68. List<Individual> population = new ArrayList<Individual>();
  69. if(moreInformation)console.println("Size To Test:" + population.size());
  70. for(int generation = 0; generation< maxGenerations; generation++) {
  71. population.clear();
  72. population = constructSolutionsBiasedBy(pheromones);
  73. if(moreInformation)console.println("Generation" + generation + " start with Fitness: " + best.fitness);
  74. for(Individual i : population) {
  75. i.fitness = evaluatePosition(i.position);
  76. if(moreInformation)console.println("Fitness" + i.fitness);
  77. if(i.fitness < best.fitness) best = i;
  78. }
  79. runList.add(best.fitness);
  80. if(moreInformation)console.println("________________");
  81. vaporizeIntensifiePheromons(pheromones, best.position, problemSize);
  82. double cf = calculateConvergenceFactor(pheromones, problemSize);
  83. if(moreInformation)console.println("ConvergenceFactor = " + cf);
  84. if(cf > this.convergenceFactorReset) {
  85. pheromones = initPheromones(problemSize);
  86. }
  87. if(cancel)return null;
  88. }
  89. console.println(" End With:" + best.fitness);
  90. db.insertNewRun(runList);
  91. return best;
  92. }
  93. /**
  94. * tj1 is the pheromon level in the j position
  95. * cf is the convergence factor cf e [0;1]
  96. * difference = | tj1 - tj0 | = | tj1 - (1 - tj1) |
  97. *
  98. *
  99. *
  100. * @param pheromones
  101. * @return cf
  102. */
  103. private double calculateConvergenceFactor(List<Double> pheromones,int problemSize) {
  104. double sumOfDifference = pheromones.stream().map(tj1 -> Math.abs(tj1 - (1.0 - tj1))).reduce(0.0, Double::sum);
  105. double cf = sumOfDifference / (double)problemSize;
  106. return cf;
  107. }
  108. /**
  109. * pheromone <- (1-p) * pheromone;
  110. * if(best is true at this position) pheromone <- pheromone + p;
  111. * @param pheromones
  112. * @param position
  113. */
  114. private void vaporizeIntensifiePheromons(List<Double> pheromones, List<Boolean> position, int problemSize) {
  115. ListIterator<Double> iterPheromone = pheromones.listIterator();
  116. ListIterator<Boolean> iterBest = position.listIterator();
  117. for(int i = 0; i < problemSize; i++) {
  118. double pheromone = iterPheromone.next();
  119. boolean bestDecision = iterBest.next();
  120. iterPheromone.set((1.0 - p) * pheromone + (bestDecision?p:0.0));
  121. }
  122. }
  123. /**
  124. *
  125. * @param pheromones
  126. * @return
  127. */
  128. private List<Individual> constructSolutionsBiasedBy(List<Double> pheromones) {
  129. List<Individual> population = new ArrayList<Individual>();
  130. for(int i = 0; i < popsize; i++) {
  131. population.add(constructASolutionBiasedBy(pheromones));
  132. }
  133. return population;
  134. }
  135. /**
  136. * Walks the path with a ant and decide by pheromones if should take true or false;
  137. * A pheromone have a level of 0 < pheromone < 1.
  138. * A Pheromone is equal to the probability.
  139. * @param pheromones
  140. * @return
  141. */
  142. private Individual constructASolutionBiasedBy(List<Double> pheromones) {
  143. Individual result = new Individual();
  144. result.position = new ArrayList<Boolean>();
  145. for(double pheromone : pheromones) {
  146. result.position.add((Random.nextDouble() < pheromone));
  147. }
  148. return result;
  149. }
  150. /**
  151. * Initialize Pheromons with 0.5
  152. */
  153. private List<Double> initPheromones(int problemSize) {
  154. List<Double> result = new ArrayList<Double>();
  155. for(int i = 0; i < problemSize;i++) {
  156. result.add(0.5);
  157. }
  158. return result;
  159. }
  160. }