Esempio n. 1
0
        public void FindsSimpleSolution()
        {
            var random    = new SystemRandom(42);
            var factory   = new SimpleGenomeFactory(random);
            var selection = new BinaryTournamentSelection(random);

            Environment environment = new SimpleEnvironment();
            Ranking     ranking     = new CrowdingDistanceRanking(1);

            var evolution = new Evolution(100, 0.4, 0.4, factory, selection);
            var genomes   = evolution.Initialize();

            double score = 0;

            for (int i = 0; i < 100; i++)
            {
                // evaluate
                var population = genomes.Select(environment.Evaluate).ToList();

                // rank
                ranking.Rank(population);

                // keep score
                var best = population[0];
                Console.WriteLine(best);
                score = best.GetScore(0);

                // evolve
                genomes = evolution.Evolve(population, random);
            }

            Assert.Greater(score, 30);
        }
        public void TestSelect()
        {
            var population = new List <Genotype>()
            {
                new Genotype(new[] { 0 })
                {
                    Cost = 0
                },
                new Genotype(new[] { 1 })
                {
                    Cost = 1
                },
                new Genotype(new[] { 2 })
                {
                    Cost = 2
                },
                new Genotype(new[] { 3 })
                {
                    Cost = 3
                },
                new Genotype(new[] { 4 })
                {
                    Cost = 4
                },
            };
            var selection = new BinaryTournamentSelection(new TestNumberGenerator());

            var expected = new List <(Genotype, Genotype)>
            {
                (population[0], population[2]),
                (population[0], population[1]),
                (population[3], population[0]),
            };
            var actual = selection.SelectParents(population, 3);

            Assert.IsTrue(expected.SequenceEqual(actual));
        }
        public void FindsSimpleSolution()
        {
            var random    = new SystemRandom(42);
            var factory   = new SimpleGenomeFactory(random);
            var selection = new BinaryTournamentSelection(random);

            Environment environment = new SimpleEnvironment();
            Ranking     ranking     = new CrowdingDistanceRanking(1);

            var writer    = File.CreateText("stats.csv");
            var evolution = new ElitistEvolution(100, 20, factory, environment, 1, writer);
            var genomes   = evolution.Initialize();

            double score = 0;

            for (int i = 0; i < 100; i++)
            {
                genomes = evolution.Evolve(genomes, random);
            }

            writer.Close();

            Assert.Greater(score, 30);
        }