Parameterization and Customization¶
RGP is a modular system build from components that are simply just R functions . RGP's data structures are based on well known R data structures to make interoperability with other R packages as painless as possible. For a streamlined user experience, RGP uses S3 classes and R attributes to provide custom printing and plotting of RGP objects. Yet under the hood, these objects are plain R data structures. For example, a RGP population is just a List of R Functions that can be manipulated with every tool in the R ecosystem.
A RGP is nothing more than a loose collection of R functions, configuration and parameterization is very flexible but also quite complex. In future revisions, RGP will provide a more user-friendly configuration interface for common tasks.
Example: Setting the Crossover Probability¶
To change the crossover probability in a GP run started with the geneticProgramming() (or any other of the provided GP run wrapper
functions), the crossover function has to be replaced with a custom version (here: myCrossover() that calls the built-in crossover function with custom parameters, as in the following pseudo-code:
myCrossover <- function(func1, func2,
breedingFitness = function(individual) TRUE,
breedingTries = 50)
crossover(func1, func2, crossoverprob = 0.33,
breedingFitness = breedingFitness,
breedingTries = breedingTries)
To enable some RGP features, we also set the class of our custom crossover function:
class(myCrossover) <- c("recombinationOperator", "function")
Note that we fix the
crossoverprob parameter to our custom value of 0.33 and pass all other parameters unchanged to RGP's crossover() function. The parameters of every RGP component function are documented and accessible via R's help system.
We can now use our custom crossover function by passing it to geneticProgramming():
geneticProgramming([other parameters], crossoverFunction = myCrossover, [...])
Example 2: Change the max depth of the individuals¶
To change the max depth of each indivdual in the initial population, the makePopulation function has to be modified:
makePopulation <- function(size, funcset, inset, conset,
maxfuncdepth = 8, constprob = 0.2,
breedingFitness = function(individual) TRUE,
breedingTries = 50,
extinctionPrevention = FALSE,
funcfactory = function() randfuncRampedHalfAndHalf(funcset, inset, conset,
maxfuncdepth, constprob = constprob,
breedingFitness = breedingFitness, breedingTries = breedingTries)) {
pop <- if (extinctionPrevention) {
resultPop <- list()
l <- 0
repeat {
candidate <- funcfactory()
if (!contains(resultPop, candidate)) {
resultPop <- c(resultPop, candidate)
l <- l + 1
}
if (l == size) break()
}
resultPop
} else {
lapply(1:size, function(i) funcfactory())
}
class(pop) <- c("untypedPopulation", "population", "list")
pop
}
The maxfuncdepth-parameter has to be altered. The trees are created with the randfuncRampedHalfAndHalf-function. This function has a fifty-fifty chance to create either a full sized three up to the given maxdepth or a random sized three.
Next Steps¶
See the RGP Documentation Workshop for detailed documentation on RGP's algorithms and components.