public void Start_TplManySlowFitness_Timeout()
        {
            var taskExecutor = new TplTaskExecutor();

            taskExecutor.Timeout = TimeSpan.FromMilliseconds(1000);

            var selection  = new RouletteWheelSelection();
            var crossover  = new OnePointCrossover(1);
            var mutation   = new UniformMutation();
            var chromosome = new ChromosomeStub();
            var target     = new GeneticAlgorithm(new TplPopulation(100, 150, chromosome),
                                                  new FitnessStub()
            {
                SupportsParallel = true, ParallelSleep = 1500
            }, selection, crossover, mutation);

            target.OperatorsStrategy = new TplOperatorsStrategy();
            target.TaskExecutor      = taskExecutor;

            Assert.Catch <TimeoutException>(() =>
            {
                target.Start();
            }, "The fitness evaluation reached the 00:00:01 timeout.");

            Assert.IsFalse(target.IsRunning);
            Assert.AreEqual(GeneticAlgorithmState.Stopped, target.State);
        }
Exemplo n.º 2
0
        public void Start_AnyTaskWithException_Exception()
        {
            var pipeline = "";
            var target   = new TplTaskExecutor();

            target.Add(() =>
            {
                throw new Exception("1");
            });
            target.Add(() =>
            {
                Thread.Sleep(5);
                pipeline += "2";
            });
            target.Add(() =>
            {
                Thread.Sleep(5);
                pipeline += "3";
            });

            Assert.Catch <Exception>(() =>
            {
                target.Start();
            }, "1");
        }
Exemplo n.º 3
0
        public void Start_ManyTasksWithGreaterNumberOfThreads_ParallelExecuted()
        {
            var pipeline = "";
            var target   = new TplTaskExecutor();

            target.Add(() =>
            {
                pipeline += "1";
            });
            target.Add(() =>
            {
                Thread.Sleep(100);
                pipeline += "2";
            });
            target.Add(() =>
            {
                Thread.Sleep(10);
                pipeline += "3";
            });

            var actual = target.Start();

            Assert.IsTrue(actual);
            Assert.AreEqual("132", pipeline);
        }
Exemplo n.º 4
0
        public void Stop_Tasks_ShutdownCalled()
        {
            var pipeline = "";
            var target   = new TplTaskExecutor
            {
                Timeout = TimeSpan.FromMilliseconds(1000)
            };

            target.Add(() =>
            {
                Thread.Sleep(500);
                pipeline += "1";
            });
            target.Add(() =>
            {
                Thread.Sleep(500);
                pipeline += "2";
            });
            target.Add(() =>
            {
                Thread.Sleep(500);
                pipeline += "3";
            });

            Parallel.Invoke(
                () => target.Start(),
                () =>
            {
                Thread.Sleep(100);
                target.Stop();
            });

            Assert.IsFalse(target.IsRunning);
        }
Exemplo n.º 5
0
        public void Stop_ManyTasks_StopAll()
        {
            var pipeline = "";
            var target   = new TplTaskExecutor();

            target.Timeout = TimeSpan.FromMilliseconds(1000);

            target.Add(() =>
            {
                Thread.Sleep(5);
                pipeline += "1";
            });
            target.Add(() =>
            {
                Thread.Sleep(5);
                pipeline += "2";
            });
            target.Add(() =>
            {
                Thread.Sleep(5);
                pipeline += "3";
            });

            Parallel.Invoke(
                () => Assert.IsTrue(target.Start()),
                () =>
            {
                Thread.Sleep(100);
                target.Stop();
            });
        }
        public void Start_TplManyGenerations_Optimization()
        {
            var taskExecutor = new TplTaskExecutor();
            var selection    = new EliteSelection();
            var crossover    = new OnePointCrossover(1);
            var mutation     = new UniformMutation();
            var chromosome   = new ChromosomeStub();

            FlowAssert.IsAtLeastOneAttemptOk(20, () =>
            {
                var target = new GeneticAlgorithm(new TplPopulation(100, 150, chromosome),
                                                  new FitnessStub()
                {
                    SupportsParallel = true
                }, selection, crossover, mutation);
                target.OperatorsStrategy = new TplOperatorsStrategy();
                target.TaskExecutor      = taskExecutor;

                Assert.AreEqual(GeneticAlgorithmState.NotStarted, target.State);
                Assert.IsFalse(target.IsRunning);

                target.Start();

                Assert.AreEqual(GeneticAlgorithmState.TerminationReached, target.State);
                Assert.IsFalse(target.IsRunning);
                Assert.IsTrue(target.Population.CurrentGeneration.Chromosomes.Count >= 100);
                Assert.IsTrue(target.Population.CurrentGeneration.Chromosomes.Count <= 150);
                Assert.IsNotNull(target.Population.BestChromosome);
                Assert.IsTrue(target.Population.BestChromosome.Fitness >= 0.9);
                Assert.IsTrue(target.Population.Generations.Count > 0);
            });

            FlowAssert.IsAtLeastOneAttemptOk(20, () =>
            {
                var target = new GeneticAlgorithm(new TplPopulation(100, 150, chromosome),
                                                  new FitnessStub()
                {
                    SupportsParallel = true
                }, selection, crossover, mutation);
                target.OperatorsStrategy = new TplOperatorsStrategy();
                target.TaskExecutor      = taskExecutor;
                target.Start();
                Assert.IsTrue(target.Population.CurrentGeneration.Chromosomes.Count >= 100);
                Assert.IsTrue(target.Population.CurrentGeneration.Chromosomes.Count <= 150);
                Assert.IsNotNull(target.Population.BestChromosome);
                Assert.IsTrue(target.Population.BestChromosome.Fitness >= 0.9);
                Assert.IsTrue(target.Population.Generations.Count > 0);
            });
        }
Exemplo n.º 7
0
        public void Start_Timeout_False()
        {
            var pipeline = "1";
            var target   = new TplTaskExecutor();

            target.Timeout = TimeSpan.FromMilliseconds(2);

            target.Add(() =>
            {
                Thread.Sleep(10);
                pipeline += "2";
            });

            var actual = target.Start();

            Assert.IsFalse(actual);
        }
Exemplo n.º 8
0
        public void Start_MaxThreads1_DoNotBlockOtherThreads()
        {
            var target = new TplTaskExecutor
            {
                MinThreads = 1,
                MaxThreads = 1
            };

            target.Add(() =>
            {
            });
            target.Add(() =>
            {
                Thread.Sleep(200);
            });
            target.Add(() =>
            {
                Thread.Sleep(10);
            });


            int otherThreadCount = 0;
            var otherThread      = new System.Timers.Timer(50)
            {
                AutoReset = true
            };

            otherThread.Elapsed += (sender, arg) =>
            {
                otherThreadCount++;
            };
            otherThread.Start();

            Task.Run(() =>
            {
                target.Start();
            }).Wait();

            otherThread.Stop();
            Assert.GreaterOrEqual(otherThreadCount, 2);
        }