AcoAlgorithm.java 6.3 KB

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