GeneticSharp is a fast, extensible, multi-platform, and multithreading C# Genetic Algorithm library that simplifies the development of applications using Genetic Algorithms (GAs).
It can be used in any kind of .NET 6, .NET Standard, and .NET Framework apps, like ASP .NET MVC, ASP .NET Core, Blazor, Web Forms, UWP, Windows Forms, GTK#, Xamarin, MAUI and Unity3D games.
Add your own fitness evaluation, implementing IFitness interface.
Only GeneticSharp:
install-package GeneticSharp
GeneticSharp and extensions (TSP, AutoConfig, Bitmap equality, Equality equation, Equation solver, Function builder, etc):
install-package GeneticSharp.Extensions
You should use the UnityNuGet to install GeneticSharp directly from NuGet.
Or you can use the latest GeneticSharp.unitypackage available on our release page.
To install previous version that support .NET Standard 2.0 and .NET Framework 4.6.2:
install-package GeneticSharp -Version 2.6.0
To install previous version that support .NET Framework 3.5:
install-package GeneticSharp -Version 1.2.0
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.
public class MyProblemFitness : IFitness
{
public double Evaluate (IChromosome chromosome)
{
// Evaluate the fitness of 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();
}
}
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);
dotnet new
If you're using .NET 6 or .NET Core, you can install GeneticSharp.Templates:
dotnet new -i GeneticSharp.Templates
There are 4 templates in GeneticSharp.Templates:
A Blazor client application template with GeneticSharp ready to run a Travelling Salesman Problem (TSP).
dotnet new GeneticSharpTspBlazorApp -n MyNamespace -o MyOutoputFolder
A console application template with GeneticSharp, you just need to implement the chromosome and fitness function.
dotnet new GeneticSharpConsoleApp -n MyNamespace -o MyOutoputFolder
A console application template with GeneticSharp ready to run a Travelling Salesman Problem (TSP).
dotnet new GeneticSharpTspConsoleApp -n MyNamespace -o MyOutoputFolder
A Unity3D template with GeneticSharp ready to run a Travelling Salesman Problem (TSP).
dotnet new GeneticSharpTspUnity3d -n MyNamespace -o MyOutoputFolder
Having troubles?
Create a fork of GeneticSharp.
Did you change it? Submit a pull request.
Licensed under the The MIT License (MIT). In others words, you can use this library for developement any kind of software: open source, commercial, proprietary, etc.