|
@@ -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());
|