SimulationManager.java 7.5 KB

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