c GA
inline fun <V, F> cGA(population: CellularPopulation<V, F>, noinline fitnessFunction: (V) -> F, config: CellularConfigScope<V, F>.() -> Unit): CellularGA<V, F>
Creates CellularGA with Kotlin DSL.
Example for OneMax task:
cGA(
// Set a population configuration
// population size = 6 * 6 * 6 = 216 (cube)
population = population(dimens = Dimens.cube(length = 6)) {
booleans(size = 100)
},
// Set a fitness function
fitnessFunction = { value -> value.count { it } },
{
random = Random(seed = 42) // set pseudo random number generator
elitism = true // set elitism
cellularType = CellularType.Synchronous // set cellular type
neighborhood = Moore(radius = 1) // set cellular neighborhood
before {
println("GA STARTED, Init population: $population")
}
evolve {
// Start to evolve all cells of cellular population with their neighborhoods
// This operator perform N evolutionary strategies for each cell, where N - cells count
evolveCells {
selTournament(size = 3) // select neighbor as second parent
cxOnePoint(chance = 0.8) // crossover
mutFlipBit(chance = 0.2, flipBitChance = 0.01) // mutate child
evaluation() // evaluate child
}
stopBy(maxIteration = 50) { bestFitness == 100 } // finish GA by conditions
}
after {
println("GA FINISHED, Result = $best")
}
}.startBlocking()
Content copied to clipboard
Parameters
population
population of CellularGA
fitness Function
fitness function for evaluation step
config
scope function to initialize CellularGA