|
@@ -52,8 +52,8 @@ public class SimulationManager {
|
|
|
resetConnections(singleSubNet.getObjects().get(0), new ArrayList<>(), new ArrayList<>());
|
|
|
}
|
|
|
for (SubNet singleSubNet : subNets) {
|
|
|
- float production = calculateEnergy("prod", singleSubNet, timeStep);
|
|
|
- float consumption = calculateEnergy("cons", singleSubNet, timeStep);
|
|
|
+ float production = calculateEnergyWithoutFlexDevices("prod", singleSubNet, timeStep);
|
|
|
+ float consumption = calculateEnergyWithoutFlexDevices("cons", singleSubNet, timeStep);
|
|
|
// surplus of energy is computed by sum, since consumption is a negative value
|
|
|
float energySurplus = production + consumption;
|
|
|
|
|
@@ -69,8 +69,8 @@ public class SimulationManager {
|
|
|
}
|
|
|
|
|
|
// recompute after having examined/turned on all flexible devices
|
|
|
- production = calculateEnergy("prod", singleSubNet, timeStep);
|
|
|
- consumption = calculateEnergy("cons", singleSubNet, timeStep);
|
|
|
+ production = calculateEnergyWithFlexDevices("prod", singleSubNet, timeStep);
|
|
|
+ consumption = calculateEnergyWithFlexDevices("cons", singleSubNet, timeStep);
|
|
|
energySurplus = production + consumption;
|
|
|
}
|
|
|
|
|
@@ -126,40 +126,59 @@ public class SimulationManager {
|
|
|
for (HolonObject holonOb : subNet.getObjects()) {
|
|
|
for (HolonElement holonEl : holonOb.getElements()) {
|
|
|
|
|
|
+ // if this element is flexible and active (can be considered for calculations)
|
|
|
if (holonEl.isFlexible() && holonEl.isActive()) {
|
|
|
- // if this element is flexible
|
|
|
- float actuallyUsedEnergy = holonEl.getEnergyPerElement();
|
|
|
- float energyAvailable = holonEl.getAvailableEnergyAt(timestep) - actuallyUsedEnergy;
|
|
|
-
|
|
|
- // ------------- flexible consumer -------------
|
|
|
- if (energyAvailable < 0 && energySurplus > 0) {
|
|
|
-// System.out.println("flexible consumer in " + subNet.toString(timestep));
|
|
|
- // if there is more wasted Energy than energy that this device can give
|
|
|
- if (Math.abs(energyAvailable) <= Math.abs(energySurplus)) {
|
|
|
- energySurplus += energyAvailable;
|
|
|
+ float energyAvailableSingle = holonEl.getAvailableEnergyAt(timestep);
|
|
|
+ float energyAvailableMult = energyAvailableSingle * holonEl.getAmount();
|
|
|
+
|
|
|
+
|
|
|
+ // ------------- flexible consumer / OVERPRODUCTION -------------
|
|
|
+ if (energyAvailableMult < 0 && energySurplus > 0) {
|
|
|
+
|
|
|
+ // if there is more wasted energy than energy that this device can give, give all energy available
|
|
|
+ if (Math.abs(energyAvailableMult) <= Math.abs(energySurplus)) {
|
|
|
+ energySurplus += energyAvailableMult;
|
|
|
// set the new energy consumption to the maximum
|
|
|
- holonEl.setEnergyPerElement(holonEl.getAvailableEnergyAt(timestep));
|
|
|
- flexDevicesTurnedOnThisTurn.put(holonEl, energyAvailable);
|
|
|
+ holonEl.setEnergyPerElement(energyAvailableSingle);
|
|
|
+ flexDevicesTurnedOnThisTurn.put(holonEl, energyAvailableMult);
|
|
|
}
|
|
|
- // else if we just need to turn on part of the flexible energy available
|
|
|
+ // else: we just need to turn on part of the flexible energy available
|
|
|
else {
|
|
|
float energyNeeded = -energySurplus;
|
|
|
energySurplus += energyNeeded; // should give 0, but was kept this was for consistency
|
|
|
- // add to the currently used energy the energy needed divided through the amount of elements
|
|
|
- float newEnergyConsumption = holonEl.getEnergyPerElement() + energyNeeded / holonEl.getAmount();
|
|
|
- holonEl.setEnergyPerElement(newEnergyConsumption);
|
|
|
+ // the energy needed divided through the amount of elements
|
|
|
+ holonEl.setEnergyPerElement(energyNeeded / holonEl.getAmount());
|
|
|
flexDevicesTurnedOnThisTurn.put(holonEl, energyNeeded);
|
|
|
}
|
|
|
|
|
|
- System.out.println("One element was turned on, wastedEnergy after: " + energySurplus + "\n");
|
|
|
}
|
|
|
|
|
|
- // ------------- flexible producer -------------
|
|
|
- else if (energyAvailable > 0 && energySurplus < 0) {
|
|
|
- // System.out.println("flexible producer in " + subNet.toString(timestep));
|
|
|
- // TODO
|
|
|
+ // ------------- flexible producer / UNDEPRODUCTION -------------
|
|
|
+ else if (energyAvailableMult > 0 && energySurplus < 0) {
|
|
|
+
|
|
|
+ // if there is more energy needed than this device can give, give all the energy available
|
|
|
+ if (Math.abs(energyAvailableMult) <= Math.abs(energySurplus)) {
|
|
|
+ energySurplus += energyAvailableMult;
|
|
|
+ // set the new energy consumption to the maximum
|
|
|
+ holonEl.setEnergyPerElement(energyAvailableSingle);
|
|
|
+ flexDevicesTurnedOnThisTurn.put(holonEl, energyAvailableMult);
|
|
|
+ }
|
|
|
+ // else: we just need to turn on part of the flexible energy available
|
|
|
+ else {
|
|
|
+ float energyNeeded = -energySurplus;
|
|
|
+ int i = 0;
|
|
|
+ energySurplus += energyNeeded; // should give 0, but was kept this was for consistency
|
|
|
+ // the energy needed divided through the amount of elements
|
|
|
+ holonEl.setEnergyPerElement(energyNeeded / holonEl.getAmount());
|
|
|
+ flexDevicesTurnedOnThisTurn.put(holonEl, energyNeeded);
|
|
|
+ }
|
|
|
+
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+ if (energySurplus == 0) {
|
|
|
+ break;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -321,28 +340,64 @@ public class SimulationManager {
|
|
|
|
|
|
/**
|
|
|
* calculates the energy of either all producers or consumers.
|
|
|
+ * Flexible devices are filtered out
|
|
|
*
|
|
|
* @param type Type
|
|
|
* @param sN Subnet
|
|
|
* @param x Integer
|
|
|
* @return The Energy
|
|
|
*/
|
|
|
- private float calculateEnergy(String type, SubNet sN, int x) {
|
|
|
+ private float calculateEnergyWithoutFlexDevices(String type, SubNet sN, int x) {
|
|
|
float energy = 0;
|
|
|
for (HolonObject hl : sN.getObjects()) {
|
|
|
+ float currentEnergyWithoutFlexibles = hl.getCurrentEnergyAtTimeStepWithoutFlexiblesAndResetFlexibles(x);
|
|
|
+
|
|
|
+ if (type.equals("prod")) {
|
|
|
+ if (currentEnergyWithoutFlexibles > 0) {
|
|
|
+ energy += currentEnergyWithoutFlexibles;
|
|
|
+ hl.setState(HolonObject.PRODUCER);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (type.equals("cons")) {
|
|
|
+ if (currentEnergyWithoutFlexibles < 0) {
|
|
|
+ energy = energy + currentEnergyWithoutFlexibles;
|
|
|
+ hl.setState(HolonObject.NOT_SUPPLIED);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (currentEnergyWithoutFlexibles == 0) {
|
|
|
+ hl.setState(HolonObject.NO_ENERGY);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return energy;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * calculates the energy of either all producers or consumers.
|
|
|
+ * Flexible devices are filtered out
|
|
|
+ *
|
|
|
+ * @param type Type
|
|
|
+ * @param sN Subnet
|
|
|
+ * @param x Integer
|
|
|
+ * @return The Energy
|
|
|
+ */
|
|
|
+ private float calculateEnergyWithFlexDevices(String type, SubNet sN, int x) {
|
|
|
+ float energy = 0;
|
|
|
+ for (HolonObject hl : sN.getObjects()) {
|
|
|
+ float currentEnergy = hl.getCurrentEnergyAtTimeStep(x);
|
|
|
+
|
|
|
if (type.equals("prod")) {
|
|
|
- if (hl.getCurrentEnergyAtTimeStep(x) > 0) {
|
|
|
- energy = energy + hl.getCurrentEnergyAtTimeStep(x);
|
|
|
+ if (currentEnergy > 0) {
|
|
|
+ energy += currentEnergy;
|
|
|
hl.setState(HolonObject.PRODUCER);
|
|
|
}
|
|
|
}
|
|
|
if (type.equals("cons")) {
|
|
|
- if (hl.getCurrentEnergyAtTimeStep(x) < 0) {
|
|
|
- energy = energy + hl.getCurrentEnergyAtTimeStep(x);
|
|
|
+ if (currentEnergy < 0) {
|
|
|
+ energy = energy + currentEnergy;
|
|
|
hl.setState(HolonObject.NOT_SUPPLIED);
|
|
|
}
|
|
|
}
|
|
|
- if (hl.getCurrentEnergyAtTimeStep(x) == 0) {
|
|
|
+ if (currentEnergy == 0) {
|
|
|
hl.setState(HolonObject.NO_ENERGY);
|
|
|
}
|
|
|
}
|