SimulationManager.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package holeg.ui.controller;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.LinkedList;
  5. import java.util.ListIterator;
  6. import java.util.Optional;
  7. import java.util.logging.Logger;
  8. import holeg.model.*;
  9. import holeg.model.Edge.EdgeState;
  10. import holeg.ui.model.DecoratedNetwork;
  11. import holeg.ui.model.DecoratedState;
  12. import holeg.ui.model.DecoratedSwitch;
  13. import holeg.ui.model.MinimumModel;
  14. import holeg.ui.model.MinimumNetwork;
  15. import holeg.ui.model.Model;
  16. import holeg.ui.model.VisualRepresentationalState;
  17. import holeg.ui.model.DecoratedSwitch.SwitchState;
  18. import holeg.ui.model.Model.FairnessModel;
  19. /**
  20. * Controller for Simulation.
  21. *
  22. * @author Gruppe14
  23. */
  24. public class SimulationManager {
  25. private static final Logger log = Logger.getLogger(SimulationManager.class.getName());
  26. private Model model;
  27. private int timeStep;
  28. public Optional<DecoratedState> actualDecorState = Optional.empty();
  29. /**
  30. * Constructor.
  31. *
  32. * @param m Model
  33. */
  34. public SimulationManager(Model m) {
  35. log.fine("Construct SimulationManager");
  36. model = m;
  37. }
  38. /**
  39. * calculates the flow of the edges and the supply for objects and consider old
  40. * timesteps for burned cables.
  41. *
  42. * @param timestep current Iteration
  43. * @param updateVisual Determine if the Visuals should also be calculated
  44. */
  45. public void calculateStateForTimeStep(int timestep, boolean updateVisual) {
  46. log.fine("Calculate");
  47. timeStep = timestep;
  48. long start = System.currentTimeMillis();
  49. ArrayList<MinimumNetwork> list = new ArrayList<MinimumNetwork>();
  50. MinimumModel minimumModel = new MinimumModel(model.getCanvas(), model.getEdgesOnCanvas(),
  51. model.getCurrentIteration());
  52. ArrayList<Edge> leftOver = new ArrayList<Edge>();
  53. boolean doAnotherLoop = true;
  54. do {
  55. doAnotherLoop = false;
  56. list = calculateNetworks(minimumModel, timestep, leftOver);
  57. for (MinimumNetwork net : list) {
  58. float energyOnCables = net.getHolonObjectList().stream().map(object -> object.getActualEnergy())
  59. .filter(energy -> energy > 0.0f).reduce(0.0f, (Float::sum));
  60. // find the cable with the energy supplied from his two connected objects are
  61. // the biggest, from all cables that the network give more energy than the
  62. // cablecapacity.
  63. Edge cable = net.getEdgeList().stream()
  64. .filter(aCable -> energyOnCables > aCable.maxCapacity && !aCable.isUnlimitedCapacity())
  65. .max((lhs, rhs) -> Float.compare(lhs.getEnergyFromConneted(), rhs.getEnergyFromConneted()))
  66. .orElse(null);
  67. if (cable != null) {
  68. cable.setState(EdgeState.Burned);
  69. doAnotherLoop = true;
  70. }
  71. }
  72. } while (doAnotherLoop);
  73. ArrayList<DecoratedNetwork> decorNetworks = new ArrayList<DecoratedNetwork>();
  74. FairnessModel actualFairnessModel = model.getFairnessModel();
  75. for (MinimumNetwork net : list) {
  76. decorNetworks.add(new DecoratedNetwork(net, timestep, actualFairnessModel));
  77. }
  78. for (Edge cable : leftOver) {
  79. cable.setActualFlow(0.0f);
  80. }
  81. ArrayList<DecoratedSwitch> listOfDecoratedSwitches = decorateSwitches(minimumModel, timestep);
  82. DecoratedState stateFromThisTimestep = new DecoratedState(decorNetworks, leftOver, listOfDecoratedSwitches,
  83. timestep);
  84. actualDecorState = Optional.of(stateFromThisTimestep);
  85. long end = System.currentTimeMillis();
  86. log.finer("Simulation: " + (end - start) + "ms");
  87. }
  88. /**
  89. * Decorate a switch
  90. *
  91. * @param minModel
  92. * @param iteration
  93. * @return
  94. */
  95. public static ArrayList<DecoratedSwitch> decorateSwitches(MinimumModel minModel, int iteration) {
  96. ArrayList<DecoratedSwitch> aListOfDecoratedSwitches = new ArrayList<DecoratedSwitch>();
  97. for (HolonSwitch hSwitch : minModel.getSwitchList()) {
  98. aListOfDecoratedSwitches.add(
  99. new DecoratedSwitch(hSwitch, hSwitch.getState(iteration) ? SwitchState.Closed : SwitchState.Open));
  100. }
  101. return aListOfDecoratedSwitches;
  102. }
  103. /**
  104. * SubFunction to calculate the Networks from the model.
  105. *
  106. * @param minModel
  107. * @param Iteration
  108. * @param leftOver
  109. * @return
  110. */
  111. ArrayList<MinimumNetwork> calculateNetworks(MinimumModel minModel, int Iteration, ArrayList<Edge> leftOver) {
  112. // Copy minModel ObjectList
  113. ArrayList<HolonObject> holonObjectList = new ArrayList<HolonObject>();
  114. for (HolonObject holonObject : minModel.getHolonObjectList()) {
  115. holonObjectList.add(holonObject);
  116. }
  117. // Copy minModelEdgeList
  118. ArrayList<Edge> edgeList = new ArrayList<Edge>();
  119. for (Edge cable : minModel.getEdgeList()) {
  120. edgeList.add(cable);
  121. }
  122. ArrayList<MinimumNetwork> listOfNetworks = new ArrayList<MinimumNetwork>();
  123. while (!holonObjectList.isEmpty()) {
  124. // lookAt the first holonObject and find his neighbors
  125. HolonObject lookAtObject = holonObjectList.get(0);
  126. // delete out of list
  127. holonObjectList.remove(0);
  128. // create a new Network
  129. MinimumNetwork actualNetwork = new MinimumNetwork(new ArrayList<HolonObject>(), new ArrayList<Edge>());
  130. actualNetwork.getHolonObjectList().add(lookAtObject);
  131. // create List of neighbors
  132. LinkedList<AbstractCanvasObject> neighbors = new LinkedList<AbstractCanvasObject>();
  133. populateListOfNeighbors(edgeList, lookAtObject, actualNetwork, neighbors);
  134. while (!neighbors.isEmpty()) {
  135. AbstractCanvasObject lookAtNeighbor = neighbors.getFirst();
  136. if (lookAtNeighbor instanceof HolonObject hO) {
  137. actualNetwork.getHolonObjectList().add(hO);
  138. holonObjectList.remove(lookAtNeighbor);
  139. } else {
  140. actualNetwork.getNodeAndSwitches().add(lookAtNeighbor);
  141. }
  142. // When HolonSwitch Check if closed
  143. if (!(lookAtNeighbor instanceof HolonSwitch sw) || sw.getState(Iteration)) {
  144. populateListOfNeighbors(edgeList, lookAtNeighbor, actualNetwork, neighbors);
  145. }
  146. neighbors.removeFirst();
  147. }
  148. listOfNetworks.add(actualNetwork);
  149. }
  150. if (leftOver != null) {
  151. leftOver.clear();
  152. for (Edge cable : edgeList) {
  153. leftOver.add(cable);
  154. }
  155. }
  156. return listOfNetworks;
  157. }
  158. /**
  159. * Adds the neighbors.
  160. *
  161. * @param edgeList
  162. * @param lookAtObject
  163. * @param actualNetwork
  164. * @param neighbors
  165. */
  166. void populateListOfNeighbors(ArrayList<Edge> edgeList, AbstractCanvasObject lookAtObject,
  167. MinimumNetwork actualNetwork, LinkedList<AbstractCanvasObject> neighbors) {
  168. ListIterator<Edge> iter = edgeList.listIterator();
  169. while (iter.hasNext()) {
  170. Edge lookAtEdge = iter.next();
  171. if (lookAtEdge.getState() == EdgeState.Working && lookAtEdge.isConnectedTo(lookAtObject)) {
  172. iter.remove();
  173. actualNetwork.getEdgeList().add(lookAtEdge);
  174. // Add neighbar
  175. AbstractCanvasObject edgeNeighbor;
  176. if (lookAtEdge.getA().equals(lookAtObject)) {
  177. edgeNeighbor = lookAtEdge.getB();
  178. } else {
  179. edgeNeighbor = lookAtEdge.getA();
  180. }
  181. if (!neighbors.contains(edgeNeighbor)) {
  182. neighbors.add(edgeNeighbor);
  183. }
  184. }
  185. }
  186. }
  187. public Optional<DecoratedState> getActualDecorState() {
  188. return actualDecorState;
  189. }
  190. public Optional<VisualRepresentationalState> getActualVisualRepresentationalState() {
  191. return Optional.ofNullable(savesVisual.get(timeStep));
  192. }
  193. public Optional<VisualRepresentationalState> getVisualRepresentationalState(int timestep) {
  194. return Optional.ofNullable(savesVisual.get(timestep));
  195. }
  196. }