SimulationManager.java 11 KB

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