Browse Source

Average and refactor

Tom Troppmann 4 years ago
parent
commit
b344fc50fc

File diff suppressed because it is too large
+ 0 - 0
Research Data/GA/ga50runs.txt


+ 0 - 0
Research Data/GA/Ga 100 Gen 100Pop Test Tournamentsize and SwapRate 0.01.txt → Research Data/GA/old/Ga 100 Gen 100Pop Test Tournamentsize and SwapRate 0.01.txt


+ 0 - 0
Research Data/GA/Ga 100 Gen 100Pop Test Tournamentsize and SwapRate.txt → Research Data/GA/old/Ga 100 Gen 100Pop Test Tournamentsize and SwapRate.txt


+ 0 - 0
Research Data/GA/old.txt → Research Data/GA/old/old.txt


+ 0 - 0
Research Data/GA/very bad.txt → Research Data/GA/old/very bad.txt


File diff suppressed because it is too large
+ 0 - 0
Research Data/PSO/pso50runs.txt


+ 10 - 16
src/api/AlgorithmFrameworkFlex.java

@@ -95,6 +95,7 @@ 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
@@ -446,7 +447,14 @@ 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;
@@ -455,24 +463,10 @@ 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;
 	}
 

+ 16 - 8
src/exampleAlgorithms/PsoAlgorithm.java

@@ -4,6 +4,7 @@ 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;
 
@@ -57,7 +58,10 @@ 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() {
@@ -102,13 +106,13 @@ public class PsoAlgorithm extends AlgorithmFrameworkFlex{
 		initDependentParameter();
 		Individual globalBest = new Individual();
 		globalBest.position = extractPositionAndAccess();
-		globalBest.fitness = evaluatePosition(globalBest.position);
+		globalBest.fitness = evaluatePosition(globalBest.position, logString(-2, -1, globalBest.position,  -1.));
 		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);
+		evaluation(globalBest, swarm, -1);
 		runList.add(globalBest.fitness);
 		for (int iteration = 0; iteration < maxIterations ; iteration++) {
 			int mutationAllowed = iteration % mutationInterval;
@@ -142,7 +146,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);
+			evaluation(globalBest, swarm, iteration);
 			runList.add(globalBest.fitness);
 			if(moreInformation) console.println("------------------------");
 		}
@@ -164,7 +168,7 @@ public class PsoAlgorithm extends AlgorithmFrameworkFlex{
 			for (int index = 0; index < j; index++){
 				aRandomPosition.add(Random.nextBoolean());
 			}
-			swarm.add(new Particle(aRandomPosition));
+			swarm.add(new Particle(aRandomPosition, particleNumber));
 		}
 		return swarm;
 	}
@@ -180,9 +184,10 @@ public class PsoAlgorithm extends AlgorithmFrameworkFlex{
 	 * @param globalBest
 	 * @param swarm
 	 */
-	private void evaluation(Individual globalBest, List<Particle> swarm) {
+	private void evaluation(Individual globalBest, List<Particle> swarm, int iteration) {
 		for(Particle p: swarm) {
-			double localEvaluationValue = evaluatePosition(p.xPhenotype);
+			double averageVelocity = p.velocity.stream().mapToDouble(a -> a).average().orElse(-1.0);
+			double localEvaluationValue = evaluatePosition(p.xPhenotype, logString(iteration, p.number, p.xPhenotype, averageVelocity));
 			if(moreInformation) console.println("Fitness " + localEvaluationValue);
 			p.checkNewEvaluationValue(localEvaluationValue);
 			if(localEvaluationValue < globalBest.fitness) {
@@ -309,6 +314,8 @@ public class PsoAlgorithm extends AlgorithmFrameworkFlex{
 	 * Class to represent a Particle.
 	 */
 	private class Particle{
+		
+		public int number;
 		/**
 		 * The velocity of a particle.
 		 */
@@ -324,7 +331,8 @@ public class PsoAlgorithm extends AlgorithmFrameworkFlex{
 		
 		public Individual localBest;
 		
-		Particle(List<Boolean> position){
+		Particle(List<Boolean> position, int number){
+			this.number = number;
 			this.xPhenotype = position;
 			//Init velocity, xGenotype with 0.0 values.
 			this.velocity = position.stream().map(bool -> 0.0).collect(Collectors.toList());

Some files were not shown because too many files changed in this diff