Ejemplo n.º 1
0
        public void Evaluate_DiffChromosomes_DiffFitness()
        {
            var target = new EqualityFitness();

            var chromosome = new EquationChromosome(30, 4);

            chromosome.ReplaceGene(0, new Gene(0));
            chromosome.ReplaceGene(1, new Gene(7));
            chromosome.ReplaceGene(2, new Gene(-43));
            chromosome.ReplaceGene(3, new Gene(32));

            var actual = target.Evaluate(chromosome);

            Assert.Less(actual, 0);

            chromosome = new EquationChromosome(30, 4);
            chromosome.ReplaceGene(0, new Gene(17));
            chromosome.ReplaceGene(1, new Gene(7));
            chromosome.ReplaceGene(2, new Gene(-43));
            chromosome.ReplaceGene(3, new Gene(32));

            actual = target.Evaluate(chromosome);
            Assert.AreEqual(0, actual);
        }
        public void Evaluate_DiffChromosomes_DiffFitness()
        {
            // Equation A + B = 3.
            var target = new EquationSolverFitness(
                3,
                (genes) =>
            {
                return(genes.Select(g => (int)g.Value).Sum());
            });

            var chromosome = new EquationChromosome(3, 2);

            chromosome.ReplaceGene(0, new Gene(1));
            chromosome.ReplaceGene(1, new Gene(2));

            var actual = target.Evaluate(chromosome);

            Assert.AreEqual(0, actual);

            chromosome.ReplaceGene(1, new Gene(3));

            actual = target.Evaluate(chromosome);
            Assert.AreEqual(-1, actual);
        }