Browse Source

fitness function alpha

Rolf Egert 6 years ago
parent
commit
e1ee54ae15
3 changed files with 30 additions and 33 deletions
  1. 1 1
      src/classes/HolonObject.java
  2. 6 2
      src/psoAlgoCode/HelpFunctions.java
  3. 23 30
      src/psoAlgoCode/SGFunctions.java

+ 1 - 1
src/classes/HolonObject.java

@@ -18,8 +18,8 @@ public class HolonObject extends AbstractCpsObject {
      * (whether it needs no energy, whether it is not supplied, fully supplied,
      * a producer, partially supplied or over-supplied)
      */
-    public final static int NO_ENERGY = 0;
     public final static int NOT_SUPPLIED = 1;
+    public final static int NO_ENERGY = 0;
     public final static int SUPPLIED = 2;
     public final static int PRODUCER = 3;
     public final static int PARTIALLY_SUPPLIED = 4;

+ 6 - 2
src/psoAlgoCode/HelpFunctions.java

@@ -20,7 +20,7 @@ import ui.controller.SimulationManager;
 public class HelpFunctions {
 
 	private ArrayList<AbstractCpsObject> objectsToHandle;
-
+	static SimulateSimulator sim;
 	public static void runRepairConnections(Model model) {
 		repairAllEdgesOnMainCanvas(model);
 	}
@@ -48,7 +48,11 @@ public class HelpFunctions {
 	}
 
 	public static void calculateStateForTimeStepPSO(Model model, Control control) {
-		SimulateSimulator sim = new SimulateSimulator(model);
+		sim = new SimulateSimulator(model);
 		sim.calculateStateForTimeStep(model.getCurIteration());
 	}
+	
+	public static ArrayList<SubNet> getCurrentSubnets(){
+		return sim.getSubNets();
+	}
 }

+ 23 - 30
src/psoAlgoCode/SGFunctions.java

@@ -49,12 +49,17 @@ public class SGFunctions {
 		implementState();
 		HelpFunctions.calculateStateForTimeStepPSO(model, control);
 		double value = 0.0;
+		double nw_fitness =0.0;
+		double object_fitness = 0.0;
 
 		//int nrOfSubnets = control.getSimManager().getSubNets().size();
 		//System.out.println("nr of subnets: " + nrOfSubnets);
-		value += networkFitness();
-		value += holonObjectFitness();
+		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();
@@ -94,15 +99,9 @@ public class SGFunctions {
 	 */
 	private double networkFitness() {
 		double result = 0.0;
-		ArrayList<SubNet> tmp_sn = control.getSimManager().getSubNets();
-		int totalNrOfObjects = model.getObjectsOnCanvas().size();
-		
-		System.out.println("Total number of objects is: " + totalNrOfObjects);
-		for (SubNet subNet : tmp_sn) {
-			System.out.println("Subnetsize: " + (subNet.getObjects().size()));
-		}
+		ArrayList<SubNet> tmp_sn = HelpFunctions.getCurrentSubnets();
 		
-		result = (tmp_sn.size() - 1) * 300;
+		result = (tmp_sn.size() - 1) * 10000;
 		return result;
 	}
 	/**
@@ -121,8 +120,7 @@ public class SGFunctions {
 	private double holonObjectFitness() {
 		double result = 0.0;
 		ArrayList<AbstractCpsObject> objList = model.getObjectsOnCanvas();
-		System.out.println("total consumption works: " + control.getTotalConsumption(objList));
-		//TODO Annoying as fuck... think think
+
 		for (AbstractCpsObject obj : objList) {
 			if (obj instanceof HolonObject) {
 				float supply = ((HolonObject) obj).getCurrentSupply();
@@ -130,21 +128,15 @@ public class SGFunctions {
 				float suppPercentage = ((HolonObject) obj).getSuppliedPercentage();
 				float ownProduction = ((HolonObject) obj).getSelfMadeEnergy(model.getCurIteration());
 				float maxEnergy = ((HolonObject) obj).getMaxPossibleConsumption();
+				int elem = ((HolonObject) obj).getNumberOfActiveElements();
 			
-				System.out.println(obj.getName());
 				if(!(obj.getName().contains("Plant"))) {
 					result += holonObjectSupplyPenaltyFunction(suppPercentage);
 					result += holonObjectStatePenalty((HolonObject)obj);
 					
 				}
 					result += inactiveHolonElementPenalty((HolonObject)obj);
-					
-					
-				System.out.println("min ele" + ((HolonObject) obj).getMinimalConsumingElement().toString());
-				//if(((HolonObject) obj).getCurrentEnergyAtTimeStep(model.getCurIteration())/maxEnergy <;
-				
-				System.out.println("Own production: " + ownProduction + "Provided Supply and energy: " + supply + " and " + energy + " with total possible energy: " + maxEnergy
-						+ " is supply percentage: " + suppPercentage);
+		
 			}
 			
 		}
@@ -161,16 +153,17 @@ public class SGFunctions {
 		float result = 0;
 		if(supplyPercentage == 1)
 			return result;
-		else if(supplyPercentage < 1 && supplyPercentage >= 0.25)
-			result = (float) (1/supplyPercentage *1.5);
-		else if (supplyPercentage < 0.25)
-			result = 1/supplyPercentage * 2; //Scalar value might be problematic for large/specific examples
-		else if (supplyPercentage < 1.25) 
-			result = 1 * supplyPercentage *2 ;
-		else result = 1 * supplyPercentage * 3;
+		else if(supplyPercentage < 1 && supplyPercentage >= 0.25) // undersupplied inbetween 25% and 100%
+			result = (float) (1/supplyPercentage *15);
+		else if (supplyPercentage < 0.25) //undersupplied with less than 25%
+			result = 1/supplyPercentage * 20;
+		else if (supplyPercentage < 1.25)  //Oversupplied less than 25%
+			result = 1 * supplyPercentage *20 ;
+		else result = 1 * supplyPercentage * 30; //Oversupplied more than 25%
+		
 		if(Float.isInfinite(result) || Float.isNaN(result))
 			result = 100;
-		System.out.println("Low penalty function value: " + result + " with supply value: " + supplyPercentage);
+
 		return result;
 	}
 	
@@ -182,8 +175,8 @@ public class SGFunctions {
 		if(activeElements == maxElements)
 			result =0;
 		else if(activeElements == 0) {
-			result = 100;
-		}else result = ((maxElements -activeElements)*200);
+			result = 10000;
+		}else result = ((maxElements -activeElements)*300);
 		
 	return result;