Forráskód Böngészése

Fixes Red-Houses Bug

Adds HolonObject Comparators to Compare Min-, Total- and Diff- Energy.
Implements a HolonObject.getMinEnergy Method.
Improves Color Handling of Subnets, with just some supplied Elements.
Andreas T. Meyer-Berg 7 éve
szülő
commit
9729e57ff7

+ 22 - 0
src/classes/HolonObject.java

@@ -324,6 +324,28 @@ public class HolonObject extends AbstractCpsObject {
             return false;
         }
     }
+    
+    /**
+     * Calculates the minimumEnergy Needed to turn on atleast on device
+     * of this HolonObject
+     * @param x Timestep of the calculation
+     * @return minEnergy, -inf if no Devices are consuming power
+     */
+    public float getMinEnergy(int x) {
+        if (getElements().size() == 0) {
+            return Float.NEGATIVE_INFINITY;
+        }
+        float minConsum = Float.NEGATIVE_INFINITY;
+        for (HolonElement e : getElements()) {
+            if (e.isActive()) {
+                float overallEnergy = e.getOverallEnergyAtTimeStep(x);
+                if (minConsum < 0 && (overallEnergy > minConsum && overallEnergy < 0)) {
+                    minConsum = overallEnergy;
+                } 
+            }
+        }
+        return minConsum;
+    }
 
     /**
      * Get the Color.

+ 41 - 0
src/classes/comparator/EnergyMinToMaxComparator.java

@@ -0,0 +1,41 @@
+package classes.comparator;
+
+import java.util.Comparator;
+
+import classes.HolonObject;
+
+/**
+ * Comparator of the Energy Difference between Total Energy Consumption and Min Consumption
+ * of a Holon Objects.
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class EnergyMinToMaxComparator implements Comparator<HolonObject> {
+
+	private int timeStep = 0;
+	
+	/**
+	 * Comparator for the totalEnergy of HolonObjects
+	 * @param timeStep
+	 */
+	public EnergyMinToMaxComparator(int timeStep) {
+		this.timeStep = timeStep;
+	}
+
+	@Override
+	public int compare(HolonObject o1, HolonObject o2) {
+		float minEnergy1 = o1.getMinEnergy(timeStep);
+		float minEnergy2 = o2.getMinEnergy(timeStep);
+		float totalEnergy1 = o1.getCurrentEnergyAtTimeStep(timeStep);
+		float totalEnergy2 = o2.getCurrentEnergyAtTimeStep(timeStep);
+		float difference1 = totalEnergy1 - minEnergy1;
+		float difference2 = totalEnergy2 - minEnergy2;
+		if(difference1 < difference2)
+			return 1;
+		else if (difference1 == difference2) 
+			return 0;
+		else
+			return -1;
+	}
+
+}

+ 36 - 0
src/classes/comparator/MinEnergyComparator.java

@@ -0,0 +1,36 @@
+package classes.comparator;
+
+import java.util.Comparator;
+
+import classes.HolonObject;
+
+/**
+ * Comparator for Min Consumption of Holon Objects.
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class MinEnergyComparator implements Comparator<HolonObject> {
+
+	private int timeStep = 0;
+	
+	/**
+	 * Comparator for the minEnergy of HolonObjects
+	 * @param timeStep
+	 */
+	public MinEnergyComparator(int timeStep) {
+		this.timeStep = timeStep;
+	}
+
+	@Override
+	public int compare(HolonObject o1, HolonObject o2) {
+		float minEnergy1 = o1.getMinEnergy(timeStep);
+		float minEnergy2 = o2.getMinEnergy(timeStep);
+		if(minEnergy1<minEnergy2)
+			return -1;
+		else if (minEnergy1 == minEnergy2) 
+			return 0;
+		else
+			return 1;
+	}
+
+}

+ 36 - 0
src/classes/comparator/TotalEnergyComparator.java

@@ -0,0 +1,36 @@
+package classes.comparator;
+
+import java.util.Comparator;
+
+import classes.HolonObject;
+
+/**
+ * Comparator for the totalEnergy of HolonObjects
+ *
+ * @author Andreas T. Meyer-Berg
+ */
+public class TotalEnergyComparator implements Comparator<HolonObject> {
+
+	private int timeStep = 0;
+	
+	/**
+	 * Comparator for the totalEnergy of HolonObjects
+	 * @param timeStep
+	 */
+	public TotalEnergyComparator(int timeStep) {
+		this.timeStep = timeStep;
+	}
+
+	@Override
+	public int compare(HolonObject o1, HolonObject o2) {
+		float totalEnergy1 = o1.getCurrentEnergyAtTimeStep(timeStep);
+		float totalEnergy2 = o2.getCurrentEnergyAtTimeStep(timeStep);
+		if(totalEnergy1<totalEnergy2)
+			return -1;
+		else if (totalEnergy1 == totalEnergy2) 
+			return 0;
+		else
+			return 1;
+	}
+
+}

+ 55 - 1
src/ui/controller/SimulationManager.java

@@ -1,6 +1,9 @@
 package ui.controller;
 
 import classes.*;
+import classes.comparator.EnergyMinToMaxComparator;
+import classes.comparator.MinEnergyComparator;
+import classes.comparator.TotalEnergyComparator;
 import ui.model.Model;
 import ui.view.FlexiblePane;
 import ui.view.MyCanvas;
@@ -78,6 +81,20 @@ public class SimulationManager {
             setFlowSimulation(singleSubNet);
 
             // --------------- visualise graph ---------------
+           
+            
+            /**
+             * production of subnets, that might be partially turned on/off
+             */
+            float currentProduction = production;
+            /**
+             * HolonObjects that can be partially Supplied but might be fully Supplied
+             */
+            ArrayList<HolonObject> partiallySuppliedList = new ArrayList<HolonObject>();
+            /**
+             * supply Buildings with minimal Energy first, if conflicts happen
+             */
+            singleSubNet.getObjects().sort(new MinEnergyComparator(x));
             for (HolonObject hl : singleSubNet.getObjects()) {
                 if (hl.getState() != HolonObject.NO_ENERGY
                         && hl.getState() != HolonObject.PRODUCER) {
@@ -97,12 +114,32 @@ public class SimulationManager {
                                 } else if (hl.checkIfPartiallySupplied(timeStep)) {
                                     hl.setState(HolonObject.PARTIALLY_SUPPLIED);
                                 } else {
-                                    hl.setState(HolonObject.NOT_SUPPLIED);
+                                	/**
+                                	 * Case that only some HolonObjects can be supplied
+                                	 */
+                                	if(-hl.getCurrentEnergyAtTimeStep(x)<=currentProduction){
+                                		hl.setState(HolonObject.PARTIALLY_SUPPLIED);
+                                		currentProduction += hl.getMinEnergy(x);
+                                		partiallySuppliedList.add(hl);
+                                	}else if(-hl.getMinEnergy(x)<=currentProduction){
+                                		hl.setState(HolonObject.PARTIALLY_SUPPLIED);
+                                		currentProduction += hl.getMinEnergy(x);
+                                		partiallySuppliedList.add(hl);
+                                	}else{
+                                		hl.setState(HolonObject.NOT_SUPPLIED);
+                                		//currentProduction += 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) {
@@ -110,6 +147,23 @@ public class SimulationManager {
                     }
                 }
             }
+            
+            /**
+             * check if some partially supplied building might be fully supplied.
+             */
+            partiallySuppliedList.sort(new EnergyMinToMaxComparator(x));
+            for(HolonObject part: partiallySuppliedList){
+            	currentProduction -= part.getMinEnergy(x);
+            	/*
+            	 * if possible, supply fully
+            	 */
+            	if(-part.getCurrentEnergyAtTimeStep(x)<=currentProduction){
+            		part.setState(HolonObject.SUPPLIED);
+            		currentProduction += part.getCurrentEnergyAtTimeStep(x);
+            	}else{
+            		currentProduction += part.getMinEnergy(x);
+            	}
+            }
         }
         canvas.repaint();
         flexPane.recalculate();