DecoratedNetwork.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. package ui.model;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.util.stream.Collectors;
  5. import classes.HolonElement;
  6. import classes.HolonObject;
  7. import holeg.HolegGateway;
  8. import ui.controller.FlexManager;
  9. import ui.model.DecoratedCable.CableState;
  10. import ui.model.DecoratedHolonObject.HolonObjectState;
  11. import ui.model.Model.FairnessModel;
  12. public class DecoratedNetwork {
  13. private ArrayList<Supplier> supplierList = new ArrayList<Supplier>();
  14. private ArrayList<Consumer> consumerList = new ArrayList<Consumer>();
  15. private ArrayList<Consumer> consumerSelfSuppliedList = new ArrayList<Consumer>();
  16. private ArrayList<Passiv> passivNoEnergyList = new ArrayList<Passiv>();
  17. private ArrayList<DecoratedCable> decoratedCableList = new ArrayList<DecoratedCable>();
  18. private int timestep;
  19. public DecoratedNetwork(MinimumNetwork minimumNetwork, int Iteration, FairnessModel actualFairnessModel, FlexManager flexManager){
  20. this.timestep = Iteration;
  21. switch(actualFairnessModel) {
  22. case AllEqual:
  23. calculateAllEqualNetwork(minimumNetwork, Iteration, flexManager);
  24. break;
  25. case MininumDemandFirst:
  26. calculateMinimumDemandFirstNetwork(minimumNetwork, Iteration, flexManager);
  27. break;
  28. case PowerFlowAnalysis:
  29. // TODO: (Henrik)
  30. HolegGateway.solve(minimumNetwork, Iteration, flexManager, this);
  31. calculateStates();
  32. break;
  33. }
  34. }
  35. //Getter:
  36. public ArrayList<Supplier> getSupplierList() {
  37. return supplierList;
  38. }
  39. public ArrayList<Consumer> getConsumerList() {
  40. return consumerList;
  41. }
  42. public ArrayList<Consumer> getConsumerSelfSuppliedList() {
  43. return consumerSelfSuppliedList;
  44. }
  45. public ArrayList<Passiv> getPassivNoEnergyList() {
  46. return passivNoEnergyList;
  47. }
  48. public ArrayList<DecoratedCable> getDecoratedCableList(){
  49. return decoratedCableList;
  50. }
  51. //Calculations:
  52. private void calculateMinimumDemandFirstNetwork(MinimumNetwork minimumNetwork, int Iteration, FlexManager flexManager) {
  53. categorize(minimumNetwork, Iteration, flexManager);
  54. //Sort SupplierList according to the EnergyToSupplyNetwork maximum first.
  55. //Sort ConsumerList according to the MinimumConsumingElementEnergy minimum first.
  56. supplierList.sort((Supplier lhs,Supplier rhs) -> -Float.compare(lhs.getEnergyToSupplyNetwork(), rhs.getEnergyToSupplyNetwork()));
  57. consumerList.sort((Consumer lhs,Consumer rhs) -> Float.compare(lhs.getMinimumConsumingElementEnergy() , rhs.getMinimumConsumingElementEnergy()));
  58. //consumerList.forEach((con) -> System.out.println(con.getMinimumConsumingElementEnergy()));
  59. //consumerList.forEach((con) -> System.out.println("AfterSorting" + con));
  60. float energyToSupplyInTheNetwork = supplierList.stream().map(supplier -> supplier.getEnergyToSupplyNetwork() - supplier.getEnergySupplied()).reduce( 0.0f, (a, b) -> a + b);
  61. decorateCable(minimumNetwork, energyToSupplyInTheNetwork);
  62. outerLoop:
  63. for(Consumer con : consumerList)
  64. {
  65. //gehe Supplier list durch wer ihn supplien kann.
  66. for(Supplier sup : supplierList) {
  67. float energyRdyToSupply = sup.getEnergyToSupplyNetwork() - sup.getEnergySupplied();
  68. if(energyRdyToSupply == 0.0f) continue;
  69. float energyNeededForMinimumConsumingElement=con.getMinimumConsumingElementEnergy()-con.getEnergyFromNetwork();
  70. if(energyNeededForMinimumConsumingElement>energyToSupplyInTheNetwork) {
  71. //Dont supply a minimumElement when you cant supply it fully
  72. break outerLoop;
  73. }
  74. if(energyRdyToSupply>=energyNeededForMinimumConsumingElement) {
  75. energyToSupplyInTheNetwork -= energyNeededForMinimumConsumingElement;
  76. supply(con, sup, energyNeededForMinimumConsumingElement);
  77. continue outerLoop;
  78. }else
  79. {
  80. energyToSupplyInTheNetwork -= energyRdyToSupply;
  81. supply(con, sup, energyRdyToSupply);
  82. }
  83. }
  84. //No more Energy in the network
  85. break;
  86. }
  87. //consumerList.forEach((con) -> System.out.println("AfterSuppliing MinimumDemand " + con));
  88. //Sort ConsumerList according to the EnergyNeeded to supply fully after minimum Demand First.
  89. consumerList.sort((Consumer lhs,Consumer rhs) -> Float.compare(lhs.getEnergyNeededFromNetwork()-lhs.getEnergyFromNetwork() , rhs.getEnergyNeededFromNetwork()-rhs.getEnergyFromNetwork() ));
  90. //Supply consumer fully
  91. outerLoop:
  92. for(Consumer con : consumerList)
  93. {
  94. //gehe Supplier list durch wer ihn supplien kann.
  95. for(Supplier sup : supplierList) {
  96. float energyRdyToSupply = sup.getEnergyToSupplyNetwork() - sup.getEnergySupplied();
  97. if(energyRdyToSupply == 0.0f) continue;
  98. float energyNeededForFullySupply = con.getEnergyNeededFromNetwork() - con.getEnergyFromNetwork();
  99. if(energyNeededForFullySupply == 0.0f) continue outerLoop;
  100. if(energyRdyToSupply>=energyNeededForFullySupply) {
  101. supply(con, sup, energyNeededForFullySupply);
  102. continue outerLoop;
  103. }else
  104. {
  105. supply(con, sup, energyRdyToSupply);
  106. }
  107. }
  108. //No more Energy in the network
  109. break;
  110. }
  111. //consumerList.forEach((con) -> System.out.println("AfterFullySuplieing" + con));
  112. //If Energy Left Supply all equal
  113. //Count EnergyLeft
  114. float energyLeft = supplierList.stream().map(supplier -> supplier.getEnergyToSupplyNetwork() - supplier.getEnergySupplied()).reduce( 0.0f, (a, b) -> a + b);
  115. //System.out.println("EnergyLeft: " + energyLeft);
  116. if(energyLeft > 0.0f && (consumerList.size() + consumerSelfSuppliedList.size() != 0))
  117. {
  118. float equalAmountOfEnergyToSupply = energyLeft / ((float)(consumerList.size() + consumerSelfSuppliedList.size()));
  119. outerLoop:
  120. for(Consumer con : consumerList)
  121. {
  122. //gehe Supplier list durch wer ihn supplien kann.
  123. for(Supplier sup : supplierList) {
  124. float energyRdyToSupply = sup.getEnergyToSupplyNetwork() - sup.getEnergySupplied();
  125. if(energyRdyToSupply == 0.0f) continue;
  126. float energyNeededToSupplyConsumerTheEqualAmount = equalAmountOfEnergyToSupply +con.getEnergyNeededFromNetwork()- con.getEnergyFromNetwork();
  127. if(energyRdyToSupply>=energyNeededToSupplyConsumerTheEqualAmount) {
  128. supply(con, sup, energyNeededToSupplyConsumerTheEqualAmount);
  129. continue outerLoop;
  130. }else
  131. {
  132. supply(con, sup, energyRdyToSupply);
  133. }
  134. }
  135. //No more Energy in the network
  136. break;
  137. }
  138. outerLoop:
  139. for(Consumer con : consumerSelfSuppliedList)
  140. {
  141. //gehe Supplier list durch wer ihn supplien kann.
  142. for(Supplier sup : supplierList) {
  143. float energyRdyToSupply = sup.getEnergyToSupplyNetwork() - sup.getEnergySupplied();
  144. if(energyRdyToSupply == 0.0f) continue;
  145. float energyNeededToSupplyConsumerTheEqualAmount = equalAmountOfEnergyToSupply +con.getEnergyNeededFromNetwork()- con.getEnergyFromNetwork();
  146. if(energyRdyToSupply>=energyNeededToSupplyConsumerTheEqualAmount) {
  147. supply(con, sup, energyNeededToSupplyConsumerTheEqualAmount);
  148. continue outerLoop;
  149. }else
  150. {
  151. supply(con, sup, energyRdyToSupply);
  152. }
  153. }
  154. //No more Energy in the network
  155. break;
  156. }
  157. }
  158. //consumerList.forEach((con) -> System.out.println("AfterOverSuppleiing" + con));
  159. //consumerSelfSuppliedList.forEach((con) -> System.out.println("AfterOverSuppleiing" + con));
  160. calculateStates();
  161. }
  162. private void decorateCable(MinimumNetwork minimumNetwork, float energyToSupplyInTheNetwork) {
  163. //DecoratedCables
  164. //Minimum demand first:
  165. for(IntermediateCableWithState edge: minimumNetwork.getEdgeList()) {
  166. decoratedCableList.add(new DecoratedCable(edge.getModel(), edge.getState(), (edge.getState() == CableState.Working) ? energyToSupplyInTheNetwork : 0.0f));
  167. }
  168. }
  169. private void calculateAllEqualNetwork(MinimumNetwork minimumNetwork, int Iteration, FlexManager flexManager) {
  170. categorize(minimumNetwork, Iteration, flexManager);
  171. float energyToSupplyInTheNetwork = supplierList.stream().map(supplier -> supplier.getEnergyToSupplyNetwork() - supplier.getEnergySupplied()).reduce( 0.0f, (a, b) -> a + b);
  172. float energyForEachConsumer = (consumerList.size() != 0) ? energyToSupplyInTheNetwork / consumerList.size() : 0.0f;
  173. decorateCable(minimumNetwork, energyToSupplyInTheNetwork);
  174. //Supply consumer equal
  175. outerLoop:
  176. for(Consumer con : consumerList)
  177. {
  178. //gehe Supplier list durch wer ihn supplien kann.
  179. float energyNeededForEqualSupply = energyForEachConsumer;
  180. for(Supplier sup : supplierList) {
  181. float energyRdyToSupply = sup.getEnergyToSupplyNetwork() - sup.getEnergySupplied();
  182. if(energyRdyToSupply == 0.0f) continue;
  183. if(energyRdyToSupply>=energyNeededForEqualSupply) {
  184. supply(con, sup, energyNeededForEqualSupply);
  185. continue outerLoop;
  186. }else
  187. {
  188. supply(con, sup, energyRdyToSupply);
  189. energyNeededForEqualSupply -= energyRdyToSupply;
  190. }
  191. }
  192. //No more Energy in the network
  193. break;
  194. }
  195. calculateStates();
  196. }
  197. private void calculateStates() {
  198. //CalculateStates:
  199. supplierList.forEach(sup -> sup.setState(HolonObjectState.PRODUCER));
  200. passivNoEnergyList.forEach(sup -> sup.setState(HolonObjectState.NO_ENERGY));
  201. for(Consumer con : this.consumerList)
  202. {
  203. setConsumerState(con);
  204. }
  205. for(Consumer con : this.consumerSelfSuppliedList)
  206. {
  207. setConsumerState(con);
  208. }
  209. }
  210. private void categorize(MinimumNetwork minimumNetwork, int Iteration, FlexManager flexManager) {
  211. //Categorize
  212. for(HolonObject hObject: minimumNetwork.getHolonObjectList()) {
  213. float energyNeeded = hObject.getEnergyNeededFromConsumingElementsWithFlex(Iteration, flexManager);
  214. float energySelfProducing = hObject.getEnergySelfProducingFromProducingElementsWithFlex(Iteration, flexManager);
  215. if(energyNeeded < energySelfProducing) {
  216. Supplier sup = new Supplier(hObject, energySelfProducing - energyNeeded, energyNeeded);
  217. supplierList.add(sup);
  218. } else if (energyNeeded > energySelfProducing) {
  219. Consumer con = new Consumer(hObject);
  220. con.setEnergyNeededFromNetwork(energyNeeded - energySelfProducing);
  221. con.setMinimumConsumingElementEnergy(hObject.getMinimumConsumingElementEnergyWithFlex(Iteration, flexManager));
  222. con.setEnergyFromConsumingElemnets(hObject.getEnergyNeededFromConsumingElementsWithFlex(Iteration, flexManager));
  223. con.setEnergySelfSupplied(hObject.getEnergySelfProducingFromProducingElementsWithFlex(Iteration, flexManager));
  224. consumerList.add(con);
  225. }else if(energyNeeded == energySelfProducing) {
  226. if (energySelfProducing == 0.0f) {
  227. Passiv pas = new Passiv(hObject);
  228. passivNoEnergyList.add(pas);
  229. } else {
  230. Consumer con = new Consumer(hObject);
  231. con.setEnergyNeededFromNetwork(0.0f);
  232. con.setMinimumConsumingElementEnergy(hObject.getMinimumConsumingElementEnergyWithFlex(Iteration, flexManager));
  233. con.setEnergyFromConsumingElemnets(hObject.getEnergyNeededFromConsumingElementsWithFlex(Iteration, flexManager));
  234. con.setEnergySelfSupplied(hObject.getEnergySelfProducingFromProducingElementsWithFlex(Iteration, flexManager));
  235. consumerSelfSuppliedList.add(con);
  236. }
  237. }
  238. }
  239. }
  240. private void setConsumerState(Consumer con) {
  241. if(con.getEnergySelfSupplied() + con.getEnergyFromNetwork() > con.getEnergyFromConsumingElemnets()) {
  242. con.setState(HolonObjectState.OVER_SUPPLIED);
  243. }else if(con.getEnergySelfSupplied() + con.getEnergyFromNetwork() == con.getEnergyFromConsumingElemnets()) {
  244. con.setState(HolonObjectState.SUPPLIED);
  245. }else if(con.getEnergySelfSupplied() + con.getEnergyFromNetwork() >= con.getMinimumConsumingElementEnergy()) {
  246. con.setState(HolonObjectState.PARTIALLY_SUPPLIED);
  247. }else {
  248. con.setState(HolonObjectState.NOT_SUPPLIED);
  249. }
  250. }
  251. /**
  252. * No Checks.
  253. * @param con
  254. * @param sup
  255. * @param energy
  256. */
  257. private void supply(Consumer con, Supplier sup, float energy) {
  258. sup.getConsumerList().add(sup.new ConsumerListEntry(con , energy));
  259. sup.setEnergySupplied(sup.getEnergySupplied() + energy);
  260. con.getSupplierList().add(con.new SupplierListEntry(sup, energy));
  261. con.setEnergyFromNetwork(con.getEnergyFromNetwork() + energy);
  262. }
  263. public int getAmountOfActiveElements() {
  264. return supplierList.stream().map(object -> object.getModel().getNumberOfActiveElements()).reduce(0, Integer::sum)+
  265. consumerList.stream().map(object -> object.getModel().getNumberOfActiveElements()).reduce(0, Integer::sum)+
  266. consumerSelfSuppliedList.stream().map(object -> object.getModel().getNumberOfActiveElements()).reduce(0, Integer::sum);
  267. }
  268. public int getAmountOfElements() {
  269. return supplierList.stream().map(object -> object.getModel().getNumberOfElements()).reduce(0, Integer::sum)+
  270. consumerList.stream().map(object -> object.getModel().getNumberOfElements()).reduce(0, Integer::sum)+
  271. consumerSelfSuppliedList.stream().map(object -> object.getModel().getNumberOfElements()).reduce(0, Integer::sum);
  272. }
  273. public int getAmountOfConsumer() {
  274. return consumerList.size() + this.consumerSelfSuppliedList.size();
  275. }
  276. public int getAmountOfSupplier() {
  277. return supplierList.size();
  278. }
  279. public int getAmountOfConsumerWithState(HolonObjectState state) {
  280. return (int) (consumerList.stream().filter(con -> con.getState() == state).count() + consumerSelfSuppliedList.stream().filter(con -> con.getState() == state).count());
  281. }
  282. public int getAmountOfPassiv() {
  283. return passivNoEnergyList.size();
  284. }
  285. public int getAmountOfHolonObjects() {
  286. return getAmountOfConsumer() + getAmountOfSupplier() + getAmountOfPassiv();
  287. }
  288. public float getTotalConsumption() {
  289. float energy = consumerList.stream().map(con -> con.getEnergyFromConsumingElemnets()).reduce(0.f, Float::sum) + consumerSelfSuppliedList.stream().map(con -> con.getEnergyFromConsumingElemnets()).reduce(0.f, Float::sum);
  290. energy += supplierList.stream().map(sup -> sup.getEnergySelfConsuming()).reduce(0.f, Float::sum);
  291. return energy;
  292. }
  293. public float getAverageConsumptionInNetworkForHolonObject(){
  294. return getTotalConsumption() / (float)getAmountOfHolonObjects();
  295. }
  296. public float getTotalProduction() {
  297. float energy = consumerList.stream().map(con -> con.getEnergySelfSupplied()).reduce(0.f, Float::sum) + consumerSelfSuppliedList.stream().map(con -> con.getEnergySelfSupplied()).reduce(0.f, Float::sum);
  298. energy += supplierList.stream().map(sup -> sup.getEnergyProducing()).reduce(0.f, Float::sum);
  299. return energy;
  300. }
  301. public float getAverageProductionInNetworkForHolonObject() {
  302. return getTotalProduction() / (float) getAmountOfHolonObjects();
  303. }
  304. /**
  305. * returns the Varianz in Poduction
  306. * @return
  307. */
  308. public float getVarianzInProductionInNetworkForHolonObjects() {
  309. float average = getAverageProductionInNetworkForHolonObject();
  310. float sum = consumerList.stream().map(con -> squared(con.getEnergySelfSupplied() - average)).reduce(0.f, Float::sum)
  311. + consumerSelfSuppliedList.stream().map(con -> squared(con.getEnergySelfSupplied() - average)).reduce(0.f, Float::sum)
  312. + supplierList.stream().map(sup -> squared(sup.getEnergyProducing() - average)).reduce(0.f, Float::sum);
  313. return sum / (float) getAmountOfHolonObjects();
  314. }
  315. public float getDeviationInProductionInNetworkForHolonObjects() {
  316. return (float)Math.sqrt(getVarianzInProductionInNetworkForHolonObjects());
  317. }
  318. public float getVarianzInConsumptionInNetworkForHolonObjects() {
  319. float average = getAverageConsumptionInNetworkForHolonObject();
  320. float sum = consumerList.stream().map(con -> squared(con.getEnergyFromConsumingElemnets() - average)).reduce(0.f, Float::sum)
  321. + consumerSelfSuppliedList.stream().map(con -> squared(con.getEnergyFromConsumingElemnets() - average)).reduce(0.f, Float::sum)
  322. + supplierList.stream().map(sup -> squared(sup.getEnergySelfConsuming() - average)).reduce(0.f, Float::sum);
  323. return sum / (float) getAmountOfHolonObjects();
  324. }
  325. public float getDeviationInConsumptionInNetworkForHolonObjects() {
  326. return (float)Math.sqrt(getVarianzInConsumptionInNetworkForHolonObjects());
  327. }
  328. //HelperFunction
  329. /**
  330. *
  331. * @return a list of energy
  332. */
  333. public List<Float> getListOfEnergyThatIsOfferedByFlexibilitiesInThisNetwork() {
  334. List<HolonElement> eleList = consumerList.stream().flatMap(con-> con.getModel().getElements().stream()).collect(Collectors.toList());
  335. eleList.addAll(consumerSelfSuppliedList.stream().flatMap(con-> con.getModel().getElements().stream()).collect(Collectors.toList()));
  336. eleList.addAll(supplierList.stream().flatMap(con-> con.getModel().getElements().stream()).collect(Collectors.toList()));
  337. eleList.addAll(passivNoEnergyList.stream().flatMap(con-> con.getModel().getElements().stream()).collect(Collectors.toList()));
  338. return eleList.stream().filter(ele -> (ele.flexList.stream().anyMatch(flex -> flex.offered && flex.fulfillsConstrains())) ).map(ele -> -ele.getEnergyAtTimeStep(timestep) ).collect(Collectors.toList()) ;
  339. }
  340. public List<Float> getListOfEnergyInProductionThatIsOfferedByFlexibilitiesInThisNetwork(){
  341. return getListOfEnergyThatIsOfferedByFlexibilitiesInThisNetwork().stream().filter(value -> (value > 0.f)).collect(Collectors.toList());
  342. }
  343. public List<Float> getListOfEnergyInConsumptionThatIsOfferedByFlexibilitiesInThisNetwork(){
  344. return getListOfEnergyThatIsOfferedByFlexibilitiesInThisNetwork().stream().filter(value -> (value < 0.f)).map(value -> -value).collect(Collectors.toList());
  345. }
  346. public float getFlexibilityProductionCapacity() {
  347. return getListOfEnergyInProductionThatIsOfferedByFlexibilitiesInThisNetwork().stream().reduce(0.f, Float::sum);
  348. }
  349. public float getFlexibilityConsumptionCapacity() {
  350. return getListOfEnergyInConsumptionThatIsOfferedByFlexibilitiesInThisNetwork().stream().reduce(0.f, Float::sum);
  351. }
  352. public int getAmountOfProductionFlexibilities() {
  353. return getListOfEnergyInProductionThatIsOfferedByFlexibilitiesInThisNetwork().size();
  354. }
  355. public int getAmountOfConsumptionFlexibilities() {
  356. return getListOfEnergyInConsumptionThatIsOfferedByFlexibilitiesInThisNetwork().size();
  357. }
  358. public float getAverageFlexibilityProduction() {
  359. int amount = getAmountOfProductionFlexibilities();
  360. return (amount > 0)? getFlexibilityProductionCapacity() / (float)amount : 0.f;
  361. }
  362. public float getAverageFlexibilityConsumption() {
  363. int amount = getAmountOfConsumptionFlexibilities();
  364. return (amount > 0)? getFlexibilityConsumptionCapacity() / (float)amount : 0.f;
  365. }
  366. public float getVarianzInFlexibilitieConsumption() {
  367. float average = getAverageFlexibilityConsumption();
  368. float sum = getListOfEnergyInConsumptionThatIsOfferedByFlexibilitiesInThisNetwork().stream().map(energy -> squared(energy - average)).reduce(0.f, Float::sum);
  369. int amountOfFlexibilities = getAmountOfConsumptionFlexibilities();
  370. return (amountOfFlexibilities > 0)? sum / (float) amountOfFlexibilities : 0.f;
  371. }
  372. public float getVarianzInFlexibilitieProduction() {
  373. float average = getAverageFlexibilityProduction();
  374. float sum = getListOfEnergyInProductionThatIsOfferedByFlexibilitiesInThisNetwork().stream().map(energy -> squared(energy - average)).reduce(0.f, Float::sum);
  375. int amountOfFlexibilities = getAmountOfProductionFlexibilities();
  376. return (amountOfFlexibilities > 0)? sum / (float) amountOfFlexibilities : 0.f;
  377. }
  378. public float getDiviationInFlexibilityConsumption() {
  379. return (float) Math.sqrt(getVarianzInFlexibilitieConsumption());
  380. }
  381. public float getDiviationInFlexibilityProduction() {
  382. return (float) Math.sqrt(getVarianzInFlexibilitieProduction());
  383. }
  384. //Help Function
  385. private float squared(float input) {
  386. return (float) Math.pow(input, 2);
  387. }
  388. }