Beispiel #1
0
        public async Task Can_Increase_GenerationNumber_Every_Chromosomes_Stored()
        {
            var population = new Population();

            Assert.Null(population.Chromosomes);
            Assert.Equal(-1, population.GenerationNumber);
            population.Chromosomes = MockObject.CreateChromosomes(10).ToImmutableHashSet();
            Assert.Equal(0, population.GenerationNumber);

            IGeneticAlgorithm genetic = new GeneticAlgorithm(MockObject.CreateOperationStrategy());
            var number = population.GenerationNumber;
            var result = await genetic.EvolveUntil(population, s => s.EvolutionCount >= 5);

            Assert.Equal(result.EvolutionCount, population.GenerationNumber - number);
        }
Beispiel #2
0
        public async Task Can_Evolve_Until_Termination_Condition_Fulfilled_Or_Canceled_Without_Throwing()
        {
            const int delay      = 100;
            var       strategy   = MockObject.CreateOperationStrategy();
            var       population = MockObject.CreatePopulation();

            population.Chromosomes = MockObject.CreateChromosomes(10).ToImmutableHashSet();
            IGeneticAlgorithm genetic = new GeneticAlgorithm(strategy);

            using (var cts = new CancellationTokenSource())
            {
                var preEvolutionStates = genetic.EvolutionStates;
                var evolution          = genetic.TryEvolveUntil(population, _ => false, cts.Token);
                await Task.Delay(delay);

                cts.Cancel();
                var(result, succeed) = await evolution;
                Assert.False(succeed);
                var afterEvolutionStates = genetic.EvolutionStates;
                Assert.True(afterEvolutionStates.EvolutionCount > preEvolutionStates.EvolutionCount);
                Assert.True(afterEvolutionStates.EvolutionTime > preEvolutionStates.EvolutionTime);
                var differentTime = afterEvolutionStates.EvolutionTime - preEvolutionStates.EvolutionTime;
                Assert.True(differentTime >= MockObject.CreateProximateAccuracyTimeSpan(delay));
                Assert.Equal(afterEvolutionStates.EvolutionCount - preEvolutionStates.EvolutionCount,
                             result.EvolutionCount);
                Assert.Equal(afterEvolutionStates.EvolutionTime - preEvolutionStates.EvolutionTime,
                             result.EvolutionTime);
            }

            using (var cts = new CancellationTokenSource())
            {
                const int time      = 50;
                var       evolution = genetic.TryEvolveUntil(
                    population,
                    s => s.EvolutionTime >= TimeSpan.FromMilliseconds(time),
                    cts.Token);
                await Task.Delay(delay);

                var(result, succeed) = await evolution;
                Assert.True(succeed);
                Assert.True(result.EvolutionTime >= MockObject.CreateProximateAccuracyTimeSpan(time));
            }
        }
Beispiel #3
0
        public async Task Can_Evolve_Until_Cancelled_Whithout_Throwing()
        {
            const int time       = 100;
            var       population = MockObject.CreatePopulation();

            population.Chromosomes = MockObject.CreateChromosomes(10).ToImmutableHashSet();
            IGeneticAlgorithm genetic = new Core.GeneticAlgorithm(MockObject.CreateOperationStrategy());

            using (var cts = new CancellationTokenSource())
            {
                var evolution = genetic.TryEvolve(population, cts.Token);
                await Task.Delay(time);

                cts.Cancel();
                var result = await evolution;
                Assert.True(result.EvolutionTime >= MockObject.CreateProximateAccuracyTimeSpan(time));
                Assert.True(cts.IsCancellationRequested);
            }
        }
Beispiel #4
0
        public async Task Can_Evolve_Until_Cancelled()
        {
            const int time       = 100;
            var       population = MockObject.CreatePopulation();

            population.Chromosomes = MockObject.CreateChromosomes(10).ToImmutableHashSet();
            IGeneticAlgorithm genetic = new Core.GeneticAlgorithm(MockObject.CreateOperationStrategy());

            using (var cts = new CancellationTokenSource())
            {
                var evolution = genetic.Evolve(population, cts.Token);
                await Task.Delay(time);

                cts.Cancel();
                await Assert.ThrowsAnyAsync <OperationCanceledException>(() => evolution);

                Assert.True(cts.IsCancellationRequested);
            }
        }
Beispiel #5
0
 public void Throw_When_Receive_Chromosomes_Out_Of_Capacity()
 {
     {
         var population = new Population();
         var number     = population.GenerationNumber;
         Assert.Throws <SizeOutOfCapacityException>(() =>
                                                    population.Chromosomes = MockObject.CreateChromosomes(1).ToImmutableHashSet());
         Assert.Equal(number, population.GenerationNumber);
         population.Chromosomes = MockObject.CreateChromosomes(2).ToImmutableHashSet();
         Assert.Equal(number + 1, population.GenerationNumber);
     }
     {
         var population = new Population
         {
             Capacity = new PopulationCapacity(10)
         };
         var number = population.GenerationNumber;
         Assert.Throws <SizeOutOfCapacityException>(() =>
                                                    population.Chromosomes = MockObject.CreateChromosomes(9).ToImmutableHashSet());
         Assert.Equal(number, population.GenerationNumber);
         Assert.Throws <SizeOutOfCapacityException>(() =>
                                                    population.Chromosomes = MockObject.CreateChromosomes(11).ToImmutableHashSet());
         Assert.Equal(number, population.GenerationNumber);
         population.Chromosomes = MockObject.CreateChromosomes(10).ToImmutableHashSet();
         Assert.Equal(number + 1, population.GenerationNumber);
     }
     {
         var population = new Population
         {
             Capacity = new PopulationCapacity(5, 10)
         };
         var number = population.GenerationNumber;
         Assert.Throws <SizeOutOfCapacityException>(() =>
                                                    population.Chromosomes = MockObject.CreateChromosomes(4).ToImmutableHashSet());
         Assert.Equal(number, population.GenerationNumber);
         Assert.Throws <SizeOutOfCapacityException>(() =>
                                                    population.Chromosomes = MockObject.CreateChromosomes(11).ToImmutableHashSet());
         Assert.Equal(number, population.GenerationNumber);
         population.Chromosomes = MockObject.CreateChromosomes(7).ToImmutableHashSet();
         Assert.Equal(number + 1, population.GenerationNumber);
     }
 }
Beispiel #6
0
        public async Task Can_Evolve_Until_Termination_Condition_Fulfilled()
        {
            var strategy   = MockObject.CreateOperationStrategy();
            var population = MockObject.CreatePopulation();

            population.Chromosomes = MockObject.CreateChromosomes(10).ToImmutableHashSet();
            IGeneticAlgorithm genetic = new GeneticAlgorithm(strategy);
            {
                const int count             = 10;
                var       preEvolutionCount = genetic.EvolutionStates.EvolutionCount;
                var       result            = await genetic.EvolveUntil(population, s => s.EvolutionCount >= count);

                Assert.True(genetic.EvolutionStates.EvolutionCount > preEvolutionCount);
                Assert.True(result.EvolutionCount >= count);
            }
            {
                var time             = TimeSpan.FromMilliseconds(100);
                var preEvolutionTime = genetic.EvolutionStates.EvolutionTime;
                var result           = await genetic.EvolveUntil(population, s => s.EvolutionTime >= time);

                Assert.True(genetic.EvolutionStates.EvolutionTime > preEvolutionTime);
                Assert.True(result.EvolutionTime >= time);
            }
        }