This is a basic implementation of a Genetic Algorithm in Kotlin, that is capable to generate solutions for optimization and search problems relying on bio-inspired operations such as crossover, mutation and selection.
The following example showcases a simple usage of the algorithm. It creates a random population of genes, represented as a list with uniformly distributed zeros (disease genome) and ones (health genome) and evolves the genes towards a healthy population afterwards.
import java.lang.Math.random
fun main(args : Array<String>) {
val population = (1..100).map { (1..10).map { if (random() < 0.5) 0 else 1 } }
val algorithm = GeneticAlgorithm(
population,
score = { it.sum().toDouble() },
cross = { it.first.mapIndexed { index, i -> if (random() < 0.5) i else it.second[index] } },
mutate = { it.map { if (random() < 0.9) it else if (random() < 0.5) 0 else 1 } },
select = ::fitnessProportionateSelection
)
val result = algorithm.run()
print("Best individual: ")
result.forEach { print(it) }
}
Best individual: 1111111111
Process finished with exit code 0