Created
March 12, 2016 21:00
-
-
Save jenetics/43f306608f3b342e882f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static java.lang.Math.PI; | |
import static org.jenetics.engine.EvolutionResult.toBestPhenotype; | |
import java.util.Set; | |
import java.util.function.Predicate; | |
import java.util.stream.Collectors; | |
import org.jenetics.DoubleGene; | |
import org.jenetics.Genotype; | |
import org.jenetics.Phenotype; | |
import org.jenetics.engine.Engine; | |
import org.jenetics.engine.EvolutionResult; | |
import org.jenetics.engine.codecs; | |
import org.jenetics.util.DoubleRange; | |
final class SamePopulationTermination | |
implements Predicate<EvolutionResult<?, ?>> | |
{ | |
private final double _threshold; | |
private SamePopulationTermination(final double threshold) { | |
_threshold = threshold; | |
} | |
@Override | |
public boolean test(final EvolutionResult<?, ?> result) { | |
final Set<Genotype<?>> genotypes = result.getPopulation().stream() | |
.map(Phenotype::getGenotype) | |
.collect(Collectors.toSet()); | |
final double sameElementRate = | |
(double)(result.getPopulation().size() - genotypes.size())/ | |
(double)result.getPopulation().size(); | |
return sameElementRate > _threshold; | |
} | |
public static void main(final String[] args) { | |
final Engine<DoubleGene, Double> engine = Engine | |
.builder( | |
x -> x[0] + x[1], // Fitness function | |
codecs.ofVector(DoubleRange.of(0.0, 2.0*PI), 2)) | |
.build(); | |
final Phenotype<DoubleGene, Double> best = engine.stream() | |
.limit(new SamePopulationTermination(0.6)) | |
.collect(toBestPhenotype()); | |
System.out.println(best); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment