Created
January 14, 2010 04:31
-
-
Save jfinkels/276863 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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