Skip to content

Instantly share code, notes, and snippets.

@jfinkels
Created January 14, 2010 04:31
Show Gist options
  • Save jfinkels/276863 to your computer and use it in GitHub Desktop.
Save jfinkels/276863 to your computer and use it in GitHub Desktop.
Factory<Byte> bitFactory = new BitFactory();
int length = 20;
PartialDeepCopyableListFactory<Byte> individualFactory = new PartialDeepCopyableListFactory<Byte>();
individualFactory.setElementFactory(bitFactory);
individualFactory.setSize(length);
int numberOfIndividuals = 100;
DefaultListFactory<DeepCopyableList<Byte>> populationFactory = new DefaultListFactory<DeepCopyableList<Byte>>();
populationFactory.setSize(numberOfIndividuals);
populationFactory.setElementFactory(individualFactory);
List<DeepCopyableList<Byte>> population = null;
try {
population = populationFactory.createObject();
} catch (InitializationException exception) {
// handle the exception
}
GeneticEvolutionContext<DeepCopyableList<Byte>> context = new GAEvolutionContext<DeepCopyableList<Byte>>(population);
context.setCrossoverFunction(new TwoPointCrossoverFunction<Byte>());
context.setFitnessFunction(new OnesFitnessFunction(length));
context.setMutationFunction(new OnesMutationFunction());
context.setSelectionFunction(new FitnessProportionateSelection<DeepCopyableList<Byte>>());
int maxGenerations = 100;
MaxGenerationCompletionCondition<DeepCopyableList<Byte>> condition = new MaxGenerationCompletionCondition<DeepCopyableList<Byte>>();
condition.setMaxGenerations(maxGenerations);
try {
while (!condition.execute(context)) {
context.stepGeneration();
System.out.println("Generation " + context.currentGeneration() + ": " + context.currentPopulation());
}
} catch (EvolutionException exception) {
// handle the exception
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment