Rolf Egert 6 年之前
父节点
当前提交
8f9bb9fc02

+ 20 - 11
src/classes/HolonObject.java

@@ -89,20 +89,23 @@ public class HolonObject extends AbstractCpsObject {
         if (getMaxActiveEnergy() > 0) {
             setState(PRODUCER);
             stateColor = Color.lightGray;
-        } else {
-            if (getMaxActiveEnergy() == 0) {
+        } 
+        
+        if (getMaxActiveEnergy() == 0) {
                 setState(NO_ENERGY);
                 stateColor = Color.WHITE;
-            } else {
-                if (checkIfPartiallySupplied(0)) {
-                    stateColor = Color.yellow;
-                } else {
-                    stateColor = new Color(230, 120, 100);
-                }
-
-            }
+        } 
+        
+        if (checkIfPartiallySupplied(0)) {
+        	setState(PARTIALLY_SUPPLIED);
+            stateColor = Color.yellow;
+        }else {
+        	stateColor = new Color(230, 120, 100);
+        	setState(NOT_SUPPLIED);
+    //TODO THis is not correct!!!!   
         }
     }
+    
 
     /**
      * Getter for all Elements in the HolonObject.
@@ -278,25 +281,31 @@ public class HolonObject extends AbstractCpsObject {
         switch (state) {
             case NO_ENERGY:
                 stateColor = Color.WHITE;
+                this.state = NO_ENERGY;
                 break;
 
             case NOT_SUPPLIED:
+            	this.state = NOT_SUPPLIED;
                 stateColor = new Color(230, 120, 100);
                 break;
 
             case SUPPLIED:
+            	this.state = SUPPLIED;
                 stateColor = Color.GREEN;
                 break;
 
             case PRODUCER:
+            	this.state = PRODUCER;
                 stateColor = Color.lightGray;
                 break;
 
             case PARTIALLY_SUPPLIED:
+            	this.state = PARTIALLY_SUPPLIED;
                 stateColor = Color.YELLOW;
                 break;
 
             case OVER_SUPPLIED:
+            	this.state =OVER_SUPPLIED;
                 // find different purple-tones at
                 // http://www.rapidtables.com/web/color/purple-color.htm
                 stateColor = new Color(138, 43, 226);
@@ -397,7 +406,7 @@ public class HolonObject extends AbstractCpsObject {
      * @param x Timestep of the calculation
      * @return minEnergy, -inf if no Devices are consuming power
      */
-    public float getMinEnergy(int x) {
+    public float getMinEnergyNecessaryAtTimestep(int x) {
         if (getElements().size() == 0) {
             return Float.NEGATIVE_INFINITY;
         }

+ 2 - 2
src/classes/comparator/EnergyMinToMaxComparator.java

@@ -24,8 +24,8 @@ public class EnergyMinToMaxComparator implements Comparator<HolonObject> {
 
 	@Override
 	public int compare(HolonObject o1, HolonObject o2) {
-		float minEnergy1 = o1.getMinEnergy(timeStep);
-		float minEnergy2 = o2.getMinEnergy(timeStep);
+		float minEnergy1 = o1.getMinEnergyNecessaryAtTimestep(timeStep);
+		float minEnergy2 = o2.getMinEnergyNecessaryAtTimestep(timeStep);
 		float totalEnergy1 = o1.getCurrentEnergyAtTimeStep(timeStep);
 		float totalEnergy2 = o2.getCurrentEnergyAtTimeStep(timeStep);
 		float difference1 = totalEnergy1 - minEnergy1;

+ 2 - 2
src/classes/comparator/MinEnergyComparator.java

@@ -23,8 +23,8 @@ public class MinEnergyComparator implements Comparator<HolonObject> {
 
 	@Override
 	public int compare(HolonObject o1, HolonObject o2) {
-		float minEnergy1 = o1.getMinEnergy(timeStep);
-		float minEnergy2 = o2.getMinEnergy(timeStep);
+		float minEnergy1 = o1.getMinEnergyNecessaryAtTimestep(timeStep);
+		float minEnergy2 = o2.getMinEnergyNecessaryAtTimestep(timeStep);
 		if(minEnergy1<minEnergy2)
 			return 1;
 		else if (minEnergy1 == minEnergy2) 

+ 4 - 0
src/psoAlgoCode/PSO.java

@@ -69,9 +69,13 @@ public class PSO implements CpsAlgorithm {
 					((HolonObject) model.getObjectsOnCanvas().get(j)).getElements().get(h)
 							.setActive((boolean) bestConfig.getCoord(1).get(position));
 					position++;
+					
 				}
 
+				HolonObject tmp = (HolonObject) model.getObjectsOnCanvas().get(j);
+				System.out.println("object number: " + tmp.getId() + " with current state: " + tmp.getState());
 			}
+		
 		}
 		controller.calculateStateForCurrentTimeStep();
 	}

+ 37 - 41
src/psoAlgoCode/SGFunctions.java

@@ -52,44 +52,15 @@ public class SGFunctions {
 		double nw_fitness =0.0;
 		double object_fitness = 0.0;
 
-		//int nrOfSubnets = control.getSimManager().getSubNets().size();
-		//System.out.println("nr of subnets: " + nrOfSubnets);
 		nw_fitness = networkFitness();
 		object_fitness = holonObjectFitness();
 		
 		System.out.println("NW_fitness: " + nw_fitness +" objectFitness: " + object_fitness);
 		
 		value = nw_fitness + object_fitness;
-		/*for (AbstractCpsObject obj : model.getObjectsOnCanvas()) {
-			if (obj instanceof HolonObject) {
-				int state = ((HolonObject) obj).getState();
-				double suppliedPerc = ((HolonObject) obj).getSuppliedPercentage();
-				double elementsActive = (double) elementsActive((HolonObject) obj);
-				double totalElements = (double) ((HolonObject) obj).getElements().size();
-				double difference = Math.abs(1.0 - suppliedPerc) * 100;
-				double differenceEle = (1.0 - (elementsActive / totalElements)) * 100;
-				// Supply States (see HolonObject class for more informations)
-				switch (state) {
-				case 0:
-					value += (100 * 8);
-					break;
-				case 1:
-					value += difference * 4 + differenceEle;
-					break;
-				case 2:
-					value += difference + differenceEle;
-					break;
-				case 4:
-					value += difference + differenceEle;
-					break;
-				case 5:
-					value += difference * 2 + differenceEle;
-					break;
-				default:
-					break;
-				}
-			}
-		}*/
+	
+		
+	
 		return value;
 	}
 
@@ -101,7 +72,27 @@ public class SGFunctions {
 		double result = 0.0;
 		ArrayList<SubNet> tmp_sn = HelpFunctions.getCurrentSubnets();
 		
-		result = (tmp_sn.size() - 1) * 10000;
+		for (SubNet subNet : tmp_sn) {
+			ArrayList<HolonObject> tmp = subNet.getObjects();
+			for (HolonObject holonObject : tmp) {
+				System.out.println("Current state: " +holonObject.getState());
+			}
+		//	float sn_prod =0;
+			//float sn_cons =0;
+			
+			float production = control.getSimManager().calculateEnergyWithoutFlexDevices("prod",
+					subNet, model.getCurIteration());
+			float consumption = control.getSimManager().calculateEnergyWithoutFlexDevices("cons",
+					subNet, model.getCurIteration());
+			System.out.println("current production: " + production + " current consumption: " + consumption);
+			
+		result += Math.abs(production+consumption)*10;	
+		}
+		
+		//result = (tmp_sn.size() - 1) * 100;
+		
+		
+		
 		return result;
 	}
 	/**
@@ -123,13 +114,13 @@ public class SGFunctions {
 
 		for (AbstractCpsObject obj : objList) {
 			if (obj instanceof HolonObject) {
-				float supply = ((HolonObject) obj).getCurrentSupply();
-				float energy =((HolonObject) obj).getMaxActiveEnergy();
 				float suppPercentage = ((HolonObject) obj).getSuppliedPercentage();
+				/*float supply = ((HolonObject) obj).getCurrentSupply();
+				float energy =((HolonObject) obj).getMaxActiveEnergy();
 				float ownProduction = ((HolonObject) obj).getSelfMadeEnergy(model.getCurIteration());
 				float maxEnergy = ((HolonObject) obj).getMaxPossibleConsumption();
 				int elem = ((HolonObject) obj).getNumberOfActiveElements();
-			
+			*/
 				if(!(obj.getName().contains("Plant"))) {
 					result += holonObjectSupplyPenaltyFunction(suppPercentage);
 					result += holonObjectStatePenalty((HolonObject)obj);
@@ -176,28 +167,33 @@ public class SGFunctions {
 			result =0;
 		else if(activeElements == 0) {
 			result = 10000;
-		}else result = ((maxElements -activeElements)*300);
+		}else result = ((maxElements -activeElements)*600);
 		
 	return result;
 		
 	}
-	
+	/**
+	 * Pentalty Function that is based on the different states an object can have.
+	 * @param obj The holon object that is used for the assessment
+	 * @return pentalty value
+	 */
 	private double holonObjectStatePenalty(HolonObject obj) {
 		float result = 0;
 		
+		//TODO currently no penalties on undesired states, since the state assignment is broken...
 		int state = obj.getState();
 		switch (state) {
 		case 0: result = 0;
 				break;
-		case 1: result = 10;
+		case 1: result = 0;
 			break;
 		case 2: result = 0;
 			break;
 		case 3: result = 0;
 			break;
-		case 4: result = 5;
+		case 4: result = 0;
 			break;
-		case 5:	result = 5;
+		case 5:	result = 0;
 			break;
 			
 

+ 2 - 2
src/psoAlgoCode/SimulateSimulator.java

@@ -203,7 +203,7 @@ public class SimulateSimulator {
 								continue;
 							if (hO.getCurrentEnergyAtTimeStep(x) > 0)
 								continue;
-							float minEnergy = -hO.getMinEnergy(x); // Energy from getMinEnergy is negative -> convert to
+							float minEnergy = -hO.getMinEnergyNecessaryAtTimestep(x); // Energy from getMinEnergy is negative -> convert to
 																	// positive
 							if (minEnergy <= currentProduction) {
 								hO.setCurrentSupply(minEnergy);
@@ -249,7 +249,7 @@ public class SimulateSimulator {
 					} else if (currentSupply == neededEnergy) {
 						hO.setState(HolonObject.SUPPLIED);
 					} else if (currentSupply < neededEnergy) {
-						float minEnergy = -hO.getMinEnergy(x);
+						float minEnergy = -hO.getMinEnergyNecessaryAtTimestep(x);
 						if (currentSupply >= minEnergy || hO.getSelfMadeEnergy(x) >= minEnergy) {
 							hO.setState(HolonObject.PARTIALLY_SUPPLIED);
 						} else {

+ 8 - 8
src/ui/controller/SimulationManager.java

@@ -262,7 +262,7 @@ public class SimulationManager {
 						{
 							if(hO.checkIfPartiallySupplied(x))continue;
 							if(hO.getCurrentEnergyAtTimeStep(x) > 0)continue;
-							float minEnergy = -hO.getMinEnergy(x); //Energy from getMinEnergy is negative -> convert to positive
+							float minEnergy = -hO.getMinEnergyNecessaryAtTimestep(x); //Energy from getMinEnergy is negative -> convert to positive
 							if(minEnergy <= currentProduction)
 							{
 								hO.setCurrentSupply(minEnergy);
@@ -318,7 +318,7 @@ public class SimulationManager {
 						hO.setState(HolonObject.SUPPLIED);
 					}else if (currentSupply <  neededEnergy)
 					{
-						float minEnergy = -hO.getMinEnergy(x);
+						float minEnergy = -hO.getMinEnergyNecessaryAtTimestep(x);
 						if(currentSupply >= minEnergy || hO.getSelfMadeEnergy(x)  >= minEnergy )
 						{
 							hO.setState(HolonObject.PARTIALLY_SUPPLIED);
@@ -384,7 +384,7 @@ public class SimulationManager {
 							} else if (energyPerHolonObject == -neededEnergy) {
 								hl.setState(HolonObject.SUPPLIED);
 							} else if (energyPerHolonObject >= -hl
-									.getMinEnergy(x)) {
+									.getMinEnergyNecessaryAtTimestep(x)) {
 								hl.setState(HolonObject.PARTIALLY_SUPPLIED);
 							} else {
 								hl.setState(HolonObject.NOT_SUPPLIED);
@@ -404,7 +404,7 @@ public class SimulationManager {
 											.getCurrentEnergyAtTimeStep(x));
 								}
 							} else {
-								float minEnergy = hl.getMinEnergy(x);									
+								float minEnergy = hl.getMinEnergyNecessaryAtTimestep(x);									
 								if (hl.checkIfPartiallySupplied(timeStep)) {
 									hl.setState(HolonObject.PARTIALLY_SUPPLIED);
 									currentProduction += minEnergy;
@@ -420,7 +420,7 @@ public class SimulationManager {
 										currentProduction += minEnergy;
 										hl.setCurrentSupply(-minEnergy);
 										partiallySuppliedList.add(hl);
-									} else if (-hl.getMinEnergy(x) <= currentProduction) {
+									} else if (-hl.getMinEnergyNecessaryAtTimestep(x) <= currentProduction) {
 										hl.setState(HolonObject.PARTIALLY_SUPPLIED);
 										currentProduction += minEnergy;
 										hl.setCurrentSupply(-minEnergy);
@@ -459,7 +459,7 @@ public class SimulationManager {
 			 */
 			partiallySuppliedList.sort(new EnergyMinToMaxComparator(x));
 			for (HolonObject hl : partiallySuppliedList) {
-				float minEnergy = hl.getMinEnergy(x);
+				float minEnergy = hl.getMinEnergyNecessaryAtTimestep(x);
 				currentProduction -= minEnergy;
 				/*
 				 * if possible, supply fully
@@ -784,7 +784,7 @@ public class SimulationManager {
 	 *            Integer
 	 * @return The Energy
 	 */
-	private float calculateEnergyWithoutFlexDevices(String type, SubNet sN,
+	public float calculateEnergyWithoutFlexDevices(String type, SubNet sN,
 			int x) {
 		float energy = 0;
 		for (HolonObject hl : sN.getObjects()) {
@@ -822,7 +822,7 @@ public class SimulationManager {
 	 *            Integer
 	 * @return The Energy
 	 */
-	private float calculateEnergyWithFlexDevices(String type, SubNet sN, int x) {
+	public float calculateEnergyWithFlexDevices(String type, SubNet sN, int x) {
 		float energy = 0;
 		for (HolonObject hl : sN.getObjects()) {
 			float currentEnergy = hl.getCurrentEnergyAtTimeStep(x);