SimulationManager.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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.*;
  10. /**
  11. * Controller for Simulation.
  12. *
  13. * @author Gruppe14
  14. */
  15. public class SimulationManager {
  16. int global = 0;
  17. private Model model;
  18. private HashMap<Integer, DecoratedState> saves = new HashMap<Integer, DecoratedState>();
  19. private HashMap<Integer, VisualRepresentationalState> savesVisual = new HashMap<Integer, VisualRepresentationalState>();
  20. private HashMap<Integer, FlexManager> savesFlexManger = new HashMap<Integer, FlexManager>();
  21. private List<Flexibility> actualOrders = new ArrayList<Flexibility>();
  22. private int timeStep;
  23. private FlexiblePane flexPane;
  24. /**
  25. * Constructor.
  26. *
  27. * @param m
  28. * Model
  29. */
  30. public SimulationManager(Model m) {
  31. model = m;
  32. }
  33. /**
  34. * calculates the flow of the edges and the supply for objects and consider old timesteps for burned cables.
  35. *
  36. * @param timestep
  37. * current Iteration
  38. * @param updateVisual TODO
  39. */
  40. public void calculateStateForTimeStep(int timestep, boolean updateVisual) {
  41. FlexManager newFlexManager = new FlexManager(model, timestep, savesFlexManger.getOrDefault(timestep-1, null));
  42. //Actual ordered not delete:
  43. FlexManager flexManagerFromActual = savesFlexManger.getOrDefault(timestep, null);
  44. if(flexManagerFromActual != null) {
  45. newFlexManager.orderFlexFromList(flexManagerFromActual.getAllFlexesOrderedThisTimeStep());
  46. }
  47. savesFlexManger.put(timestep, newFlexManager);
  48. HashMap<CpsEdge, CableState> map = new HashMap<CpsEdge, CableState>();
  49. if(timestep > 0 && saves.containsKey(timestep-1)) //if the state before exist
  50. {
  51. //make cable hastmap
  52. DecoratedState theStateBefore = saves.get(timestep-1);
  53. //edges without HolonObjects or burned
  54. for(DecoratedCable edge : theStateBefore.getLeftOverEdges())
  55. {
  56. map.put(edge.getModel(), edge.getState());
  57. }
  58. }
  59. timeStep = timestep;
  60. ArrayList<MinimumNetwork> list = new ArrayList<MinimumNetwork>();
  61. MinimumModel minimumModel = new MinimumModel(model.getObjectsOnCanvas(), model.getEdgesOnCanvas());
  62. //set all BreakedManuel Cable Burned:
  63. for(IntermediateCableWithState cable : minimumModel.getEdgeList()) {
  64. if(cable.getModel().isBreakedManuel()) cable.setState(CableState.Burned);
  65. }
  66. //set all the state before:
  67. for(IntermediateCableWithState cable : minimumModel.getEdgeList()) {
  68. if(map.containsKey(cable.getModel())) cable.setState(map.get(cable.getModel()));
  69. }
  70. ArrayList<IntermediateCableWithState> leftOver = new ArrayList<IntermediateCableWithState>();
  71. boolean doAnotherLoop = true;
  72. while(doAnotherLoop) {
  73. doAnotherLoop = false;
  74. list = calculateNetworks(minimumModel, timestep, leftOver);
  75. for(MinimumNetwork net : list) {
  76. //if there is storage and it is needed turn it on//TODO:!!!
  77. if(!blackstartRunning() && !net.getStorageElementList().isEmpty()) {
  78. StorageProductionController SPC = new StorageProductionController(net.getStorageElementList()); //TODO: unschoen
  79. SPC.disableStorageDischarging(-1);
  80. //calc consuption and production
  81. float consumption = calculateConsumption(net);
  82. float production = calculateProduction(net);
  83. float difference = production - consumption;
  84. System.out.println("Consumption " + consumption);
  85. System.out.println("production " + production);
  86. System.out.println("Difference " + difference);
  87. if(difference > 0){
  88. SPC.enableStorageCharging(difference);
  89. }
  90. if(difference < 0){
  91. SPC.enableStorageDischarging(-difference);
  92. }
  93. }
  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(Objects::nonNull)
  111. .filter(ele -> (ele.getStatus().equals(StorageElement.Mode.EMIT)
  112. || (ele.getStatus().equals(StorageElement.Mode.COLLECT))))
  113. .forEach(StorageElement::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. //add storages
  192. for (HolonObject holonObject : actualNetwork.getHolonObjectList()) {
  193. for (HolonElement ele : holonObject.getElements()) {
  194. if(ele instanceof StorageElement){
  195. actualNetwork.getStorageElementList().add((StorageElement) ele);
  196. }
  197. }
  198. }
  199. listOfNetworks.add(actualNetwork);
  200. }
  201. if(leftOver!= null) {
  202. leftOver.clear();
  203. for(IntermediateCableWithState cable: edgeList) {
  204. leftOver.add(cable);
  205. }
  206. }
  207. return listOfNetworks;
  208. }
  209. /**
  210. * Adds the neighbors.
  211. * @param edgeList
  212. * @param lookAtObject
  213. * @param actualNetwork
  214. * @param neighbors
  215. */
  216. void populateListOfNeighbors(ArrayList<IntermediateCableWithState> edgeList, AbstractCpsObject lookAtObject,
  217. MinimumNetwork actualNetwork, LinkedList<AbstractCpsObject> neighbors) {
  218. ListIterator<IntermediateCableWithState> iter = edgeList.listIterator();
  219. while(iter.hasNext())
  220. {
  221. IntermediateCableWithState lookAtEdge = iter.next();
  222. if(lookAtEdge.getState() == CableState.Working && lookAtEdge.getModel().isConnectedTo(lookAtObject)) {
  223. iter.remove();
  224. actualNetwork.getEdgeList().add(lookAtEdge);
  225. //Add neighbar
  226. AbstractCpsObject edgeNeighbor;
  227. if(lookAtEdge.getModel().getA().equals(lookAtObject)) {
  228. edgeNeighbor = lookAtEdge.getModel().getB();
  229. }else {
  230. edgeNeighbor = lookAtEdge.getModel().getA();
  231. }
  232. if(!neighbors.contains(edgeNeighbor)) {
  233. neighbors.add(edgeNeighbor);
  234. }
  235. }
  236. }
  237. }
  238. /**
  239. * Get all Subnets.Not functional.
  240. *
  241. * @return all Subnets
  242. */
  243. @Deprecated
  244. public ArrayList<SubNet> getSubNets() {
  245. return new ArrayList<SubNet>();
  246. }
  247. public FlexiblePane getFlexiblePane() {
  248. return flexPane;
  249. }
  250. void setFlexiblePane(FlexiblePane fp) {
  251. flexPane = fp;
  252. }
  253. public DecoratedState getActualDecorState() {
  254. return getDecorState(timeStep);
  255. }
  256. public VisualRepresentationalState getActualVisualRepresentationalState(){
  257. return savesVisual.getOrDefault(timeStep, null);
  258. }
  259. public FlexManager getActualFlexManager() {
  260. return savesFlexManger.getOrDefault(timeStep, null);
  261. }
  262. public void resetFlexManager(){
  263. savesFlexManger.clear();
  264. }
  265. public void resetFlexManagerForTimeStep(int timestep) {
  266. FlexManager newFlexManager = new FlexManager(model, timestep, savesFlexManger.getOrDefault(timestep-1, null));
  267. savesFlexManger.put(timestep, newFlexManager);
  268. }
  269. public DecoratedState getDecorState(int timestep) {
  270. return saves.getOrDefault(timestep, null);
  271. }
  272. public VisualRepresentationalState getVisualRepresentationalState(int timestep) {
  273. return savesVisual.getOrDefault(timestep, null);
  274. }
  275. ////////////////////////////////////////TODO:comments
  276. public boolean blackstartRunning(){
  277. for (HolonElement ele : getPowerPlant().getElements()) {
  278. if (ele.getEleName().equals("Blackstart")) {
  279. if (ele.isActive()) {
  280. return true;
  281. } else {
  282. return false;
  283. }
  284. }
  285. }
  286. return false;
  287. }
  288. public HolonObject getPowerPlant() {
  289. for (AbstractCpsObject cps : model.getAllHolonObjectsOnCanvas()) {
  290. // geht nur wenn nur ein power plant vorhanden ist
  291. if (cps instanceof HolonObject) {
  292. if (cps.getObjName().equals("Power Plant")) {
  293. return (HolonObject) cps;
  294. }
  295. }
  296. }
  297. return null;
  298. }
  299. public float calculateProduction(MinimumNetwork net){
  300. float production = 0;
  301. for (HolonObject holonObject : net.getHolonObjectList()) {
  302. for(HolonElement holonElement : holonObject.getElements()){
  303. if(holonElement.isActive()){
  304. if(holonElement.getEnergyAtTimeStep(model.getCurIteration()) > 0){
  305. production = production + holonElement.getEnergyAtTimeStep(model.getCurIteration());
  306. }
  307. }
  308. }
  309. }
  310. return production;
  311. }
  312. public float calculateConsumption(MinimumNetwork net){
  313. float consumption = 0;
  314. for (HolonObject holonObject : net.getHolonObjectList()) {
  315. for(HolonElement holonElement : holonObject.getElements()){
  316. if(holonElement.isActive()){
  317. if(holonElement.getEnergyPerElement() < 0){
  318. consumption = consumption - holonElement.getEnergyPerElement() * holonElement.getAmount();
  319. }
  320. }
  321. }
  322. }
  323. return consumption;
  324. }
  325. }