Bladeren bron

ObjectiveFunction and ParameterStepping update

tk 5 jaren geleden
bovenliggende
commit
4922c2cda9

+ 18 - 4
src/api/AlgorithmFrameworkFlex.java

@@ -365,10 +365,12 @@ public abstract class AlgorithmFrameworkFlex implements AddOn{
 		stepsSizeLabel.setEnabled(false);
 		singleParameterPanel.add(stepsSizeLabel);
 		
-		JFormattedTextField stepsSizeTextField = new  JFormattedTextField(doubleFormatter);
+		NumberFormatter doubleFormatterForStepping = new NumberFormatter(doubleFormat);
+		doubleFormatterForStepping.setCommitsOnValidEdit(true);
+		JFormattedTextField stepsSizeTextField = new  JFormattedTextField(doubleFormatterForStepping);
 		stepsSizeTextField.setEnabled(false);
 		stepsSizeTextField.setValue(1.0);
-		stepsSizeTextField.setToolTipText("Only double \u2208 [" + minValue + "," + maxValue + "]");
+		stepsSizeTextField.setToolTipText("Only double");
 		stepsSizeTextField.addPropertyChangeListener(actionEvent -> doubleParameterStepping.stepSize = Double.parseDouble(stepsSizeTextField.getValue().toString()));
 		stepsSizeTextField.setMaximumSize(new Dimension(40, 30));
 		stepsSizeTextField.setPreferredSize(new Dimension(40, 30));
@@ -482,6 +484,7 @@ public abstract class AlgorithmFrameworkFlex implements AddOn{
 		runPrinter.openStream();
 		runPrinter.println("");
 		runPrinter.println("Start:" + stringStatFromActualState());
+		runPrinter.closeStream();
 		if(this.useStepping) {
 			initParameterStepping();
 			do {
@@ -496,7 +499,6 @@ public abstract class AlgorithmFrameworkFlex implements AddOn{
 		updateVisual();
 		runProgressbar.finishedCancel();
 		control.guiDisable(false);
-		runPrinter.closeStream();
 	}
 	@SuppressWarnings("rawtypes")
 	private void initParameterStepping() {
@@ -545,8 +547,11 @@ public abstract class AlgorithmFrameworkFlex implements AddOn{
 		double startFitness = evaluatePosition(extractPositionAndAccess());
 		console.println("BitLength: " + access.size());
 		resetChain.removeLast();
+		runPrinter.openStream();
 		runPrinter.println("");
 		runPrinter.println(algoInformationToPrint());
+		console.println(algoInformationToPrint());
+		runPrinter.closeStream();
 		runProgressbar.start();
 		Individual runBest = new Individual();
 		runBest.fitness = Double.MAX_VALUE;
@@ -556,9 +561,11 @@ public abstract class AlgorithmFrameworkFlex implements AddOn{
 			startTimer();	
 			Individual  roundBest = executeAlgo();
 			long executionTime = printElapsedTime();
+			runPrinter.openStream();
 			runPrinter.println(runList.stream().map(Object::toString).collect(Collectors.joining(", ")));
 			runPrinter.println(stringStatFromActualState());
 			runPrinter.println("Result: " + roundBest.fitness + " ExecutionTime:" + executionTime);
+			runPrinter.closeStream();
 			if(cancel)return;
 			resetState();
 			if(roundBest.fitness < runBest.fitness) runBest = roundBest;
@@ -1016,7 +1023,14 @@ public abstract class AlgorithmFrameworkFlex implements AddOn{
 		 * @return
 		 */
 		public static int nextIntegerInRange(int min, int max) {
-			return min + random.nextInt(max - min);
+			int result = min;
+			try {
+				result = min + random.nextInt(max - min);
+			}catch(java.lang.IllegalArgumentException e){
+				System.err.println("min : " + min + " max : " + max);
+				System.err.println("max should be more then min");
+			}
+			return result;
 		}
 	}
 	

+ 28 - 10
src/exampleAlgorithms/ObjectiveFunctionByCarlos.java

@@ -3,6 +3,7 @@ package exampleAlgorithms;
 import ui.model.DecoratedNetwork;
 import ui.model.DecoratedState;
 import java.lang.Exception;
+import java.util.Locale;
 
 import classes.Flexibility;
 import classes.HolonElement.Priority;
@@ -82,12 +83,13 @@ public class ObjectiveFunctionByCarlos {
 		double f_eb = 0;
 		//sum over all objects
 		for(DecoratedNetwork net : state.getNetworkList()) {
-			f_eb += net.getConsumerList().stream().map(con -> con.getEnergySelfSupplied() - con.getEnergyFromConsumingElemnets()).reduce(0.f, Float::sum);
-			f_eb += net.getConsumerSelfSuppliedList().stream().map(con -> con.getEnergySelfSupplied() - con.getEnergyFromConsumingElemnets()).reduce(0.f, Float::sum);
-			f_eb += net.getSupplierList().stream().map(sup -> sup.getEnergyProducing() - sup.getEnergySelfConsuming()).reduce(0.f, Float::sum);
+			double netEnergyDifference = 0;
+			netEnergyDifference += net.getConsumerList().stream().map(con -> con.getEnergySelfSupplied() - con.getEnergyFromConsumingElemnets()).reduce(0.f, Float::sum);
+			netEnergyDifference += net.getConsumerSelfSuppliedList().stream().map(con -> con.getEnergySelfSupplied() - con.getEnergyFromConsumingElemnets()).reduce(0.f, Float::sum);
+			netEnergyDifference += net.getSupplierList().stream().map(sup -> sup.getEnergyProducing() - sup.getEnergySelfConsuming()).reduce(0.f, Float::sum);
+			//abs
+			f_eb += Math.abs(netEnergyDifference);
 		}
-		//abs
-		f_eb = Math.abs(f_eb);
 		
 		//Calculate f_state the penalty function for the supply state
 		double f_state = 0;
@@ -122,11 +124,20 @@ public class ObjectiveFunctionByCarlos {
 			
 			double con = net.getTotalConsumption();
 			double prod = net.getTotalProduction();
-			double f_change_positive = (con > 0.0)? net.getFlexibilityProductionCapacity() / con : 0.0;
-			double f_change_negativ = (prod > 0.0)? net.getFlexibilityConsumptionCapacity() / prod: 0.0;
-			
-			f_holon += f_elements_diviation_production + f_elements_diviation_consumption 
+			double flexcapProd = net.getFlexibilityProductionCapacity();
+			double flexcapCon = net.getFlexibilityConsumptionCapacity();
+			double f_change_positive = (con > 0.0)?  flexcapProd / con : 1.0;
+			double f_change_negativ = (prod > 0.0)? flexcapCon / prod: 1.0;
+			double sum = f_elements_diviation_production + f_elements_diviation_consumption 
 					+ f_flexibility_diviation_consumption + f_flexibility_diviation_production + f_change_positive +f_change_negativ;
+			f_holon += sum;
+//			System.out.print( "f+elements=" + doubleToString(f_elements_diviation_production));
+//			System.out.print( " f-elements=" + doubleToString(f_elements_diviation_consumption));
+//			System.out.print( " f+flexibility" + doubleToString(f_flexibility_diviation_consumption));
+//			System.out.print( " f-flexibility" + doubleToString(f_flexibility_diviation_production));
+//			System.out.print( " f+change(" +  doubleToString(flexcapProd) + "/" + doubleToString(con) + ")=" + doubleToString(f_change_positive));
+//			System.out.print( " f-change(" +  doubleToString(flexcapCon) + "/" + doubleToString(prod) + ")="+ doubleToString(f_change_negativ));
+//			System.out.println( " sum=" + doubleToString(sum));
 		}
 		
 		
@@ -142,7 +153,14 @@ public class ObjectiveFunctionByCarlos {
 		return (float) (w_eb * squash(f_eb, k_eb) + w_state * squash(f_state, k_state) + w_pro * squash(f_pro, k_pro) + w_perf * squash(f_perf, k_perf) + w5 * squash(f_holon, k_holon));
 	}
 	
-
+	private static String doubleToString(double value) {
+		return String.format (Locale.US, "%.2f", value);
+	}
+	
+	
+	
+	
+	
 	/**
 	 * The squashing function in paper
 	 * @param x the input

+ 1 - 1
src/ui/model/DecoratedNetwork.java

@@ -397,7 +397,7 @@ public class DecoratedNetwork {
 		return getListOfEnergyThatIsOfferedByFlexibilitiesInThisNetwork().stream().filter(value -> (value > 0.f)).collect(Collectors.toList());
 	}
 	public List<Float> getListOfEnergyInConsumptionThatIsOfferedByFlexibilitiesInThisNetwork(){
-		return getListOfEnergyThatIsOfferedByFlexibilitiesInThisNetwork().stream().filter(value -> (value < 0.f)).collect(Collectors.toList());
+		return getListOfEnergyThatIsOfferedByFlexibilitiesInThisNetwork().stream().filter(value -> (value < 0.f)).map(value -> -value).collect(Collectors.toList());
 	}
 	
 	public float getFlexibilityProductionCapacity() {