public void Evolve_ManyGenerations_Fast() { var selection = new EliteSelection(); var crossover = new ThreeParentCrossover(); var mutation = new UniformMutation(true); var fitness = new FunctionBuilderFitness( new FunctionBuilderInput( new double[] { 1, 2, 3 }, 6) , new FunctionBuilderInput( new double[] { 2, 3, 4 }, 24) ); var chromosome = new FunctionBuilderChromosome(fitness.AvailableOperations, 5); var population = new Population(100, 200, chromosome); var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation); ga.Termination = new FitnessThresholdTermination(0); ga.Start(); var bestChromosome = ga.BestChromosome as FunctionBuilderChromosome; Assert.AreEqual(0.0, bestChromosome.Fitness.Value); var actual = fitness.GetFunctionResult( bestChromosome.BuildFunction(), new FunctionBuilderInput(new double[] { 3, 4, 5 }, 60) ); Assert.AreEqual(60.0, actual); }
/// <summary> /// Initializes a new instance of the <see cref="GeneticSharp.Extensions.Mathematic.FunctionBuilderFitness"/> class. /// </summary> /// <param name="inputs">The arguments values and expected results of the function.</param> public FunctionBuilderFitness(params FunctionBuilderInput[] inputs) { m_inputs = inputs; var parametersCount = m_inputs[0].Arguments.Count; AvailableOperations = FunctionBuilderChromosome.BuildAvailableOperations(parametersCount); m_parameterNames = FunctionBuilderChromosome.GetParameterNames(parametersCount); }
public void Evaluate_AllResultsEquals_MaxFitness() { var target = new FunctionBuilderFitness( new FunctionBuilderInput(new double[] { 1 }, 1), new FunctionBuilderInput(new double[] { 2 }, 2)); var c = new FunctionBuilderChromosome(target.AvailableOperations, 2); c.ReplaceGene(0, new Gene("A")); c.ReplaceGene(1, new Gene("")); var actual = target.Evaluate(c); Assert.AreEqual(0, actual); }
public void Evaluate_InvalidFunction_WorstFitness() { var target = new FunctionBuilderFitness( new FunctionBuilderInput(new double[] { 1 }, 2), new FunctionBuilderInput(new double[] { 1 }, 3)); var c = new FunctionBuilderChromosome(target.AvailableOperations, 2); c.ReplaceGene(0, new Gene("-")); c.ReplaceGene(1, new Gene("")); var actual = target.Evaluate(c); Assert.AreEqual(double.MinValue, actual); }