SimulationManager.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package ui.controller;
  2. import classes.*;
  3. import classes.comparator.EnergyMinToMaxComparator;
  4. import classes.comparator.MinEnergyComparator;
  5. import classes.comparator.WeakestBattery;
  6. import holeg.HolegPowerFlowContext;
  7. import ui.model.IntermediateCableWithState;
  8. import ui.controller.FlexManager.FlexState;
  9. import ui.model.DecoratedCable;
  10. import ui.model.DecoratedCable.CableState;
  11. import ui.model.DecoratedSwitch.SwitchState;
  12. import ui.model.DecoratedNetwork;
  13. import ui.model.DecoratedState;
  14. import ui.model.DecoratedSwitch;
  15. import ui.model.MinimumModel;
  16. import ui.model.MinimumNetwork;
  17. import ui.model.Model;
  18. import ui.model.Model.FairnessModel;
  19. import ui.model.VisualRepresentationalState;
  20. import ui.view.FlexiblePane;
  21. import ui.view.GUI;
  22. import ui.view.MyCanvas;
  23. import ui.view.Outliner;
  24. import java.util.ArrayList;
  25. import java.util.Collections;
  26. import java.util.HashMap;
  27. import java.util.LinkedList;
  28. import java.util.List;
  29. import java.util.ListIterator;
  30. import javax.swing.JPanel;
  31. /**
  32. * Controller for Simulation.
  33. *
  34. * @author Gruppe14
  35. */
  36. public class SimulationManager {
  37. int global = 0;
  38. private Model model;
  39. private HashMap<Integer, DecoratedState> saves = new HashMap<Integer, DecoratedState>();
  40. private HashMap<Integer, VisualRepresentationalState> savesVisual = new HashMap<Integer, VisualRepresentationalState>();
  41. private HashMap<Integer, FlexManager> savesFlexManger = new HashMap<Integer, FlexManager>();
  42. private List<Flexibility> actualOrders = new ArrayList<Flexibility>();
  43. private int timeStep;
  44. private FlexiblePane flexPane;
  45. private HolegPowerFlowContext holegPowerFlowContext = new HolegPowerFlowContext();
  46. /**
  47. * Constructor.
  48. *
  49. * @param m
  50. * Model
  51. */
  52. public SimulationManager(Model m) {
  53. model = m;
  54. }
  55. /**
  56. * calculates the flow of the edges and the supply for objects and consider old timesteps for burned cables.
  57. *
  58. * @param timestep
  59. * current Iteration
  60. * @param updateVisual TODO
  61. */
  62. public void calculateStateForTimeStep(int timestep, boolean updateVisual) {
  63. boolean doesFlexManagerExist = savesFlexManger.containsKey(timestep);
  64. boolean createNew = updateVisual || !doesFlexManagerExist;
  65. FlexManager newFlexManager;
  66. if(createNew) {
  67. newFlexManager = new FlexManager(model, timestep, savesFlexManger.get(timestep-1));
  68. if(doesFlexManagerExist) newFlexManager.orderFlexFromList(savesFlexManger.get(timestep).getAllFlexesOrderedThisTimeStep());
  69. savesFlexManger.put(timestep, newFlexManager);
  70. }else {
  71. newFlexManager = savesFlexManger.get(timestep);
  72. }
  73. HashMap<Edge, CableState> map = new HashMap<Edge, CableState>();
  74. if(timestep > 0 && saves.containsKey(timestep-1)) //if the state before exist
  75. {
  76. //make cable hastmap
  77. DecoratedState theStateBefore = saves.get(timestep-1);
  78. //edges without HolonObjects or burned
  79. for(DecoratedCable edge : theStateBefore.getLeftOverEdges())
  80. {
  81. map.put(edge.getModel(), edge.getState());
  82. }
  83. }
  84. timeStep = timestep;
  85. ArrayList<MinimumNetwork> list = new ArrayList<MinimumNetwork>();
  86. MinimumModel minimumModel = new MinimumModel(model.getObjectsOnCanvas(), model.getEdgesOnCanvas());
  87. //set all BreakedManuel Cable Burned:
  88. for(IntermediateCableWithState cable : minimumModel.getEdgeList()) {
  89. if(cable.getModel().isBreakedManuel()) cable.setState(CableState.Burned);
  90. }
  91. //set all the state before:
  92. for(IntermediateCableWithState cable : minimumModel.getEdgeList()) {
  93. if(map.containsKey(cable.getModel())) cable.setState(map.get(cable.getModel()));
  94. }
  95. ArrayList<IntermediateCableWithState> leftOver = new ArrayList<IntermediateCableWithState>();
  96. boolean doAnotherLoop = true;
  97. while(doAnotherLoop) {
  98. doAnotherLoop = false;
  99. list = calculateNetworks(minimumModel, timestep, leftOver);
  100. for(MinimumNetwork net : list) {
  101. float energyOnCables = net.getHolonObjectList().stream().filter(object -> object.getEnergyAtTimeStepWithFlex(timestep, newFlexManager) > 0.0f).map(object -> object.getEnergyAtTimeStepWithFlex(timestep, newFlexManager)).reduce(0.0f, ((a,b) -> a + b));
  102. //find the cable with the energy supplied from his two connected objects are the biggest, from all cables that the network give more energy than the cablecapacity.
  103. IntermediateCableWithState cable = net.getEdgeList().stream().filter(aCable -> energyOnCables > aCable.getModel().getCapacity() && !aCable.getModel().isUnlimitedCapacity()).max((lhs,rhs) -> Float.compare(lhs.getEnergyFromConnetedAtTimestep(timestep, newFlexManager), rhs.getEnergyFromConnetedAtTimestep(timestep, newFlexManager))).orElse(null);
  104. if(cable != null) {
  105. cable.setState(CableState.Burned);
  106. doAnotherLoop = true;
  107. }
  108. }
  109. }
  110. ArrayList<DecoratedNetwork> decorNetworks = new ArrayList<DecoratedNetwork>();
  111. FairnessModel actualFairnessModel = model.getFairnessModel();
  112. for (MinimumNetwork net : list) {
  113. decorNetworks.add(new DecoratedNetwork(holegPowerFlowContext, net, timestep, actualFairnessModel, newFlexManager));
  114. }
  115. ArrayList<DecoratedCable> leftOverDecoratedCables = new ArrayList<DecoratedCable>();
  116. for(IntermediateCableWithState cable: leftOver) {
  117. leftOverDecoratedCables.add(new DecoratedCable(cable.getModel(), cable.getState(), 0, 0));
  118. }
  119. ArrayList<DecoratedSwitch> listOfDecoratedSwitches = decorateSwitches(minimumModel, timestep);
  120. DecoratedState stateFromThisTimestep = new DecoratedState(decorNetworks, leftOverDecoratedCables, listOfDecoratedSwitches, newFlexManager, timestep);
  121. saves.put(timestep, stateFromThisTimestep);
  122. if(updateVisual)savesVisual.put(timestep, new VisualRepresentationalState(stateFromThisTimestep, minimumModel));
  123. }
  124. /**
  125. * Decorate a switch
  126. * @param minModel
  127. * @param iteration
  128. * @return
  129. */
  130. public static ArrayList<DecoratedSwitch> decorateSwitches(MinimumModel minModel, int iteration) {
  131. ArrayList<DecoratedSwitch> aListOfDecoratedSwitches = new ArrayList<DecoratedSwitch>();
  132. for(HolonSwitch hSwitch: minModel.getSwitchList()) {
  133. aListOfDecoratedSwitches.add(new DecoratedSwitch(hSwitch, hSwitch.getState(iteration) ? SwitchState.Closed : SwitchState.Open));
  134. }
  135. return aListOfDecoratedSwitches;
  136. }
  137. /**
  138. * SubFunction to calculate the Networks from the model.
  139. * @param minModel
  140. * @param Iteration
  141. * @param leftOver
  142. * @return
  143. */
  144. ArrayList<MinimumNetwork> calculateNetworks(MinimumModel minModel, int Iteration, ArrayList<IntermediateCableWithState> leftOver){
  145. //Copy minModel ObjectList
  146. ArrayList<HolonObject> holonObjectList = new ArrayList<HolonObject>();
  147. for(HolonObject holonObject: minModel.getHolonObjectList()) {
  148. holonObjectList.add(holonObject);
  149. }
  150. //Copy minModelEdgeList
  151. ArrayList<IntermediateCableWithState> edgeList = new ArrayList<IntermediateCableWithState>();
  152. for(IntermediateCableWithState cable: minModel.getEdgeList()) {
  153. edgeList.add(cable);
  154. }
  155. ArrayList<MinimumNetwork> listOfNetworks = new ArrayList<MinimumNetwork>();
  156. while(!holonObjectList.isEmpty()) {
  157. //lookAt the first holonObject and find his neighbors
  158. HolonObject lookAtObject = holonObjectList.get(0);
  159. //delete out of list
  160. holonObjectList.remove(0);
  161. //create a new Network
  162. MinimumNetwork actualNetwork = new MinimumNetwork(new ArrayList<HolonObject>(), new ArrayList<IntermediateCableWithState>());
  163. actualNetwork.getHolonObjectList().add(lookAtObject);
  164. //create List of neighbors
  165. LinkedList<AbstractCanvasObject> neighbors = new LinkedList<AbstractCanvasObject>();
  166. populateListOfNeighbors(edgeList, lookAtObject, actualNetwork, neighbors);
  167. while(!neighbors.isEmpty()) {
  168. AbstractCanvasObject lookAtNeighbor = neighbors.getFirst();
  169. if(lookAtNeighbor instanceof HolonObject) {
  170. actualNetwork.getHolonObjectList().add((HolonObject) lookAtNeighbor);
  171. holonObjectList.remove(lookAtNeighbor);
  172. }else {
  173. actualNetwork.getNodeAndSwitches().add(lookAtNeighbor);
  174. }
  175. //When HolonSwitch Check if closed
  176. if(!(lookAtNeighbor instanceof HolonSwitch) || ((HolonSwitch)lookAtNeighbor).getState(Iteration)) {
  177. populateListOfNeighbors(edgeList, lookAtNeighbor, actualNetwork, neighbors);
  178. }
  179. neighbors.removeFirst();
  180. }
  181. listOfNetworks.add(actualNetwork);
  182. }
  183. if(leftOver!= null) {
  184. leftOver.clear();
  185. for(IntermediateCableWithState cable: edgeList) {
  186. leftOver.add(cable);
  187. }
  188. }
  189. return listOfNetworks;
  190. }
  191. /**
  192. * Adds the neighbors.
  193. * @param edgeList
  194. * @param lookAtObject
  195. * @param actualNetwork
  196. * @param neighbors
  197. */
  198. void populateListOfNeighbors(ArrayList<IntermediateCableWithState> edgeList, AbstractCanvasObject lookAtObject,
  199. MinimumNetwork actualNetwork, LinkedList<AbstractCanvasObject> neighbors) {
  200. ListIterator<IntermediateCableWithState> iter = edgeList.listIterator();
  201. while(iter.hasNext())
  202. {
  203. IntermediateCableWithState lookAtEdge = iter.next();
  204. if(lookAtEdge.getState() == CableState.Working && lookAtEdge.getModel().isConnectedTo(lookAtObject)) {
  205. iter.remove();
  206. actualNetwork.getEdgeList().add(lookAtEdge);
  207. //Add neighbar
  208. AbstractCanvasObject edgeNeighbor;
  209. if(lookAtEdge.getModel().getA().equals(lookAtObject)) {
  210. edgeNeighbor = lookAtEdge.getModel().getB();
  211. }else {
  212. edgeNeighbor = lookAtEdge.getModel().getA();
  213. }
  214. if(!neighbors.contains(edgeNeighbor)) {
  215. neighbors.add(edgeNeighbor);
  216. }
  217. }
  218. }
  219. }
  220. /**
  221. * Get all Subnets.Not functional.
  222. *
  223. * @return all Subnets
  224. */
  225. @Deprecated
  226. public ArrayList<SubNet> getSubNets() {
  227. return new ArrayList<SubNet>();
  228. }
  229. public FlexiblePane getFlexiblePane() {
  230. return flexPane;
  231. }
  232. void setFlexiblePane(FlexiblePane fp) {
  233. flexPane = fp;
  234. }
  235. public DecoratedState getActualDecorState() {
  236. return getDecorState(timeStep);
  237. }
  238. public VisualRepresentationalState getActualVisualRepresentationalState(){
  239. return savesVisual.getOrDefault(timeStep, null);
  240. }
  241. public FlexManager getActualFlexManager() {
  242. return savesFlexManger.getOrDefault(timeStep, null);
  243. }
  244. public void resetFlexManager(){
  245. savesFlexManger.clear();
  246. }
  247. public void resetFlexManagerForTimeStep(int timestep) {
  248. savesFlexManger.get(timestep).reset();
  249. }
  250. public DecoratedState getDecorState(int timestep) {
  251. return saves.get(timestep);
  252. }
  253. public VisualRepresentationalState getVisualRepresentationalState(int timestep) {
  254. return savesVisual.getOrDefault(timestep, null);
  255. }
  256. }