Last active
May 31, 2019 00:32
-
-
Save giacomelli/b6861313762464fe62b652ef7d03b91c to your computer and use it in GitHub Desktop.
Function optimization with GeneticSharp: http://diegogiacomelli.com.br/function-optimization-with-geneticsharp/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using GeneticSharp.Domain; | |
using GeneticSharp.Domain.Chromosomes; | |
using GeneticSharp.Domain.Crossovers; | |
using GeneticSharp.Domain.Fitnesses; | |
using GeneticSharp.Domain.Mutations; | |
using GeneticSharp.Domain.Populations; | |
using GeneticSharp.Domain.Selections; | |
using GeneticSharp.Domain.Terminations; | |
namespace FunctionOptimizationWithGeneticSharp | |
{ | |
class MainClass | |
{ | |
public static void Main(string[] args) | |
{ | |
float maxWidth = 998f; | |
float maxHeight = 680f; | |
var chromosome = new FloatingPointChromosome( | |
new double[] { 0, 0, 0, 0 }, | |
new double[] { maxWidth, maxHeight, maxWidth, maxHeight }, | |
new int[] { 10, 10, 10, 10 }, | |
new int[] { 0, 0, 0, 0 }); | |
var population = new Population(50, 100, chromosome); | |
var fitness = new FuncFitness((c) => | |
{ | |
var fc = c as FloatingPointChromosome; | |
var values = fc.ToFloatingPoints(); | |
var x1 = values[0]; | |
var y1 = values[1]; | |
var x2 = values[2]; | |
var y2 = values[3]; | |
return Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2)); | |
}); | |
var selection = new EliteSelection(); | |
var crossover = new UniformCrossover(0.5f); | |
var mutation = new FlipBitMutation(); | |
var termination = new FitnessStagnationTermination(100); | |
var ga = new GeneticAlgorithm( | |
population, | |
fitness, | |
selection, | |
crossover, | |
mutation); | |
ga.Termination = termination; | |
Console.WriteLine("Generation: (x1, y1), (x2, y2) = distance"); | |
var latestFitness = 0.0; | |
ga.GenerationRan += (sender, e) => | |
{ | |
var bestChromosome = ga.BestChromosome as FloatingPointChromosome; | |
var bestFitness = bestChromosome.Fitness.Value; | |
if (bestFitness != latestFitness) | |
{ | |
latestFitness = bestFitness; | |
var phenotype = bestChromosome.ToFloatingPoints(); | |
Console.WriteLine( | |
"Generation {0,2}: ({1},{2}),({3},{4}) = {5}", | |
ga.GenerationsNumber, | |
phenotype[0], | |
phenotype[1], | |
phenotype[2], | |
phenotype[3], | |
bestFitness | |
); | |
} | |
}; | |
ga.Start(); | |
Console.ReadKey(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
float maxWidth = 998f; | |
float maxHeight = 680f; | |
var chromosome = new FloatingPointChromosome( | |
new double[] { 0, 0, 0, 0 }, | |
new double[] { maxWidth, maxHeight, maxWidth, maxHeight }, | |
new int[] { 10, 10, 10, 10}, | |
new int[] { 0, 0, 0, 0 }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var crossover = new UniformCrossover(0.5f); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fitness = new FuncFitness((c) => | |
{ | |
var fc = c as FloatingPointChromosome; | |
var values = fc.ToFloatingPoints(); | |
var x1 = values[0]; | |
var y1 = values[1]; | |
var x2 = values[2]; | |
var y2 = values[3]; | |
return Math.Sqrt(Math.Pow(x2 - x1, 2) + Math.Pow(y2 - y1, 2)); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mutation = new FlipBitMutation(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var population = new Population(50, 100, chromosome); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var selection = new EliteSelection(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var termination = new FitnessStagnationTermination(100); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ga = new GeneticAlgorithm( | |
population, | |
fitness, | |
selection, | |
crossover, | |
mutation); | |
ga.Termination = termination; | |
ga.Start(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Console.WriteLine("Generation: (x1, y1), (x2, y2) = distance"); | |
var latestFitness = 0.0; | |
ga.GenerationRan += (sender, e) => | |
{ | |
var bestChromosome = ga.BestChromosome as FloatingPointChromosome; | |
var bestFitness = bestChromosome.Fitness.Value; | |
if (bestFitness != latestFitness) | |
{ | |
latestFitness = bestFitness; | |
var phenotype = bestChromosome.ToFloatingPoints(); | |
Console.WriteLine( | |
"Generation {0,2}: ({1},{2}),({3},{4}) = {5}", | |
ga.GenerationsNumber, | |
phenotype[0], | |
phenotype[1], | |
phenotype[2], | |
phenotype[3], | |
bestFitness | |
); | |
} | |
}; | |
ga.Start(); | |
Console.ReadKey(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment