Main.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. package holon_control_unit_evaluation;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.io.PrintStream;
  6. import java.util.ArrayList;
  7. import java.util.HashMap;
  8. import java.util.List;
  9. import javax.swing.UIManager;
  10. import javax.swing.UnsupportedLookAndFeelException;
  11. import org.apache.commons.compress.archivers.ArchiveException;
  12. import classes.Holon;
  13. import classes.HolonObject;
  14. import ui.controller.Control;
  15. import ui.controller.FlexManager.FlexState;
  16. import ui.model.Consumer;
  17. import ui.model.MinimumModel;
  18. import ui.model.Model;
  19. import ui.model.Passiv;
  20. import ui.model.Supplier;
  21. import ui.model.VisualRepresentationalState;
  22. import ui.view.GUI;
  23. import ui.view.IndexTranslator;
  24. public class Main {
  25. public static void main(String[] args) throws FileNotFoundException {
  26. Stats stats = new Stats();
  27. if (!System.getProperty("os.name").startsWith("Linux")) {
  28. try {
  29. UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  30. } catch(ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. String fName = "scenario03-50h-3";
  35. String fileName = "C:\\Users\\Jonas\\Dropbox\\Mein PC (DESKTOP-L7IQHES)\\Documents\\Uni\\TUD\\Bachelorarbeit\\bachelor-evaluation\\Eval\\inputs\\"+fName+"_"+System.currentTimeMillis();
  36. String path = "C:\\Users\\Jonas\\Dropbox\\Mein PC (DESKTOP-L7IQHES)\\Documents\\Uni\\TUD\\Bachelorarbeit\\HOLEG\\exampleNetworks\\MyExamples\\"+fName+".holon";
  37. PrintStream systemOut = System.out;
  38. Model model = new Model();
  39. Control control = new Control(model);
  40. GUI view = new GUI(control);
  41. IndexTranslator.model = model;
  42. view.getFrmCyberPhysical().setVisible(true);
  43. File outputFile = new File(fileName+".txt");
  44. PrintStream printStream = new PrintStream(outputFile);
  45. System.setOut(printStream);
  46. for(int i=0; i<1; i++) {
  47. try {
  48. control.loadFile(path);
  49. } catch (IOException | ArchiveException e) {
  50. // TODO Auto-generated catch block
  51. e.printStackTrace();
  52. return;
  53. }
  54. for(int j=0; j<100; j++) {
  55. control.getSimManager().calculateStateForTimeStep(j, true);
  56. VisualRepresentationalState visualState = control.getSimManager().getActualVisualRepresentationalState();
  57. ArrayList<MinimumModel> independentHolarchies = control.getSimManager().getIndependentHolarchies(j);
  58. eval(visualState, independentHolarchies, control.getModel().getStateHolon(), j, stats, j >= 99);
  59. }
  60. control = new Control(model);
  61. }
  62. printStream.close();
  63. System.setOut(systemOut);
  64. }
  65. private static class Stats {
  66. private HashMap<Holon, ArrayList<Float>> averagesPerHolon;
  67. private HashMap<Holon, ArrayList<Integer>> mergesPerHolon, splitsPerHolon, indePerHolon, kickPerHolon, flexesPerHolon, statesPerHolon;
  68. private ArrayList<Float> avgPower;
  69. private ArrayList<Integer> indeHolons, offeredFlexes, usedFlexes, coolFlexes;
  70. private ArrayList<Float> avgHolonSize, powerProd, powerCon;
  71. public Stats() {
  72. this.averagesPerHolon = new HashMap<Holon, ArrayList<Float>>();
  73. this.mergesPerHolon = new HashMap<Holon, ArrayList<Integer>>();
  74. this.splitsPerHolon = new HashMap<Holon, ArrayList<Integer>>();
  75. this.flexesPerHolon = new HashMap<Holon, ArrayList<Integer>>();
  76. this.statesPerHolon = new HashMap<Holon, ArrayList<Integer>>();
  77. this.indePerHolon = new HashMap<Holon, ArrayList<Integer>>();
  78. this.kickPerHolon = new HashMap<Holon, ArrayList<Integer>>();
  79. this.avgPower = new ArrayList<>();
  80. this.indeHolons = new ArrayList<>();
  81. this.avgHolonSize = new ArrayList<>();
  82. this.offeredFlexes = new ArrayList<>();
  83. this.usedFlexes = new ArrayList<>();
  84. this.coolFlexes = new ArrayList<>();
  85. this.powerProd = new ArrayList<>();
  86. this.powerCon = new ArrayList<>();
  87. }
  88. }
  89. private static void eval(VisualRepresentationalState visualState, ArrayList<MinimumModel> independentHolarchies,
  90. Holon stateHolon, int timeStep, Stats stats, boolean print) {
  91. if(print) {
  92. System.out.println("\n=========================================================================\n");
  93. }
  94. //evaluate power usage in each holon object
  95. float consumed = 0f, produced = 0f;
  96. for(Consumer con : visualState.getConsumerList()) {
  97. evalPower(con.getModel().holon, con.getSupplyBarPercentage(), timeStep, stats, print);
  98. consumed += con.getEnergyFromConsumingElemnets();
  99. produced += con.getEnergySelfSupplied();
  100. }
  101. HashMap<MinimumModel, Float> map = new HashMap<>();
  102. for(Supplier sup : visualState.getSupplierList()) {
  103. //find the holarchy where the sup is
  104. MinimumModel holarchy = null;
  105. for(MinimumModel mm : independentHolarchies) {
  106. if(mm.getHolonObjectList().contains(sup.getModel()))
  107. holarchy = mm;
  108. }
  109. //find out how much energy is produced and consumed in total inside this holarchy
  110. //c/p is satisfaction of supplier
  111. float sat = 0f;
  112. if(holarchy != null) {
  113. if(map.containsKey(holarchy)) {
  114. sat = map.get(holarchy);
  115. } else {
  116. float totalProd = 0f;
  117. float totalCon = 0f;
  118. for(HolonObject ho : holarchy.getHolonObjectList()) {
  119. float p = ho.getEnergyAtTimeStepFlex(timeStep);
  120. if(p > 0) {
  121. totalProd += p;
  122. } else {
  123. totalCon -= p;
  124. }
  125. }
  126. sat = totalCon/totalProd;
  127. map.put(holarchy, sat);
  128. }
  129. }
  130. evalPower(sup.getModel().holon, sat, timeStep, stats, print);
  131. consumed += sup.getEnergySelfConsuming();
  132. produced += sup.getEnergyProducing();
  133. }
  134. for(Passiv pas : visualState.getPassivList()) {
  135. evalPower(pas.getModel().holon, pas.getEnergy(), timeStep, stats, print);
  136. }
  137. float avg = produced/consumed;
  138. if(timeStep >= stats.avgPower.size()) {
  139. stats.avgPower.add(avg);
  140. } else {
  141. stats.avgPower.remove(timeStep);
  142. stats.avgPower.add(timeStep, avg);
  143. }
  144. if(timeStep >= stats.powerProd.size()) {
  145. stats.powerProd.add(produced);
  146. } else {
  147. stats.powerProd.remove(timeStep);
  148. stats.powerProd.add(timeStep, produced);
  149. }
  150. if(timeStep >= stats.powerCon.size()) {
  151. stats.powerCon.add(consumed);
  152. } else {
  153. stats.powerCon.remove(timeStep);
  154. stats.powerCon.add(timeStep, consumed);
  155. }
  156. int indeHolons = stateHolon.getChildCount();
  157. if(timeStep >= stats.indeHolons.size()) {
  158. stats.indeHolons.add(indeHolons);
  159. } else {
  160. stats.indeHolons.remove(timeStep);
  161. stats.indeHolons.add(indeHolons);
  162. }
  163. if(timeStep >= stats.avgHolonSize.size()) {
  164. stats.avgHolonSize.add((float) 21/indeHolons);
  165. } else {
  166. stats.avgHolonSize.remove(timeStep);
  167. stats.avgHolonSize.add((float) 21/indeHolons);
  168. }
  169. int counterOffered = 0, counterUsed = 0, counterCool = 0;
  170. List<Holon> queue = new ArrayList<>();
  171. queue.addAll(stateHolon.childHolons);
  172. Holon curr = queue.remove(0);
  173. while(curr != null) {
  174. queue.addAll(curr.childHolons);
  175. counterOffered += curr.holonControlUnit.getFlexMan().getAmountFlexesForState(timeStep, FlexState.OFFERED);
  176. counterUsed += curr.holonControlUnit.getFlexMan().getAmountFlexesForState(timeStep, FlexState.IN_USE);
  177. counterCool += curr.holonControlUnit.getFlexMan().getAmountFlexesForState(timeStep, FlexState.ON_COOLDOWN);
  178. curr = queue.size() > 0 ? queue.remove(0) : null;
  179. }
  180. if(timeStep >= stats.offeredFlexes.size()) {
  181. stats.offeredFlexes.add(counterOffered);
  182. } else {
  183. stats.offeredFlexes.remove(timeStep);
  184. stats.offeredFlexes.add(counterOffered);
  185. }
  186. if(timeStep >= stats.usedFlexes.size()) {
  187. stats.usedFlexes.add(counterUsed);
  188. } else {
  189. stats.usedFlexes.remove(timeStep);
  190. stats.usedFlexes.add(counterUsed);
  191. }
  192. if(timeStep >= stats.coolFlexes.size()) {
  193. stats.coolFlexes.add(counterCool);
  194. } else {
  195. stats.coolFlexes.remove(timeStep);
  196. stats.coolFlexes.add(counterCool);
  197. }
  198. if(print) {
  199. System.out.println("TOTAL: "+stats.avgPower+"\nHolons: "+stats.indeHolons+"\nHolons: "+stats.avgHolonSize
  200. +"\noffered flexes: "+stats.offeredFlexes+"\nused flexes: "+stats.usedFlexes+"\ncool flexes: "+stats.coolFlexes
  201. +"\nTotal power produced: "+stats.powerProd+"\nTotal power consumed: "+stats.powerCon);
  202. }
  203. }
  204. private static void evalPower(Holon h, float curr, int timeStep, Stats stats, boolean print) {
  205. ArrayList<Float> avgList = stats.averagesPerHolon.getOrDefault(h, new ArrayList<>());
  206. if(timeStep >= avgList.size()) {
  207. avgList.add(curr);
  208. } else {
  209. avgList.remove(timeStep);
  210. avgList.add(timeStep, curr);
  211. }
  212. stats.averagesPerHolon.put(h, avgList);
  213. ArrayList<Integer> mergeList = stats.mergesPerHolon.getOrDefault(h, new ArrayList<>());
  214. int merges = h.getMergeCounter();// - ((timeStep-1 >= 0 && mergeList.size() > timeStep-1) ? mergeList.get(timeStep-1) : 0);
  215. if(timeStep >= mergeList.size()) {
  216. mergeList.add(merges);
  217. } else {
  218. mergeList.remove(timeStep);
  219. mergeList.add(timeStep, merges);
  220. }
  221. stats.mergesPerHolon.put(h, mergeList);
  222. ArrayList<Integer> splitList = stats.splitsPerHolon.getOrDefault(h, new ArrayList<>());
  223. int splits = h.getSplitCounter();// - ((timeStep-1 >= 0 && splitList.size() > timeStep-1) ? splitList.get(timeStep-1) : 0);
  224. if(timeStep >= splitList.size()) {
  225. splitList.add(splits);
  226. } else {
  227. splitList.remove(timeStep);
  228. splitList.add(timeStep, splits);
  229. }
  230. stats.splitsPerHolon.put(h, splitList);
  231. ArrayList<Integer> indeList = stats.indePerHolon.getOrDefault(h, new ArrayList<>());
  232. int indes = h.getIndeCounter();// - ((timeStep-1 >= 0 && splitList.size() > timeStep-1) ? splitList.get(timeStep-1) : 0);
  233. if(timeStep >= indeList.size()) {
  234. indeList.add(indes);
  235. } else {
  236. indeList.remove(timeStep);
  237. indeList.add(timeStep, indes);
  238. }
  239. stats.indePerHolon.put(h, indeList);
  240. ArrayList<Integer> kickList = stats.kickPerHolon.getOrDefault(h, new ArrayList<>());
  241. int kicks = h.getKickCounter();// - ((timeStep-1 >= 0 && splitList.size() > timeStep-1) ? splitList.get(timeStep-1) : 0);
  242. if(timeStep >= kickList.size()) {
  243. kickList.add(kicks);
  244. } else {
  245. kickList.remove(timeStep);
  246. kickList.add(timeStep, kicks);
  247. }
  248. stats.kickPerHolon.put(h, kickList);
  249. ArrayList<Integer> flexList = stats.flexesPerHolon.getOrDefault(h, new ArrayList<>());
  250. int flexes = h.holonControlUnit.getFlexMan().getAppliedFlexCounter();// - ((timeStep-1 >= 0 && splitList.size() > timeStep-1) ? splitList.get(timeStep-1) : 0);
  251. if(timeStep >= flexList.size()) {
  252. flexList.add(flexes);
  253. } else {
  254. flexList.remove(timeStep);
  255. flexList.add(timeStep, flexes);
  256. }
  257. stats.flexesPerHolon.put(h, flexList);
  258. ArrayList<Integer> stateList = stats.statesPerHolon.getOrDefault(h, new ArrayList<>());
  259. int states = h.stateEval();
  260. if(timeStep >= stateList.size()) {
  261. stateList.add(states);
  262. } else {
  263. stateList.remove(timeStep);
  264. stateList.add(timeStep, states);
  265. }
  266. stats.statesPerHolon.put(h, stateList);
  267. // float avg = 0f;
  268. // for(float f:list)
  269. // avg += f;
  270. // avg = avg/list.size();
  271. // float dev = curr-1f;
  272. // h.countState(dev);
  273. if(print) {
  274. // System.out.println("next: "+h.getUniqueID()+"\n\tpower stats: "+curr+"\tavg: "+avg+"\t"+list+"\n\tmerges: "+h.getMergeCounter()
  275. // +"\tsplits: "+h.getSplitCounter()+"\n\tDeviation: "+dev+"\t["+h.getStateCounter()[0]+", "+h.getStateCounter()[1]+", "
  276. // +h.getStateCounter()[2]+"]\n\tHolarchy: "
  277. // +h.getMinimumModel().getHolonObjectList().stream().map(ho -> ho.holon.getUniqueID()).collect(Collectors.toList()));
  278. System.out.println("next: "+h.getUniqueID()
  279. +"\n\tpower stats: "+avgList
  280. +"\n\tmerges: "+mergeList
  281. +"\n\tsplits: "+splitList
  282. +"\n\tflexes: "+flexList
  283. +"\n\tstates: "+stateList
  284. +"\n\tindes: "+indeList
  285. +"\n\tkicks: "+kickList);
  286. }
  287. }
  288. }