SimulationManager.java 11 KB

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