Browse Source

#76: fixed bugs, added support for flexible producers

I. Dix 7 years ago
parent
commit
78ab278797

+ 6 - 16
src/classes/HolonElement.java

@@ -13,8 +13,8 @@ import java.util.LinkedList;
  */
 public class HolonElement {
 
-    // Points on the UnitGraph
-    LinkedList<Point> graphPoints;
+    /* Points on the UnitGraph */
+    private LinkedList<Point> graphPoints;
     /* Name of the gadget */
     @Expose
     private String eleName;
@@ -27,28 +27,19 @@ public class HolonElement {
     /* Whether the gadget is active or not (currently uses/produces the energy in energyPerElement) */
     @Expose
     private boolean active;
-    /*
-     * Gives us whether this element is flexible and can flexibly use any part of the energy in flexibleEnergyAvailable
-     */
+    /* Gives us whether this element is flexible and can flexibly use any part of the energy in flexibleEnergyAvailable */
     @Expose
     private boolean flexible;
-    /*
-     * Flexibility (meaning the actual
-     */
+    /* Flexibility (meaning the actual */
     @Expose
     private float flexibleEnergyAvailable;
-    /* Total Energy */
-    @Expose
-    private float totalEnergy;
     /* -: for Consumers and +: Producers */
     @Expose
     private char sign;
     /* Place where the Object is Stored */
     @Expose
     private Pair<String, String> saving;
-    /*
-     * ID
-     */
+    /* ID */
     @Expose
     private int id;
     /*
@@ -239,7 +230,7 @@ public class HolonElement {
      * @return totalEnergy (actual)
      */
     public float getOverallEnergy() {
-        totalEnergy = ((float) amount) * energyPerElement;
+        float totalEnergy = ((float) amount) * energyPerElement;
         return totalEnergy;
     }
 
@@ -251,7 +242,6 @@ public class HolonElement {
      */
     public float getOverallEnergyAtTimeStep(int x) {
         if (flexible) {
-            // TODO: what happens if energyPerElement > availableEnergyPerElementAt[x]?
             return ((float) amount) * energyPerElement;
         } else {
             return ((float) amount) * availableEnergyPerElementAt[x];

+ 19 - 0
src/classes/HolonObject.java

@@ -155,6 +155,25 @@ public class HolonObject extends AbstractCpsObject {
         return currentEnergy;
     }
 
+    /**
+     * Getter for the current energy at a given timestep.
+     *
+     * @param x timestep
+     * @return corresponding energy
+     */
+    public float getCurrentEnergyAtTimeStepWithoutFlexiblesAndResetFlexibles(int x) {
+        float temp = 0;
+        for (HolonElement e : getElements()) {
+            if (e.isActive() && !e.isFlexible()) {
+                temp += e.getOverallEnergyAtTimeStep(x);
+            } else if (e.isFlexible()) {
+                e.setEnergyPerElement(0);
+            }
+        }
+        currentEnergy = temp;
+        return currentEnergy;
+    }
+
     /**
      * deletes Element at a given index.
      *

+ 4 - 4
src/classes/SubNet.java

@@ -63,12 +63,12 @@ public class SubNet {
             HolonObject hl = getObjects().get(j);
             sb.append("    " + hl.getName() + " " + hl.getId());
         }
-        System.out.println("  Edges:");
+        sb.append("  Edges:");
         for (int j = 0; j < getEdges().size(); j++) {
             CpsEdge edge = getEdges().get(j);
             sb.append("     " + edge.getA().getName() + " connected To " + edge.getB().getName());
         }
-        System.out.println("  Switches:");
+        sb.append("  Switches:");
         for (int j = 0; j < getSwitches().size(); j++) {
             HolonSwitch sw = getSwitches().get(j);
             sb.append("    " + sw.getName() + " " + sw.getId() + " State:" + sw.getActiveAt()[timeStep]);
@@ -85,12 +85,12 @@ public class SubNet {
             HolonObject hl = getObjects().get(j);
             sb.append("    " + hl.getName() + " " + hl.getId());
         }
-        System.out.println("  Edges:");
+        sb.append("  Edges:");
         for (int j = 0; j < getEdges().size(); j++) {
             CpsEdge edge = getEdges().get(j);
             sb.append("     " + edge.getA().getName() + " connected To " + edge.getB().getName());
         }
-        System.out.println("  Switches:");
+        sb.append("  Switches:");
         for (int j = 0; j < getSwitches().size(); j++) {
             HolonSwitch sw = getSwitches().get(j);
             sb.append("    " + sw.getName() + " " + sw.getId() + " State:" + sw.getActiveAt());

+ 86 - 31
src/ui/controller/SimulationManager.java

@@ -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);
             }
         }

+ 2 - 4
src/ui/controller/UpdateController.java

@@ -200,13 +200,11 @@ public class UpdateController {
 	 * @return selected CpsObject
 	 */
 	public AbstractCpsObject getActualCps() {
-		AbstractCpsObject tempCps = null;
-		if (model.getSelectedCpsObjects().size() == 1) {
+        AbstractCpsObject tempCps;
+        if (model.getSelectedCpsObjects().size() == 1) {
 			tempCps = model.getSelectedCpsObjects().get(0);
 		} else {
-			// tempCps = model.getSelectedCpsObject();
 			int tempID = model.getSelectedObjectID();
-			// System.out.println(model.getSelectedObjectID());
 			tempCps = controller.searchByID(tempID);
 		}
 		return tempCps;

+ 5 - 0
src/ui/view/GUI.java

@@ -1061,6 +1061,11 @@ public class GUI implements CategoryListener {
                             else if (selectedValueX == 3) {
                                 Float ftemp = Float.parseFloat(newStuff);
                                 eleTemp.setFlexibleEnergyAvailable(ftemp);
+
+                                if (eleTemp.isFlexible()) {
+                                    eleTemp.setEnergyPerElement(0);
+                                    // TODO: recalculate energy
+                                }
                             }
                             // Amount of Elements update
                             else if (selectedValueX == 4) {

+ 5 - 5
src/ui/view/UnitGraph.java

@@ -23,8 +23,8 @@ import java.util.LinkedList;
 public class UnitGraph extends JPanel implements MouseListener, MouseMotionListener, ComponentListener {
 
 	private static final long serialVersionUID = 1L;
-    GeneralPath graphCurve = new GeneralPath();
-    private float maximum = 0;
+	private GeneralPath graphCurve = new GeneralPath();
+	private float maximum = 0;
 	// Information shown when a Point is Dragged
 	private String dragInformation = "";
 	// Points
@@ -125,9 +125,9 @@ public class UnitGraph extends JPanel implements MouseListener, MouseMotionListe
 
 				// Draw the Points
 				g2.setColor(Color.BLUE);
-				for (int i = 0; i < pointList.size() - 0; i++) {
-					g2.fillOval((int) (pointList.get(i).getX() * scaleX - recSize.getX() / 2) + border,
-							(int) (pointList.get(i).getY() * scaleY - recSize.getY() / 2) + border,
+				for (Point aPointList : pointList) {
+					g2.fillOval((int) (aPointList.getX() * scaleX - recSize.getX() / 2) + border,
+							(int) (aPointList.getY() * scaleY - recSize.getY() / 2) + border,
 							(int) recSize.getX(), (int) recSize.getY());
 				}