Popularity
6.7
Stable
Activity
3.1
-
876
75
272

Description

GeneticSharp is a fast, extensible, multi-platform and multithreading C# Genetic Algorithm library that simplifies the development of applications using Genetic Algorithms (GAs).

Can be used in any kind of .NET Core and .NET Framework apps, like ASP .NET MVC, ASP .NET Core, Web Forms, UWP, Windows Forms, GTK#, Xamarin and Unity3D games.

Programming language: C#
License: MIT License
Latest version: v2.6.0

GeneticSharp alternatives and similar packages

Based on the "Artificial Intelligence" category.
Alternatively, view GeneticSharp alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of GeneticSharp or a related project?

Add another 'Artificial Intelligence' Package

README

[](docs/logo/logo-small.png)

Build status Quality status Coverage Status License Nuget Stack Overflow

GeneticSharp is a fast, extensible, multi-platform and multithreading C# Genetic Algorithm library that simplifies the development of applications using Genetic Algorithms (GAs).

Can be used in any kind of .NET Core and .NET Framework apps, like ASP .NET MVC, ASP .NET Core, Blazor, Web Forms, UWP, Windows Forms, GTK#, Xamarin and Unity3D games.


Projects, papers, journals, books, tutorials, courses and apps using GeneticSharp

Features

[Chromosomes](src/GeneticSharp.Domain/Chromosomes)

  • [FloatingPointChromosome](src/GeneticSharp.Domain/Chromosomes/FloatingPointChromosome.cs)
  • [IntegerChromosome](src/GeneticSharp.Domain/Chromosomes/IntegerChromosome.cs)
  • Add your own chromosome representation implementing [IChromosome](src/GeneticSharp.Domain/Chromosomes/IChromosome.cs) / [IBinaryChromosome](src/GeneticSharp.Domain/Chromosomes/IBinaryChromosome.cs) interfaces or extending [ChromosomeBase](src/GeneticSharp.Domain/Chromosomes/ChromosomeBase.cs) / [BinaryChromosomeBase](src/GeneticSharp.Domain/Chromosomes/BinaryChromosomeBase.cs).

[Fitness](src/GeneticSharp.Domain/Fitnesses)

Add your own fitness evaluation, implementing [IFitness](src/GeneticSharp.Domain/Fitnesses/IFitness.cs) interface.

[Populations](src/GeneticSharp.Domain/Populations)

  • [Population](src/GeneticSharp.Domain/Populations/Population.cs)
  • [TplPopulation](src/GeneticSharp.Domain/Populations/TplPopulation.cs)
  • Others populations can be added implementing [IPopulation](src/GeneticSharp.Domain/Populations/IPopulation.cs) interface or extending [Population](src/GeneticSharp.Domain/Populations/Population.cs).

[Generations](src/GeneticSharp.Domain/Populations/Generation.cs)

  • [Generation](src/GeneticSharp.Domain/Populations/Generation.cs)
  • [Generation strategy](src/GeneticSharp.Domain/Populations/IGenerationStrategy.cs)
    • [Performance strategy](src/GeneticSharp.Domain/Populations/PerformanceGenerationStrategy.cs)
    • [Tracking strategy](src/GeneticSharp.Domain/Populations/TrackingGenerationStrategy.cs)
    • Others generation strategies can be added implementing [IGenerationStrategy.cs](src/GeneticSharp.Domain/IGenerationStrategy.cs) interface.

[Operators strategy](src/GeneticSharp.Domain/src/GeneticSharp.Domain/IOperatorsStrategy.cs)

  • [DefaultOperatorsStrategy](src/GeneticSharp.Domain/DefaultOperatorsStrategy.cs)
  • [TplOperatorsStrategy](src/GeneticSharp.Domain/TplOperatorsStrategy.cs)
  • Others operators strategies can be added implementing [IOperatorsStrategy](src/GeneticSharp.Domain/IOperatorsStrategy.cs) interface.

[Selections](src/GeneticSharp.Domain/Selections)

  • [Elite](src/GeneticSharp.Domain/Selections/EliteSelection.cs) (also know as Truncate or Truncation)
  • [Roulette Wheel](src/GeneticSharp.Domain/Selections/RouletteWheelSelection.cs)
  • [Stochastic Universal Sampling](src/GeneticSharp.Domain/Selections/StochasticUniversalSamplingSelection.cs)
  • [Tournament](src/GeneticSharp.Domain/Selections/TournamentSelection.cs)
  • Others selections can be added implementing [ISelection](src/GeneticSharp.Domain/Selections/ISelection.cs) interface or extending [SelectionBase](src/GeneticSharp.Domain/Selections/SelectionBase.cs).

[Crossovers](src/GeneticSharp.Domain/Crossovers)

  • [Alternating-position (AP)](src/GeneticSharp.Domain/Crossovers/AlternatingPositionCrossover.cs)
  • [Cut and Splice](src/GeneticSharp.Domain/Crossovers/CutAndSpliceCrossover.cs)
  • [Cycle (CX)](src/GeneticSharp.Domain/Crossovers/CycleCrossover.cs)
  • [One-Point (C1)](src/GeneticSharp.Domain/Crossovers/OnePointCrossover.cs)
  • [Order-based (OX2)](src/GeneticSharp.Domain/Crossovers/OrderBasedCrossover.cs)
  • [Ordered (OX1)](src/GeneticSharp.Domain/Crossovers/OrderedCrossover.cs)
  • [Partially Mapped (PMX)](src/GeneticSharp.Domain/Crossovers/PartiallyMappedCrossover.cs)
  • [Position-based (POS)](src/GeneticSharp.Domain/Crossovers/PositionBasedCrossover.cs)
  • [Three parent](src/GeneticSharp.Domain/Crossovers/ThreeParentCrossover.cs)
  • [Two-Point (C2)](src/GeneticSharp.Domain/Crossovers/TwoPointCrossover.cs)
  • [Uniform](src/GeneticSharp.Domain/Crossovers/UniformCrossover.cs)
  • [Voting Recombination (VR)](src/GeneticSharp.Domain/Crossovers/VotingRecombinationCrossover.cs)
  • Others crossovers can be added implementing [ICrossover](src/GeneticSharp.Domain/Crossovers/ICrossover.cs) interface or extending [CrossoverBase](src/GeneticSharp.Domain/Crossovers/CrossoverBase.cs).

[Mutations](src/GeneticSharp.Domain/Mutations)

  • [Displacement](src/GeneticSharp.Domain/Mutations/DisplacementMutation.cs)
  • [Flip Bit](src/GeneticSharp.Domain/Mutations/FlipBitMutation.cs)
  • [Insertion](src/GeneticSharp.Domain/Mutations/InsertionMutation.cs)
  • [Partial Shuffle (PSM)](src/GeneticSharp.Domain/Mutations/PartialShuffleMutation.cs)
  • [Reverse Sequence (RSM)](src/GeneticSharp.Domain/Mutations/ReverseSequenceMutation.cs)
  • [Twors](src/GeneticSharp.Domain/Mutations/TworsMutation.cs)
  • [Uniform](src/GeneticSharp.Domain/Mutations/UniformMutation.cs)
  • Others mutations can be added implementing [IMutation](src/GeneticSharp.Domain/Mutations/IMutation.cs) interface or extending [MutationBase](src/GeneticSharp.Domain/Mutations/MutationBase.cs) / [SequenceMutationBase](src/GeneticSharp.Domain/Mutations/SequenceMutationBase.cs).

[Reinsertions](src/GeneticSharp.Domain/Reinsertions)

  • [Elitist](src/GeneticSharp.Domain/Reinsertions/ElitistReinsertion.cs)
  • [Fitness Based](src/GeneticSharp.Domain/Reinsertions/FitnessBasedReinsertion.cs)
  • [Pure](src/GeneticSharp.Domain/Reinsertions/PureReinsertion.cs)
  • [Uniform](src/GeneticSharp.Domain/Reinsertions/UniformReinsertion.cs)
  • Others reinsertions can be added implementing [IReinsertion](src/GeneticSharp.Domain/Reinsertions/IReinsertion.cs) interface or extending [ReinsertionBase](src/GeneticSharp.Domain/Reinsertions/ReinsertionBase.cs).

[Terminations](src/GeneticSharp.Domain/Terminations)

  • [Generation number](src/GeneticSharp.Domain/Terminations/GenerationNu)
  • [Time evolving](src/GeneticSharp.Domain/Terminations/TimeEvolvingTermination.cs)
  • [Fitness stagnation](src/GeneticSharp.Domain/Terminations/FitnessStagnationTermination.cs)
  • [Fitness threshold](src/GeneticSharp.Domain/Terminations/FitnessThresholdTermination.cs)
  • [And](src/GeneticSharp.Domain/Terminations/AndTermination.cs) e [Or](src/GeneticSharp.Domain/Terminations/OrTermination.cs) (allows combine others terminations)
  • Others terminations can be added implementing [ITermination](src/GeneticSharp.Domain/Terminations/ITermination.cs) interface or extending [TerminationBase](src/GeneticSharp.Domain/Terminations/TerminationBase.cs).

[Randomizations](src/GeneticSharp.Domain/Randomizations)

  • [Basic randomization](src/GeneticSharp.Domain/Randomizations/BasicRandomization.cs) (using System.Random)
  • [Fast random](src/GeneticSharp.Domain/Randomizations/FastRandomRandomization.cs)
  • If you need a special kind of randomization for your GA, just implement the [IRandomization](src/GeneticSharp.Domain/Randomizations/IRandomization.cs) interface.

[Task executors](src/GeneticSharp.Infrastructure.Framework/Threading/ITaskExecutor.cs)

  • [LinearTaskExecutor](src/GeneticSharp.Infrastructure.Framework/Threading/LinearTaskExecutor.cs)
  • [ParallelTaskExecutor](src/GeneticSharp.Infrastructure.Framework/Threading/ParallelTaskExecutor.cs)
  • [TplTaskExecutor](src/GeneticSharp.Infrastructure.Framework/Threading/TplTaskExecutor.cs)
  • Others task executors can be added implementing [ITaskExecutor](src/GeneticSharp.Infrastructure.Framework/Threading/ITaskExecutor.cs) interface.

