SimulationManager.java 14 KB

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