Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion ecj/src/main/java/ec/pso/PSOBreeder.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ public class PSOBreeder extends Breeder
public double informantCoeff = 0.5 ; // coefficient for informants/neighbours
public double globalCoeff = 0.5 ; // coefficient for global best, this is not done in the standard PSO
public int neighborhoodSize = 3 ;
public boolean includeSelf = false;
public boolean includeSelf = false;
public boolean usePersonalBest = false; // use fitness or personal best to calculate the neighborhood best

public double[][] globalBest = null ; // one for each subpopulation
public Fitness[] globalBestFitness = null;
Expand Down Expand Up @@ -149,6 +150,7 @@ public Population breedPopulation(EvolutionState state)
// update global best, neighborhood best, and personal best
for(int subpop = 0; subpop < state.population.subpops.size(); subpop++)
{
// update global best and personal best
for(int ind = 0; ind < state.population.subpops.get(subpop).individuals.size() ; ind++)
{
if (globalBestFitness[subpop] == null ||
Expand All @@ -159,6 +161,11 @@ public Population breedPopulation(EvolutionState state)
}
((Particle) state.population.subpops.get(subpop).individuals.get(ind)).update(state, subpop, ind, 0);
}

// update neighborhood best
for(int ind = 0; ind < state.population.subpops.get(subpop).individuals.size() ; ind++)
((Particle) state.population.subpops.get(subpop).individuals.get(ind)).updateNeighborhood(state, subpop, ind, 0, usePersonalBest);

// clone global best
globalBest[subpop] = (double[])(globalBest[subpop].clone());
globalBestFitness[subpop] = (Fitness)(globalBestFitness[subpop].clone());
Expand Down
44 changes: 34 additions & 10 deletions ecj/src/main/java/ec/pso/Particle.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ public void update(final EvolutionState state, int subpop, int myindex, int thre
personalBestFitness = (Fitness)(fitness.clone());
personalBestGenome = (double[])(genome.clone());
}

}

public void updateNeighborhood(final EvolutionState state, int subpop, int myindex, int thread, boolean usePersonalBest)
{
// initialize neighborhood if it's not been created yet
PSOBreeder psob = (PSOBreeder)(state.breeder);
if (neighborhood == null || psob.neighborhood == psob.C_NEIGHBORHOOD_RANDOM_EACH_TIME)
Expand All @@ -180,16 +183,35 @@ else if (psob.neighborhood == psob.C_NEIGHBORHOOD_TOROIDAL || psob.neighborhood
}

// identify neighborhood best
neighborhoodBestFitness = fitness; // initially me
neighborhoodBestGenome = genome;
for(int i = 0 ; i < neighborhood.length ; i++)
if (!usePersonalBest)
{
int ind = neighborhood[i] ;
if (state.population.subpops.get(subpop).individuals.get(ind).fitness.betterThan(fitness))
// using the current fitness
neighborhoodBestFitness = state.population.subpops.get(subpop).individuals.get(neighborhood[0]).fitness; // initially the first neighbor
neighborhoodBestGenome = ((DoubleVectorIndividual)(state.population.subpops.get(subpop).individuals.get(neighborhood[0]))).genome;
for(int i = 1 ; i < neighborhood.length ; i++)
{
neighborhoodBestFitness = state.population.subpops.get(subpop).individuals.get(ind).fitness;
neighborhoodBestGenome = ((DoubleVectorIndividual)(state.population.subpops.get(subpop).individuals.get(ind))).genome;
int ind = neighborhood[i] ;
if (state.population.subpops.get(subpop).individuals.get(ind).fitness.betterThan(neighborhoodBestFitness))
{
neighborhoodBestFitness = state.population.subpops.get(subpop).individuals.get(ind).fitness;
neighborhoodBestGenome = ((DoubleVectorIndividual)(state.population.subpops.get(subpop).individuals.get(ind))).genome;
}
}
}
else
{
// using the personal best
neighborhoodBestFitness = ((Particle) state.population.subpops.get(subpop).individuals.get(neighborhood[0])).personalBestFitness; // initially the first neighbor
neighborhoodBestGenome = ((Particle) state.population.subpops.get(subpop).individuals.get(neighborhood[0])).personalBestGenome;
for(int i = 1 ; i < neighborhood.length ; i++)
{
int ind = neighborhood[i] ;
if (((Particle) state.population.subpops.get(subpop).individuals.get(ind)).personalBestFitness.betterThan(neighborhoodBestFitness))
{
neighborhoodBestFitness = ((Particle) state.population.subpops.get(subpop).individuals.get(ind)).personalBestFitness;
neighborhoodBestGenome = ((Particle) state.population.subpops.get(subpop).individuals.get(ind)).personalBestGenome;
}
}
}

// clone neighborhood best
Expand Down Expand Up @@ -239,11 +261,13 @@ int[] createRandomPattern(int myIndex, boolean includeSelf, int popsize, int nei
{
neighbors = new int[neighborhoodSize + 1];
neighbors[neighborhoodSize] = myIndex; // put me at the top
already.add(Integer.valueOf(myIndex));
}
else
neighbors = new int[neighborhoodSize];

// exclude me from the random selection
already.add(Integer.valueOf(myIndex));

Integer n = null;
for(int i = 0; i < neighborhoodSize; i++)
{
Expand Down Expand Up @@ -278,7 +302,7 @@ int[] createToroidalPattern(int myindex, boolean includeSelf, int popsize, int n
neighbors[pos++] = ((i % popsize) + popsize) % popsize;
}

for(int i = myindex + 1; i < neighborhoodSize - (neighborhoodSize / 2) + 1; i++)
for(int i = myindex + 1; i < myindex + (neighborhoodSize / 2) + 1 + neighborhoodSize%2; i++)
{
neighbors[pos++] = ((i % popsize) + popsize) % popsize;
}
Expand Down