AcoAlgorithm.java 6.6 KB

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