Example #1
0
        public void Evolve_ManyGenerations_Fast()
        {
            int numberOfCities = 40;
            var selection      = new EliteSelection();
            var crossover      = new OrderedCrossover();
            var mutation       = new TworsMutation();
            var chromosome     = new TspChromosome(numberOfCities);
            var fitness        = new TspFitness(numberOfCities, 0, 1000, 0, 1000);

            var population = new Population(40, 40, chromosome);

            var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation);

            ga.Start();
            var firstDistance = ((TspChromosome)ga.Population.BestChromosome).Distance;

            ga.Termination = new GenerationNumberTermination(1001);

            TimeAssert.LessThan(100000, () =>
            {
                ga.Start();
            });

            var lastDistance = ((TspChromosome)ga.Population.BestChromosome).Distance;

            Assert.Less(lastDistance, firstDistance);
        }
Example #2
0
    public async Task Test4ThensAsync500MsEach2000MsTotal()
    {
        // Start the runner asynchronously.
        Task t = Runner.Run(S).Then(S, S, S).Async();

        // Take a lap right away. This should be only a few milliseconds in
        // if the runner tasks really started asynchronously.
        Laps.Add(Lapper.Lap());
        // Wait for all of the runner tasks to finish.
        await t.ConfigureAwait(false);

        Lapper.Stop();
        // There should be 5 laps; one that we took right away and one each per runner task.
        Assert.Equal(5, Laps.Count);
        // The first task should have been lapped right away, a few milliseconds after starting.
        // Let's give a 500% epsilon since the expected time is so short.
        TimeAssert.DeltaEquals(TimeSpan.FromMilliseconds(10), Lapper.Laps[0], 5);
        // The rest should each take about 500ms.
        foreach (var lap in Laps.Skip(1))
        {
            TimeAssert.DeltaEquals(TimeSpan.FromMilliseconds(500), lap, 0.1);
        }
        // ... And the total time should have been about 4 x 500ms = 2000ms.
        TimeAssert.DeltaEquals(TimeSpan.FromMilliseconds(2000), Lapper.Elapsed, 0.1);
    }
Example #3
0
    public async Task Test4AndsAsync500MsEach500MsTotal()
    {
        // Start the runner asynchronously.
        Task t = Runner.Run(S).And(S, S, S).Async();

        // Take a lap right away. This should be only a few milliseconds in
        // if the runner tasks really started asynchronously.
        Laps.Add(Lapper.Lap());
        // Wait for the runner to finish.
        await t.ConfigureAwait(false);

        Lapper.Stop();
        // The async lap (first lap) should have been only a few ms after its stopwatch started.
        TimeAssert.EpsilonEquals(TimeSpan.FromMilliseconds(25), Laps[0], TimeSpan.FromMilliseconds(50));
        // We should have one lap per task run.
        Assert.Equal(5, Laps.Count);
        // Each lap should be about 500ms...
        // Skip the first lap because it was the one we took right away.
        foreach (TimeSpan lap in Laps.Skip(1))
        {
            TimeAssert.DeltaEquals(TimeSpan.FromMilliseconds(500), lap, 0.1);
        }
        // And the elapsed time should be 500ms since the tasks were all run in parallel.
        TimeAssert.DeltaEquals(TimeSpan.FromMilliseconds(500), Lapper.Elapsed, 0.1);
    }
Example #4
0
 public void Test4ThensSync500MsEach2000MsTotal()
 {
     // Run the four runner tasks synchronously.
     Runner.Run(S).Then(S, S, S).Sync();
     // Stop the timer after the four tasks have finished.
     Lapper.Stop();
     // We should have four laps since we ran four tasks.
     Assert.Equal(4, Laps.Count);
     // Each lap should have been about 500ms...
     foreach (var lap in Laps)
     {
         TimeAssert.DeltaEquals(TimeSpan.FromMilliseconds(500), lap, 0.1);
     }
     // ... And the total time should have been 4 x 500ms = 2000ms.
     TimeAssert.DeltaEquals(TimeSpan.FromMilliseconds(2000), Lapper.Elapsed, 0.1);
 }
Example #5
0
 public void Test4AndsSync500MsEach500MsTotal()
 {
     // Execute the runner synchronously.
     Runner.Run(S).And(S, S, S).Sync();
     // Take a lap once the runner is finished.
     Lapper.Stop();
     // We should have one lap per task run.
     Assert.Equal(4, Laps.Count);
     // Each lap should be about 500ms...
     foreach (TimeSpan lap in Laps)
     {
         TimeAssert.DeltaEquals(TimeSpan.FromMilliseconds(500), lap, 0.1);
     }
     // ... And the total time should also be about 500ms since we ran all four tasks in parallel.
     TimeAssert.DeltaEquals(TimeSpan.FromMilliseconds(500), Lapper.Elapsed, 0.1);
 }
Example #6
0
        public void Start_NotParallelManyGenerations_Fast()
        {
            var selection  = new EliteSelection();
            var crossover  = new OnePointCrossover(2);
            var mutation   = new UniformMutation();
            var chromosome = new ChromosomeStub();
            var target     = new GeneticAlgorithm(new Population(100, 199, chromosome),
                                                  new FitnessStub()
            {
                SupportsParallel = false
            }, selection, crossover, mutation);

            target.Termination = new GenerationNumberTermination(100);

            TimeAssert.LessThan(300, () => {
                target.Start();
            });

            Assert.AreEqual(100, target.Population.Generations.Count);
            Assert.Greater(target.TimeEvolving.TotalMilliseconds, 1);
        }
Example #7
0
        public void Evolve_ManyGenerations_Fast()
        {
            int movesAhead = 10;
            int boardSize  = 10;
            var selection  = new EliteSelection();
            var crossover  = new OrderedCrossover();
            var mutation   = new TworsMutation();
            var chromosome = new CheckersChromosome(movesAhead, boardSize);
            var fitness    = new CheckersFitness(new CheckersBoard(boardSize));

            var population = new Population(40, 40, chromosome);

            var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation);

            ga.GenerationRan += delegate
            {
                if (ga.Population.GenerationsNumber % 100 == 0)
                {
                    fitness.Update(ga.Population.BestChromosome as CheckersChromosome);
                }
            };

            ga.Start();
            var firstFitness = ((CheckersChromosome)ga.Population.BestChromosome).Fitness;

            ga.Termination = new GenerationNumberTermination(2001);

            TimeAssert.LessThan(100000, () =>
            {
                ga.Start();
            });

            var lastFitness = ((CheckersChromosome)ga.Population.BestChromosome).Fitness;

            Assert.LessOrEqual(firstFitness, lastFitness);
        }
        public void Cross_ParentsWith8Genes_Cross()
        {
            var target = new PartiallyMappedCrossover();

            // 1 2 3 4 5 6 7 8
            var chromosome1 = MockRepository.GenerateStub <ChromosomeBase>(8);

            chromosome1.ReplaceGenes(0, new Gene[] {
                new Gene(1),
                new Gene(2),
                new Gene(3),
                new Gene(4),
                new Gene(5),
                new Gene(6),
                new Gene(7),
                new Gene(8)
            });
            chromosome1.Expect(c => c.CreateNew()).Return(MockRepository.GenerateStub <ChromosomeBase>(8));

            // 3 7 5 1 6 8 2 4
            var chromosome2 = MockRepository.GenerateStub <ChromosomeBase>(8);

            chromosome2.ReplaceGenes(0, new Gene[]
            {
                new Gene(3),
                new Gene(7),
                new Gene(5),
                new Gene(1),
                new Gene(6),
                new Gene(8),
                new Gene(2),
                new Gene(4)
            });
            chromosome2.Expect(c => c.CreateNew()).Return(MockRepository.GenerateStub <ChromosomeBase>(8));

            var rnd = MockRepository.GenerateMock <IRandomization>();

            rnd.Expect(r => r.GetUniqueInts(2, 0, 8)).Return(new int[] { 3, 5 });
            RandomizationProvider.Current = rnd;

            IList <IChromosome> actual = null;;

            TimeAssert.LessThan(30, () =>
            {
                actual = target.Cross(new List <IChromosome>()
                {
                    chromosome1, chromosome2
                });
            });

            Assert.AreEqual(2, actual.Count);
            Assert.AreEqual(8, actual[0].Length);
            Assert.AreEqual(8, actual[1].Length);

            //Assert.AreEqual(8, actual[0].GetGenes().Distinct().Count());
            //Assert.AreEqual(8, actual[1].GetGenes().Distinct().Count());

            // offspring 1: (4 2 3 1 6 8 7 5)
            Assert.AreEqual(4, actual[0].GetGene(0).Value);
            Assert.AreEqual(2, actual[0].GetGene(1).Value);
            Assert.AreEqual(3, actual[0].GetGene(2).Value);
            Assert.AreEqual(1, actual[0].GetGene(3).Value);
            Assert.AreEqual(6, actual[0].GetGene(4).Value);
            Assert.AreEqual(8, actual[0].GetGene(5).Value);
            Assert.AreEqual(7, actual[0].GetGene(6).Value);
            Assert.AreEqual(5, actual[0].GetGene(7).Value);

            //          // offspring 2: (3 7 8 4 5 6 2 1)
            Assert.AreEqual(3, actual[1].GetGene(0).Value);
            Assert.AreEqual(7, actual[1].GetGene(1).Value);
            Assert.AreEqual(8, actual[1].GetGene(2).Value);
            Assert.AreEqual(4, actual[1].GetGene(3).Value);
            Assert.AreEqual(5, actual[1].GetGene(4).Value);
            Assert.AreEqual(6, actual[1].GetGene(5).Value);
            Assert.AreEqual(2, actual[1].GetGene(6).Value);
            Assert.AreEqual(1, actual[1].GetGene(7).Value);
        }
