Browse Source

Calculation v0.1 : frist integration

tolatesry 6 years ago
parent
commit
a0970067d0

+ 6 - 1
src/classes/HolonBattery.java

@@ -118,5 +118,10 @@ public class HolonBattery extends AbstractCpsObject{
 		}
 		
 	}
-	
+	public String toString()
+	{
+		return "HolonBattery ID:" + this.getId() + " State:" + getState().name() 
+				+ " InRatio:" + getInRatio() + " OutRatio:" + getOutRatio() 
+				+ " Akku: " + getStateOfCharge() + "/" + getCapasity();
+	}
 }

+ 33 - 0
src/classes/comparator/WeakestBattery.java

@@ -0,0 +1,33 @@
+package classes.comparator;
+
+import java.util.Comparator;
+
+import classes.HolonBattery;
+
+public class WeakestBattery implements Comparator<HolonBattery>{
+
+	
+	@Override
+	public int compare(HolonBattery o1, HolonBattery o2) {
+		//Sort Battery by the value of StateOfCharge/Capasity
+		float O1capasity = o1.getCapasity();		
+		float O2capasity = o2.getCapasity();
+		if(O1capasity == 0)
+		{
+			return 1; 
+		}
+		else if(O2capasity == 0)
+		{
+			return -1;
+		}
+		if (o1.getStateOfCharge() / O1capasity < o2.getStateOfCharge() / O2capasity)
+		{
+			return -1;
+		}else if(o1.getStateOfCharge() / O1capasity > o2.getStateOfCharge() / O2capasity)
+		{
+			return 1;
+		}
+		return 0;
+	}
+
+}

+ 282 - 143
src/ui/controller/SimulationManager.java

@@ -4,11 +4,13 @@ import classes.*;
 import classes.comparator.EnergyMinToMaxComparator;
 import classes.comparator.MinEnergyComparator;
 import classes.comparator.TotalEnergyComparator;
+import classes.comparator.WeakestBattery;
 import ui.model.Model;
 import ui.view.FlexiblePane;
 import ui.view.MyCanvas;
 
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.HashMap;
 
 import com.sun.xml.internal.ws.util.xml.XMLReaderComposite.State;
