Ejemplo n.º 1
0
        public void EqualToTest()
        {
            var fitness1 = new Fitness(new Fraction(1, 2));
            var fitness2 = new Fitness(new Fraction(2, 4));

            Assert.IsTrue(fitness1.CompareTo(fitness2) == 0);
        }
Ejemplo n.º 2
0
        public void GreaterThanTest()
        {
            var fitness1 = new Fitness(new Fraction(1, 3));
            var fitness2 = new Fitness(new Fraction(1, 2));

            Assert.IsTrue(fitness1.CompareTo(fitness2) < 0);
        }
        private static void SolveUnknown(int numUnknowns, Fraction[] geneSet, FnEquationDelegate[] equations,
                                         FnGenesToImputsDelegate fnGenesToImputs)
        {
            var watch  = Stopwatch.StartNew();
            var maxAge = 50;
            var window = new Window(Math.Max(1, geneSet.Length / (2 * maxAge)),
                                    Math.Max(1, geneSet.Length / 3),
                                    geneSet.Length / 2);
            var geneIndexes   = Enumerable.Range(0, numUnknowns).ToArray();
            var sortedGeneSet = geneSet.OrderBy(gene => gene).ToList();

            void FnDispaly(Chromosome <Fraction, Fitness> candidate) => Display(candidate, watch, fnGenesToImputs);
            Fitness FnGetFitness(IReadOnlyList <Fraction> genes) => GetFitness(genes, equations);
            void FnMutate(List <Fraction> genes) => Mutate(genes, sortedGeneSet, window, geneIndexes);

            var optimalFitness = new Fitness(new Fraction(0));
            var best           = Genetic <Fraction, Fitness> .GetBest(FnGetFitness, numUnknowns, optimalFitness, geneSet, FnDispaly, FnMutate,
                                                                      null, maxAge);

            Assert.IsTrue(optimalFitness.CompareTo(best.Fitness) <= 0);
            Assert.AreEqual(0.0, (float)best.Fitness.TotalDifference, 0.0001);
        }
Ejemplo n.º 4
0
        public void ReferenceEqualsTest()
        {
            var fitness = new Fitness(new Fraction(3, 2));

            Assert.IsTrue(fitness.CompareTo(fitness) == 0);
        }