GreedyAlgorithm.java 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package algorithm.binary;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import api.AlgorithmFrameworkFlex;
  5. import utility.StringFormat;
  6. public class GreedyAlgorithm extends AlgorithmFrameworkFlex{
  7. private int problemSize = 0;
  8. private boolean moreInformation = false;
  9. public GreedyAlgorithm() {
  10. super();
  11. addBooleanParameter("More Information", moreInformation, booleanValue -> moreInformation = booleanValue);
  12. }
  13. @Override
  14. protected Individual executeAlgo() {
  15. Individual best = new Individual();
  16. best.position = extractPositionAndAccess();
  17. println("Bit-Array_length: " + best.position.size());
  18. best.fitness = evaluatePosition(best.position);
  19. List<Double> runList = new ArrayList<Double>();
  20. runList.add(best.fitness);
  21. println("Start with: " + StringFormat.doubleFixedPlaces(2, best.fitness));
  22. problemSize = best.position.size();
  23. List<Integer> indexList = new ArrayList<Integer>();
  24. for(int index = 0; index < problemSize; index++)
  25. {
  26. indexList.add(index);
  27. }
  28. Individual bestAfterRound = new Individual(best);
  29. for(int round = 0; round < problemSize; round++)
  30. {
  31. Individual startInThisRound = new Individual(bestAfterRound);
  32. Individual bestInThisRound = new Individual();
  33. int indexToRemove = -1;
  34. bestInThisRound.fitness = Float.MAX_VALUE;
  35. for(Integer i : indexList) {
  36. boolean actualValue = startInThisRound.position.get(i);
  37. startInThisRound.position.set(i, !actualValue);
  38. double fitness = evaluatePosition(startInThisRound.position);
  39. if(fitness < bestInThisRound.fitness) {
  40. bestInThisRound = new Individual(startInThisRound);
  41. bestInThisRound.fitness = fitness;
  42. indexToRemove = i;
  43. }
  44. startInThisRound.position.set(i, actualValue);
  45. }
  46. if(cancel) return null;
  47. bestAfterRound = bestInThisRound;
  48. println("Fitness: " + bestInThisRound.fitness + "FlippedIndex = " + indexToRemove + "(" +indexList.size() + ")");
  49. if(bestAfterRound.fitness < best.fitness) {
  50. best = bestAfterRound;
  51. }
  52. runList.add(best.fitness);
  53. indexList.remove(Integer.valueOf(indexToRemove));
  54. }
  55. this.runList = runList;
  56. console.println("Fitness: " + best.fitness);
  57. return best;
  58. }
  59. protected void println(String value) {
  60. if(moreInformation)console.println(value);
  61. }
  62. @Override
  63. protected int getProgressBarMaxCount() {
  64. List<Boolean> best = extractPositionAndAccess();
  65. problemSize = best.size();
  66. return ((problemSize * (problemSize + 1)) / 2) * rounds;
  67. }
  68. @Override
  69. protected String algoInformationToPrint() {
  70. return "NoParameter";
  71. }
  72. @Override
  73. protected String plottFileName() {
  74. return "plottGreedy.txt";
  75. }
  76. }