瀏覽代碼

psoalgo edits

Rolf Egert 6 年之前
父節點
當前提交
3a668f98d8

+ 4 - 0
src/classes/HolonElement.java

@@ -354,6 +354,10 @@ public class HolonElement implements IGraphedElement{
             setFlexibleEnergyAvailable(0);
         }
     }
+    
+    public boolean isProducer (){
+    	return (energyPerElement > 0) ? true : false;
+    }
 
     /**
      * Get the Id of the selected HolonElement.

+ 39 - 4
src/classes/HolonObject.java

@@ -88,11 +88,11 @@ public class HolonObject extends AbstractCpsObject {
      * partially or over-supplied
      */
     public void setState() {
-        if (getCurrentEnergy() > 0) {
+        if (getMaxActiveEnergy() > 0) {
             setState(PRODUCER);
             stateColor = Color.lightGray;
         } else {
-            if (getCurrentEnergy() == 0) {
+            if (getMaxActiveEnergy() == 0) {
                 setState(NO_ENERGY);
                 stateColor = Color.WHITE;
             } else {
@@ -114,6 +114,17 @@ public class HolonObject extends AbstractCpsObject {
     public ArrayList<HolonElement> getElements() {
         return elements;
     }
+    
+    public HolonElement getMinimalConsumingElement() {
+    	HolonElement min = elements.get(0);
+    	for (HolonElement e : elements) {
+    		if(e.getOverallEnergy() < 0 && e.getOverallEnergy() > min.getOverallEnergy()) {
+    			min = e;
+    		}
+    		
+		}
+    	return min;
+    }
 
     /**
      * Set a new ArrayList with HolonElements into the HolonObject.
@@ -139,7 +150,7 @@ public class HolonObject extends AbstractCpsObject {
      *
      * @return the currentEnergy
      */
-    public float getCurrentEnergy() {
+    public float getMaxActiveEnergy() {
         float temp = 0;
         for (HolonElement e : getElements()) {
             if (e.isActive()) {
@@ -149,6 +160,19 @@ public class HolonObject extends AbstractCpsObject {
         currentEnergy = temp;
         return currentEnergy;
     }
+    /**
+     * Returns the maximum possible consumption of the holon object without flexibility potential.
+     * @return
+     */
+    public float getMaxPossibleConsumption() {
+    	float energy = 0;
+    	for (HolonElement e : getElements()) {
+    		if(!e.isProducer()) {
+    			energy += e.getOverallEnergy();			
+    		}
+    	}
+    	return energy;
+    }
 
     /**
      * Getter for the current energy at a given timestep.
@@ -173,7 +197,7 @@ public class HolonObject extends AbstractCpsObject {
             }
         }
         currentEnergy = temp;
-        suppliedPercentage = prod / -cons;
+        suppliedPercentage = prod /-cons;
         return currentEnergy;
     }
 
@@ -578,4 +602,15 @@ public class HolonObject extends AbstractCpsObject {
 	public float getSuppliedPercentage(){
 		return suppliedPercentage;
 	}
+	
+	// Check the amount of HolonElements that are active
+	public int getNumberOfActiveElements() {
+		int counter = 0;
+		for (HolonElement ele : elements) {
+			if (ele.isActive()) {
+				counter++;
+			}
+		}
+		return counter;
+	}
 }

+ 2 - 2
src/psoAlgoCode/Constants.java

@@ -17,8 +17,8 @@ public class Constants {
 	public static int ROUNDS = 1;
 
 	// Variables PSO
-	public static double START_OMEGA = 0.7;
-	public static double END_OMEGA = 0.4;
+	public static double START_OMEGA = 1.2;//0.7;
+	public static double END_OMEGA = 0.2;//0.4;
 	public static double DIFF_OMEGA = ((START_OMEGA - END_OMEGA) / MAX_ITERATION);
 
 	// Dimensions determines the length of the

+ 1 - 0
src/psoAlgoCode/LineChart.java

@@ -9,6 +9,7 @@ import java.util.Vector;
 import javax.swing.JButton;
 import javax.swing.JDialog;
 import javax.swing.JPanel;
+
 import org.jfree.chart.ChartFactory;
 import org.jfree.chart.ChartPanel;
 import org.jfree.chart.JFreeChart;

+ 108 - 9
src/psoAlgoCode/SGFunctions.java

@@ -1,10 +1,12 @@
 package psoAlgoCode;
 
+import java.util.ArrayList;
 import java.util.Vector;
 
 import classes.AbstractCpsObject;
 import classes.HolonElement;
 import classes.HolonObject;
+import classes.SubNet;
 import ui.controller.Control;
 import ui.model.Model;
 
@@ -45,7 +47,13 @@ public class SGFunctions {
 		implementState();
 		HelpFunctions.calculateStateForTimeStepPSO(model, control);
 		double value = 0.0;
-		for (AbstractCpsObject obj : model.getObjectsOnCanvas()) {
+
+		//int nrOfSubnets = control.getSimManager().getSubNets().size();
+		//System.out.println("nr of subnets: " + nrOfSubnets);
+		networkFitness();
+		value += holonObjectFitness();
+		
+		/*for (AbstractCpsObject obj : model.getObjectsOnCanvas()) {
 			if (obj instanceof HolonObject) {
 				int state = ((HolonObject) obj).getState();
 				double suppliedPerc = ((HolonObject) obj).getSuppliedPercentage();
@@ -74,19 +82,110 @@ public class SGFunctions {
 					break;
 				}
 			}
-		}
+		}*/
 		return value;
 	}
 
-	// Check the amount of HolonElements that are active
-	private int elementsActive(HolonObject obj) {
-		int counter = 0;
-		for (HolonElement ele : obj.getElements()) {
-			if (ele.isActive()) {
-				counter++;
+	/**
+	 * Calculates a fitness value for the overall abstract network
+	 * @return
+	 */
+	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()));
+		}
+		
+		
+		return result;
+	}
+	/**
+	 * Calculate a fitnessvalue concerned with the performance of individual holons of the network
+	 * @return
+	 */
+	private double holonFitness() {
+		double result = 0.0;
+		
+		return result;
+	}
+	/**
+	 * Calculates a fitnessvalue for the individual holon objects in the holons
+	 * @return
+	 */
+	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();
+				float energy =((HolonObject) obj).getMaxActiveEnergy();
+				float suppPercentage = ((HolonObject) obj).getSuppliedPercentage();
+				float ownProduction = ((HolonObject) obj).getSelfMadeEnergy(model.getCurIteration());
+				float maxEnergy = ((HolonObject) obj).getMaxPossibleConsumption();
+			
+				System.out.println(obj.getName());
+				if(!(obj.getName().contains("Plant"))) {
+					result += holonObjectSupplyPenaltyFunction(suppPercentage);
+					
+				}
+					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);
 			}
+			
 		}
-		return counter;
+		
+		return result;
+	}
+	
+	/**
+	 * Calculates a penalty value based on the HOs current supply percentage
+	 * @param supplyPercentage
+	 * @return
+	 */
+	private double holonObjectSupplyPenaltyFunction(float supplyPercentage) {
+		float result = 0;
+		if(supplyPercentage == 1)
+			return result;
+		else if(supplyPercentage < 1 && supplyPercentage >= 0.25)
+			result = 100/supplyPercentage;
+		else if (supplyPercentage < 0.25)
+			result = 100/supplyPercentage * 2; //Scalar value might be problematic for large/specific examples
+		else if (supplyPercentage < 1.25) 
+			result = 100 * supplyPercentage *2 ;
+		else result = 100 * supplyPercentage * 3;
+		if(Float.isInfinite(result) || Float.isNaN(result))
+			result = 10000;
+		System.out.println("Low penalty function value: " + result + " with supply value: " + supplyPercentage);
+		return result;
 	}
+	
+	private double inactiveHolonElementPenalty(HolonObject obj) {
+		float result = 0;
+		int activeElements = obj.getNumberOfActiveElements();
+		int maxElements = obj.getElements().size();
+		
+		if(activeElements == maxElements)
+			result =0;
+		else if(activeElements == 0) {
+			result = 10000;
+		}else result = ((maxElements -activeElements)*600);
+		
+	return result;
+		
+	}
+	
+
 
 }

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

@@ -48,7 +48,7 @@ public class UpdateController {
 			if (tempCps != null && tempCps.getClass() == HolonObject.class) {
 				if (table.getRowCount() != 0) {
 					table.removeRow(2);
-					Object[] tempEnergy = { Languages.getLanguage()[73], ((HolonObject) tempCps).getCurrentEnergy() };
+					Object[] tempEnergy = { Languages.getLanguage()[73], ((HolonObject) tempCps).getMaxActiveEnergy() };
 					table.insertRow(2, tempEnergy);
 					((HolonObject) tempCps).updateTotalFlex();
 					table.removeRow(3);
@@ -297,7 +297,7 @@ public class UpdateController {
 			// consumption) is calculated
 			if (obj instanceof HolonObject) {
 				refreshTableHolonElement(model.getMultiTable(), model.getSingleTable());
-				Object[] tempEnergy = { Languages.getLanguage()[73], ((HolonObject) obj).getCurrentEnergy() };
+				Object[] tempEnergy = { Languages.getLanguage()[73], ((HolonObject) obj).getMaxActiveEnergy() };
 				Object[] tempFlex = { "Flexibility", ((HolonObject) obj).getTotalFlex() };
 				model.getPropertyTable().addRow(tempEnergy);
 				model.getPropertyTable().addRow(tempFlex);

+ 1 - 0
src/ui/view/AlgorithmMenu.java

@@ -148,6 +148,7 @@ public class AlgorithmMenu extends JMenu {
 
 			// Compile source file.
 			JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
+			System.out.println(System.getProperty("java.home"));
 
 			if (ToolProvider.getSystemJavaCompiler() == null) {
 				missingCompiler = true;