elbraulio / genetical

genetic algorithm implementation
http://genetical.elbraulio.com
MIT License
0 stars 0 forks source link
ga genetic-algorithm

License Build Status codecov codebeat badge

genetical is a java tool for using genetic algorithm.

1. Install

maven

<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>

gradle

dependencies {
        implementation 'com.elbraulio:genetical:{version}'
}
allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

2. How to use

2.1 Indiviual and genes

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);

2.2 Population

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.

2.2.1 evolve

_genetical provide a default evolve wich is FittestEvolve, wich needs a FittestSelection, Crosses and Mutation.

FittestSelection

Follows the concept of Natural Selection. Wich says that the fittest individual will survive and reproduce.

Crosses

Defines the way that two Individuals will cross.

Mutation

Make mutations to the decendant's genes after the cross.

2.3 Evolve

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 */}
                            )
                    );