All Projects → lagodiuk → Genetic Algorithm

lagodiuk / Genetic Algorithm

Licence: other
Generic implementation of genetic algorithm in Java.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Genetic Algorithm

Watchcarslearn
Self driving cars using NEAT
Stars: ✭ 59 (-47.32%)
Mutual labels:  genetic-algorithm
Rl Course Experiments
Stars: ✭ 73 (-34.82%)
Mutual labels:  genetic-algorithm
Metaheuristics
Implement the-state-of-the-art meta-heuristic algorithms using python (numpy)
Stars: ✭ 95 (-15.18%)
Mutual labels:  genetic-algorithm
Vehicleroutingproblem
Solved using AI techniques: Savings, Sweep, Genetic Algorithm, Google OR Tools
Stars: ✭ 61 (-45.54%)
Mutual labels:  genetic-algorithm
Path planning
This repository contains path planning algorithms in C++ for a grid based search.
Stars: ✭ 70 (-37.5%)
Mutual labels:  genetic-algorithm
Cross Adaptive Audio
Evolving Artificial Neural Networks for Cross-Adaptive Audio Effects
Stars: ✭ 82 (-26.79%)
Mutual labels:  genetic-algorithm
Genann
simple neural network library in ANSI C
Stars: ✭ 1,088 (+871.43%)
Mutual labels:  genetic-algorithm
Oxigen
Fast, parallel, extensible and adaptable genetic algorithms framework written in Rust
Stars: ✭ 108 (-3.57%)
Mutual labels:  genetic-algorithm
Mealpy
A collection of the state-of-the-art MEta-heuristics ALgorithms in PYthon (mealpy)
Stars: ✭ 72 (-35.71%)
Mutual labels:  genetic-algorithm
Evoasm.rb
An AIMGP (Automatic Induction of Machine code by Genetic Programming) engine
Stars: ✭ 91 (-18.75%)
Mutual labels:  genetic-algorithm
Mgo
Purely functional genetic algorithms for multi-objective optimisation
Stars: ✭ 63 (-43.75%)
Mutual labels:  genetic-algorithm
Radiate
Radiate is a parallel genetic programming engine capable of evolving solutions to many problems as well as training learning algorithms.
Stars: ✭ 65 (-41.96%)
Mutual labels:  genetic-algorithm
Genetic Algorithm Montage
genetic algorithm for self-referential image approximation.
Stars: ✭ 86 (-23.21%)
Mutual labels:  genetic-algorithm
Cgp Cnn
A Genetic Programming Approach to Designing CNN Architectures, In GECCO 2017 (oral presentation, Best Paper Award)
Stars: ✭ 59 (-47.32%)
Mutual labels:  genetic-algorithm
Genetic Programming
Symbolic regression solver, based on genetic programming methodology.
Stars: ✭ 98 (-12.5%)
Mutual labels:  genetic-algorithm
Applying eanns
A 2D Unity simulation in which cars learn to navigate themselves through different courses. The cars are steered by a feedforward neural network. The weights of the network are trained using a modified genetic algorithm.
Stars: ✭ 1,093 (+875.89%)
Mutual labels:  genetic-algorithm
Rubikswift
Rubik's cube API in Swift + a genetic solver algorithm
Stars: ✭ 78 (-30.36%)
Mutual labels:  genetic-algorithm
Rusty Genes
Genetic algorithm implementations in Rust with animated visualizations
Stars: ✭ 109 (-2.68%)
Mutual labels:  genetic-algorithm
Openclga
A Python Library for Genetic Algorithm on OpenCL
Stars: ✭ 103 (-8.04%)
Mutual labels:  genetic-algorithm
Time Table Scheduler
Time Table generation using Genetic Algorithms ( Java-Struts2)
Stars: ✭ 90 (-19.64%)
Mutual labels:  genetic-algorithm

genetic-algorithm

Generic implementation of Genetic algorithm in Java.

About

quick start

  1. git clone https://github.com/lagodiuk/genetic-algorithm.git
  2. mvn -f genetic-algorithm/pom.xml install

architecture overview

Architecture

basic example

import java.util.Arrays;
import java.util.List;
import java.util.Random;

