FunctionMax
The problem is to maximize the following function of variables x and y:
f = (x^2 + y^2)^(1/2)
In addition, x and y must be between -100 and 100.
Real solution: 141.42136 with x=-100 and y=-100 or x=-100 and y=100 or x=100 and y=100 or x=100 and y=-100
Code:
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());
private 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