Skip to content

Genetic Algorithms in Java. Easy to use and extensible by design

Notifications You must be signed in to change notification settings

roman91DE/geneticAlgorithms4j

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GeneticAlgorithms4j

Description

This is a simple library for genetic algorithms in Java. It is designed to be easy to use and to be easily extensible.

Usage

Running a simple genetic algorithm

The following code shows how to run a simple genetic algorithm built with some sample components included in the library.

import com.geneticalgorithms4j.*;
import java.io.PrintStream;

public class Main {
    public static void main(String[] args) {
        
        // provide a prototype solution
        Solution prototypeSolution = new Solution(0, 1, 10);
        // define the size of the population
        int populationSize = 1000;
        // define the fitness function
        FitnessFunction fitnessFunction = new OneMaxFitness();
        // define the genetic operators
        SelectionOperator selectionOperator = new TournamentSelection(2);
        CrossoverOperator crossoverOperator = new NPointCrossover(2, 0.8d);
        MutationOperator mutationOperator = new RandomMutation(0.05d);
        TerminationCondition terminationCondition = new MaxNumberOfFitnessEvaluations(1_000_000);
        // pass an output stream to print the results
        PrintStream outputStream = System.out;

        // create the genetic algorithm from the components
        GeneticAlgorithm geneticAlgorithm = new simpleGA(
                populationSize,
                prototypeSolution,
                fitnessFunction,
                selectionOperator,
                crossoverOperator,
                mutationOperator,
                terminationCondition,
                outputStream);
        
        // run the genetic algorithm
        geneticAlgorithm.run();
        
    }
}

Using an external program as a fitness function

The following code shows how to run a genetic algorithm using an external python program as a fitness function. This is useful if you want to tune a machine learning model implemented in a slower scripting language.

import java.io.PrintStream;

public class Main {
    public static void main(String[] args) {

        int populationSize = 50;
        int chromosomeLength = 10;
        int minValue = 1;
        int maxValue = 2;
        FitnessFunction fitnessFunction = new ExternalFitnessFunction("/usr/bin/python3", "./src/fitnessFunction.py");
        SelectionOperator selectionOperator = new TournamentSelection(2);
        CrossoverOperator crossoverOperator = new NPointCrossover(2, 0.8d);
        MutationOperator mutationOperator = new RandomMutation(0.05d);
        TerminationCondition terminationCondition = new MaxNumberOfFitnessEvaluations(1_000);
        PrintStream outputStream = System.out;


        GeneticAlgorithm geneticAlgorithm = new simpleGA(
                populationSize,
                prototypeSolution,
                fitnessFunction,
                selectionOperator,
                crossoverOperator,
                mutationOperator,
                terminationCondition,
                outputStream);

        geneticAlgorithm.run();


    }
}

The corresponding python program can be implemented as the following: Read the chromosome from the standard input and print the fitness value to the standard output.

import argparse
from functools import reduce
from typing import List
from sys import stdout

"""
Synopsis: Fitness function for genetic algorithms

Description: This is an example fitness function for genetic algorithms. It takes a list of integers
representing a chromosome and returns a float representing the fitness of the chromosome. The fitness
function is the product of all the integers in the chromosome. This fitness function is used in the


Usage: python3 fitnessFunction.py <chromosome>
"""

def fitnessFunction(chromosome: List[int]) -> float:
    """ Example Fitness function for genetic algorithms """
    return float(reduce(lambda x, y: x * y, chromosome))


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description='Fitness function for genetic algorithms')
    parser.add_argument('chromosome', metavar='chromosome', type=int, nargs='+',
                        help='a space separated list of integers representing the chromosome')
    args = parser.parse_args()
    return_value = fitnessFunction(args.chromosome)

    stdout.write(str(return_value))
    stdout.write('\n')
    stdout.flush()

Permutation-based genetic algorithms

Often the solution to a problem is a permutation of a set of elements. For example, the solution to the travelling salesman problem is a permutation of the cities to visit. The library provides a set of components to easily implement permutation-based genetic algorithms as shown in the following example.

import java.io.PrintStream;

public class Main {
    public static void main(String[] args) {

        int populationSize = 50;
        // Use the PermutationSolution subclass instead of the Solution class
        Solution prototypeSolution = new PermutationSolution(0, 9, 10);
        FitnessFunction fitnessFunction = new OneMaxFitness();
        SelectionOperator selectionOperator = new TournamentSelection(2);
        // use instances of CrossOverOperator and MutationOperator that work with permutations
        CrossoverOperator crossoverOperator = new OrderCrossover(0.8d);
        MutationOperator mutationOperator = new SwapMutation(0.05d);
        TerminationCondition terminationCondition = new MaxNumberOfFitnessEvaluations(1_000);
        PrintStream outputStream = System.out;


        GeneticAlgorithm geneticAlgorithm = new simpleGA(
                populationSize,
                prototypeSolution,
                fitnessFunction,
                selectionOperator,
                crossoverOperator,
                mutationOperator,
                terminationCondition,
                outputStream);

        geneticAlgorithm.run();


    }
}

About

Genetic Algorithms in Java. Easy to use and extensible by design

Topics

Resources

Stars

Watchers

Forks