Home > Uncategorized > “Hello World” in #GeneticAlgorithms in C#

“Hello World” in #GeneticAlgorithms in C#


Genetic algoritms are a computational representation of what happens in nature, how evolution solves problems, so it applies a “survival of the fittest” approach to solving a problem.

Like Machine Learning, instead of prescribing the solution to a problem you provide problem examples and corresponding solutions, and let the GA (Genetic Algorithm) try to figure out how to solve that problem.

Like in nature, (If we ignore religion for a moment!), nobody designed a cheetah to run faster than a gazelle, but when presented with a problem of a fast gazelle, the solution was to run faster than it. Other solutions, like running backwards, were not effective, and so were not selected for.

So, TL;DR; here is the github repo, which as code in C# using GeneticSharp to solve a very simple problem; https://github.com/infiniteloopltd/geneticAlgorithm_helloWorld

Here, the problem posed is simple addition;

// 1 + 1 = 2
// 2 + 2 = 4
// 3 + 2 = 5

However, you can substitute the parameters with more complex equations, and the GA will still try to solve it, it just will take longer.

The “Genes” of the GA are addition, substration, multiplation, division, and constant numbers (1,2,3). So, it will probably struggle to solve Sin(), Tan(), Cos() etc. Like in the same way that DNA is limited to making protiens, so you can’t make Petrol using DNA.

So, to wrap up this post, here is the code to run a simple equation solver using Genetic Algorithms;

static void Main(string[] args)
// Problem: Simple addition
// 1 + 1 = 2
// 2 + 2 = 4
// 3 + 2 = 5
var myInputs = new List<FunctionBuilderInput>
new FunctionBuilderInput(new List<double> {1, 1}, 2),
new FunctionBuilderInput(new List<double> {2, 2}, 4),
new FunctionBuilderInput(new List<double> {3, 2}, 5)
var myFitness = new FunctionBuilderFitness(myInputs.ToArray());
var myChromosome = new FunctionBuilderChromosome(myFitness.AvailableOperations, 5);
var selection = new EliteSelection();
var crossover = new ThreeParentCrossover();
var mutation = new UniformMutation(true);
var fitness = myFitness;
var chromosome = myChromosome;
var population = new Population(100, 200, chromosome)
GenerationStrategy = new PerformanceGenerationStrategy()
var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation)
Termination = new FitnessThresholdTermination(0)
ga.GenerationRan += delegate
var bestChromosome = ga.Population.BestChromosome;
Console.WriteLine(“Generations: {0}”, ga.Population.GenerationsNumber);
Console.WriteLine(“Fitness: {0,10}”, bestChromosome.Fitness);
Console.WriteLine(“Time: {0}”, ga.TimeEvolving);
Console.WriteLine(“Speed (gen/sec): {0:0.0000}”, ga.Population.GenerationsNumber / ga.TimeEvolving.TotalSeconds);
var best = bestChromosome as FunctionBuilderChromosome;
Console.WriteLine(“Function: {0}”, best.BuildFunction());


Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: