public void PerfectKnowledgeTest()
        {
            var minGenes = 10;
            var maxGenes = 20;
            var geneset  = CreateGeneSet();
            var watch    = Stopwatch.StartNew();

            void FnDisplay(Chromosome <Rule, Fitness> candidate, int?length) =>
            Display(candidate, watch);

            Fitness FnGetFitness(IReadOnlyList <Rule> genes) =>
            GetFitness(genes);

            var mutationRoundCounts = new List <int> {
                1
            };

            var mutationOperators = new FnMutateDelegate[]
            {
                genes => MutateAdd(genes, geneset),
                genes => MutateReplace(genes, geneset),
                MutateRemove,
                MutateSwapAdjacent,
                MutateMove,
            };

            void FnMutate(List <Rule> genes) =>
            Mutate(genes, FnGetFitness, mutationOperators, mutationRoundCounts);

            List <Rule> FnCrossover(IReadOnlyList <Rule> parent, IReadOnlyList <Rule> doner)
            {
                var child = parent.Take(parent.Count / 2).Concat(doner.Skip(doner.Count / 2)).ToList();

                FnMutate(child);
                return(child);
            }

            List <Rule> FnCreate() =>
            Rand.RandomSample(geneset, Rand.Random.Next(minGenes, maxGenes));

            var optimalFitness = new Fitness(620, 120, 0, 11);

            var best = Genetic <Rule, Fitness> .GetBest(FnGetFitness, minGenes, optimalFitness, null, FnDisplay, FnMutate,
                                                        FnCreate, 500, 20, FnCrossover);

            Assert.IsTrue(optimalFitness.CompareTo(best.Fitness) <= 0);
        }
Пример #2
0
        public void FitnessToStringTest()
        {
            var fitness = new Fitness(10, 20, 30, 100);

            Assert.AreEqual("50.0% Losses (30), 33.3% Ties (20), 16.7% Wins (10), 100 rules", fitness.ToString());
        }