DecoratedNetwork.java 19 KB

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