AcoAlgorithm.java 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package algorithm.topologie;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.ListIterator;
  5. import java.util.TreeSet;
  6. import java.util.stream.Collectors;
  7. import algorithm.objectiveFunction.ObjectiveFunctionByCarlos;
  8. import algorithm.objectiveFunction.TopologieObjectiveFunction;
  9. import api.TopologieAlgorithmFramework;
  10. import api.AlgorithmFrameworkFlex.Individual;
  11. import ui.model.DecoratedState;
  12. public class AcoAlgorithm extends TopologieAlgorithmFramework {
  13. private int popsize = 20;
  14. private int maxGenerations = 100;
  15. private boolean moreInformation = false;
  16. /**
  17. * The vaporization factor;
  18. */
  19. private double p = 0.05;
  20. private double convergenceFactorReset = 0.90;
  21. public AcoAlgorithm(){
  22. addIntParameter("popsize", popsize, intValue -> popsize = intValue, () -> popsize, 1);
  23. addIntParameter("maxGenerations", maxGenerations, intValue -> maxGenerations = intValue, () -> maxGenerations, 1);
  24. addDoubleParameter("Vaporization", p, doubleValue -> p = doubleValue, () -> p, 0.0, 1.0);
  25. addDoubleParameter("FactorReset", convergenceFactorReset, doubleValue -> convergenceFactorReset = doubleValue, () -> convergenceFactorReset, 0.0, 1.0);
  26. addBooleanParameter("moreInformation", moreInformation, booleanValue -> moreInformation = booleanValue);
  27. }
  28. @Override
  29. protected double evaluateState(DecoratedState actualstate) {
  30. return TopologieObjectiveFunction.getFitnessValueForState(actualstate);
  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 "GA for topologie generation";
  76. }
  77. @Override
  78. protected String plottFileName() {
  79. return "ga-topologie.txt";
  80. }
  81. /**
  82. * tj1 is the pheromon level in the j position
  83. * cf is the convergence factor cf e [0;1]
  84. *
  85. *
  86. *
  87. * @param pheromones
  88. * @return cf
  89. */
  90. private double calculateConvergenceFactor(List<List<Double>> pheromones,int problemSize) {
  91. double sumofmax = pheromones.stream().map(listPheromons -> listPheromons.stream().max((a,b) -> Double.compare(a,b)).get()).reduce(0.0, Double::sum);
  92. double cf = sumofmax / (double)problemSize;
  93. return cf;
  94. }
  95. /**
  96. * pheromone <- (1-p) * pheromone;
  97. * if(best is true at this position) pheromone <- pheromone + p;
  98. * @param pheromones
  99. * @param position
  100. */
  101. private void vaporizeIntensifiePheromons(List<List<Double>> pheromones, List<Integer> position, int problemSize) {
  102. ListIterator<List<Double>> iterPheromone = pheromones.listIterator();
  103. ListIterator<Integer> iterBest = position.listIterator();
  104. for(int i = 0; i < problemSize; i++) {
  105. List<Double> tauList = iterPheromone.next();
  106. int bestDecision = iterBest.next();
  107. ListIterator<Double> tauListiter = tauList.listIterator();
  108. for(int k = 0; tauListiter.hasNext(); k++) {
  109. double value = tauListiter.next();
  110. tauListiter.set((1.0 - p) * value + (k == bestDecision?p:0.0));
  111. }
  112. }
  113. }
  114. /**
  115. *
  116. * @param pheromones
  117. * @return
  118. */
  119. private List<Individual> constructSolutionsBiasedBy(List<List<Double>> pheromones) {
  120. List<Individual> population = new ArrayList<Individual>();
  121. for(int i = 0; i < popsize; i++) {
  122. population.add(constructASolutionBiasedBy(pheromones));
  123. }
  124. return population;
  125. }
  126. /**
  127. * Walks the path with a ant and decide by pheromones if should take true or false;
  128. * A pheromone have a level of 0 < pheromone < 1.
  129. * A Pheromone is equal to the probability.
  130. * @param pheromones
  131. * @return
  132. */
  133. private Individual constructASolutionBiasedBy(List<List<Double>> pheromones) {
  134. Individual result = new Individual();
  135. result.position = new ArrayList<Integer>();
  136. for(List<Double> pheromoneList : pheromones) {
  137. ListIterator<Double> tauListiter = pheromoneList.listIterator();
  138. double radnomValue = Random.nextDouble();
  139. for(int i = 0;tauListiter.hasNext(); i++) {
  140. double actualtau = tauListiter.next();
  141. if(radnomValue > actualtau) {
  142. radnomValue -= actualtau;
  143. }else {
  144. result.position.add(i);
  145. break;
  146. }
  147. }
  148. }
  149. return result;
  150. }
  151. /**
  152. * Initialize Pheromons with 1.0 / maxIndex;
  153. */
  154. private List<List<Double>> initPheromones(int problemSize) {
  155. List<List<Double>> result = new ArrayList<List<Double>>();
  156. for(int i = 0; i < problemSize;i++) {
  157. //generate list equal tau values with max Int
  158. int maxIndex = this.getMaximumIndexObjects(i);
  159. double tauValue = 1.0 / (double) (maxIndex + 1);
  160. List<Double> tauList = new ArrayList<Double>();
  161. for(int tau= 0; tau < maxIndex + 1; tau++) {
  162. tauList.add(tauValue);
  163. }
  164. result.add(tauList);
  165. }
  166. return result;
  167. }
  168. }