123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- 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<MinimumModel> 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<Holon, ArrayList<Float>> averagesPerHolon;
- private HashMap<Holon, ArrayList<Integer>> mergesPerHolon, splitsPerHolon, indePerHolon, kickPerHolon, flexesPerHolon, statesPerHolon;
- private ArrayList<Float> avgPower;
- private ArrayList<Integer> indeHolons, offeredFlexes, usedFlexes, coolFlexes;
- private ArrayList<Float> avgHolonSize, powerProd, powerCon;
- public Stats() {
- this.averagesPerHolon = new HashMap<Holon, ArrayList<Float>>();
- this.mergesPerHolon = new HashMap<Holon, ArrayList<Integer>>();
- this.splitsPerHolon = new HashMap<Holon, ArrayList<Integer>>();
- this.flexesPerHolon = new HashMap<Holon, ArrayList<Integer>>();
- this.statesPerHolon = new HashMap<Holon, ArrayList<Integer>>();
- this.indePerHolon = new HashMap<Holon, ArrayList<Integer>>();
- this.kickPerHolon = new HashMap<Holon, ArrayList<Integer>>();
- 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<MinimumModel> 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<MinimumModel, Float> 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<Holon> 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<Float> 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<Integer> 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<Integer> 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<Integer> 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<Integer> 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<Integer> 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<Integer> 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);
- }
- }
- }
|