import com.lagodiuk.ga.Chromosome;
import com.lagodiuk.ga.Fitness;
import com.lagodiuk.ga.GeneticAlgorithm;
import com.lagodiuk.ga.IterartionListener;
import com.lagodiuk.ga.Population;

public class Demo {

	public static void main(String[] args) {
		Population<MyVector> population = createInitialPopulation(5);

		Fitness<MyVector, Double> fitness = new MyVectorFitness();

		GeneticAlgorithm<MyVector, Double> ga = new GeneticAlgorithm<MyVector, Double>(population, fitness);

		addListener(ga);

		ga.evolve(500);
	}

	/**
	 * The simplest strategy for creating initial population <br/>
	 * in real life it could be more complex
	 */
	private static Population<MyVector> createInitialPopulation(int populationSize) {
		Population<MyVector> population = new Population<MyVector>();
		MyVector base = new MyVector();
		for (int i = 0; i < populationSize; i++) {
			// each member of initial population
			// is mutated clone of base chromosome
			MyVector chr = base.mutate();
			population.addChromosome(chr);
		}
		return population;
	}

	/**
	 * After each iteration Genetic algorithm notifies listener
	 */
	private static void addListener(GeneticAlgorithm<MyVector, Double> ga) {
		// just for pretty print
		System.out.println(String.format("%s\t%s\t%s", "iter", "fit", "chromosome"));

		// Lets add listener, which prints best chromosome after each iteration
		ga.addIterationListener(new IterartionListener<MyVector, Double>() {

			private final double threshold = 1e-5;

			@Override
			public void update(GeneticAlgorithm<MyVector, Double> ga) {

				MyVector best = ga.getBest();
				double bestFit = ga.fitness(best);
				int iteration = ga.getIteration();

				// Listener prints best achieved solution
				System.out.println(String.format("%s\t%s\t%s", iteration, bestFit, best));

				// If fitness is satisfying - we can stop Genetic algorithm
				if (bestFit < this.threshold) {
					ga.terminate();
				}
			}
		});
	}

	/**
	 * Chromosome, which represents vector of five integers
	 */
	public static class MyVector implements Chromosome<MyVector>, Cloneable {

		private static final Random random = new Random();

		private final int[] vector = new int[5];

		/**
		 * Returns clone of current chromosome, which is mutated a bit
		 */
		@Override
		public MyVector mutate() {
			MyVector result = this.clone();

			// just select random element of vector
			// and increase or decrease it on small value
			int index = random.nextInt(this.vector.length);
			int mutationValue = random.nextInt(3) - random.nextInt(3);
			result.vector[index] += mutationValue;

			return result;
		}

		/**
		 * Returns list of siblings <br/>
		 * Siblings are actually new chromosomes, <br/>
		 * created using any of crossover strategy
		 */
		@Override
		public List<MyVector> crossover(MyVector other) {
			MyVector thisClone = this.clone();
			MyVector otherClone = other.clone();

			// one point crossover
			int index = random.nextInt(this.vector.length - 1);
			for (int i = index; i < this.vector.length; i++) {
				int tmp = thisClone.vector[i];
				thisClone.vector[i] = otherClone.vector[i];
				otherClone.vector[i] = tmp;
			}

			return Arrays.asList(thisClone, otherClone);
		}

		@Override
		protected MyVector clone() {
			MyVector clone = new MyVector();
			System.arraycopy(this.vector, 0, clone.vector, 0, this.vector.length);
			return clone;
		}

		public int[] getVector() {
			return this.vector;
		}

		@Override
		public String toString() {
			return Arrays.toString(this.vector);
		}
	}

	/**
	 * Fitness function, which calculates difference between chromosomes vector
	 * and target vector
	 */
	public static class MyVectorFitness implements Fitness<MyVector, Double> {

		private final int[] target = { 10, 20, 30, 40, 50 };

		@Override
		public Double calculate(MyVector chromosome) {
			double delta = 0;
			int[] v = chromosome.getVector();
			for (int i = 0; i < 5; i++) {
				delta += this.sqr(v[i] - this.target[i]);
			}
			return delta;
		}

		private double sqr(double x) {
			return x * x;
		}
	}
}

Population size

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].