AcoAlgorithm.java 6.4 KB

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