AcoAlgorithm.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package algorithm.topologie;
  2. import java.util.ArrayList;
  3. import java.util.LinkedList;
  4. import java.util.List;
  5. import java.util.ListIterator;
  6. import algorithm.objectiveFunction.TopologieObjectiveFunction;
  7. import api.TopologieAlgorithmFramework;
  8. import ui.model.DecoratedState;
  9. import utility.Random;
  10. public class AcoAlgorithm extends TopologieAlgorithmFramework {
  11. private int popsize = 20;
  12. private int maxGenerations = 100;
  13. private boolean moreInformation = false;
  14. /**
  15. * The vaporization factor;
  16. */
  17. private double p = 0.05;
  18. private double convergenceFactorReset = 0.90;
  19. public AcoAlgorithm(){
  20. addIntParameter("popsize", popsize, intValue -> popsize = intValue, () -> popsize, 1);
  21. addIntParameter("maxGenerations", maxGenerations, intValue -> maxGenerations = intValue, () -> maxGenerations, 1);
  22. addSeperator();
  23. addDoubleParameter("Vaporization", p, doubleValue -> p = doubleValue, () -> p, true, 0.0, 1.0);
  24. addDoubleParameter("FactorReset", convergenceFactorReset, doubleValue -> convergenceFactorReset = doubleValue, () -> convergenceFactorReset, true, 0.0, 1.0);
  25. addSeperator();
  26. addBooleanParameter("moreInformation", moreInformation, booleanValue -> moreInformation = booleanValue, new LinkedList<String>(), new LinkedList<String>());
  27. }
  28. @Override
  29. protected double evaluateState(DecoratedState actualstate, int amountOfAddedSwitch, double addedCableMeters, boolean moreInformation) {
  30. return TopologieObjectiveFunction.getFitnessValueForState(actualstate, amountOfAddedSwitch, addedCableMeters, moreInformation);
  31. }
  32. @Override
  33. protected Individual executeAlgo() {
  34. resetWildcards();
  35. Individual best = new Individual();
  36. best.position = extractPositionAndAccess();
  37. int problemSize = best.position.size();
  38. best.fitness = evaluatePosition(best.position);
  39. List<Double> runList = new ArrayList<Double>();
  40. runList.add(best.fitness);
  41. console.println("Integer_Array_length: " + best.position.size());
  42. List<List<Double>> pheromones = initPheromones(problemSize);
  43. List<Individual> population = new ArrayList<Individual>();
  44. if(moreInformation)console.println("Size To Test:" + population.size());
  45. for(int generation = 0; generation< maxGenerations; generation++) {
  46. population.clear();
  47. population = constructSolutionsBiasedBy(pheromones);
  48. if(moreInformation)console.println("Generation" + generation + " start with Fitness: " + best.fitness);
  49. for(Individual i : population) {
  50. i.fitness = evaluatePosition(i.position);
  51. if(moreInformation)console.println("Fitness" + i.fitness);
  52. if(i.fitness < best.fitness) best = i;
  53. }
  54. runList.add(best.fitness);
  55. if(moreInformation)console.println("________________");
  56. vaporizeIntensifiePheromons(pheromones, best.position, problemSize);
  57. double cf = calculateConvergenceFactor(pheromones, problemSize);
  58. if(moreInformation)console.println("ConvergenceFactor = " + cf);
  59. if(moreInformation)console.println("pheromones:" + pheromones);
  60. if(cf > this.convergenceFactorReset) {
  61. pheromones = initPheromones(problemSize);
  62. }
  63. if(cancel)return null;
  64. }
  65. console.println(" End with:" + best.fitness);
  66. this.runList = runList;
  67. return best;
  68. }
  69. @Override
  70. protected int getProgressBarMaxCount() {
  71. return rounds * maxGenerations * popsize + 1;
  72. }
  73. @Override
  74. protected String algoInformationToPrint() {
  75. return "Aco for Topologie "+ " Rounds:" + rounds
  76. + " maxGenerations:" + maxGenerations
  77. + " popsize:" + popsize
  78. + " vaporization:" + p
  79. + " convergenceFactorReset:" + convergenceFactorReset;
  80. }
  81. @Override
  82. protected String plottFileName() {
  83. return "aco-topologie.txt";
  84. }
  85. /**
  86. * tj1 is the pheromon level in the j position
  87. * cf is the convergence factor cf e [0;1]
  88. *
  89. *
  90. *
  91. * @param pheromones
  92. * @return cf
  93. */
  94. private double calculateConvergenceFactor(List<List<Double>> pheromones,int problemSize) {
  95. double sumofmax = pheromones.stream().map(listPheromons -> listPheromons.stream().max((a,b) -> Double.compare(a,b)).get()).reduce(0.0, Double::sum);
  96. double cf = sumofmax / (double)problemSize;
  97. return cf;
  98. }
  99. /**
  100. * pheromone <- (1-p) * pheromone;
  101. * if(best is true at this position) pheromone <- pheromone + p;
  102. * @param pheromones
  103. * @param position
  104. */
  105. private void vaporizeIntensifiePheromons(List<List<Double>> pheromones, List<Integer> position, int problemSize) {
  106. ListIterator<List<Double>> iterPheromone = pheromones.listIterator();
  107. ListIterator<Integer> iterBest = position.listIterator();
  108. for(int i = 0; i < problemSize; i++) {
  109. List<Double> tauList = iterPheromone.next();
  110. int bestDecision = iterBest.next();
  111. ListIterator<Double> tauListiter = tauList.listIterator();
  112. for(int k = 0; tauListiter.hasNext(); k++) {
  113. double value = tauListiter.next();
  114. tauListiter.set((1.0 - p) * value + (k == bestDecision?p:0.0));
  115. }
  116. }
  117. }
  118. /**
  119. *
  120. * @param pheromones
  121. * @return
  122. */
  123. private List<Individual> constructSolutionsBiasedBy(List<List<Double>> pheromones) {
  124. List<Individual> population = new ArrayList<Individual>();
  125. for(int i = 0; i < popsize; i++) {
  126. population.add(constructASolutionBiasedBy(pheromones));
  127. }
  128. return population;
  129. }
  130. /**
  131. * Walks the path with a ant and decide by pheromones if should take true or false;
  132. * A pheromone have a level of 0 < pheromone < 1.
  133. * A Pheromone is equal to the probability.
  134. * @param pheromones
  135. * @return
  136. */
  137. private Individual constructASolutionBiasedBy(List<List<Double>> pheromones) {
  138. Individual result = new Individual();
  139. result.position = new ArrayList<Integer>();
  140. for(List<Double> pheromoneList : pheromones) {
  141. ListIterator<Double> tauListiter = pheromoneList.listIterator();
  142. double radnomValue = Random.nextDouble();
  143. for(int i = 0;tauListiter.hasNext(); i++) {
  144. double actualtau = tauListiter.next();
  145. if(radnomValue > actualtau) {
  146. radnomValue -= actualtau;
  147. }else {
  148. result.position.add(i);
  149. break;
  150. }
  151. }
  152. }
  153. return result;
  154. }
  155. /**
  156. * Initialize Pheromons with 1.0 / maxIndex;
  157. */
  158. private List<List<Double>> initPheromones(int problemSize) {
  159. List<List<Double>> result = new ArrayList<List<Double>>();
  160. for(int i = 0; i < problemSize;i++) {
  161. //generate list equal tau values with max Int
  162. int maxIndex = this.getMaximumIndexObjects(i);
  163. double tauValue = 1.0 / (double) (maxIndex + 1);
  164. List<Double> tauList = new ArrayList<Double>();
  165. for(int tau= 0; tau < maxIndex + 1; tau++) {
  166. tauList.add(tauValue);
  167. }
  168. result.add(tauList);
  169. }
  170. return result;
  171. }
  172. }