jenetics/jenetics

Name: jenetics

Description: Jenetics - Java Genetic Algorithm Library

Created: 2013-12-21 21:24:50.0

Updated: 2018-01-16 04:15:10.0

Pushed: 2018-01-14 18:58:04.0

Homepage: http://jenetics.io

Size: 70533

Language: Java

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Jenetics

Build Status Maven Central Javadoc

Jenetics is an Genetic Algorithm, Evolutionary Algorithm and Genetic Programming library, respectively, 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 Stream API.

Other languages

Documentation

The library is fully documented (javadoc) and comes with an user manual (pdf).

Requirements
Runtime
Build time
Test compile/execution
Build Jenetics

For building the Jenetics library from source, download the most recent, stable package version from Github (or Sourceforge) and extract it to some build directory.

$ unzip jenetics-<version>.zip -d <builddir>

<version> denotes the actual Jenetics version and <builddir> the actual build directory. Alternatively you can check out the master branch from Github.

$ git clone https://github.com/jenetics/jenetics.git <builddir>

Jenetics uses Gradle as build system and organizes the source into sub-projects (modules). Each sub-project is located in it?s own sub-directory:

Published projects

The following projects/modules are also published to Maven.

Non-published projects

For building the library change into the <builddir> directory (or one of the module directory) and call one of the available tasks:

For building the library jar from the source call

$ cd <build-dir>
$ ./gradlew jar
Example
Hello World (Ones counting)

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

rt io.jenetics.BitChromosome;
rt io.jenetics.BitGene;
rt io.jenetics.Genotype;
rt io.jenetics.engine.Engine;
rt io.jenetics.engine.EvolutionResult;
rt io.jenetics.util.Factory;

ic class HelloWorld {
// 2.) Definition of the fitness function.
private static Integer eval(Genotype<BitGene> gt) {
    return gt.getChromosome()
        .as(BitChromosome.class)
        .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:

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

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

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

  4. 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 Stream API. The final result, the best Genotype in our example, is then collected with one of the predefined collectors of the EvolutionResult class.

Evolving images

This example tries to approximate a given image by semitransparent polygons. It comes with an Swing UI, where you can immediately start your own experiments. After compiling the sources with

$ ./gradlew compileTestJava

you can start the example by calling

$ ./jrun io.jenetics.example.image.EvolvingImages

Evolving images

The previous image shows the GUI after evolving the default image for about 4,000 generations. With the »Open« button it is possible to load other images for polygonization. The »Save« button allows to store polygonized images in PNG format to disk. At the button of the UI, you can change some of the GA parameters of the example.

Projects using Jenetics
Citations
Blogs
License

The library is licensed under the Apache License, Version 2.0.

Copyright 2007-2018 Franz Wilhelmstötter

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Release notes
4.1.0
Improvements Bugs

All Release Notes

Used software

IntelliJ


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.