AcoAlgorithm.java 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package algorithm.binary;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.ListIterator;
  5. import javax.swing.JFrame;
  6. import algorithm.objectiveFunction.ObjectiveFunctionByCarlos;
  7. import api.AlgorithmFrameworkFlex;
  8. import ui.model.DecoratedState;
  9. import utility.StringFormat;
  10. public class AcoAlgorithm extends AlgorithmFrameworkFlex{
  11. //Parameter for Algo with default Values:
  12. /**
  13. * Should be even.
  14. */
  15. private int popsize = 20;
  16. private int maxGenerations = 200;
  17. /**
  18. * The vaporization factor;
  19. */
  20. private double p = 0.3;
  21. private double convergenceFactorReset = 0.99;
  22. private boolean moreInformation = false;
  23. public AcoAlgorithm() {
  24. super();
  25. addIntParameter("Population", 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. public static void main(String[] args)
  32. {
  33. JFrame newFrame = new JFrame("exampleWindow");
  34. AcoAlgorithm instance = new AcoAlgorithm();
  35. newFrame.setContentPane(instance.getPanel());
  36. newFrame.pack();
  37. newFrame.setVisible(true);
  38. newFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  39. }
  40. @Override
  41. protected int getProgressBarMaxCount() {
  42. return rounds * popsize * maxGenerations;
  43. }
  44. /**
  45. * Algorithm 20 !! Fitness is better when smaller.:
  46. * PseudoCode:
  47. * Best <- actual;
  48. * pheromones = initPheromons();
  49. * for(maxGeneration times){
  50. * population = createSolutionsBiasedBy(pheromones);
  51. * for(each Individual i from population){
  52. * fitness <- evaluatePosition(i);
  53. * if(fitness < best.fitnessValue) Best <- i;
  54. * }
  55. * vaporizeIntensifiePheromons(pheromones);
  56. * }
  57. * @return
  58. */
  59. @Override
  60. protected Individual executeAlgo() {
  61. Individual best = new Individual();
  62. best.position = extractPositionAndAccess();
  63. if(moreInformation )console.println("Bit-Array_length: " + best.position.size());
  64. best.fitness = evaluatePosition(best.position);
  65. List<Double> runList = new ArrayList<Double>();
  66. runList.add(best.fitness);
  67. console.print("Start with: " + StringFormat.doubleFixedPlaces(2, best.fitness));
  68. if(moreInformation)console.println("");
  69. int problemSize = best.position.size();
  70. if(problemSize == 0) return best;
  71. List<Double> pheromones = initPheromones(problemSize);
  72. List<Individual> population = new ArrayList<Individual>();
  73. if(moreInformation)console.println("Size To Test:" + population.size());
  74. for(int generation = 0; generation< maxGenerations; generation++) {
  75. population.clear();
  76. population = constructSolutionsBiasedBy(pheromones);
  77. if(moreInformation)console.println("Generation" + generation + " start with Fitness: " + best.fitness);
  78. for(Individual i : population) {
  79. i.fitness = evaluatePosition(i.position);
  80. if(moreInformation)console.println("Fitness" + StringFormat.doubleFixedPlaces(2, i.fitness));
  81. if(i.fitness < best.fitness) best = i;
  82. }
  83. runList.add(best.fitness);
  84. if(moreInformation)console.println("________________");
  85. vaporizeIntensifiePheromons(pheromones, best.position, problemSize);
  86. double cf = calculateConvergenceFactor(pheromones, problemSize);
  87. if(moreInformation)console.println("ConvergenceFactor = " + cf);
  88. if(cf > this.convergenceFactorReset) {
  89. pheromones = initPheromones(problemSize);
  90. }
  91. if(cancel)return null;
  92. }
  93. console.println(" End With:" + StringFormat.doubleFixedPlaces(2, best.fitness));
  94. this.runList = runList;
  95. return best;
  96. }
  97. /**
  98. * tj1 is the pheromon level in the j position
  99. * cf is the convergence factor cf e [0;1]
  100. * difference = | tj1 - tj0 | = | tj1 - (1 - tj1) |
  101. *
  102. *
  103. *
  104. * @param pheromones
  105. * @return cf
  106. */
  107. private double calculateConvergenceFactor(List<Double> pheromones,int problemSize) {
  108. double sumOfDifference = pheromones.stream().map(tj1 -> Math.abs(tj1 - (1.0 - tj1))).reduce(0.0, Double::sum);
  109. double cf = sumOfDifference / (double)problemSize;
  110. return cf;
  111. }
  112. /**
  113. * pheromone <- (1-p) * pheromone;
  114. * if(best is true at this position) pheromone <- pheromone + p;
  115. * @param pheromones
  116. * @param position
  117. */
  118. private void vaporizeIntensifiePheromons(List<Double> pheromones, List<Boolean> position, int problemSize) {
  119. ListIterator<Double> iterPheromone = pheromones.listIterator();
  120. ListIterator<Boolean> iterBest = position.listIterator();
  121. for(int i = 0; i < problemSize; i++) {
  122. double pheromone = iterPheromone.next();
  123. boolean bestDecision = iterBest.next();
  124. iterPheromone.set((1.0 - p) * pheromone + (bestDecision?p:0.0));
  125. }
  126. }
  127. /**
  128. *
  129. * @param pheromones
  130. * @return
  131. */
  132. private List<Individual> constructSolutionsBiasedBy(List<Double> pheromones) {
  133. List<Individual> population = new ArrayList<Individual>();
  134. for(int i = 0; i < popsize; i++) {
  135. population.add(constructASolutionBiasedBy(pheromones));
  136. }
  137. return population;
  138. }
  139. /**
  140. * Walks the path with a ant and decide by pheromones if should take true or false;
  141. * A pheromone have a level of 0 < pheromone < 1.
  142. * A Pheromone is equal to the probability.
  143. * @param pheromones
  144. * @return
  145. */
  146. private Individual constructASolutionBiasedBy(List<Double> pheromones) {
  147. Individual result = new Individual();
  148. result.position = new ArrayList<Boolean>();
  149. for(double pheromone : pheromones) {
  150. result.position.add((Random.nextDouble() < pheromone));
  151. }
  152. return result;
  153. }
  154. /**
  155. * Initialize Pheromons with 0.5
  156. */
  157. private List<Double> initPheromones(int problemSize) {
  158. List<Double> result = new ArrayList<Double>();
  159. for(int i = 0; i < problemSize;i++) {
  160. result.add(0.5);
  161. }
  162. return result;
  163. }
  164. @Override
  165. protected String algoInformationToPrint() {
  166. // private int popsize = 20;
  167. // private int maxGenerations = 200;
  168. // private double p = 0.3;
  169. // private double convergenceFactorReset = 0.99;
  170. return "AcoAlgo"
  171. + " Rounds: " + rounds
  172. + " Iterations: " + maxGenerations
  173. + " Individuals: " + popsize
  174. + " vaporization: " + StringFormat.doubleAllPlaces(p)
  175. + " convergenceFactorReset: " + StringFormat.doubleAllPlaces(convergenceFactorReset);
  176. }
  177. @Override
  178. protected String plottFileName() {
  179. return "plottAcoAlgo.txt";
  180. }
  181. }