@@ -80,25 +82,7 @@ public class SimulationManager {
 			// negative value
 			float energySurplus = production + consumption;
 			
-			//-->TODO: OutSourcing later in method and just use whats needed
-			float getMaxInPut = 0;
-			float getMaxOutPut = 0;
-			for(HolonBattery hB : singleSubNet.getBatteries())
-			{
-				getMaxInPut += hB.getIN();
-				getMaxOutPut += hB.getOUT();
-				if(energySurplus < 0)
-					hB.setState(HolonBattery.State.EMIT);
-				else if (energySurplus > 0)
-					hB.setState(HolonBattery.State.COLLECT);
-					
-			}
-			if(energySurplus < 0)
-				energySurplus += getMaxOutPut;
-			else if (energySurplus > 0)
-				energySurplus -= getMaxInPut;
-			System.out.println(energySurplus);
-			//<--
+			
 			//float minConsumption = calculateMinimumEnergy(singleSubNet, timeStep);
 
 			// --------------- use flexible devices ---------------
@@ -145,155 +129,310 @@ public class SimulationManager {
 			 * energy each HolonObject receives in AlleEqualModus
 			 */
 			float energyPerHolonObject = 0;
-			if (model.getFairnessModel() == fairnessMininumDemandFirst) {
-				/**
-				 * supply Buildings with minimal Energy first, if conflicts
-				 * happen
-				 */
-				singleSubNet.getObjects().sort(new MinEnergyComparator(x));
-			} else if (model.getFairnessModel() == fairnessAllEqual) {
-				/*
-				 * give every building (just consumer) the same energy
-				 */
-				numberOfConsumers = singleSubNet
-						.getObjects()
-						.stream()
-						.filter(hl -> (hl.getState() != HolonObject.NO_ENERGY
-								&& hl.getState() != HolonObject.PRODUCER && hl
-								.getConnectedTo().stream()
-								.filter(e -> (e.getFlow() > 0)).count() > 0))
-						.count();
-				if (numberOfConsumers != 0)
-					energyPerHolonObject = production / numberOfConsumers;
+			if(energySurplus >= 0)
+			{
+				//Supply all consumer
+				for(HolonObject hO : singleSubNet.getObjects())
+				{
+					float neededEnergy = hO.getCurrentEnergyAtTimeStep(x);
+					if(neededEnergy < 0)
+					{
+						hO.setCurrentSupply(-neededEnergy);
+						currentProduction -= -neededEnergy; //Subtract the Energy from the Production
+					}
+				}
+				//Supply all Batterys with the left currentProduction
+				System.out.println("production: "+production + " consumption: "+ consumption);
+				singleSubNet.getBatteries().sort(new WeakestBattery());//Sort all batteries by the Value of ther StateOfCharge/Capasity
+				for(HolonBattery hB : singleSubNet.getBatteries())
+				{
+					float energyToCollect = hB.getIN();
+					if(currentProduction >= energyToCollect)
+					{
+						//TODO: change StateofCharge soc = soc + energyToCollect
+						currentProduction -= energyToCollect;
+					}else
+					{
+						//TODO: change StateofCharge soc = soc + currentProduction
+						currentProduction = 0;
+						break; //because no more energy
+					}
+					System.out.println(hB.toString() + " currentProduction: "+ currentProduction);
+				}
+				//Over_Supply all consumer equal
+				long nOConsumer = singleSubNet.getObjects().stream().filter(hl -> (hl.getCurrentEnergyAtTimeStep(x) < 0)).count();			
+				if(nOConsumer != 0)
+				{
+					//energy to seperated equal 
+					float EnergyOverSupplyPerHolonObject = currentProduction / nOConsumer;
+					for(HolonObject hO : singleSubNet.getObjects())
+					{
+						float neededEnergy = hO.getCurrentEnergyAtTimeStep(x);
+						if(neededEnergy < 0)
+						{
+							hO.setCurrentSupply(hO.getCurrentSupply() + EnergyOverSupplyPerHolonObject);
+						}
+					}
+					currentProduction = 0;
+				}
 			}
-			for (HolonObject hl : singleSubNet.getObjects()) {
-				hl.setCurrentSupply(0);
-				if (hl.getState() != HolonObject.NO_ENERGY
-						&& hl.getState() != HolonObject.PRODUCER) {
-					for (int i = 0; i < hl.getConnections().size(); i++) {
-						CpsEdge edge = hl.getConnectedTo().get(i);
-						if (edge.isWorking()
-								&& edge.getFlow() > 0
-								|| edge.getCapacity() == CpsEdge.CAPACITY_INFINITE) {
-
-							if (model.getFairnessModel() == fairnessAllEqual) {
-								/* fairness: everyone receives the Same Energy */
-								float neededEnergy = hl
-										.getCurrentEnergyAtTimeStep(x);
-								if (neededEnergy > 0) {
-									hl.setState(HolonObject.PRODUCER);
-								} else if (energyPerHolonObject > -neededEnergy) {
+			else
+			{
+				
+				//Check all Battries what they can provide
+				if(energySurplus + GetOutAllBatteries(singleSubNet.getBatteries()) >= 0)
+				{
+					singleSubNet.getBatteries().sort(new WeakestBattery());//.reverse();
+					Collections.reverse(singleSubNet.getBatteries()); //most supplyed first
+					//Get the NEEDED energy
+					for(HolonBattery hB : singleSubNet.getBatteries())
+					{
+						float neededEnergyFromBattery = currentProduction + consumption; //Energy is negativ
+						float maxEnergyAvailable = hB.getOUT(); //energy is positiv
+						if(maxEnergyAvailable >= -neededEnergyFromBattery)
+						{
+							//TODO: change StateofCharge soc = soc - -neededEnergyFromBattery
+							currentProduction += -neededEnergyFromBattery;
+							break; //When a energy can supply the last needed energy break;
+						}
+						else
+						{
+							//TODO: change StateofCharge soc = soc - maxEnergyAvailable
+							currentProduction += maxEnergyAvailable;
+						}
+					}
+					//Supply all consumer all ar in state Supplied no one is oversupplied because 
+					//	just the energy that is needed is gained from the batteries
+					for(HolonObject hO : singleSubNet.getObjects())
+					{
+						float neededEnergy = hO.getCurrentEnergyAtTimeStep(x);
+						if(neededEnergy < 0)
+						{
+							hO.setCurrentSupply(-neededEnergy);
+							currentProduction -= -neededEnergy; //Subtract the Energy from the Production
+						}
+					}
+				}
+				else //Objects have to be partially supplied
+				{
+					//Get all Energy out of battries as possible
+					for(HolonBattery hB : singleSubNet.getBatteries())
+					{
+						float maxEnergyAvailable = hB.getOUT(); //energy is positiv
+						//TODO: change StateofCharge soc = soc - maxEnergyAvailable
+						currentProduction += maxEnergyAvailable;
+					}
+					calculation(x, singleSubNet, production, consumption, energySurplus, currentProduction,
+							partiallySuppliedList, notSuppliedList, energyPerHolonObject);
+				}
+				
+			}
+			//Visualize the Color
+			for(HolonObject hO : singleSubNet.getObjects())
+			{
+				
+				float neededEnergy = hO.getCurrentEnergyAtTimeStep(x);
+				if(neededEnergy > 0)
+				{
+					hO.setState(HolonObject.PRODUCER);
+				}
+				else if(neededEnergy < 0)
+				{
+					float currentSupply = hO.getCurrentSupply();
+					if(currentSupply > -neededEnergy)
+					{
+						hO.setState(HolonObject.OVER_SUPPLIED);
+					}else if (currentSupply ==  -neededEnergy)
+					{
+						hO.setState(HolonObject.SUPPLIED);
+					}else if (currentSupply <  -neededEnergy)
+					{
+						if(currentSupply >= hO.getMinEnergy(x))
+						{
+							hO.setState(HolonObject.PARTIALLY_SUPPLIED);
+						}else
+						{
+							hO.setState(HolonObject.NOT_SUPPLIED);
+						}
+					}
+				}
+				else if(neededEnergy == 0)
+				{
+					hO.setState(HolonObject.NO_ENERGY);
+				}
+			}
+			//Old Calculation
+//			calculation(x, singleSubNet, production, consumption, energySurplus, currentProduction,
+//					partiallySuppliedList, notSuppliedList, energyPerHolonObject);
+		}
+		canvas.repaint();
+		flexPane.recalculate();
+	}
+
+	private void calculation(int x, SubNet singleSubNet, float production, float consumption, float energySurplus,
+			float currentProduction, ArrayList<HolonObject> partiallySuppliedList,
+			ArrayList<HolonObject> notSuppliedList, float energyPerHolonObject) {
+		long numberOfConsumers;
+		if (model.getFairnessModel() == fairnessMininumDemandFirst) {
+			/**
+			 * supply Buildings with minimal Energy first, if conflicts
+			 * happen
+			 */
+			singleSubNet.getObjects().sort(new MinEnergyComparator(x));
+		} else if (model.getFairnessModel() == fairnessAllEqual) {
+			/*
+			 * give every building (just consumer) the same energy
+			 */
+			numberOfConsumers = singleSubNet
+					.getObjects()
+					.stream()
+					.filter(hl -> (hl.getState() != HolonObject.NO_ENERGY
+							&& hl.getState() != HolonObject.PRODUCER && hl
+							.getConnectedTo().stream()
+							.filter(e -> (e.getFlow() > 0)).count() > 0))
+					.count();
+			if (numberOfConsumers != 0)
+				energyPerHolonObject = currentProduction / numberOfConsumers;
+		}
+		for (HolonObject hl : singleSubNet.getObjects()) {
+			hl.setCurrentSupply(0);
+			if (hl.getState() != HolonObject.NO_ENERGY
+					&& hl.getState() != HolonObject.PRODUCER) {
+				for (int i = 0; i < hl.getConnections().size(); i++) {
+					CpsEdge edge = hl.getConnectedTo().get(i);
+					if (edge.isWorking()
+							&& edge.getFlow() > 0
+							|| edge.getCapacity() == CpsEdge.CAPACITY_INFINITE) {
+
+						if (model.getFairnessModel() == fairnessAllEqual) {
+							/* fairness: everyone receives the Same Energy */
+							float neededEnergy = hl
+									.getCurrentEnergyAtTimeStep(x);
+							if (neededEnergy > 0) {
+								hl.setState(HolonObject.PRODUCER);
+							} else if (energyPerHolonObject > -neededEnergy) {
+								hl.setState(HolonObject.OVER_SUPPLIED);
+							} else if (energyPerHolonObject == -neededEnergy) {
+								hl.setState(HolonObject.SUPPLIED);
+							} else if (energyPerHolonObject >= -hl
+									.getMinEnergy(x)) {
+								hl.setState(HolonObject.PARTIALLY_SUPPLIED);
+							} else {
+								hl.setState(HolonObject.NOT_SUPPLIED);
+							}
+							hl.setCurrentSupply(energyPerHolonObject);
+						} else if (model.getFairnessModel() == fairnessMininumDemandFirst) {
+							/* fairness: minimum demand gets Energy first */
+							if ((production + consumption) >= 0) {
+								if (energySurplus > 0) {
 									hl.setState(HolonObject.OVER_SUPPLIED);
-								} else if (energyPerHolonObject == -neededEnergy) {
-									hl.setState(HolonObject.SUPPLIED);
-								} else if (energyPerHolonObject >= -hl
-										.getMinEnergy(x)) {
-									hl.setState(HolonObject.PARTIALLY_SUPPLIED);
+									hl.setCurrentSupply((-energySurplus
+											/ consumption + 1)
+											* hl.getCurrentEnergyAtTimeStep(x));
 								} else {
-									hl.setState(HolonObject.NOT_SUPPLIED);
+									hl.setState(HolonObject.SUPPLIED);
+									hl.setCurrentSupply(-hl
+											.getCurrentEnergyAtTimeStep(x));
 								}
-								hl.setCurrentSupply(energyPerHolonObject);
-							} else if (model.getFairnessModel() == fairnessMininumDemandFirst) {
-								/* fairness: minimum demand gets Energy first */
-								if ((production + consumption) >= 0) {
-									if (energySurplus > 0) {
-										hl.setState(HolonObject.OVER_SUPPLIED);
-										hl.setCurrentSupply((-energySurplus
-												/ consumption + 1)
-												* hl.getCurrentEnergyAtTimeStep(x));
-									} else {
-										hl.setState(HolonObject.SUPPLIED);
-										hl.setCurrentSupply(-hl
-												.getCurrentEnergyAtTimeStep(x));
-									}
+							} else {
+								float minEnergy = hl.getMinEnergy(x);									
+								if (hl.checkIfPartiallySupplied(timeStep)) {
+									hl.setState(HolonObject.PARTIALLY_SUPPLIED);
+									currentProduction += minEnergy;
+									hl.setCurrentSupply(-minEnergy);
+									partiallySuppliedList.add(hl);
 								} else {
-									float minEnergy = hl.getMinEnergy(x);									
-									if (hl.checkIfPartiallySupplied(timeStep)) {
+									/**
+									 * Case that only some HolonObjects can
+									 * be supplied
+									 */
+									if (-hl.getCurrentEnergyAtTimeStep(x) <= currentProduction) {
+										hl.setState(HolonObject.PARTIALLY_SUPPLIED);
+										currentProduction += minEnergy;
+										hl.setCurrentSupply(-minEnergy);
+										partiallySuppliedList.add(hl);
+									} else if (-hl.getMinEnergy(x) <= currentProduction) {
 										hl.setState(HolonObject.PARTIALLY_SUPPLIED);
 										currentProduction += minEnergy;
 										hl.setCurrentSupply(-minEnergy);
 										partiallySuppliedList.add(hl);
 									} else {
-										/**
-										 * Case that only some HolonObjects can
-										 * be supplied
-										 */
-										if (-hl.getCurrentEnergyAtTimeStep(x) <= currentProduction) {
-											hl.setState(HolonObject.PARTIALLY_SUPPLIED);
-											currentProduction += minEnergy;
-											hl.setCurrentSupply(-minEnergy);
-											partiallySuppliedList.add(hl);
-										} else if (-hl.getMinEnergy(x) <= currentProduction) {
-											hl.setState(HolonObject.PARTIALLY_SUPPLIED);
-											currentProduction += minEnergy;
-											hl.setCurrentSupply(-minEnergy);
-											partiallySuppliedList.add(hl);
-										} else {
-											hl.setState(HolonObject.NOT_SUPPLIED);
-											hl.setCurrentSupply(0.0f);
-											notSuppliedList.add(hl);
-											// currentProduction +=
-											// hl.getCurrentEnergyAtTimeStep(x);
-
-										}
+										hl.setState(HolonObject.NOT_SUPPLIED);
+										hl.setCurrentSupply(0.0f);
+										notSuppliedList.add(hl);
+										// currentProduction +=
+										// hl.getCurrentEnergyAtTimeStep(x);
+
 									}
 								}
 							}
-							hl.getCurrentEnergyAtTimeStep(x);
-							break;
 						}
+						hl.getCurrentEnergyAtTimeStep(x);
+						break;
 					}
+				}
 
-					/**
-					 * check if some object cn self supply itself
-					 */
-					if (hl.checkIfPartiallySupplied(timeStep)
-							&& hl.getState() != HolonObject.SUPPLIED
-							&& hl.getState() != HolonObject.OVER_SUPPLIED) {
-						hl.setState(HolonObject.PARTIALLY_SUPPLIED);
-					}
+				/**
+				 * check if some object cn self supply itself
+				 */
+				if (hl.checkIfPartiallySupplied(timeStep)
+						&& hl.getState() != HolonObject.SUPPLIED
+						&& hl.getState() != HolonObject.OVER_SUPPLIED) {
+					hl.setState(HolonObject.PARTIALLY_SUPPLIED);
 				}
 			}
+		}
 
-			if (model.getFairnessModel() == fairnessMininumDemandFirst) {
-				/**
-				 * check if some partially supplied building might be fully
-				 * supplied.
+		if (model.getFairnessModel() == fairnessMininumDemandFirst) {
+			/**
+			 * check if some partially supplied building might be fully
+			 * supplied.
+			 */
+			partiallySuppliedList.sort(new EnergyMinToMaxComparator(x));
+			for (HolonObject hl : partiallySuppliedList) {
+				float minEnergy = hl.getMinEnergy(x);
+				currentProduction -= minEnergy;
+				/*
+				 * if possible, supply fully
 				 */
-				partiallySuppliedList.sort(new EnergyMinToMaxComparator(x));
-				for (HolonObject hl : partiallySuppliedList) {
-					float minEnergy = hl.getMinEnergy(x);
-					currentProduction -= minEnergy;
-					/*
-					 * if possible, supply fully
-					 */
-					float currentEnergyAtTimeStep = hl
-							.getCurrentEnergyAtTimeStep(x);
-					if (-currentEnergyAtTimeStep <= currentProduction) {
-						hl.setState(HolonObject.SUPPLIED);
-						currentProduction += currentEnergyAtTimeStep;
-						hl.setCurrentSupply(-currentEnergyAtTimeStep);
-						hl.getCurrentEnergyAtTimeStep(x);
-					} else {
-						currentProduction += minEnergy;
-						notSuppliedList.add(hl);
-					}
+				float currentEnergyAtTimeStep = hl
+						.getCurrentEnergyAtTimeStep(x);
+				if (-currentEnergyAtTimeStep <= currentProduction) {
+					hl.setState(HolonObject.SUPPLIED);
+					currentProduction += currentEnergyAtTimeStep;
+					hl.setCurrentSupply(-currentEnergyAtTimeStep);
+					hl.getCurrentEnergyAtTimeStep(x);
+				} else {
+					currentProduction += minEnergy;
+					notSuppliedList.add(hl);
 				}
-				if (!notSuppliedList.isEmpty() && currentProduction > 0) {
-					float energyPerHolon = currentProduction
-							/ notSuppliedList.size();
-					for (HolonObject hl : notSuppliedList) {
-						hl.setCurrentSupply(hl.getCurrentSupply()
-								+ energyPerHolon);
-						hl.getCurrentEnergyAtTimeStep(x);
-					}
+			}
+			if (!notSuppliedList.isEmpty() && currentProduction > 0) {
+				float energyPerHolon = currentProduction
+						/ notSuppliedList.size();
+				for (HolonObject hl : notSuppliedList) {
+					hl.setCurrentSupply(hl.getCurrentSupply()
+							+ energyPerHolon);
+					hl.getCurrentEnergyAtTimeStep(x);
 				}
 			}
 		}
-		canvas.repaint();
-		flexPane.recalculate();
 	}
-
+	/**
+	 * add all battries.getOut() from a list of battries and return them
+	 * @param aL a List of HolonBattries likely from subnet.getBatteries()
+	 * @return 
+	 * 
+	 */
+	private float GetOutAllBatteries(ArrayList<HolonBattery> aL)
+	{
+		float OutEnergy = 0;
+		for(HolonBattery hB : aL)
+		{
+			OutEnergy += hB.getOUT();
+		}
+		return OutEnergy;
+	}
 	/**
 	 * search for all flexible devices in the network and turn them on, until
 	 * energy surplus = 0 or all devices have been examined.