|
@@ -11,6 +11,7 @@ import ui.view.MyCanvas;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.Collections;
|
|
|
import java.util.HashMap;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* Controller for Simulation.
|
|
@@ -67,8 +68,16 @@ public class SimulationManager {
|
|
|
timeStep = x;
|
|
|
searchForSubNets();
|
|
|
for (SubNet singleSubNet : subNets) {
|
|
|
- resetConnections(singleSubNet.getObjects().get(0),
|
|
|
+ if(singleSubNet.getObjects().size() == 0)
|
|
|
+ {
|
|
|
+ resetConnections(singleSubNet.getBatteries().get(0),
|
|
|
+ new ArrayList<>(), new ArrayList<>());
|
|
|
+ }else
|
|
|
+ {
|
|
|
+ resetConnections(singleSubNet.getObjects().get(0),
|
|
|
new ArrayList<>(), new ArrayList<>());
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
for (SubNet singleSubNet : subNets) {
|
|
|
float production = calculateEnergyWithoutFlexDevices("prod",
|
|
@@ -113,24 +122,26 @@ public class SimulationManager {
|
|
|
* HolonObjects that can be partially Supplied but might be fully
|
|
|
* Supplied
|
|
|
*/
|
|
|
+ /*
|
|
|
ArrayList<HolonObject> partiallySuppliedList = new ArrayList<HolonObject>();
|
|
|
/**
|
|
|
* HolonObjects that can get the spare energy
|
|
|
*/
|
|
|
- ArrayList<HolonObject> notSuppliedList = new ArrayList<HolonObject>();
|
|
|
+//
|
|
|
+// ArrayList<HolonObject> notSuppliedList = new ArrayList<HolonObject>();
|
|
|
/**
|
|
|
* Number of HolonObjects that need to be supplied
|
|
|
*/
|
|
|
- long 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();
|
|
|
+// long 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();
|
|
|
/**
|
|
|
* energy each HolonObject receives in AlleEqualModus
|
|
|
*/
|
|
|
- float energyPerHolonObject = 0;
|
|
|
+
|
|
|
if(energySurplus >= 0)
|
|
|
{
|
|
|
//Supply all consumer
|
|
@@ -144,26 +155,22 @@ public class SimulationManager {
|
|
|
}
|
|
|
}
|
|
|
//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
|
|
|
+ singleSubNet.getBatteries().sort(new WeakestBattery(x));//Sort all batteries by the Value of ther StateOfCharge/Capasity
|
|
|
for(HolonBattery hB : singleSubNet.getBatteries())
|
|
|
{
|
|
|
- float energyToCollect = hB.getInAtTimeStep(x);
|
|
|
+ float energyToCollect = hB.getInAtTimeStep(x-1);
|
|
|
if(currentProduction >= energyToCollect)
|
|
|
{
|
|
|
- //TODO: change StateofCharge soc = soc + energyToCollect
|
|
|
- hB.setNewStateOfCharge(hB.getStateOfCharge() + energyToCollect);
|
|
|
+ //change StateofCharge soc = soc + energyToCollect
|
|
|
hB.setStateOfChargeAtTimeStep(hB.getStateOfChargeAtTimeStep(x-1) + energyToCollect, x);
|
|
|
currentProduction -= energyToCollect;
|
|
|
}else
|
|
|
{
|
|
|
- //TODO: change StateofCharge soc = soc + currentProduction
|
|
|
- hB.setNewStateOfCharge(hB.getStateOfCharge() + currentProduction);
|
|
|
+ //change StateofCharge soc = soc + currentProduction
|
|
|
hB.setStateOfChargeAtTimeStep(hB.getStateOfChargeAtTimeStep(x-1) + currentProduction, x);
|
|
|
currentProduction = 0;
|
|
|
//no break must be calculatet for all 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();
|
|
@@ -184,28 +191,27 @@ public class SimulationManager {
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
-
|
|
|
//Check all Battries what they can provide
|
|
|
if(energySurplus + GetOutAllBatteries(singleSubNet.getBatteries(), x) >= 0)
|
|
|
{
|
|
|
- singleSubNet.getBatteries().sort(new WeakestBattery());//.reverse();
|
|
|
+ singleSubNet.getBatteries().sort(new WeakestBattery(x));//.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.getOutAtTimeStep(x); //energy is positiv
|
|
|
+ float maxEnergyAvailable = hB.getOutAtTimeStep(x-1); //energy is positiv
|
|
|
if(maxEnergyAvailable >= -neededEnergyFromBattery)
|
|
|
{
|
|
|
- //TODO: change StateofCharge soc = soc - -neededEnergyFromBattery
|
|
|
- hB.setNewStateOfCharge(hB.getStateOfCharge() - -neededEnergyFromBattery);
|
|
|
+ //change StateofCharge soc = soc - -neededEnergyFromBattery
|
|
|
+ hB.setStateOfChargeAtTimeStep(hB.getStateOfChargeAtTimeStep(x-1) - -neededEnergyFromBattery, x);
|
|
|
currentProduction += -neededEnergyFromBattery;
|
|
|
//no break must be calculatet for all beabreak; //When a energy can supply the last needed energy break;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- //TODO: change StateofCharge soc = soc - maxEnergyAvailable
|
|
|
- hB.setNewStateOfCharge(hB.getStateOfCharge() - maxEnergyAvailable);
|
|
|
+ //change StateofCharge soc = soc - maxEnergyAvailable
|
|
|
+ hB.setStateOfChargeAtTimeStep(hB.getStateOfChargeAtTimeStep(x-1) - maxEnergyAvailable, x);
|
|
|
currentProduction += maxEnergyAvailable;
|
|
|
}
|
|
|
}
|
|
@@ -226,37 +232,92 @@ public class SimulationManager {
|
|
|
//Get all Energy out of battries as possible
|
|
|
for(HolonBattery hB : singleSubNet.getBatteries())
|
|
|
{
|
|
|
- float maxEnergyAvailable = hB.getOutAtTimeStep(x); //energy is positiv
|
|
|
- //TODO: change StateofCharge soc = soc - maxEnergyAvailable
|
|
|
- hB.setNewStateOfCharge(hB.getStateOfCharge() - maxEnergyAvailable);
|
|
|
+ float maxEnergyAvailable = hB.getOutAtTimeStep(x-1); //energy is positiv
|
|
|
+ //change StateofCharge soc = soc - maxEnergyAvailable
|
|
|
+ hB.setStateOfChargeAtTimeStep(hB.getStateOfChargeAtTimeStep(x-1) - maxEnergyAvailable, x);
|
|
|
currentProduction += maxEnergyAvailable;
|
|
|
}
|
|
|
- calculation(x, singleSubNet, production, consumption, energySurplus, currentProduction,
|
|
|
- partiallySuppliedList, notSuppliedList, energyPerHolonObject);
|
|
|
+ //Calc
|
|
|
+ singleSubNet.getObjects().stream().forEach(hl -> hl.setCurrentSupply(0));
|
|
|
+ if(model.getFairnessModel() == fairnessAllEqual)
|
|
|
+ {
|
|
|
+ long nOConsumer = singleSubNet.getObjects().stream().filter(hl -> (hl.getCurrentEnergyAtTimeStep(x) < 0)).count();
|
|
|
+ float energyPerHolonObject = 0;
|
|
|
+ if (nOConsumer != 0)
|
|
|
+ energyPerHolonObject = currentProduction / nOConsumer;
|
|
|
+ for(HolonObject hO : singleSubNet.getObjects())
|
|
|
+ {
|
|
|
+ if(hO.getCurrentEnergyAtTimeStep(x) < 0) //Just Consumer need Energy
|
|
|
+ {
|
|
|
+ hO.setCurrentSupply(energyPerHolonObject);
|
|
|
+ currentProduction -= energyPerHolonObject; //Subtract the Energy from the Production
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else //(model.getFairnessModel() == fairnessMininumDemandFirst)
|
|
|
+ {
|
|
|
+ singleSubNet.getObjects().sort(new MinEnergyComparator(x));
|
|
|
+ //SupplyAllMinimumEnergy
|
|
|
+ for(HolonObject hO : singleSubNet.getObjects())
|
|
|
+ {
|
|
|
+ if(hO.checkIfPartiallySupplied(x))continue;
|
|
|
+ if(hO.getCurrentEnergyAtTimeStep(x) > 0)continue;
|
|
|
+ float minEnergy = -hO.getMinEnergy(x); //Energy from getMinEnergy is negative -> convert to positive
|
|
|
+ if(minEnergy <= currentProduction)
|
|
|
+ {
|
|
|
+ hO.setCurrentSupply(minEnergy);
|
|
|
+ currentProduction -= minEnergy;
|
|
|
+ }else
|
|
|
+ {
|
|
|
+ hO.setCurrentSupply(currentProduction);
|
|
|
+ currentProduction = 0;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ singleSubNet.getObjects().sort(new EnergyMinToMaxComparator(x));
|
|
|
+ //supplyFullytillEnd ... because its cant be fully supplied
|
|
|
+ for(HolonObject hO : singleSubNet.getObjects())
|
|
|
+ {
|
|
|
+ float neededEnergy = -hO.getCurrentEnergyAtTimeStep(x);
|
|
|
+ if(neededEnergy < 0)continue; //Producer
|
|
|
+ if(neededEnergy <= currentProduction)
|
|
|
+ {
|
|
|
+ hO.setCurrentSupply(neededEnergy+hO.getCurrentSupply());
|
|
|
+ currentProduction -= neededEnergy;
|
|
|
+ }else
|
|
|
+ {
|
|
|
+ hO.setCurrentSupply(currentProduction+hO.getCurrentSupply());
|
|
|
+ currentProduction = 0;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
}
|
|
|
//Visualize the Color
|
|
|
for(HolonObject hO : singleSubNet.getObjects())
|
|
|
{
|
|
|
-
|
|
|
- float neededEnergy = hO.getCurrentEnergyAtTimeStep(x);
|
|
|
- if(neededEnergy > 0)
|
|
|
+ float neededEnergy = -hO.getCurrentEnergyAtTimeStep(x); // convert negative energy in positive for calculations
|
|
|
+ if(neededEnergy < 0)
|
|
|
{
|
|
|
hO.setState(HolonObject.PRODUCER);
|
|
|
}
|
|
|
- else if(neededEnergy < 0)
|
|
|
+ else if(neededEnergy > 0)
|
|
|
{
|
|
|
- float currentSupply = hO.getCurrentSupply();
|
|
|
- if(currentSupply > -neededEnergy)
|
|
|
+ float currentSupply = hO.getCurrentSupply() ;
|
|
|
+ if(currentSupply > neededEnergy)
|
|
|
{
|
|
|
hO.setState(HolonObject.OVER_SUPPLIED);
|
|
|
- }else if (currentSupply == -neededEnergy)
|
|
|
+ }else if (currentSupply == neededEnergy)
|
|
|
{
|
|
|
hO.setState(HolonObject.SUPPLIED);
|
|
|
- }else if (currentSupply < -neededEnergy)
|
|
|
+ }else if (currentSupply < neededEnergy)
|
|
|
{
|
|
|
- if(currentSupply >= hO.getMinEnergy(x))
|
|
|
+ float minEnergy = -hO.getMinEnergy(x);
|
|
|
+ if(currentSupply >= minEnergy || hO.getSelfMadeEnergy(x) >= minEnergy )
|
|
|
{
|
|
|
hO.setState(HolonObject.PARTIALLY_SUPPLIED);
|
|
|
}else
|
|
@@ -270,9 +331,6 @@ public class SimulationManager {
|
|
|
hO.setState(HolonObject.NO_ENERGY);
|
|
|
}
|
|
|
}
|
|
|
- //Old Calculation
|
|
|
-// calculation(x, singleSubNet, production, consumption, energySurplus, currentProduction,
|
|
|
-// partiallySuppliedList, notSuppliedList, energyPerHolonObject);
|
|
|
}
|
|
|
canvas.repaint();
|
|
|
flexPane.recalculate();
|
|
@@ -430,7 +488,7 @@ public class SimulationManager {
|
|
|
/**
|
|
|
* add all battries.getOut() from a list of battries and return them
|
|
|
* @param aL a List of HolonBattries likely from subnet.getBatteries()
|
|
|
- * @param x TODO
|
|
|
+ * @param x TimeStep
|
|
|
* @return
|
|
|
*
|
|
|
*/
|
|
@@ -439,8 +497,10 @@ public class SimulationManager {
|
|
|
float OutEnergy = 0;
|
|
|
for(HolonBattery hB : aL)
|
|
|
{
|
|
|
- OutEnergy += hB.getOutAtTimeStep(x);
|
|
|
+ //System.out.println("Iteration: "+ x +"OutBattery: "+ hB.getOutAtTimeStep(x-1));
|
|
|
+ OutEnergy += hB.getOutAtTimeStep(x-1);
|
|
|
}
|
|
|
+ //System.out.println("Iteration: "+ x +"GetOutAllBatteries: "+ OutEnergy);
|
|
|
return OutEnergy;
|
|
|
}
|
|
|
/**
|
|
@@ -838,7 +898,7 @@ public class SimulationManager {
|
|
|
SubNet singleSubNet = new SubNet(new ArrayList<>(),
|
|
|
new ArrayList<>(), new ArrayList<>(), new ArrayList<>());
|
|
|
singleSubNet = buildSubNet(cps, new ArrayList<>(), singleSubNet);
|
|
|
- if (singleSubNet.getObjects().size() != 0) {
|
|
|
+ if (singleSubNet.getObjects().size() + singleSubNet.getBatteries().size() != 0 ) {
|
|
|
subNets.add(singleSubNet);
|
|
|
}
|
|
|
if (0 == objectsToHandle.size()) {
|