SimpleSum
The problem is to maximize the following function of variables x and y:
f = x + y
In addition, x and y must be between 0 and 500.
Real solution: 1000
Code:
var cor = new Cormorant();
// Create genes
var genA = new Gene("A", 0d, 500d);
var genB = new Gene("B", 0d, 500d);
// Create the genome and set the genes and the genome
var genome = new Genome();
genome.SetGene(genA);
genome.SetGene(genB);
cor.SetGenome(genome);
// Set some properties
cor.SetFitnessFunction(FitnessFunction);
cor.SetCrossoverType(ECrossoverType.OnePoint);
cor.SetMutation(10);
// Optional and just for test purpose: set the solution if it is known to get the absolute and relative error
cor.SetSolution(1000);
// Compute the algorithm
cor.Compute();
// Show the report
Console.WriteLine(cor.GetReport());
private static double FitnessFunction(Individual individual)
{
var geneA = individual.Genotype.GetGeneByName("A");
var geneB = individual.Genotype.GetGeneByName("B");
return geneA.Value + geneB.Value;
}
Output:
==================================================
Parameters
==================================================
Population size: 50
Generations: 25
Maintain: 15%
Mutation: 10%
Fitness type: Maximize
Crossover type: OnePoint
==================================================
Best individual by generation
==================================================
Generation 0: * 965,8679802217329
Generation 1: ********** 974,9898032624305
Generation 2: ********** 974,9898032624305
Generation 3: ********** 974,9898032624305
Generation 4: ********** 974,9898032624305
Generation 5: ********** 974,9898032624305
Generation 6: ********** 974,9898032624305
Generation 7: ********** 974,9898032624305
Generation 8: ********** 974,9898032624305
Generation 9: ********** 974,9898032624305
Generation 10: ********** 974,9898032624305
Generation 11: ********** 974,9898032624305
Generation 12: ********** 974,9898032624305
Generation 13: ********** 974,9898032624305
Generation 14: ********** 974,9898032624305
Generation 15: ********** 974,9898032624305
Generation 16: ********** 974,9898032624305
Generation 17: ********** 974,9898032624305
Generation 18: ************************** 991,6308586661528
Generation 19: ************************** 991,6308586661528
Generation 20: ************************** 991,6308586661528
Generation 21: ************************** 991,6308586661528
Generation 22: ************************** 991,6308586661528
Generation 23: ************************** 991,6308586661528
Generation 24: ************************** 991,6308586661528
Generation 25: ************************** 991,6308586661528
==================================================
Best individual
==================================================
Fitness: 991,6308586661528
Genotype:
- Gene A, Value: 493,7099002815823
- Gene B, Value: 497,92095838457055
==================================================
Errors
==================================================
Absolute error: 8,36914133384721
Relative error: 0,836914133384721 %
© Cormorant 2023-2025 Written by Juan Expósito | Powered by .NET 8.0