Package io.jenetics
Jenetics is an Genetic Algorithm, respectively an Evolutionary Algorithm, library written in Java. It is designed with a clear separation of the several concepts of the algorithm, e. g. Gene, Chromosome, Genotype, Phenotype, Population and fitness Function. Jenetics allows you to minimize and maximize the given fitness function without tweaking it. In contrast to other GA implementations, the library uses the concept of an evolution stream (EvolutionStream) for executing the evolution steps. Since the EvolutionStream implements the Java Stream interface, it works smoothly with the rest of the Java streaming API.
Getting StartedThe minimum evolution Engine setup needs a genotype factory, Factory<Genotype<?>>, and a fitness Function. The Genotype implements the Factory interface and can therefore be used as prototype for creating the initial Population and for creating new random Genotypes.
import io.jenetics.BitChromosome;
import io.jenetics.BitGene;
import io.jenetics.Genotype;
import io.jenetics.engine.Engine;
import io.jenetics.engine.EvolutionResult;
import io.jenetics.util.Factory;
public class HelloWorld {
// 2.) Definition of the fitness function.
private static Integer eval(Genotype<BitGene> gt) {
return ((BitChromosome)gt.chromosome()).bitCount();
}
public static void main(String[] args) {
// 1.) Define the genotype (factory) suitable
// for the problem.
Factory<Genotype<BitGene>> gtf =
Genotype.of(BitChromosome.of(10, 0.5));
// 3.) Create the execution environment.
Engine<BitGene, Integer> engine = Engine
.builder(HelloWorld::eval, gtf)
.build();
// 4.) Start the execution (evolution) and
// collect the result.
Genotype<BitGene> result = engine.stream()
.limit(100)
.collect(EvolutionResult.toBestGenotype());
System.out.println("Hello World:\n" + result);
}
}
In contrast to other GA implementations, the library uses the concept of an evolution stream (EvolutionStream) for executing the evolution steps. Since the EvolutionStream implements the Java Stream interface, it works smoothly with the rest of the Java streaming API. Now let's have a closer look at listing above and discuss this simple program step by step:
- The probably most challenging part, when setting up a new evolution Engine, is to transform the problem domain into a appropriate Genotype (factory) representation. In our example we want to count the number of ones of a BitChromosome. Since we are counting only the ones of one chromosome, we are adding only one BitChromosome to our Genotype. In general, the Genotype can be created with 1 to n chromosomes.
- Once this is done, the fitness function which should be maximized, can be defined. Utilizing the new language features introduced in Java 8, we simply write a private static method, which takes the genotype we defined and calculate it's fitness value. If we want to use the optimized bit-counting method, bitCount(), we have to cast the Chromosome<BitGene> class to the actual used BitChromosome class. Since we know for sure that we created the Genotype with a BitChromosome, this can be done safely. A reference to the eval method is then used as fitness function and passed to the Engine.build method.
- In the third step we are creating the evolution Engine, which is responsible for changing, respectively evolving, a given population. The Engine is highly configurable and takes parameters for controlling the evolutionary and the computational environment. For changing the evolutionary behavior, you can set different alterers and selectors. By changing the used Executor service, you control the number of threads, the Engine is allowed to use. An new Engine instance can only be created via its builder, which is created by calling the Engine.builder method.
- In the last step, we can create a new EvolutionStream from our Engine. The EvolutionStream is the model or view of the evolutionary process. It serves as a »process handle« and also allows you, among other things, to control the termination of the evolution. In our example, we simply truncate the stream after 100 generations. If you don't limit the stream, the EvolutionStream will not terminate and run forever. Since the EvolutionStream extends the java.util.stream.Stream interface, it integrates smoothly with the rest of the Java streaming API. The final result, the best Genotype in our example, is then collected with one of the predefined collectors of the EvolutionResult class.
- Since:
- 1.0
- Version:
- 3.1
-
ClassDescriptionAbstract implementation of the alterer interface.The abstract base implementation of the Chromosome interface.The Alterer is responsible for the changing/recombining the Population.Represents the result pair of a
Alterer.alter(Seq, long)
call, which consists of the altered population and the number of altered individuals.Chromosome
implementation, which allows creating genes without explicit implementing theChromosome
interface.AnyGene<A>Gene
implementation, which allows creating genes without explicit implementing theGene
interface.Implementation of the classical BitChromosome.Implementation of a BitGene.In thisSelector
, the probability for selection is defined as.Chromosome interface forBoundedGene
s.Base interface for genes where the alleles are bound by a minimum and a maximum value.Character chromosome which represents character sequences.Character gene implementation.A chromosome consists of one or more genes.Alters a chromosome by replacing two genes by the result of a given combiner function.Performs a Crossover of twoChromosome
.Numeric chromosome implementation which holds 64-bit floating point numbers.Implementation of the NumericGene which holds a 64-bit floating point number.TheEliteSelector
copies a small proportion of the fittest candidates, without changes, into the next generation.EnumGene<A>Gene which holds enumerable (countable) genes.An alternative to the "weak"LinearRankSelector
is to assign survival probabilities to the sorted individuals using an exponential function.The GaussianMutator class performs the mutation of aNumericGene
.Genes are the atoms of the Jenetics library.The central class the GA is working with, is theGenotype
.Numeric chromosome implementation which holds 32-bit integer numbers.NumericGene implementation which holds a 32-bit integer number.This alterer takes two chromosomes (treating it as vectors) and creates a linear combination of these vectors as a result.In linear-ranking selection the individuals are sorted according to their fitness values.This alterer takes two chromosomes (treating it as vectors) and creates a linear combination of these vectors as a result.Numeric chromosome implementation which holds 64-bit integer numbers.NumericGene implementation which holds a 64-bit integer number.Alters a chromosome by replacing two genes by its mean value.The Monte Carlo selector selects the individuals from a given population randomly.Multiple point crossoverThis class is for mutating the chromosomes of a given population.Represents the result pair of one of the fourMutator.mutate
calls.Numeric chromosome interface.Base interface for numeric genes.Thisenum
determines whether the GA should maximize or minimize the fitness function.This alterer wraps a given alterer which works on a given section of the genotype's chromosomes.PartiallyMatchedCrossover<T,C extends Comparable<? super C>> ThePartiallyMatchedCrossover
(PMX) guarantees that allGene
s are found exactly once in each chromosome.This chromosome can be used to model permutations of a given (sub) set of alleles.ThePhenotype
consists of aGenotype
, the current generation and an optional fitness value.Probability selectors are a variation of fitness proportional selectors and selects individuals from a given population based on its selection probability P(i).An enhanced genetic algorithm (EGA) combine elements of existing solutions in order to create a new solution, with some of the properties of each parent.The roulette-wheel selector is also known as fitness proportional selector, but in the Jenetics library it is implemented as probability selector.Selectors are responsible for selecting a given number of individuals from the population.The shift mutation applies mutation between two randomly chosen points.This class defines theChromosome
shift indexes.Functional interface for creating random shift ranges objects for shifting sequences of a given length.The shuffle mutation, changes the order of the genes between two randomly chosen positions.Represents the chromosome range which will be shuffledFunctional interface for creating random range objects for shuffling sequences of a given length.Single point crossoverStochasticUniversalSelector
is a method for selecting a population according to some given probability in a way that minimizes chance fluctuations.TheSwapMutation
changes the order of genes in a chromosome, with the hope of bringing related genes closer together, thereby facilitating the production of building blocks.In tournament selection the best individual from a random sample of s individuals is chosen from the population Pg.In truncation selection, individuals are sorted according to their fitness.The uniform crossover uses swaps single genes between two chromosomes, instead of whole ranges as in single- and multipoint crossover.UniformOderBasedCrossover<T,C extends Comparable<? super C>> TheUniformOderBasedCrossover
guarantees that allGene
s are found exactly once in each chromosome.