Getting Started with OakGP
This page provides a guide to getting started with the OakGP genetic programming Java framework.
For a general high-level overview of genetic programming please read the introduction to genetic programming.
Step 2: Create New RunBuilder
Step 3: Set Return Type
Step 4: Set Random Number Generator (Optional)
Step 5: Set Primitive Set
Step 6: Set Fitness Function
Step 7: Set Initial Population
Step 8: Set Evolution Strategy (Optional)
Step 9: Set Termination Criteria
Step 10: Start Run
Examples
Step 1: Download OakGP
You can include the latest release version of OakGP as a dependency of your Java project by adding the following to its Maven pom.xml
file:
<dependency>
<groupId>org.oakgp</groupId>
<artifactId>oakgp</artifactId>
<version>0.0.3</version>
</dependency>
To use the latest development version of OakGP specify the following instead:
<dependency>
<groupId>org.oakgp</groupId>
<artifactId>oakgp</artifactId>
<version>0.1.0-SNAPSHOT</version>
</dependency>
Step 2: Create New RunBuilder
Create a new instance of org.oakgp.util.RunBuilder
.
new RunBuilder()
Step 3: Set Return Type
Specify the required return type of the programs automatically generated by the process.
new RunBuilder()
.setReturnType(Type.integerType())
Step 4: Set Random Number Generator (Optional)
Genetic programming uses random variables. You can optionally specify the random number generator to use. Note: If you do not specify a random number generator then a default one (which uses java.util.Random
) will be used. It is recommended that you use the default random generator rather than setting one explicitly.
new RunBuilder()
.setReturnType(Type.integerType())
.setRandom(random)
Step 5: Set Primitive Set
Specify the constants, variables and functions that are available to the process to use in the construction of the programs it automatically generates.
new RunBuilder()
.setReturnType(Type.integerType())
.setConstants(constants)
.setVariables(variableTypes)
.setFunctions(functions)
Note: As an alternative to using setConstants
/setVariables
/setFunctions
you can instead use setPrimitiveSet
to specify the primitive set.
Step 6: Set Fitness Function
Specify the fitness function to use to determine the suitability of automatically generated programs for solving the problem being investigated.
new RunBuilder()
.setReturnType(Type.integerType())
.setConstants(constants)
.setVariables(variableTypes)
.setFunctions(functions)
.setFitnessFunction(fitnessFunction)
Note: When using genetic programming to find a solution to a two-player game then setTwoPlayerGame(TwoPlayerGame)
is a better alternative to setFitnessFunction(FitnessFunction)
. Please see Grid War for an example of this approach.
Step 7: Set Initial Population
Set the initial population that will be used as a basis for evolving future generations. This can be done by specifying the the population size and max tree depth of the initial population which will then be randomly generated using the previously configured constant, variable and function sets.
new RunBuilder()
.setReturnType(Type.integerType())
.setConstants(constants)
.setVariables(variableTypes)
.setFunctions(functions)
.setFitnessFunction(fitnessFunction)
.setInitialPopulationSize(initialPopulationSize)
.setTreeDepth(initialPopulationMaxDepth)
Note: As an alternative to using setInitialPopulationSize
/setTreeDepth
you can instead use setInitialPopulation
to explicitly specify the contents of the initial population.
Step 8: Set Evolution Strategy (Optional)
You can optionally specify how generations evolve - e.g. selection strategies, genetic operators and elitism. Note: If you do not specify how generations evolve then a default strategy will be used. The default strategy is sufficient for allowing people to quickly get started with OakGP.
new RunBuilder()
.setReturnType(Type.integerType())
.setConstants(constants)
.setVariables(variableTypes)
.setFunctions(functions)
.setFitnessFunction(fitnessFunction)
.setInitialPopulationSize(initialPopulationSize)
.setTreeDepth(initialPopulationMaxDepth)
.setGenerationEvolver(generationEvolverCreator)
Step 9: Set Termination Criteria
Set the termination condition(s) for the process. The following options are available to set the termination criteria:
setTargetFitness(double)
- will stop the process once a solution has been generated that has the specified fitness value.setMaxGenerations(int)
- will stop the process once the specified number of generations have been processed.setMaxGenerationsWithoutImprovement(int)
- will stop the process once the specified number of consecutive generations have been processed without an improvement in the fitness of the solutions being generated.setTerminator(final Predicate<List<RankedCandidate>>)
- allows a user-defined termination criteria to be specified.- ...or any combination of the above.
new RunBuilder()
.setReturnType(Type.integerType())
.setConstants(constants)
.setVariables(variableTypes)
.setFunctions(functions)
.setFitnessFunction(fitnessFunction)
.setInitialPopulationSize(initialPopulationSize)
.setTreeDepth(initialPopulationMaxDepth)
.setMaxGenerations(maxGenerations)
Step 10: Start Run
Once the required configuration has been specified then the process can be started by calling the process()
method. The result returned from the process()
method will be a org.oakgp.rank.RankedCandidates
containing the candidates of the final generation before the process terminated. From the RankedCandidates
you can access the org.oakgp.node.Node
that represents the best program that was automatically generated by the process.
RankedCandidates output = new RunBuilder()
.setReturnType(Type.integerType())
.setConstants(constants)
.setVariables(variableTypes)
.setFunctions(functions)
.setFitnessFunction(fitnessFunction)
.setInitialPopulationSize(initialPopulationSize)
.setTreeDepth(initialPopulationMaxDepth)
.setMaxGenerations(maxGenerations)
.process();
Node best = output.best().getNode();