Browse Source

Adds Network Varianz and Diviation.

Tom Troppmann 4 years ago
parent
commit
9ed581d693

+ 31 - 0
src/exampleAlgorithms/InformationPanel.java

@@ -29,6 +29,7 @@ import ui.controller.FlexManager.FlexState;
 import ui.controller.FlexManager.FlexWrapper;
 import ui.model.DecoratedGroupNode;
 import ui.model.DecoratedHolonObject.HolonObjectState;
+import ui.model.DecoratedNetwork;
 import ui.model.DecoratedState;
 import ui.model.DecoratedSwitch.SwitchState;
 import ui.model.VisualRepresentationalState;
@@ -191,6 +192,7 @@ public class InformationPanel implements AddOn {
 	void updateEntrys() {
 		calculateValues();
 		entryList.forEach(entry -> entry.update());
+		printInfos();
 	}
 	
 	
@@ -271,6 +273,35 @@ public class InformationPanel implements AddOn {
 		
 	}
 	
+	private void printInfos(){
+		DecoratedState dState = control.getSimManager().getActualDecorState();
+		int count = 0;
+		for(DecoratedNetwork net :dState.getNetworkList()) {
+			System.out.print("net" + count++ + ":[");
+			for(float energy : net.getListOfEnergyThatIsOfferedByFlexibilitiesInThisNetwork()) {
+				System.out.print(energy +" ");
+			}
+			System.out.println("] ");
+			System.out.println("Consumption: " + net.getFlexibilityConsumptionCapacity());
+			System.out.println("Production: " + net.getFlexibilityProductionCapacity());
+			System.out.println("Average Consumption: " + net.getAverageFlexibilityConsumption());
+			System.out.println("Average Production: " + net.getAverageFlexibilityProduction());
+			System.out.println("Varianz Consumption: " + net.getVarianzInFlexibilitieConsumption());
+			System.out.println("Varianz Production: " + net.getVarianzInFlexibilitieProduction());
+			System.out.println("Diviation Consumption: " + net.getDiviationInFlexibilityConsumption());
+			System.out.println("Diviation Production: " + net.getDiviationInFlexibilityProduction());
+			
+			
+			
+			
+			
+		}
+		
+		
+	}
+	
+	
+	
 	
 
 	

+ 147 - 0
src/exampleAlgorithms/ObjectiveFunctionByCarlos.java

@@ -0,0 +1,147 @@
+package exampleAlgorithms;
+
+import ui.model.DecoratedNetwork;
+import ui.model.DecoratedState;
+import java.lang.Exception;
+
+public class ObjectiveFunctionByCarlos {
+	//Parameter
+	static float lambda;
+	//weight for f_g(H)
+	static float w1 = .2f, w2 = .2f, w3 = .2f, w4 = .2f, w5=.2f;
+	//kappas for squashing function
+	static float k1 = .2f, k2 = .2f, k3 = .2f, k4 = .2f, k5=.2f;
+	
+	
+	
+	
+	//pre-calculated parameters:
+	/** 
+	 * Pre calculated for the squash function
+	 * <br>
+	 *  {@link ObjectiveFunctionByCarlos#squash}
+	 */
+	static float squash_subtract = 1.0f / (1.f + (float) Math.exp(5.0));
+	
+	
+	
+	static {
+        //pre-calculations
+		System.out.println("Precalculations");
+		
+		
+		
+		double doubleBaseTest = Math.exp(300);
+		double doubleMax =  Double.MAX_VALUE;
+		float floatBaseTest = (float)Math.exp(300);
+		float floatMax =  Float.MAX_VALUE;
+		
+		
+		System.out.println("floatMax" + floatMax);
+		System.out.println("floatBaseTest" + floatBaseTest);
+		System.out.println("doubleMax" + doubleMax);
+		System.out.println("doubleBaseTest" + doubleBaseTest);
+		
+		
+		checkParameter();
+    }
+	
+	/**
+	 * Check parameter Setting and print error when wrong values are put in.
+	 * Here should all invariants be placed to be checked on initialization.
+	 */
+	private static void checkParameter() {
+		if(!(Math.abs(w1 + w2 + w3 + w4 + w5 - 1) < 0.001)) {
+			System.err.println("ParameterError in ObjectiveFunction: w1 + w2 + w3 + w4 + w5 should be 1");
+		}
+	}
+	
+	/**
+	 * ObjectifeFunction by Carlos.
+	 * Function computes f_g:
+	 * f_g = w1 * squash(f_eb, k1) + w2 * squash(f_state, k2) + w3 * squash(f_pro, k3) + w4 * squash(f_perf, k4) + w5 * squash(f_holon, k5) 
+	 * 
+	 * 
+	 * squash is the squashing function {@link ObjectiveFunctionByCarlos#squash}
+	 * 
+	 * 
+	 * @param state
+	 * @return f_g
+	 */
+	static public float getFitnessValueForState(DecoratedState state) {
+		
+		//Calculate f_eb the penalty for unbalenced energy in the network
+		
+		//TODO: Hier sollte zwischen den Netzwerken verschiedenen Holons unterschieden werden dies ist in den Formeln nicht wiedergegeben
+		// Kann somit schlechte und gute Netzwerke ausgleichen
+		// Implementierung ist wie im paper.
+		float f_eb = 0;
+		//sum over all objects
+		for(DecoratedNetwork net : state.getNetworkList()) {
+			f_eb += net.getConsumerList().stream().map(con -> con.getEnergySelfSupplied() - con.getEnergyFromConsumingElemnets()).reduce(0.f, Float::sum);
+			f_eb += net.getConsumerSelfSuppliedList().stream().map(con -> con.getEnergySelfSupplied() - con.getEnergyFromConsumingElemnets()).reduce(0.f, Float::sum);
+			f_eb += net.getSupplierList().stream().map(sup -> sup.getEnergyProducing() - sup.getEnergySelfConsuming()).reduce(0.f, Float::sum);
+		}
+		//abs
+		f_eb = (float) Math.abs(f_eb);
+		
+		//Calculate f_state the penalty function for the supply state
+		float f_state = 0;
+		for(DecoratedNetwork net : state.getNetworkList()) {
+			f_state += net.getConsumerList().stream().map(con -> supplyPenalty(con.getSupplyBarPercentage())).reduce(0.f, Float::sum);
+		}
+			
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		
+		return 0.0f + lambda;
+	}
+	
+	
+	/**
+	 * 
+	 * @param x the input
+	 * @param kappa the corresponding kappa
+	 * @return
+	 */
+	static public float squash(float x, float kappa) {
+		return 100.f/(1.0f + (float)Math.exp(-(10.f * (x - kappa/2.f))/ kappa)) - squash_subtract;
+	}
+	
+	/**
+	 * 
+	 * @param supplyPercentage from 0 to 1
+	 * @return
+	 */
+	static public float supplyPenalty(float supplyPercentage) {
+		float f_base = base(supplyPercentage);
+		return (float)(Math.pow(f_base, 100 - 100 * supplyPercentage) - Math.pow(f_base, 2)); 
+	}
+	/**
+	 * TODO: f_base wird im Paper mit e angegeben. f_eb mit exp() warum nicht einheitlich?
+	 * 
+	 * @param supplyPercentage from 0 to 1
+	 * @return 5 or 6 but with fancy math 
+	 */
+	static public float  base(float supplyPercentage) {
+		//TODO: Fancy kann aber leicht zu overflow fürhen denn e1000 ist zu groß für double max double 1.7976931348623157E308 und max float 3.4028235E38
+		//Oder BigDecimal aber müsste echt nicht sein.
+		double euler = Math.exp(1000.0 - 1000.0 * supplyPercentage);
+		return (float) ((5 * euler  + 6 ) /  (euler + 1));
+		
+		
+		
+		//Suggestion 
+		// return (supplyPercentage < 1.0f)? 5.0: 6.0; einfach und gut 
+		
+	}
+	
+}

+ 2 - 0
src/ui/controller/SimulationManager.java

@@ -201,6 +201,8 @@ public class SimulationManager {
 				if(lookAtNeighbor instanceof HolonObject) {
 					actualNetwork.getHolonObjectList().add((HolonObject) lookAtNeighbor);
 					holonObjectList.remove(lookAtNeighbor);
+				}else {
+					actualNetwork.getNodeAndSwitches().add(lookAtNeighbor);
 				}
 				//When HolonSwitch Check if closed
 				if(!(lookAtNeighbor instanceof HolonSwitch) || ((HolonSwitch)lookAtNeighbor).getState(Iteration)) {

+ 84 - 10
src/ui/model/DecoratedNetwork.java

@@ -1,7 +1,9 @@
 package ui.model;
 
 import java.util.ArrayList;
-
+import java.util.List;
+import java.util.stream.Collectors;
+import classes.HolonElement;
 import classes.HolonObject;
 import ui.controller.FlexManager;
 import ui.model.DecoratedCable.CableState;
@@ -14,8 +16,10 @@ public class DecoratedNetwork {
 	private ArrayList<Consumer> consumerSelfSuppliedList = new ArrayList<Consumer>();
 	private ArrayList<Passiv> passivNoEnergyList = new ArrayList<Passiv>();
 	private ArrayList<DecoratedCable> decoratedCableList = new ArrayList<DecoratedCable>();
+	private int timestep;
 
 	public DecoratedNetwork(MinimumNetwork minimumNetwork, int Iteration, FairnessModel actualFairnessModel, FlexManager flexManager){	
+		this.timestep = Iteration;
 		switch(actualFairnessModel) {
 		case AllEqual:
 			calculateAllEqualNetwork(minimumNetwork, Iteration, flexManager);
@@ -332,35 +336,105 @@ public class DecoratedNetwork {
 	 */
 	public float getVarianzInProductionInNetworkForHolonObjects() {
 		float average = getAverageProductionInNetworkForHolonObject();
-		float sum = consumerList.stream().map(con -> sqared(con.getEnergySelfSupplied() - average)).reduce(0.f, Float::sum) 
-				+ consumerSelfSuppliedList.stream().map(con -> sqared(con.getEnergySelfSupplied() - average)).reduce(0.f, Float::sum)
-				+ supplierList.stream().map(sup -> sqared(sup.getEnergyProducing() - average)).reduce(0.f,  Float::sum);
+		float sum = consumerList.stream().map(con -> squared(con.getEnergySelfSupplied() - average)).reduce(0.f, Float::sum) 
+				+ consumerSelfSuppliedList.stream().map(con -> squared(con.getEnergySelfSupplied() - average)).reduce(0.f, Float::sum)
+				+ supplierList.stream().map(sup -> squared(sup.getEnergyProducing() - average)).reduce(0.f,  Float::sum);
 		return sum / (float) getAmountOfHolonObjects();
 	}
 	
 	
-	public float getDispersionInProductionInNetworkForHolonObjects() {
+	public float getDiviationInProductionInNetworkForHolonObjects() {
 		return (float)Math.sqrt(getVarianzInProductionInNetworkForHolonObjects());
 	}
 	
 	
 	public float getVarianzInConsumptionInNetworkForHolonObjects() {
 		float average = getAverageConsumptionInNetworkForHolonObject();
-		float sum = consumerList.stream().map(con -> sqared(con.getEnergyFromConsumingElemnets() - average)).reduce(0.f, Float::sum) 
-				+ consumerSelfSuppliedList.stream().map(con -> sqared(con.getEnergyFromConsumingElemnets() - average)).reduce(0.f, Float::sum)
-				+ supplierList.stream().map(sup -> sqared(sup.getEnergySelfConsuming() - average)).reduce(0.f,  Float::sum);
+		float sum = consumerList.stream().map(con -> squared(con.getEnergyFromConsumingElemnets() - average)).reduce(0.f, Float::sum) 
+				+ consumerSelfSuppliedList.stream().map(con -> squared(con.getEnergyFromConsumingElemnets() - average)).reduce(0.f, Float::sum)
+				+ supplierList.stream().map(sup -> squared(sup.getEnergySelfConsuming() - average)).reduce(0.f,  Float::sum);
 		return sum / (float) getAmountOfHolonObjects();
 	}
 	
-	public float getDispersionInConsumptionInNetworkForHolonObjects() {
+	public float getDiviationInConsumptionInNetworkForHolonObjects() {
 		return (float)Math.sqrt(getVarianzInConsumptionInNetworkForHolonObjects());
 	}
 	
+	//HelperFunction
+	/**
+	 * 
+	 * @return a list of energy
+	 */
+	public List<Float> getListOfEnergyThatIsOfferedByFlexibilitiesInThisNetwork() {
+		List<HolonElement> eleList = consumerList.stream().flatMap(con-> con.getModel().getElements().stream()).collect(Collectors.toList());
+		eleList.addAll(consumerSelfSuppliedList.stream().flatMap(con-> con.getModel().getElements().stream()).collect(Collectors.toList()));
+		eleList.addAll(supplierList.stream().flatMap(con-> con.getModel().getElements().stream()).collect(Collectors.toList()));
+		eleList.addAll(passivNoEnergyList.stream().flatMap(con-> con.getModel().getElements().stream()).collect(Collectors.toList()));
+		return  eleList.stream().filter(ele -> (ele.flexList.stream().anyMatch(flex -> flex.offered && flex.fulfillsConstrains())) ).map(ele -> -ele.getEnergyAtTimeStep(timestep) ).collect(Collectors.toList()) ;
+	}
+	
+	public List<Float> getListOfEnergyInProductionThatIsOfferedByFlexibilitiesInThisNetwork(){
+		return getListOfEnergyThatIsOfferedByFlexibilitiesInThisNetwork().stream().filter(value -> (value > 0.f)).collect(Collectors.toList());
+	}
+	public List<Float> getListOfEnergyInConsumptionThatIsOfferedByFlexibilitiesInThisNetwork(){
+		return getListOfEnergyThatIsOfferedByFlexibilitiesInThisNetwork().stream().filter(value -> (value < 0.f)).collect(Collectors.toList());
+	}
+	
+	public float getFlexibilityProductionCapacity() {
+		return getListOfEnergyInProductionThatIsOfferedByFlexibilitiesInThisNetwork().stream().reduce(0.f, Float::sum);
+	}
+	public float getFlexibilityConsumptionCapacity() {
+		return getListOfEnergyInConsumptionThatIsOfferedByFlexibilitiesInThisNetwork().stream().reduce(0.f, Float::sum);
+	}
+	
+	
+	public int getAmountOfProductionFlexibilities() {
+		return getListOfEnergyInProductionThatIsOfferedByFlexibilitiesInThisNetwork().size();
+	}
+	public int getAmountOfConsumptionFlexibilities() {
+		return getListOfEnergyInConsumptionThatIsOfferedByFlexibilitiesInThisNetwork().size();
+	}
+	
+	public float getAverageFlexibilityProduction() {
+		int amount = getAmountOfProductionFlexibilities();
+		 return (amount > 0)? getFlexibilityProductionCapacity() / (float)amount : 0.f;
+	}
+	
+	public float getAverageFlexibilityConsumption() {
+		int amount = getAmountOfConsumptionFlexibilities();
+		 return (amount > 0)? getFlexibilityConsumptionCapacity() / (float)amount : 0.f;
+	}
+	
+	public float getVarianzInFlexibilitieConsumption() {
+		float average = getAverageFlexibilityConsumption();
+		float sum = getListOfEnergyInConsumptionThatIsOfferedByFlexibilitiesInThisNetwork().stream().map(energy -> squared(energy - average)).reduce(0.f, Float::sum);
+		int amountOfFlexibilities = getAmountOfConsumptionFlexibilities();
+		return (amountOfFlexibilities > 0)? sum / (float) amountOfFlexibilities : 0.f;
+	}
+	
+	
+	public float getVarianzInFlexibilitieProduction() {
+		float average = getAverageFlexibilityProduction();
+		float sum = getListOfEnergyInProductionThatIsOfferedByFlexibilitiesInThisNetwork().stream().map(energy -> squared(energy - average)).reduce(0.f, Float::sum);
+		int amountOfFlexibilities = getAmountOfProductionFlexibilities();
+		return (amountOfFlexibilities > 0)? sum / (float) amountOfFlexibilities : 0.f;
+	}
+	
+	public float getDiviationInFlexibilityConsumption() {
+		return (float) Math.sqrt(getVarianzInFlexibilitieConsumption());
+	}
+	public float getDiviationInFlexibilityProduction() {
+		return (float) Math.sqrt(getVarianzInFlexibilitieProduction());
+	}
+	
 	//Help Function
-	private float sqared(float input) {
+	private float squared(float input) {
 		return (float) Math.pow(input, 2);
 	}
 	
 	
 	
+	
+	
+	
 }

+ 7 - 0
src/ui/model/MinimumNetwork.java

@@ -1,11 +1,15 @@
 package ui.model;
 
 import java.util.ArrayList;
+
+import classes.AbstractCpsObject;
 import classes.HolonObject;
 
 public class MinimumNetwork {
 	private ArrayList<HolonObject> holonObjectList = new ArrayList<HolonObject>();
 	private ArrayList<IntermediateCableWithState> edgeList = new ArrayList<IntermediateCableWithState>();
+	//ToCalculate average path
+	private ArrayList<AbstractCpsObject> nodeAndSwitches = new ArrayList<AbstractCpsObject>();
 	public MinimumNetwork(ArrayList<HolonObject> holonObjectList, ArrayList<IntermediateCableWithState> edgeList){
 		this.holonObjectList = holonObjectList;
 		this.edgeList = edgeList;
@@ -30,4 +34,7 @@ public class MinimumNetwork {
 		edges += "]";
 		return objecte + edges;
 	}
+	public ArrayList<AbstractCpsObject> getNodeAndSwitches() {
+		return nodeAndSwitches;
+	}
 }