SimulationManager.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. //
  79. // StorageProductionController SPC = new StorageProductionController(net.getStorageElementList()); //TODO: unschoen
  80. // SPC.disableStorageDischarging(-1);
  81. //
  82. // //calc consuption and production
  83. // float consumption = calculateConsumption(net);
  84. // float production = calculateProduction(net);
  85. // float difference = production - consumption;
  86. // System.out.println("Consumption " + consumption);
  87. // System.out.println("production " + production);
  88. // System.out.println("Difference " + difference);
  89. //
  90. // if(difference > 0){
  91. // SPC.enableStorageCharging(difference);
  92. // }
  93. // if(difference < 0){
  94. // SPC.enableStorageDischarging(-difference);
  95. // }
  96. // }
  97. float energyOnCables = net.getHolonObjectList()
  98. .stream()
  99. .filter(object -> object.getEnergyAtTimeStepWithFlex(timestep, newFlexManager) > 0.0f)
  100. .map(object -> object.getEnergyAtTimeStepWithFlex(timestep, newFlexManager))
  101. .reduce(0.0f, ((a,b) -> a + b));
  102. //calculation for storage
  103. net.getHolonObjectList()
  104. .stream()
  105. .flatMap(holonObject -> holonObject.getElements().stream())
  106. .map(ele -> {
  107. if (ele instanceof StorageElement) {
  108. return (StorageElement) ele;
  109. }else {
  110. return null;
  111. }
  112. })
  113. .filter(Objects::nonNull)
  114. .filter(ele -> (ele.getStatus().equals(StorageElement.Mode.EMIT)
  115. || (ele.getStatus().equals(StorageElement.Mode.COLLECT))))
  116. .forEach(StorageElement::stateOfChargeCalculation);
  117. //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.
  118. 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);
  119. if(cable != null) {
  120. cable.setState(CableState.Burned);
  121. doAnotherLoop = true;
  122. }
  123. }
  124. }
  125. ArrayList<DecoratedNetwork> decorNetworks = new ArrayList<DecoratedNetwork>();
  126. FairnessModel actualFairnessModel = model.getFairnessModel();
  127. for (MinimumNetwork net : list) {
  128. decorNetworks.add(new DecoratedNetwork(net, timestep, actualFairnessModel, newFlexManager));
  129. }
  130. ArrayList<DecoratedCable> leftOverDecoratedCables = new ArrayList<DecoratedCable>();
  131. for(IntermediateCableWithState cable: leftOver) {
  132. leftOverDecoratedCables.add(new DecoratedCable(cable.getModel(), cable.getState(), 0.0f));
  133. }
  134. ArrayList<DecoratedSwitch> listOfDecoratedSwitches = decorateSwitches(minimumModel, timestep);
  135. DecoratedState stateFromThisTimestep = new DecoratedState(decorNetworks, leftOverDecoratedCables, listOfDecoratedSwitches, flexManagerFromActual, timestep);
  136. saves.put(timestep, stateFromThisTimestep);
  137. if(updateVisual)savesVisual.put(timestep, new VisualRepresentationalState(stateFromThisTimestep, minimumModel));
  138. }
  139. /**
  140. * Decorate a switch
  141. * @param minModel
  142. * @param iteration
  143. * @return
  144. */
  145. public static ArrayList<DecoratedSwitch> decorateSwitches(MinimumModel minModel, int iteration) {
  146. ArrayList<DecoratedSwitch> aListOfDecoratedSwitches = new ArrayList<DecoratedSwitch>();
  147. for(HolonSwitch hSwitch: minModel.getSwitchList()) {
  148. aListOfDecoratedSwitches.add(new DecoratedSwitch(hSwitch, hSwitch.getState(iteration) ? SwitchState.Closed : SwitchState.Open));
  149. }
  150. return aListOfDecoratedSwitches;
  151. }
  152. /**
  153. * SubFunction to calculate the Networks from the model.
  154. * @param minModel
  155. * @param Iteration
  156. * @param leftOver
  157. * @return
  158. */
  159. ArrayList<MinimumNetwork> calculateNetworks(MinimumModel minModel, int Iteration, ArrayList<IntermediateCableWithState> leftOver){
  160. //Copy minModel ObjectList
  161. ArrayList<HolonObject> holonObjectList = new ArrayList<HolonObject>();
  162. for(HolonObject holonObject: minModel.getHolonObjectList()) {
  163. holonObjectList.add(holonObject);
  164. }
  165. //Copy minModelEdgeList
  166. ArrayList<IntermediateCableWithState> edgeList = new ArrayList<IntermediateCableWithState>();
  167. for(IntermediateCableWithState cable: minModel.getEdgeList()) {
  168. edgeList.add(cable);
  169. }
  170. ArrayList<MinimumNetwork> listOfNetworks = new ArrayList<MinimumNetwork>();
  171. while(!holonObjectList.isEmpty()) {
  172. //lookAt the first holonObject and find his neighbors
  173. HolonObject lookAtObject = holonObjectList.get(0);
  174. //delete out of list
  175. holonObjectList.remove(0);
  176. //create a new Network
  177. MinimumNetwork actualNetwork = new MinimumNetwork(new ArrayList<HolonObject>(), new ArrayList<IntermediateCableWithState>());
  178. actualNetwork.getHolonObjectList().add(lookAtObject);
  179. //create List of neighbors
  180. LinkedList<AbstractCpsObject> neighbors = new LinkedList<AbstractCpsObject>();
  181. populateListOfNeighbors(edgeList, lookAtObject, actualNetwork, neighbors);
  182. while(!neighbors.isEmpty()) {
  183. AbstractCpsObject lookAtNeighbor = neighbors.getFirst();
  184. if(lookAtNeighbor instanceof HolonObject) {
  185. actualNetwork.getHolonObjectList().add((HolonObject) lookAtNeighbor);
  186. holonObjectList.remove(lookAtNeighbor);
  187. }
  188. //When HolonSwitch Check if closed
  189. if(!(lookAtNeighbor instanceof HolonSwitch) || ((HolonSwitch)lookAtNeighbor).getState(Iteration)) {
  190. populateListOfNeighbors(edgeList, lookAtNeighbor, actualNetwork, neighbors);
  191. }
  192. neighbors.removeFirst();
  193. }
  194. //add storages
  195. for (HolonObject holonObject : actualNetwork.getHolonObjectList()) {
  196. for (HolonElement ele : holonObject.getElements()) {
  197. if(ele instanceof StorageElement){
  198. actualNetwork.getStorageElementList().add((StorageElement) ele);
  199. }
  200. }
  201. }
  202. listOfNetworks.add(actualNetwork);
  203. }
  204. if(leftOver!= null) {
  205. leftOver.clear();
  206. for(IntermediateCableWithState cable: edgeList) {
  207. leftOver.add(cable);
  208. }
  209. }
  210. return listOfNetworks;
  211. }
  212. /**
  213. * Adds the neighbors.
  214. * @param edgeList
  215. * @param lookAtObject
  216. * @param actualNetwork
  217. * @param neighbors
  218. */
  219. void populateListOfNeighbors(ArrayList<IntermediateCableWithState> edgeList, AbstractCpsObject lookAtObject,
  220. MinimumNetwork actualNetwork, LinkedList<AbstractCpsObject> neighbors) {
  221. ListIterator<IntermediateCableWithState> iter = edgeList.listIterator();
  222. while(iter.hasNext())
  223. {
  224. IntermediateCableWithState lookAtEdge = iter.next();
  225. if(lookAtEdge.getState() == CableState.Working && lookAtEdge.getModel().isConnectedTo(lookAtObject)) {
  226. iter.remove();
  227. actualNetwork.getEdgeList().add(lookAtEdge);
  228. //Add neighbar
  229. AbstractCpsObject edgeNeighbor;
  230. if(lookAtEdge.getModel().getA().equals(lookAtObject)) {
  231. edgeNeighbor = lookAtEdge.getModel().getB();
  232. }else {
  233. edgeNeighbor = lookAtEdge.getModel().getA();
  234. }
  235. if(!neighbors.contains(edgeNeighbor)) {
  236. neighbors.add(edgeNeighbor);
  237. }
  238. }
  239. }
  240. }
  241. /**
  242. * Get all Subnets.Not functional.
  243. *
  244. * @return all Subnets
  245. */
  246. @Deprecated
  247. public ArrayList<SubNet> getSubNets() {
  248. return new ArrayList<SubNet>();
  249. }
  250. public FlexiblePane getFlexiblePane() {
  251. return flexPane;
  252. }
  253. void setFlexiblePane(FlexiblePane fp) {
  254. flexPane = fp;
  255. }
  256. public DecoratedState getActualDecorState() {
  257. return getDecorState(timeStep);
  258. }
  259. public VisualRepresentationalState getActualVisualRepresentationalState(){
  260. return savesVisual.getOrDefault(timeStep, null);
  261. }
  262. public FlexManager getActualFlexManager() {
  263. return savesFlexManger.getOrDefault(timeStep, null);
  264. }
  265. public void resetFlexManager(){
  266. savesFlexManger.clear();
  267. }
  268. public void resetFlexManagerForTimeStep(int timestep) {
  269. FlexManager newFlexManager = new FlexManager(model, timestep, savesFlexManger.getOrDefault(timestep-1, null));
  270. savesFlexManger.put(timestep, newFlexManager);
  271. }
  272. public DecoratedState getDecorState(int timestep) {
  273. return saves.getOrDefault(timestep, null);
  274. }
  275. public VisualRepresentationalState getVisualRepresentationalState(int timestep) {
  276. return savesVisual.getOrDefault(timestep, null);
  277. }
  278. ////////////////////////////////////////TODO:comments
  279. public boolean blackstartRunning(){
  280. for (HolonElement ele : getPowerplant().getElements()) {
  281. if (ele.getEleName().equals("Blackstart")) {
  282. if (ele.isActive()) {
  283. return true;
  284. } else {
  285. return false;
  286. }
  287. }
  288. }
  289. return false;
  290. }
  291. public HolonObject getPowerplant() {
  292. for (AbstractCpsObject cps : model.getAllHolonObjectsOnCanvas()) {
  293. if (cps instanceof HolonObject) {
  294. if (cps.getObjName().equals("Power Plant")) {
  295. return (HolonObject) cps;
  296. }
  297. }
  298. }
  299. return null;
  300. }
  301. public float calculateProduction(MinimumNetwork net){
  302. float production = 0;
  303. for (HolonObject holonObject : net.getHolonObjectList()) {
  304. for(HolonElement holonElement : holonObject.getElements()){
  305. if(holonElement.isActive()){
  306. if(holonElement.getEnergyAtTimeStep(model.getCurIteration()) > 0){
  307. production = production + holonElement.getEnergyAtTimeStep(model.getCurIteration());
  308. }
  309. }
  310. }
  311. }
  312. return production;
  313. }
  314. public float calculateConsumption(MinimumNetwork net){
  315. float consumption = 0;
  316. for (HolonObject holonObject : net.getHolonObjectList()) {
  317. for(HolonElement holonElement : holonObject.getElements()){
  318. if(holonElement.isActive()){
  319. if(holonElement.getEnergyPerElement() < 0){
  320. consumption = consumption - holonElement.getEnergyPerElement() * holonElement.getAmount();
  321. }
  322. }
  323. }
  324. }
  325. return consumption;
  326. }
  327. }