AcoAlgorithm.java 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 "GA for topologie generation";
  82. }
  83. @Override
  84. protected String plottFileName() {
  85. return "ga-topologie.txt";
  86. }
  87. /**
  88. * tj1 is the pheromon level in the j position
  89. * cf is the convergence factor cf e [0;1]
  90. *
  91. *
  92. *
  93. * @param pheromones
  94. * @return cf
  95. */
  96. private double calculateConvergenceFactor(List<List<Double>> pheromones,int problemSize) {
  97. double sumofmax = pheromones.stream().map(listPheromons -> listPheromons.stream().max((a,b) -> Double.compare(a,b)).get()).reduce(0.0, Double::sum);
  98. double cf = sumofmax / (double)problemSize;
  99. return cf;
  100. }
  101. /**
  102. * pheromone <- (1-p) * pheromone;
  103. * if(best is true at this position) pheromone <- pheromone + p;
  104. * @param pheromones
  105. * @param position
  106. */
  107. private void vaporizeIntensifiePheromons(List<List<Double>> pheromones, List<Integer> position, int problemSize) {
  108. ListIterator<List<Double>> iterPheromone = pheromones.listIterator();
  109. ListIterator<Integer> iterBest = position.listIterator();
  110. for(int i = 0; i < problemSize; i++) {
  111. List<Double> tauList = iterPheromone.next();
  112. int bestDecision = iterBest.next();
  113. ListIterator<Double> tauListiter = tauList.listIterator();
  114. for(int k = 0; tauListiter.hasNext(); k++) {
  115. double value = tauListiter.next();
  116. tauListiter.set((1.0 - p) * value + (k == bestDecision?p:0.0));
  117. }
  118. }
  119. }
  120. /**
  121. *
  122. * @param pheromones
  123. * @return
  124. */
  125. private List<Individual> constructSolutionsBiasedBy(List<List<Double>> pheromones) {
  126. List<Individual> population = new ArrayList<Individual>();
  127. for(int i = 0; i < popsize; i++) {
  128. population.add(constructASolutionBiasedBy(pheromones));
  129. }
  130. return population;
  131. }
  132. /**
  133. * Walks the path with a ant and decide by pheromones if should take true or false;
  134. * A pheromone have a level of 0 < pheromone < 1.
  135. * A Pheromone is equal to the probability.
  136. * @param pheromones
  137. * @return
  138. */
  139. private Individual constructASolutionBiasedBy(List<List<Double>> pheromones) {
  140. Individual result = new Individual();
  141. result.position = new ArrayList<Integer>();
  142. for(List<Double> pheromoneList : pheromones) {
  143. ListIterator<Double> tauListiter = pheromoneList.listIterator();
  144. double radnomValue = Random.nextDouble();
  145. for(int i = 0;tauListiter.hasNext(); i++) {
  146. double actualtau = tauListiter.next();
  147. if(radnomValue > actualtau) {
  148. radnomValue -= actualtau;
  149. }else {
  150. result.position.add(i);
  151. break;
  152. }
  153. }
  154. }
  155. return result;
  156. }
  157. /**
  158. * Initialize Pheromons with 1.0 / maxIndex;
  159. */
  160. private List<List<Double>> initPheromones(int problemSize) {
  161. List<List<Double>> result = new ArrayList<List<Double>>();
  162. for(int i = 0; i < problemSize;i++) {
  163. //generate list equal tau values with max Int
  164. int maxIndex = this.getMaximumIndexObjects(i);
  165. double tauValue = 1.0 / (double) (maxIndex + 1);
  166. List<Double> tauList = new ArrayList<Double>();
  167. for(int tau= 0; tau < maxIndex + 1; tau++) {
  168. tauList.add(tauValue);
  169. }
  170. result.add(tauList);
  171. }
  172. return result;
  173. }
  174. }