Genetic Algorithm for .NET
What is Cormorant?
Cormorant is a framework for C# created to solve optimization problems
through genetic algorithms (GAs) in a simple and intuitive way.
For the creation of the classes, functions and objects in general
has been used a nomenclature inspired in the field of studio of the biology,
avoiding confusing terms or mixtures of abbreviations
with the aim of making learning as easy and fast as possible.
Your suggestions and ideas are welcome.
Please, send them to support@cormorant.juanexposito.com
Quick Start
Add the CormorantCommon reference to your project.
Add the following using.
using CormorantCommon;
Create an instance of the Cormorant class.
var cor = new Cormorant();
Create the genes.
var genX = new Gene("x", -100d, 100d);
var genY = new Gene("y", -100d, 100d);
Create the genome and set the genes and the genome.
var genome = new Genome();
genome.SetGene(genX);
genome.SetGene(genY);
cor.SetGenome(genome);
Set some configurations as the maximum number of generations, the size of the population, the percentage of individuals to be maintained per generation and the percentage of individuals to be mutated per generation.
cor.SetNumberGenerations(50);
cor.SetPopulationSize(50);
cor.SetMaintain(5);
cor.SetMutation(15);
Optional and just for test purpose: set the solution if it is known to get the absolute and relative error.
cor.SetSolution(141.42136);
Create the fitness function and set it with the SetFitnessFunction method.
static double FitnessFunction(Individual individual)
{
var genX = individual.Genotype.GetGeneByName("x").Value;
var genY = individual.Genotype.GetGeneByName("y").Value;
// Function: f = (x^2 + x^2)^(1/2)
var result = Math.Sqrt(Math.Pow(genX, 2) + Math.Pow(genY, 2));
return result;
}
cor.SetFitnessFunction(FitnessFunction);
Finally, use the Compute method to compute the algorithm and get the report with the GetReport method.
cor.Compute();
Console.WriteLine(cor.GetReport());
The complete code would be the following:
var cor = new Cormorant();
// Create genes
var genX = new Gene("x", -100d, 100d);
var genY = new Gene("y", -100d, 100d);
// Create the genome and set the genes and the genome
var genome = new Genome();
genome.SetGene(genX);
genome.SetGene(genY);
cor.SetGenome(genome);
// Set some properties
cor.SetNumberGenerations(50);
cor.SetPopulationSize(50);
cor.SetFitnessFunction(FitnessFunction);
cor.SetMaintain(5);
cor.SetMutation(15);
// Optional and just for test purpose: set the solution if it is known to get the absolute and relative error
cor.SetSolution(141.42136);
// Compute the algorithm
cor.Compute();
// Show the report
Console.WriteLine(cor.GetReport());
static double FitnessFunction(Individual individual)
{
var genX = individual.Genotype.GetGeneByName("x").Value;
var genY = individual.Genotype.GetGeneByName("y").Value;
// Function: f = (x^2 + x^2)^(1/2)
var result = Math.Sqrt(Math.Pow(genX, 2) + Math.Pow(genY, 2));
return result;
}
Output:
==================================================
Parameters
==================================================
Population size: 50
Generations: 50
Maintain: 5%
Mutation: 15%
Fitness type: Maximize
Crossover type: OnePoint
==================================================
Best individual by generation
==================================================
Generation 0: * 136,62290930443666
Generation 1: * 136,62290930443666
Generation 2: * 136,62290930443666
Generation 3: * 136,62290930443666
Generation 4: ********************* 140,29694450867675
Generation 5: ********************* 140,29694450867675
Generation 6: ********************* 140,29694450867675
Generation 7: ********************* 140,29694450867675
Generation 8: ********************* 140,29694450867675
Generation 9: ********************* 140,29694450867675
Generation 10: ********************* 140,29694450867675
Generation 11: ********************* 140,29694450867675
Generation 12: ********************* 140,29694450867675
Generation 13: ********************* 140,29694450867675
Generation 14: ********************* 140,29694450867675
Generation 15: ********************* 140,29694450867675
Generation 16: ********************* 140,29694450867675
Generation 17: ********************* 140,29694450867675
Generation 18: ********************* 140,29694450867675
Generation 19: ********************* 140,29694450867675
Generation 20: ********************* 140,29694450867675
Generation 21: ********************* 140,29694450867675
Generation 22: ********************* 140,29694450867675
Generation 23: ************************** 141,20236485076495
Generation 24: ************************** 141,20236485076495
Generation 25: ************************** 141,20236485076495
Generation 26: ************************** 141,20236485076495
Generation 27: ************************** 141,20236485076495
Generation 28: ************************** 141,20236485076495
Generation 29: ************************** 141,20236485076495
Generation 30: ************************** 141,20236485076495
Generation 31: ************************** 141,20236485076495
Generation 32: ************************** 141,20236485076495
Generation 33: ************************** 141,20236485076495
Generation 34: ************************** 141,20236485076495
Generation 35: ************************** 141,20236485076495
Generation 36: ************************** 141,20236485076495
Generation 37: ************************** 141,20236485076495
Generation 38: ************************** 141,20236485076495
Generation 39: ************************** 141,20236485076495
Generation 40: ************************** 141,20236485076495
Generation 41: ************************** 141,20236485076495
Generation 42: ************************** 141,20236485076495
Generation 43: ************************** 141,20236485076495
Generation 44: ************************** 141,20236485076495
Generation 45: ************************** 141,22465435877032
Generation 46: *************************** 141,25388443440247
Generation 47: *************************** 141,25388443440247
Generation 48: *************************** 141,25388443440247
Generation 49: *************************** 141,25388443440247
Generation 50: *************************** 141,25388443440247
==================================================
Best individual
==================================================
Fitness: 141,25388443440247
Genotype:
- Gene x, Value: -99,85396962347866
- Gene y, Value: -99,90918185152418
==================================================
Errors
==================================================
Absolute error: 0,16747556559752752
Relative error: 0,11842310496627066 %
© Cormorant 2023-2025 Written by Juan Expósito | Powered by .NET 8.0