package holon_control_unit_evaluation; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.PrintStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; import org.apache.commons.compress.archivers.ArchiveException; import classes.Holon; import classes.HolonObject; import ui.controller.Control; import ui.controller.FlexManager.FlexState; import ui.model.Consumer; import ui.model.MinimumModel; import ui.model.Model; import ui.model.Passiv; import ui.model.Supplier; import ui.model.VisualRepresentationalState; import ui.view.GUI; import ui.view.IndexTranslator; public class Main { public static void main(String[] args) throws FileNotFoundException { Stats stats = new Stats(); if (!System.getProperty("os.name").startsWith("Linux")) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch(ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException e) { e.printStackTrace(); } } String fName = "scenario03-50h-3"; String fileName = "C:\\Users\\Jonas\\Dropbox\\Mein PC (DESKTOP-L7IQHES)\\Documents\\Uni\\TUD\\Bachelorarbeit\\bachelor-evaluation\\Eval\\inputs\\"+fName+"_"+System.currentTimeMillis(); String path = "C:\\Users\\Jonas\\Dropbox\\Mein PC (DESKTOP-L7IQHES)\\Documents\\Uni\\TUD\\Bachelorarbeit\\HOLEG\\exampleNetworks\\MyExamples\\"+fName+".holon"; PrintStream systemOut = System.out; Model model = new Model(); Control control = new Control(model); GUI view = new GUI(control); IndexTranslator.model = model; view.getFrmCyberPhysical().setVisible(true); File outputFile = new File(fileName+".txt"); PrintStream printStream = new PrintStream(outputFile); System.setOut(printStream); for(int i=0; i<1; i++) { try { control.loadFile(path); } catch (IOException | ArchiveException e) { // TODO Auto-generated catch block e.printStackTrace(); return; } for(int j=0; j<100; j++) { control.getSimManager().calculateStateForTimeStep(j, true); VisualRepresentationalState visualState = control.getSimManager().getActualVisualRepresentationalState(); ArrayList independentHolarchies = control.getSimManager().getIndependentHolarchies(j); eval(visualState, independentHolarchies, control.getModel().getStateHolon(), j, stats, j >= 99); } control = new Control(model); } printStream.close(); System.setOut(systemOut); } private static class Stats { private HashMap> averagesPerHolon; private HashMap> mergesPerHolon, splitsPerHolon, indePerHolon, kickPerHolon, flexesPerHolon, statesPerHolon; private ArrayList avgPower; private ArrayList indeHolons, offeredFlexes, usedFlexes, coolFlexes; private ArrayList avgHolonSize, powerProd, powerCon; public Stats() { this.averagesPerHolon = new HashMap>(); this.mergesPerHolon = new HashMap>(); this.splitsPerHolon = new HashMap>(); this.flexesPerHolon = new HashMap>(); this.statesPerHolon = new HashMap>(); this.indePerHolon = new HashMap>(); this.kickPerHolon = new HashMap>(); this.avgPower = new ArrayList<>(); this.indeHolons = new ArrayList<>(); this.avgHolonSize = new ArrayList<>(); this.offeredFlexes = new ArrayList<>(); this.usedFlexes = new ArrayList<>(); this.coolFlexes = new ArrayList<>(); this.powerProd = new ArrayList<>(); this.powerCon = new ArrayList<>(); } } private static void eval(VisualRepresentationalState visualState, ArrayList independentHolarchies, Holon stateHolon, int timeStep, Stats stats, boolean print) { if(print) { System.out.println("\n=========================================================================\n"); } //evaluate power usage in each holon object float consumed = 0f, produced = 0f; for(Consumer con : visualState.getConsumerList()) { evalPower(con.getModel().holon, con.getSupplyBarPercentage(), timeStep, stats, print); consumed += con.getEnergyFromConsumingElemnets(); produced += con.getEnergySelfSupplied(); } HashMap map = new HashMap<>(); for(Supplier sup : visualState.getSupplierList()) { //find the holarchy where the sup is MinimumModel holarchy = null; for(MinimumModel mm : independentHolarchies) { if(mm.getHolonObjectList().contains(sup.getModel())) holarchy = mm; } //find out how much energy is produced and consumed in total inside this holarchy //c/p is satisfaction of supplier float sat = 0f; if(holarchy != null) { if(map.containsKey(holarchy)) { sat = map.get(holarchy); } else { float totalProd = 0f; float totalCon = 0f; for(HolonObject ho : holarchy.getHolonObjectList()) { float p = ho.getEnergyAtTimeStepFlex(timeStep); if(p > 0) { totalProd += p; } else { totalCon -= p; } } sat = totalCon/totalProd; map.put(holarchy, sat); } } evalPower(sup.getModel().holon, sat, timeStep, stats, print); consumed += sup.getEnergySelfConsuming(); produced += sup.getEnergyProducing(); } for(Passiv pas : visualState.getPassivList()) { evalPower(pas.getModel().holon, pas.getEnergy(), timeStep, stats, print); } float avg = produced/consumed; if(timeStep >= stats.avgPower.size()) { stats.avgPower.add(avg); } else { stats.avgPower.remove(timeStep); stats.avgPower.add(timeStep, avg); } if(timeStep >= stats.powerProd.size()) { stats.powerProd.add(produced); } else { stats.powerProd.remove(timeStep); stats.powerProd.add(timeStep, produced); } if(timeStep >= stats.powerCon.size()) { stats.powerCon.add(consumed); } else { stats.powerCon.remove(timeStep); stats.powerCon.add(timeStep, consumed); } int indeHolons = stateHolon.getChildCount(); if(timeStep >= stats.indeHolons.size()) { stats.indeHolons.add(indeHolons); } else { stats.indeHolons.remove(timeStep); stats.indeHolons.add(indeHolons); } if(timeStep >= stats.avgHolonSize.size()) { stats.avgHolonSize.add((float) 21/indeHolons); } else { stats.avgHolonSize.remove(timeStep); stats.avgHolonSize.add((float) 21/indeHolons); } int counterOffered = 0, counterUsed = 0, counterCool = 0; List queue = new ArrayList<>(); queue.addAll(stateHolon.childHolons); Holon curr = queue.remove(0); while(curr != null) { queue.addAll(curr.childHolons); counterOffered += curr.holonControlUnit.getFlexMan().getAmountFlexesForState(timeStep, FlexState.OFFERED); counterUsed += curr.holonControlUnit.getFlexMan().getAmountFlexesForState(timeStep, FlexState.IN_USE); counterCool += curr.holonControlUnit.getFlexMan().getAmountFlexesForState(timeStep, FlexState.ON_COOLDOWN); curr = queue.size() > 0 ? queue.remove(0) : null; } if(timeStep >= stats.offeredFlexes.size()) { stats.offeredFlexes.add(counterOffered); } else { stats.offeredFlexes.remove(timeStep); stats.offeredFlexes.add(counterOffered); } if(timeStep >= stats.usedFlexes.size()) { stats.usedFlexes.add(counterUsed); } else { stats.usedFlexes.remove(timeStep); stats.usedFlexes.add(counterUsed); } if(timeStep >= stats.coolFlexes.size()) { stats.coolFlexes.add(counterCool); } else { stats.coolFlexes.remove(timeStep); stats.coolFlexes.add(counterCool); } if(print) { System.out.println("TOTAL: "+stats.avgPower+"\nHolons: "+stats.indeHolons+"\nHolons: "+stats.avgHolonSize +"\noffered flexes: "+stats.offeredFlexes+"\nused flexes: "+stats.usedFlexes+"\ncool flexes: "+stats.coolFlexes +"\nTotal power produced: "+stats.powerProd+"\nTotal power consumed: "+stats.powerCon); } } private static void evalPower(Holon h, float curr, int timeStep, Stats stats, boolean print) { ArrayList avgList = stats.averagesPerHolon.getOrDefault(h, new ArrayList<>()); if(timeStep >= avgList.size()) { avgList.add(curr); } else { avgList.remove(timeStep); avgList.add(timeStep, curr); } stats.averagesPerHolon.put(h, avgList); ArrayList mergeList = stats.mergesPerHolon.getOrDefault(h, new ArrayList<>()); int merges = h.getMergeCounter();// - ((timeStep-1 >= 0 && mergeList.size() > timeStep-1) ? mergeList.get(timeStep-1) : 0); if(timeStep >= mergeList.size()) { mergeList.add(merges); } else { mergeList.remove(timeStep); mergeList.add(timeStep, merges); } stats.mergesPerHolon.put(h, mergeList); ArrayList splitList = stats.splitsPerHolon.getOrDefault(h, new ArrayList<>()); int splits = h.getSplitCounter();// - ((timeStep-1 >= 0 && splitList.size() > timeStep-1) ? splitList.get(timeStep-1) : 0); if(timeStep >= splitList.size()) { splitList.add(splits); } else { splitList.remove(timeStep); splitList.add(timeStep, splits); } stats.splitsPerHolon.put(h, splitList); ArrayList indeList = stats.indePerHolon.getOrDefault(h, new ArrayList<>()); int indes = h.getIndeCounter();// - ((timeStep-1 >= 0 && splitList.size() > timeStep-1) ? splitList.get(timeStep-1) : 0); if(timeStep >= indeList.size()) { indeList.add(indes); } else { indeList.remove(timeStep); indeList.add(timeStep, indes); } stats.indePerHolon.put(h, indeList); ArrayList kickList = stats.kickPerHolon.getOrDefault(h, new ArrayList<>()); int kicks = h.getKickCounter();// - ((timeStep-1 >= 0 && splitList.size() > timeStep-1) ? splitList.get(timeStep-1) : 0); if(timeStep >= kickList.size()) { kickList.add(kicks); } else { kickList.remove(timeStep); kickList.add(timeStep, kicks); } stats.kickPerHolon.put(h, kickList); ArrayList flexList = stats.flexesPerHolon.getOrDefault(h, new ArrayList<>()); int flexes = h.holonControlUnit.getFlexMan().getAppliedFlexCounter();// - ((timeStep-1 >= 0 && splitList.size() > timeStep-1) ? splitList.get(timeStep-1) : 0); if(timeStep >= flexList.size()) { flexList.add(flexes); } else { flexList.remove(timeStep); flexList.add(timeStep, flexes); } stats.flexesPerHolon.put(h, flexList); ArrayList stateList = stats.statesPerHolon.getOrDefault(h, new ArrayList<>()); int states = h.stateEval(); if(timeStep >= stateList.size()) { stateList.add(states); } else { stateList.remove(timeStep); stateList.add(timeStep, states); } stats.statesPerHolon.put(h, stateList); // float avg = 0f; // for(float f:list) // avg += f; // avg = avg/list.size(); // float dev = curr-1f; // h.countState(dev); if(print) { // System.out.println("next: "+h.getUniqueID()+"\n\tpower stats: "+curr+"\tavg: "+avg+"\t"+list+"\n\tmerges: "+h.getMergeCounter() // +"\tsplits: "+h.getSplitCounter()+"\n\tDeviation: "+dev+"\t["+h.getStateCounter()[0]+", "+h.getStateCounter()[1]+", " // +h.getStateCounter()[2]+"]\n\tHolarchy: " // +h.getMinimumModel().getHolonObjectList().stream().map(ho -> ho.holon.getUniqueID()).collect(Collectors.toList())); System.out.println("next: "+h.getUniqueID() +"\n\tpower stats: "+avgList +"\n\tmerges: "+mergeList +"\n\tsplits: "+splitList +"\n\tflexes: "+flexList +"\n\tstates: "+stateList +"\n\tindes: "+indeList +"\n\tkicks: "+kickList); } } }