DecoratedNetwork.java 19 KB

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