DecoratedNetwork.java 20 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 holeg.HolegPowerFlowContext;
  9. import ui.controller.FlexManager;
  10. import ui.model.DecoratedCable.CableState;
  11. import ui.model.DecoratedHolonObject.HolonObjectState;
  12. import ui.model.Model.FairnessModel;
  13. public class DecoratedNetwork {
  14. private ArrayList<Supplier> supplierList = new ArrayList<Supplier>();
  15. private ArrayList<Consumer> consumerList = new ArrayList<Consumer>();
  16. private ArrayList<Consumer> consumerSelfSuppliedList = new ArrayList<Consumer>();
  17. private ArrayList<Passiv> passivNoEnergyList = new ArrayList<Passiv>();
  18. private ArrayList<DecoratedCable> decoratedCableList = new ArrayList<DecoratedCable>();
  19. private int timestep;
  20. public DecoratedNetwork(HolegPowerFlowContext context, MinimumNetwork minimumNetwork, int Iteration, FairnessModel actualFairnessModel, FlexManager flexManager){
  21. this.timestep = Iteration;
  22. switch(actualFairnessModel) {
  23. case AllEqual:
  24. calculateAllEqualNetwork(minimumNetwork, Iteration, flexManager);
  25. break;
  26. case MininumDemandFirst:
  27. calculateMinimumDemandFirstNetwork(minimumNetwork, Iteration, flexManager);
  28. break;
  29. case PowerFlowAnalysis:
  30. // TODO: (Henrik)
  31. HolegGateway.solve(context, minimumNetwork, Iteration, flexManager, this);
  32. calculateStates();
  33. break;
  34. }
  35. }
  36. //Getter:
  37. public ArrayList<Supplier> getSupplierList() {
  38. return supplierList;
  39. }
  40. public ArrayList<Consumer> getConsumerList() {
  41. return consumerList;
  42. }
  43. public ArrayList<Consumer> getConsumerSelfSuppliedList() {
  44. return consumerSelfSuppliedList;
  45. }
  46. public ArrayList<Passiv> getPassivNoEnergyList() {
  47. return passivNoEnergyList;
  48. }
  49. public ArrayList<DecoratedCable> getDecoratedCableList(){
  50. return decoratedCableList;
  51. }
  52. //Calculations:
  53. private void calculateMinimumDemandFirstNetwork(MinimumNetwork minimumNetwork, int Iteration, FlexManager flexManager) {
  54. categorize(minimumNetwork, Iteration, flexManager);
  55. //Sort SupplierList according to the EnergyToSupplyNetwork maximum first.
  56. //Sort ConsumerList according to the MinimumConsumingElementEnergy minimum first.
  57. supplierList.sort((Supplier lhs,Supplier rhs) -> -Float.compare(lhs.getEnergyToSupplyNetwork(), rhs.getEnergyToSupplyNetwork()));
  58. consumerList.sort((Consumer lhs,Consumer rhs) -> Float.compare(lhs.getMinimumConsumingElementEnergy() , rhs.getMinimumConsumingElementEnergy()));
  59. //consumerList.forEach((con) -> System.out.println(con.getMinimumConsumingElementEnergy()));
  60. //consumerList.forEach((con) -> System.out.println("AfterSorting" + con));
  61. float energyToSupplyInTheNetwork = supplierList.stream().map(supplier -> supplier.getEnergyToSupplyNetwork() - supplier.getEnergySupplied()).reduce( 0.0f, (a, b) -> a + b);
  62. decorateCable(minimumNetwork, energyToSupplyInTheNetwork);
  63. outerLoop:
  64. for(Consumer con : consumerList)
  65. {
  66. //gehe Supplier list durch wer ihn supplien kann.
  67. for(Supplier sup : supplierList) {
  68. float energyRdyToSupply = sup.getEnergyToSupplyNetwork() - sup.getEnergySupplied();
  69. if(energyRdyToSupply == 0.0f) continue;
  70. float energyNeededForMinimumConsumingElement=con.getMinimumConsumingElementEnergy()-con.getEnergyFromNetwork();
  71. if(energyNeededForMinimumConsumingElement>energyToSupplyInTheNetwork) {
  72. //Dont supply a minimumElement when you cant supply it fully
  73. break outerLoop;
  74. }
  75. if(energyRdyToSupply>=energyNeededForMinimumConsumingElement) {
  76. energyToSupplyInTheNetwork -= energyNeededForMinimumConsumingElement;
  77. supply(con, sup, energyNeededForMinimumConsumingElement);
  78. continue outerLoop;
  79. }else
  80. {
  81. energyToSupplyInTheNetwork -= energyRdyToSupply;
  82. supply(con, sup, energyRdyToSupply);
  83. }
  84. }
  85. //No more Energy in the network
  86. break;
  87. }
  88. //consumerList.forEach((con) -> System.out.println("AfterSuppliing MinimumDemand " + con));
  89. //Sort ConsumerList according to the EnergyNeeded to supply fully after minimum Demand First.
  90. consumerList.sort((Consumer lhs,Consumer rhs) -> Float.compare(lhs.getEnergyNeededFromNetwork()-lhs.getEnergyFromNetwork() , rhs.getEnergyNeededFromNetwork()-rhs.getEnergyFromNetwork() ));
  91. //Supply consumer fully
  92. outerLoop:
  93. for(Consumer con : consumerList)
  94. {
  95. //gehe Supplier list durch wer ihn supplien kann.
  96. for(Supplier sup : supplierList) {
  97. float energyRdyToSupply = sup.getEnergyToSupplyNetwork() - sup.getEnergySupplied();
  98. if(energyRdyToSupply == 0.0f) continue;
  99. float energyNeededForFullySupply = con.getEnergyNeededFromNetwork() - con.getEnergyFromNetwork();
  100. if(energyNeededForFullySupply == 0.0f) continue outerLoop;
  101. if(energyRdyToSupply>=energyNeededForFullySupply) {
  102. supply(con, sup, energyNeededForFullySupply);
  103. continue outerLoop;
  104. }else
  105. {
  106. supply(con, sup, energyRdyToSupply);
  107. }
  108. }
  109. //No more Energy in the network
  110. break;
  111. }
  112. //consumerList.forEach((con) -> System.out.println("AfterFullySuplieing" + con));
  113. //If Energy Left Supply all equal
  114. //Count EnergyLeft
  115. float energyLeft = supplierList.stream().map(supplier -> supplier.getEnergyToSupplyNetwork() - supplier.getEnergySupplied()).reduce( 0.0f, (a, b) -> a + b);
  116. //System.out.println("EnergyLeft: " + energyLeft);
  117. if(energyLeft > 0.0f && (consumerList.size() + consumerSelfSuppliedList.size() != 0))
  118. {
  119. float equalAmountOfEnergyToSupply = energyLeft / ((float)(consumerList.size() + consumerSelfSuppliedList.size()));
  120. outerLoop:
  121. for(Consumer con : consumerList)
  122. {
  123. //gehe Supplier list durch wer ihn supplien kann.
  124. for(Supplier sup : supplierList) {
  125. float energyRdyToSupply = sup.getEnergyToSupplyNetwork() - sup.getEnergySupplied();
  126. if(energyRdyToSupply == 0.0f) continue;
  127. float energyNeededToSupplyConsumerTheEqualAmount = equalAmountOfEnergyToSupply +con.getEnergyNeededFromNetwork()- con.getEnergyFromNetwork();
  128. if(energyRdyToSupply>=energyNeededToSupplyConsumerTheEqualAmount) {
  129. supply(con, sup, energyNeededToSupplyConsumerTheEqualAmount);
  130. continue outerLoop;
  131. }else
  132. {
  133. supply(con, sup, energyRdyToSupply);
  134. }
  135. }
  136. //No more Energy in the network
  137. break;
  138. }
  139. outerLoop:
  140. for(Consumer con : consumerSelfSuppliedList)
  141. {
  142. //gehe Supplier list durch wer ihn supplien kann.
  143. for(Supplier sup : supplierList) {
  144. float energyRdyToSupply = sup.getEnergyToSupplyNetwork() - sup.getEnergySupplied();
  145. if(energyRdyToSupply == 0.0f) continue;
  146. float energyNeededToSupplyConsumerTheEqualAmount = equalAmountOfEnergyToSupply +con.getEnergyNeededFromNetwork()- con.getEnergyFromNetwork();
  147. if(energyRdyToSupply>=energyNeededToSupplyConsumerTheEqualAmount) {
  148. supply(con, sup, energyNeededToSupplyConsumerTheEqualAmount);
  149. continue outerLoop;
  150. }else
  151. {
  152. supply(con, sup, energyRdyToSupply);
  153. }
  154. }
  155. //No more Energy in the network
  156. break;
  157. }
  158. }
  159. //consumerList.forEach((con) -> System.out.println("AfterOverSuppleiing" + con));
  160. //consumerSelfSuppliedList.forEach((con) -> System.out.println("AfterOverSuppleiing" + con));
  161. calculateStates();
  162. }
  163. private void decorateCable(MinimumNetwork minimumNetwork, float energyToSupplyInTheNetwork) {
  164. //DecoratedCables
  165. //Minimum demand first:
  166. for(IntermediateCableWithState edge: minimumNetwork.getEdgeList()) {
  167. decoratedCableList.add(new DecoratedCable(edge.getModel(), edge.getState(), (edge.getState() == CableState.Working) ? energyToSupplyInTheNetwork : 0.0f, 0));
  168. }
  169. }
  170. private void calculateAllEqualNetwork(MinimumNetwork minimumNetwork, int Iteration, FlexManager flexManager) {
  171. categorize(minimumNetwork, Iteration, flexManager);
  172. float energyToSupplyInTheNetwork = supplierList.stream().map(supplier -> supplier.getEnergyToSupplyNetwork() - supplier.getEnergySupplied()).reduce( 0.0f, (a, b) -> a + b);
  173. float energyForEachConsumer = (consumerList.size() != 0) ? energyToSupplyInTheNetwork / consumerList.size() : 0.0f;
  174. decorateCable(minimumNetwork, energyToSupplyInTheNetwork);
  175. //Supply consumer equal
  176. outerLoop:
  177. for(Consumer con : consumerList)
  178. {
  179. //gehe Supplier list durch wer ihn supplien kann.
  180. float energyNeededForEqualSupply = energyForEachConsumer;
  181. for(Supplier sup : supplierList) {
  182. float energyRdyToSupply = sup.getEnergyToSupplyNetwork() - sup.getEnergySupplied();
  183. if(energyRdyToSupply == 0.0f) continue;
  184. if(energyRdyToSupply>=energyNeededForEqualSupply) {
  185. supply(con, sup, energyNeededForEqualSupply);
  186. continue outerLoop;
  187. }else
  188. {
  189. supply(con, sup, energyRdyToSupply);
  190. energyNeededForEqualSupply -= energyRdyToSupply;
  191. }
  192. }
  193. //No more Energy in the network
  194. break;
  195. }
  196. calculateStates();
  197. }
  198. private void calculateStates() {
  199. //CalculateStates:
  200. supplierList.forEach(sup -> sup.setState(HolonObjectState.PRODUCER));
  201. passivNoEnergyList.forEach(sup -> sup.setState(HolonObjectState.NO_ENERGY));
  202. for(Consumer con : this.consumerList)
  203. {
  204. setConsumerState(con);
  205. }
  206. for(Consumer con : this.consumerSelfSuppliedList)
  207. {
  208. setConsumerState(con);
  209. }
  210. }
  211. private void categorize(MinimumNetwork minimumNetwork, int Iteration, FlexManager flexManager) {
  212. //Categorize
  213. for(HolonObject hObject: minimumNetwork.getHolonObjectList()) {
  214. float energyNeeded = hObject.getEnergyNeededFromConsumingElementsWithFlex(Iteration, flexManager);
  215. float energySelfProducing = hObject.getEnergySelfProducingFromProducingElementsWithFlex(Iteration, flexManager);
  216. if(energyNeeded < energySelfProducing) {
  217. Supplier sup = new Supplier(hObject, energySelfProducing - energyNeeded, energyNeeded, 0, 0, false);
  218. supplierList.add(sup);
  219. } else if (energyNeeded > energySelfProducing) {
  220. Consumer con = new Consumer(hObject, energyNeeded - energySelfProducing, 0, 0, 0,false);
  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, 0.0f, 0, 0,0,false);
  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. }