AcoAlgorithm.java 6.3 KB

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