
fr.vergne.optimization.population.impl.NBestManager Maven / Gradle / Ivy
The newest version!
package fr.vergne.optimization.population.impl;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;
import fr.vergne.optimization.population.Evaluator;
import fr.vergne.optimization.population.PopulationManager;
/**
* This {@link PopulationManager} keeps the N best {@link Individual}s provided.
* The {@link Individual}s are compared by using the given {@link Evaluator},
* where a lower value is considered as better. If there is several worst
* {@link Individual}s, the removed one is chosen arbitrarily.
*
* @author Matthieu Vergne
*
* @param
*/
public class NBestManager implements PopulationManager {
private final TreeSet population;
private int sizeLimit;
/**
*
* @param evaluator
* the {@link Evaluator} to use to compare {@link Individual}s
* @param n
* the number of {@link Individual} to keep in the population
*/
public > NBestManager(
final Evaluator evaluator, int n) {
this.sizeLimit = n;
population = new TreeSet(new Comparator() {
@Override
public int compare(Individual i1, Individual i2) {
return evaluator.evaluate(i1).compareTo(evaluator.evaluate(i2));
}
});
}
@Override
public void push(Individual individual) {
population.add(individual);
while (population.size() > sizeLimit) {
population.pollLast();
}
}
@Override
public Collection getPopulation() {
return population;
}
@Override
public Iterator getBest() {
return population.iterator();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy