|
@@ -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;
|
|
|
|
|
|
this.velocity = position.stream().map(bool -> 0.0).collect(Collectors.toList());
|