Example #9
0
        public void Cross_ParentsWith8Genes_Cross()
        {
            var target = new PositionBasedCrossover();

            // 1 2 3 4 5 6 7 8
            var chromosome1 = MockRepository.GenerateStub <ChromosomeBase>(8);

            chromosome1.ReplaceGenes(0, new Gene[] {
                new Gene(1),
                new Gene(2),
                new Gene(3),
                new Gene(4),
                new Gene(5),
                new Gene(6),
                new Gene(7),
                new Gene(8)
            });
            chromosome1.Expect(c => c.CreateNew()).Return(MockRepository.GenerateStub <ChromosomeBase>(8));

            // 2 4 6 8 7 5 3 1
            var chromosome2 = MockRepository.GenerateStub <ChromosomeBase>(8);

            chromosome2.ReplaceGenes(0, new Gene[]
            {
                new Gene(2),
                new Gene(4),
                new Gene(6),
                new Gene(8),
                new Gene(7),
                new Gene(5),
                new Gene(3),
                new Gene(1)
            });
            chromosome2.Expect(c => c.CreateNew()).Return(MockRepository.GenerateStub <ChromosomeBase>(8));

            // Child one: 1 4 6 2 3 5 7 8
            // Child two: 4 2 3 8 7 6 5 1
            var rnd = MockRepository.GenerateMock <IRandomization>();

            rnd.Expect(r => r.GetInt(1, 7)).Return(3);
            rnd.Expect(r => r.GetUniqueInts(3, 0, 8)).Return(new int[] { 1, 2, 5 });
            RandomizationProvider.Current = rnd;

            IList <IChromosome> actual = null;;

            TimeAssert.LessThan(40, () =>
            {
                actual = target.Cross(new List <IChromosome>()
                {
                    chromosome1, chromosome2
                });
            });

            Assert.AreEqual(2, actual.Count);
            var childOne = actual [0];
            var childTwo = actual [1];

            Assert.AreEqual(8, childOne.Length);
            Assert.AreEqual(8, childTwo.Length);

            Assert.AreEqual(8, childOne.GetGenes().Distinct().Count());
            Assert.AreEqual(8, childTwo.GetGenes().Distinct().Count());

            Assert.AreEqual(1, childOne.GetGene(0).Value);
            Assert.AreEqual(4, childOne.GetGene(1).Value);
            Assert.AreEqual(6, childOne.GetGene(2).Value);
            Assert.AreEqual(2, childOne.GetGene(3).Value);
            Assert.AreEqual(3, childOne.GetGene(4).Value);
            Assert.AreEqual(5, childOne.GetGene(5).Value);
            Assert.AreEqual(7, childOne.GetGene(6).Value);
            Assert.AreEqual(8, childOne.GetGene(7).Value);

            Assert.AreEqual(4, childTwo.GetGene(0).Value);
            Assert.AreEqual(2, childTwo.GetGene(1).Value);
            Assert.AreEqual(3, childTwo.GetGene(2).Value);
            Assert.AreEqual(8, childTwo.GetGene(3).Value);
            Assert.AreEqual(7, childTwo.GetGene(4).Value);
            Assert.AreEqual(6, childTwo.GetGene(5).Value);
            Assert.AreEqual(5, childTwo.GetGene(6).Value);
            Assert.AreEqual(1, childTwo.GetGene(7).Value);
        }
