GreedySinglePass.java 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package algorithm.binary;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import api.AlgorithmFrameworkFlex;
  5. import api.AlgorithmFrameworkFlex.Individual;
  6. import utility.StringFormat;
  7. public class GreedySinglePass extends AlgorithmFrameworkFlex{
  8. private int problemSize = 0;
  9. private boolean moreInformation = true;
  10. @Override
  11. protected Individual executeAlgo() {
  12. Individual best = new Individual();
  13. best.position = extractPositionAndAccess();
  14. println("Bit-Array_length: " + best.position.size());
  15. best.fitness = evaluatePosition(best.position);
  16. List<Double> runList = new ArrayList<Double>();
  17. runList.add(best.fitness);
  18. println("Start with: " + StringFormat.doubleFixedPlaces(2, best.fitness));
  19. problemSize = best.position.size();
  20. Individual bestFound = new Individual(best);
  21. bestFound.fitness = Float.MAX_VALUE;
  22. Individual start = new Individual(best);
  23. for(int index = 0; index < problemSize; index++)
  24. {
  25. boolean actualValue = start.position.get(index);
  26. start.position.set(index, !actualValue);
  27. double fitness = evaluatePosition(start.position);
  28. if(fitness < bestFound.fitness) {
  29. bestFound = new Individual(start);
  30. bestFound.fitness = fitness;
  31. }
  32. if(bestFound.fitness < best.fitness) {
  33. best = bestFound;
  34. }
  35. if(cancel) return null;
  36. println("Fitness: " + fitness + "FlippedIndex = " + index + "(" + problemSize + ")");
  37. }
  38. return best;
  39. }
  40. protected void println(String value) {
  41. if(moreInformation)console.println(value);
  42. }
  43. @Override
  44. protected int getProgressBarMaxCount() {
  45. return (problemSize * (problemSize + 1)) / 2;
  46. }
  47. @Override
  48. protected String algoInformationToPrint() {
  49. return "NoParameter";
  50. }
  51. @Override
  52. protected String plottFileName() {
  53. return "plottGreedy.txt";
  54. }
  55. }