AcoAlgorithm2.java 5.8 KB

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