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 this instance. /// </summary> public override void Initialize() { Console.WriteLine("Function arguments and expected result: arg1,arg2=expected result."); Console.WriteLine("Sample1: 1,2,3=6"); Console.WriteLine("Sample2: 2,3,4=24"); Console.WriteLine("When finish, type ENTER to start the GA."); m_inputs = new List<FunctionBuilderInput>(); do { var parts = Console.ReadLine().Split('='); if (parts.Length != 2) { Console.WriteLine("Max number of operations?"); m_maxOperations = Convert.ToInt32(Console.ReadLine()); break; } var arguments = parts[0].Split(','); var input = new FunctionBuilderInput( arguments.Select(a => Convert.ToDouble(a)).ToList(), Convert.ToDouble(parts[1])); m_inputs.Add(input); } while (true); m_fitness = new FunctionBuilderFitness(m_inputs.ToArray()); }
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); }