Browse Source

Resets false changes

Tom Troppmann 4 years ago
parent
commit
854005d756
2 changed files with 24 additions and 26 deletions
  1. 16 10
      src/api/AlgorithmFrameworkFlex.java
  2. 8 16
      src/exampleAlgorithms/PsoAlgorithm.java

+ 16 - 10
src/api/AlgorithmFrameworkFlex.java

@@ -95,7 +95,6 @@ public abstract class AlgorithmFrameworkFlex implements AddOn{
 	
 	//printing
 	private Printer runPrinter = new Printer(plottFileName());
-	private Printer logPrinter = new Printer("logFile.txt");
 	protected List<Double> runList = new LinkedList<Double>();
 
 	//Parameter
@@ -447,14 +446,7 @@ public abstract class AlgorithmFrameworkFlex implements AddOn{
 			dGroupNode = selected.object;
 		}
 	}
-	protected double evaluatePosition(List<Boolean> positionToEvaluate, String logString) {
-		double objectiveFunction = evaluatePosition(positionToEvaluate);
-		String toPrint = "oF: " + objectiveFunction + " " + logString;
-		logPrinter.openStream();
-		logPrinter.println(toPrint);
-		logPrinter.closeStream();
-		return objectiveFunction;
-	}
+	
 	protected double evaluatePosition(List<Boolean> positionToEvaluate) {
 		runProgressbar.step();
 //		long startTime = System.currentTimeMillis(), endTime;
@@ -463,10 +455,24 @@ public abstract class AlgorithmFrameworkFlex implements AddOn{
 //		console.print(" a:" + (endTime - startTime)); 
 //		startTime = endTime;
 		setState(positionToEvaluate); // execution time critical
-
+//		endTime = System.currentTimeMillis();
+//		console.print(" b:" + (endTime - startTime)); 
+//		startTime = endTime;
 		control.calculateStateOnlyForCurrentTimeStep();
+//		endTime = System.currentTimeMillis();
+//		console.print(" c:" + (endTime - startTime)); 
+//		startTime = endTime;
 		DecoratedState actualstate = control.getSimManager().getActualDecorState();
+//		endTime = System.currentTimeMillis();
+//		console.print(" d:" + (endTime - startTime)); 
+//		startTime = endTime;
 		double result = evaluateState(actualstate);
+//		endTime = System.currentTimeMillis();
+//		console.print(" e:" + (endTime - startTime)); 
+//		startTime = endTime;
+//		long inUse = actualstate.getFlexManager().getAllFlexWrapperWithState(FlexState.IN_USE).size();
+//		long all = actualstate.getFlexManager().getAllFlexWrapper().size();
+//		console.println(" [" + inUse + "/" + all + "]");
 		return result;
 	}
 

+ 8 - 16
src/exampleAlgorithms/PsoAlgorithm.java

@@ -4,7 +4,6 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.TreeSet;
 import java.util.stream.Collectors;
-import java.util.stream.DoubleStream;
 
 import javax.swing.JFrame;
 
@@ -58,10 +57,7 @@ public class PsoAlgorithm extends AlgorithmFrameworkFlex{
 	protected double evaluateState(DecoratedState actualstate) {
 		return ObjectiveFunctionByCarlos.getFitnessValueForState(actualstate);
 	}
-	String logString(int iteration, int particleNumber, List<Boolean> boolList, double velocity) {
-		String boolString = boolList.stream().map(Bool -> ((Bool)?"1":"0")).collect(Collectors.joining(""));
-		return "i:" + iteration + " pN:" + particleNumber + " b:" + boolString + " v:" + velocity;
-	}
+
 
 	@Override
 	protected int getProgressBarMaxCount() {
@@ -106,13 +102,13 @@ public class PsoAlgorithm extends AlgorithmFrameworkFlex{
 		initDependentParameter();
 		Individual globalBest = new Individual();
 		globalBest.position = extractPositionAndAccess();
-		globalBest.fitness = evaluatePosition(globalBest.position, logString(-2, -1, globalBest.position,  -1.));
+		globalBest.fitness = evaluatePosition(globalBest.position);
 		console.println("Start Value:" + globalBest.fitness);
 		int dimensions = globalBest.position.size();
 		List<Particle> swarm= initializeParticles(dimensions);
 		List<Double> runList = new ArrayList<Double>();
 		runList.add(globalBest.fitness);
-		evaluation(globalBest, swarm, -1);
+		evaluation(globalBest, swarm);
 		runList.add(globalBest.fitness);
 		for (int iteration = 0; iteration < maxIterations ; iteration++) {
 			int mutationAllowed = iteration % mutationInterval;
@@ -146,7 +142,7 @@ public class PsoAlgorithm extends AlgorithmFrameworkFlex{
 			}
 			if(moreInformation) console.println("\t\t\t\t\t\tAvgBitsMutate: " + (bitsFlipped / (double)swarmSize));
 			if(cancel)return null;
-			evaluation(globalBest, swarm, iteration);
+			evaluation(globalBest, swarm);
 			runList.add(globalBest.fitness);
 			if(moreInformation) console.println("------------------------");
 		}
@@ -168,7 +164,7 @@ public class PsoAlgorithm extends AlgorithmFrameworkFlex{
 			for (int index = 0; index < j; index++){
 				aRandomPosition.add(Random.nextBoolean());
 			}
-			swarm.add(new Particle(aRandomPosition, particleNumber));
+			swarm.add(new Particle(aRandomPosition));
 		}
 		return swarm;
 	}
@@ -184,10 +180,9 @@ public class PsoAlgorithm extends AlgorithmFrameworkFlex{
 	 * @param globalBest
 	 * @param swarm
 	 */
-	private void evaluation(Individual globalBest, List<Particle> swarm, int iteration) {
+	private void evaluation(Individual globalBest, List<Particle> swarm) {
 		for(Particle p: swarm) {
-			double averageVelocity = p.velocity.stream().mapToDouble(a -> a).average().orElse(-1.0);
-			double localEvaluationValue = evaluatePosition(p.xPhenotype, logString(iteration, p.number, p.xPhenotype, averageVelocity));
+			double localEvaluationValue = evaluatePosition(p.xPhenotype);
 			if(moreInformation) console.println("Fitness " + localEvaluationValue);
 			p.checkNewEvaluationValue(localEvaluationValue);
 			if(localEvaluationValue < globalBest.fitness) {
@@ -314,8 +309,6 @@ public class PsoAlgorithm extends AlgorithmFrameworkFlex{
 	 * Class to represent a Particle.
 	 */
 	private class Particle{
-		
-		public int number;
 		/**
 		 * The velocity of a particle.
 		 */
@@ -331,8 +324,7 @@ public class PsoAlgorithm extends AlgorithmFrameworkFlex{
 		
 		public Individual localBest;
 		
-		Particle(List<Boolean> position, int number){
-			this.number = number;
+		Particle(List<Boolean> position){
 			this.xPhenotype = position;
 			//Init velocity, xGenotype with 0.0 values.
 			this.velocity = position.stream().map(bool -> 0.0).collect(Collectors.toList());