pGA

inline fun <V, F> pGA(population: PanmicticPopulation<V, F>, noinline fitnessFunction: (V) -> F, config: PanmicticConfigScope<V, F>.() -> Unit): PanmicticGA<V, F>

Creates PanmicticGA with Kotlin DSL.

Example for OneMax task:

pGA(
// Set a population configuration
population = population(size = 200) { booleans(size = 100) },
// Set a fitness function
fitnessFunction = { value -> value.count { it } },
) {
random = Random(seed = 42) // set pseudo random number generator
elitism = 10 // set elitism

before {
println("GA STARTED, Init population: $population")
}

// create evolution strategy
evolve {
selTournament(size = 3, parallelismLimit = NO_PARALLELISM) // select
cxOnePoint(chance = 0.8) // crossover
mutFlipBit(chance = 0.2, flipBitChance = 0.01) // mutate
evaluation() // evaluate population
stopBy(maxIteration = 50) { bestFitness == 100 } // finish GA by conditions
}

after {
println("GA FINISHED, Result = $best")
}
}.startBlocking()

Parameters

population

population of PanmicticGA

fitnessFunction

fitness function for evaluation step

config

scope function to initialize PanmicticGA

See also