AcoAlgorithm.java 6.3 KB

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