Ejemplo n.º 1
0
        private static void Test(int length = 100)
        {
            var geneSet = new[] { '0', '1' };
            var watch   = Stopwatch.StartNew();

            void FnDisplay(Chromosome candidate) => Display(candidate, watch);
            int FnGetFitness(string genes) => GetFitness(genes);

            var optimalFitness = length;
            var best           = Genetic.GetBest(FnGetFitness, length, optimalFitness, geneSet, FnDisplay);

            Assert.AreEqual(optimalFitness, best.Fitness);
        }
        private static void GuessPassword(string target)
        {
            var watch = Stopwatch.StartNew();

            int FnGetFitness(string genes) => GetFitness(genes, target);
            void FnDisplay(Chromosome candidate) => Display(candidate, watch);

            var optimalFitness = target.Length;
            var best           = Genetic.GetBest(FnGetFitness, target.Length, optimalFitness, GeneSet, FnDisplay);

            Assert.AreEqual(target.Length, best.Fitness);
            Assert.AreEqual(target, best.Genes);
        }