GreedySinglePass.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package algorithm.binary;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.concurrent.atomic.AtomicInteger;
  7. import java.util.stream.Collectors;
  8. import java.util.stream.Stream;
  9. import api.AlgorithmFrameworkFlex;
  10. import classes.AbstractCanvasObject;
  11. import classes.Flexibility;
  12. import classes.GroupNode;
  13. import classes.HolonElement;
  14. import classes.HolonObject;
  15. import classes.HolonSwitch;
  16. import ui.model.Model;
  17. import utility.StringFormat;
  18. import utility.Tuple;
  19. public class GreedySinglePass extends AlgorithmFrameworkFlex {
  20. private int problemSize = 0;
  21. private boolean moreInformation = true;
  22. /**
  23. * Method to get the current Position alias a ListOf Booleans for aktive
  24. * settings on the Objects on the Canvas. Also initialize the Access Hashmap to
  25. * swap faster positions.
  26. *
  27. * @param model
  28. * @return
  29. */
  30. protected List<Integer> generateRandomLayerIndexList() {
  31. Model model = control.getModel();
  32. List<AbstractCanvasObject> nodes = (dGroupNode != null) ? dGroupNode.getModel().getNodes()
  33. : model.getObjectsOnCanvas();
  34. List<Tuple<AbstractCanvasObject, Integer>> extractedObjects = new ArrayList<>();
  35. rollOut(nodes.stream(), extractedObjects, 0);
  36. // Group With Layer
  37. Map<Integer, List<AbstractCanvasObject>> sortetObjects = extractedObjects.stream().collect(
  38. Collectors.groupingBy(Tuple::getSecond, Collectors.mapping(Tuple::getFirst, Collectors.toList())));
  39. AtomicInteger count = new AtomicInteger(0);
  40. List<Integer> indexList = new ArrayList<>();
  41. //Generate Access and shuffle layer
  42. sortetObjects.entrySet().stream()
  43. //ReverseOrder
  44. .sorted((layer0, layer1) -> -Integer.compare(layer0.getKey(), layer1.getKey()))
  45. //radomizeIndexes
  46. .forEach(entry -> {
  47. List<Integer> indexesInLayer = entry.getValue().stream().map(node -> count.getAndIncrement()).collect(Collectors.toList());
  48. //Randomize Layer
  49. Collections.shuffle(indexesInLayer);
  50. indexList.addAll(indexesInLayer);
  51. });
  52. //System.out.println(indexList.stream().map(Object::toString).collect(Collectors.joining(", ")));
  53. return indexList;
  54. }
  55. protected void rollOut(Stream<AbstractCanvasObject> nodes, List<Tuple<AbstractCanvasObject, Integer>> objects,
  56. int layer) {
  57. nodes.forEach(node -> {
  58. if (node instanceof HolonObject) {
  59. HolonObject hObject = (HolonObject) node;
  60. if (this.algoUseKillSwitch) {
  61. objects.add(new Tuple<>(node, layer));
  62. }
  63. if (this.algoUseElements) {
  64. for (@SuppressWarnings("unused") HolonElement hE : hObject.getElements()) {
  65. objects.add(new Tuple<>(node, layer));
  66. }
  67. }
  68. if (this.algoUseFlexes) {
  69. for (HolonElement hE : hObject.getElements()) {
  70. for(Flexibility flex : hE.flexList) {
  71. switch(control.getSimManager().getActualFlexManager().getFlexWrapperFromFlexibility(flex).getState()) {
  72. case IN_USE:
  73. case ON_COOLDOWN:
  74. objects.add(new Tuple<>(node, layer));
  75. break;
  76. case OFFERED:
  77. objects.add(new Tuple<>(node, layer));
  78. break;
  79. case NOT_OFFERED:
  80. case UNAVAILABLE:
  81. default:
  82. break;
  83. }
  84. }
  85. }
  86. }
  87. } else if (node instanceof HolonSwitch && algoUseSwitches) {
  88. objects.add(new Tuple<>(node, layer));
  89. }else if (node instanceof GroupNode ) {
  90. GroupNode groupNode = (GroupNode) node;
  91. rollOut(groupNode.getNodes().stream(), objects, layer +1);
  92. }
  93. });
  94. }
  95. @Override
  96. protected Individual executeAlgo() {
  97. Individual best = new Individual();
  98. best.position = extractPositionAndAccess();
  99. println("Bit-Array_length: " + best.position.size());
  100. best.fitness = evaluatePosition(best.position);
  101. List<Double> runList = new ArrayList<Double>();
  102. runList.add(best.fitness);
  103. println("Start with: " + StringFormat.doubleFixedPlaces(2, best.fitness));
  104. problemSize = best.position.size();
  105. Individual bestFound = new Individual(best);
  106. bestFound.fitness = Float.MAX_VALUE;
  107. Individual start = new Individual(best);
  108. List<Integer> radomizedForEachLayerIndexList = generateRandomLayerIndexList();
  109. for (Integer index : radomizedForEachLayerIndexList) {
  110. boolean actualValue = start.position.get(index);
  111. start.position.set(index, !actualValue);
  112. double fitness = evaluatePosition(start.position);
  113. boolean kicked = fitness < bestFound.fitness;
  114. if (kicked) {
  115. bestFound = new Individual(start);
  116. bestFound.fitness = fitness;
  117. } else {
  118. start.position.set(index, actualValue);
  119. }
  120. if (bestFound.fitness < best.fitness) {
  121. best = bestFound;
  122. }
  123. if (cancel)
  124. return null;
  125. println("Fitness: " + fitness + "\tFlippedIndex = " + index + "(" + problemSize + ")" + kicked);
  126. }
  127. return best;
  128. }
  129. protected void println(String value) {
  130. if (moreInformation)
  131. console.println(value);
  132. }
  133. @Override
  134. protected int getProgressBarMaxCount() {
  135. return (problemSize * (problemSize + 1)) / 2;
  136. }
  137. @Override
  138. protected String algoInformationToPrint() {
  139. return "NoParameter";
  140. }
  141. @Override
  142. protected String plottFileName() {
  143. return "plottGreedy.txt";
  144. }
  145. }