Samples

BlazorAI

[](docs/gifs/GeneticSharp-BlazorAi-samples.gif)

[Blazor samples](src/GeneticSharp.Runner.BlacorApp)

[Console samples](src/GeneticSharp.Runner.ConsoleApp)

  • AutoConfig
  • Bitmap equality
  • Equality equation
  • Equation solver
  • Function builder
  • Ghostwriter
  • TSP (Travelling Salesman Problem)

[](docs/gifs/GeneticSharp-ConsoleApp-EquationSolver-FunctionBuilder.gif)

[GTK# samples](src/GeneticSharp.Runner.GtkApp)

  • Bitmap equality
  • Function optimization
  • Sudoku
  • TSP (Travelling Salesman Problem)

[](docs/gifs/GeneticSharp-GtkApp.gif)

[Unity3D Samples](src/GeneticSharp.Runner.UnityApp)

  • Car2D
  • TSP (Travelling Salesman Problem)
  • Wall Builder

[](docs/screenshots/GeneticSharp-UnityApp.png) [](docs/images/google-play-badge.png)

Multi-platform

  • Mono, .NET Standard 2.0 and .NET Framework 4.6.2 support
  • Fully tested on Windows and MacOS

Code quality


Setup

.NET Standard 2.0 and .NET Framework 4.6.2

Only GeneticSharp:

install-package GeneticSharp

GeneticSharp and extensions (TSP, AutoConfig, Bitmap equality, Equality equation, Equation solver, Function builder, etc):

install-package GeneticSharp.Extensions

Unity3D

You should use the UnityNuGet to install GeneticSharp directly from NuGet.

Or you can use the latest GeneticSharp.unitypackage available on our release page.

Mono and .NET Framework 3.5

To install previous version that support .NET Framework 3.5:

install-package GeneticSharp -Version 1.2.0

Running samples

If you want to run the console, GTK# and Unity samples, just fork this repository and follow the instruction from our setup page wiki.

An easy way to run the Unity Samples, if you have a Android device, is download it from Google Play.

Usage

Creating your own fitness evaluation


public class MyProblemFitness : IFitness
{  
    public double Evaluate (IChromosome chromosome)
    {
        // Evaluate the fitness of chromosome.
    }
}

Creating your own chromosome


public class MyProblemChromosome : ChromosomeBase
{
    // Change the argument value passed to base constructor to change the length 
    // of your chromosome.
    public MyProblemChromosome() : base(10) 
    {
        CreateGenes();
    }

    public override Gene GenerateGene (int geneIndex)
    {
        // Generate a gene base on my problem chromosome representation.
    }

    public override IChromosome CreateNew ()
    {
        return new MyProblemChromosome();
    }
}

Running your GA

var selection = new EliteSelection();
var crossover = new OrderedCrossover();
var mutation = new ReverseSequenceMutation();
var fitness = new MyProblemFitness();
var chromosome = new MyProblemChromosome();
var population = new Population (50, 70, chromosome);

var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation);
ga.Termination = new GenerationNumberTermination(100);

Console.WriteLine("GA running...");
ga.Start();

Console.WriteLine("Best solution found has {0} fitness.", ga.BestChromosome.Fitness);

Templates for dotnet new

If you're using .NET Core, you can install GeneticSharp.Templates:

dotnet new -i GeneticSharp.Templates

There are 4 templates in GeneticSharp.Templates:

TSP Blazor application

A Blazor client application template with GeneticSharp ready to run a Travelling Salesman Problem (TSP).

dotnet new GeneticSharpTspBlazorApp -n MyNamespace -o MyOutoputFolder

Console application

A console application template with GeneticSharp, you just need to implement the chromosome and fitness function.

dotnet new GeneticSharpConsoleApp -n MyNamespace -o MyOutoputFolder

TSP Console application

A console application template with GeneticSharp ready to run a Travelling Salesman Problem (TSP).

dotnet new GeneticSharpTspConsoleApp -n MyNamespace -o MyOutoputFolder

TSP Unity3D

A Unity3D template with GeneticSharp ready to run a Travelling Salesman Problem (TSP).

dotnet new GeneticSharpTspUnity3d -n MyNamespace -o MyOutoputFolder

FAQ

Having troubles?


How to improve it?

Create a fork of GeneticSharp.

Did you change it? Submit a pull request.

License

Licensed under the The [MIT License (MIT)](LICENSE). In others words, you can use this library for developement any kind of software: open source, commercial, proprietary, etc.

Thanks to

  • AppVeyor: open source license for continuous integration.
  • JetBrains: open source license for all products pack.
  • SMASHINGLOGO: GeneticSharp's logo.
  • SonarCloud: open source license for online inspection.
[](docs/images/appveyor-logo.png) [](docs/images/jetbrains-logo.png) [](docs/images/smashinglogo-logo.png) [](docs/images/sonarcloud-logo.png)


*Note that all licence references and agreements mentioned in the GeneticSharp README section above are relevant to that project's source code only.