Example #10
0
        public void Cross_ParentsWith10Genes_Cross()
        {
            var target = new OrderedCrossover();

            // 8 4 7 3 6 2 5 1 9 0
            var chromosome1 = MockRepository.GenerateStub <ChromosomeBase>(10);

            chromosome1.ReplaceGenes(0, new Gene[] {
                new Gene(8),
                new Gene(4),
                new Gene(7),
                new Gene(3),
                new Gene(6),
                new Gene(2),
                new Gene(5),
                new Gene(1),
                new Gene(9),
                new Gene(0)
            });
            chromosome1.Expect(c => c.CreateNew()).Return(MockRepository.GenerateStub <ChromosomeBase>(10));

            // 0 1 2 3 4 5 6 7 8 9
            var chromosome2 = MockRepository.GenerateStub <ChromosomeBase>(10);

            chromosome2.ReplaceGenes(0, new Gene[]
            {
                new Gene(0),
                new Gene(1),
                new Gene(2),
                new Gene(3),
                new Gene(4),
                new Gene(5),
                new Gene(6),
                new Gene(7),
                new Gene(8),
                new Gene(9),
            });
            chromosome2.Expect(c => c.CreateNew()).Return(MockRepository.GenerateStub <ChromosomeBase>(10));

            // Child one: 0 4 7 3 6 2 5 1 8 9
            // Child two: 8 2 1 3 4 5 6 7 9 0
            var rnd = MockRepository.GenerateMock <IRandomization>();

            rnd.Expect(r => r.GetUniqueInts(2, 0, 10)).Return(new int[] { 7, 3 });
            RandomizationProvider.Current = rnd;

            IList <IChromosome> actual = null;;

            TimeAssert.LessThan(30, () => {
                actual = target.Cross(new List <IChromosome> ()
                {
                    chromosome1, chromosome2
                });
            });

            Assert.AreEqual(2, actual.Count);
            Assert.AreEqual(10, actual[0].Length);
            Assert.AreEqual(10, actual[1].Length);

            Assert.AreEqual(10, actual[0].GetGenes().Distinct().Count());
            Assert.AreEqual(10, actual[1].GetGenes().Distinct().Count());

            Assert.AreEqual(0, actual[0].GetGene(0).Value);
            Assert.AreEqual(4, actual[0].GetGene(1).Value);
            Assert.AreEqual(7, actual[0].GetGene(2).Value);
            Assert.AreEqual(3, actual[0].GetGene(3).Value);
            Assert.AreEqual(6, actual[0].GetGene(4).Value);
            Assert.AreEqual(2, actual[0].GetGene(5).Value);
            Assert.AreEqual(5, actual[0].GetGene(6).Value);
            Assert.AreEqual(1, actual[0].GetGene(7).Value);
            Assert.AreEqual(8, actual[0].GetGene(8).Value);
            Assert.AreEqual(9, actual[0].GetGene(9).Value);


            Assert.AreEqual(8, actual[1].GetGene(0).Value);
            Assert.AreEqual(2, actual[1].GetGene(1).Value);
            Assert.AreEqual(1, actual[1].GetGene(2).Value);
            Assert.AreEqual(3, actual[1].GetGene(3).Value);
            Assert.AreEqual(4, actual[1].GetGene(4).Value);
            Assert.AreEqual(5, actual[1].GetGene(5).Value);
            Assert.AreEqual(6, actual[1].GetGene(6).Value);
            Assert.AreEqual(7, actual[1].GetGene(7).Value);
            Assert.AreEqual(9, actual[1].GetGene(8).Value);
            Assert.AreEqual(0, actual[1].GetGene(9).Value);
        }
        public void Cross_ParentsWith6Genes_Cross()
        {
            var target = new PositionBasedCrossover();

            // 1 5 4 0 3 2
            var chromosome1 = MockRepository.GenerateStub <ChromosomeBase>(6);

            chromosome1.ReplaceGenes(0, new Gene[] {
                new Gene(1),
                new Gene(5),
                new Gene(4),
                new Gene(0),
                new Gene(3),
                new Gene(2)
            });
            chromosome1.Expect(c => c.CreateNew()).Return(MockRepository.GenerateStub <ChromosomeBase>(6));

            // 2 3 5 0 1 4
            var chromosome2 = MockRepository.GenerateStub <ChromosomeBase>(6);

            chromosome2.ReplaceGenes(0, new Gene[]
            {
                new Gene(2),
                new Gene(3),
                new Gene(5),
                new Gene(0),
                new Gene(1),
                new Gene(4)
            });
            chromosome2.Expect(c => c.CreateNew()).Return(MockRepository.GenerateStub <ChromosomeBase>(6));

            // Child one: 4 3 5 0 1 2
            // Child two: 2 5 4 0 3 1
            var rnd = MockRepository.GenerateMock <IRandomization>();

            rnd.Expect(r => r.GetInt(1, 5)).Return(3);
            rnd.Expect(r => r.GetUniqueInts(3, 0, 6)).Return(new int[] { 2, 4, 3 });
            RandomizationProvider.Current = rnd;

            IList <IChromosome> actual = null;;

            TimeAssert.LessThan(40, () =>
            {
                actual = target.Cross(new List <IChromosome>()
                {
                    chromosome1, chromosome2
                });
            });

            Assert.AreEqual(2, actual.Count);
            var childOne = actual[0];
            var childTwo = actual[1];

            Assert.AreEqual(6, childOne.Length);
            Assert.AreEqual(6, childTwo.Length);

            Assert.AreEqual(6, childOne.GetGenes().Distinct().Count());
            Assert.AreEqual(6, childTwo.GetGenes().Distinct().Count());

            Assert.AreEqual(4, childOne.GetGene(0).Value);
            Assert.AreEqual(3, childOne.GetGene(1).Value);
            Assert.AreEqual(5, childOne.GetGene(2).Value);
            Assert.AreEqual(0, childOne.GetGene(3).Value);
            Assert.AreEqual(1, childOne.GetGene(4).Value);
            Assert.AreEqual(2, childOne.GetGene(5).Value);

            Assert.AreEqual(2, childTwo.GetGene(0).Value);
            Assert.AreEqual(5, childTwo.GetGene(1).Value);
            Assert.AreEqual(4, childTwo.GetGene(2).Value);
            Assert.AreEqual(0, childTwo.GetGene(3).Value);
            Assert.AreEqual(3, childTwo.GetGene(4).Value);
            Assert.AreEqual(1, childTwo.GetGene(5).Value);
        }
Example #12
0
 public void TestThenDelaySync100MsEach500MsTotal()
 {
     Runner.Run(100).Then(100).Then(100).Then(100).Then(100).Sync();
     Lapper.Stop();
     TimeAssert.DeltaEquals(TimeSpan.FromMilliseconds(500), Lapper.Elapsed, 0.25);
 }