AcoAlgorithm2.java 5.8 KB

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