genetical is a java tool for using genetic algorithm.
<dependencies>
...
<dependency>
<groupId>com.elbraulio</groupId>
<artifactId>genetical</artifactId>
<version>{version}</version>
</dependency>
</dependencies>
<!-- elbraulio.com tools -->
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
dependencies {
implementation 'com.elbraulio:genetical:{version}'
}
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
You can define whatever you want as gene, then you will have a Individual who has those genes. genetical provides a DefaultIndividual
who only stores a list of genes and give them when someone requieres it.
For instance, we can have a genes as String
List<String> genes = buildGenes();
Individual<String> stringIndividual = new DefaultIndividual(genes);
A population is defined as a collection of Individuals that can cross each others. So, for instance we can create a Population of Individuals with genes as String
Population<String> startPop = new DefaultPopulation<>(stringIndividual);
and we can evolve it to create a new Population (generation) of Individuals by defining a way to evolve.
_genetical provide a default evolve wich is FittestEvolve, wich needs a FittestSelection, Crosses and Mutation.
Follows the concept of Natural Selection. Wich says that the fittest individual will survive and reproduce.
Defines the way that two Individuals will cross.
Make mutations to the decendant's genes after the cross.
You have to use evolve()
method and you will get a new Population which is a new generation of Individuals.
Population<String> nextGeneration = startPop.evolve(
new FittestEvolve<>(
(individuals) -> { return /* fittest */},
(genesA, genesB) -> { return /* crossed genes */},
(genes) -> { return /* mutated genes */}
)
);