Example #1
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);
        }
Example #2
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(3000, () =>
            {
                ga.Start();
            });

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

            Assert.Less(lastDistance, firstDistance);
        }
        public void Evolve_ManyGenerations_Fast()
        {
            var selection = new EliteSelection();
            var crossover = new ThreeParentCrossover();
            var mutation = new UniformMutation(true);

            var fitness = new FunctionBuilderFitness(
                new FunctionBuilderInput(
                    new double[] { 1, 2, 3 },
                    6)
                ,
                new FunctionBuilderInput(
                    new double[] { 2, 3, 4 },
                    24)
            );
            var chromosome = new FunctionBuilderChromosome(fitness.AvailableOperations, 5);

            var population = new Population(100, 200, chromosome);

            var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation);
            ga.Termination = new FitnessThresholdTermination(0);
            ga.Start();
            var bestChromosome = ga.BestChromosome as FunctionBuilderChromosome;
            Assert.AreEqual(0.0, bestChromosome.Fitness.Value);
            var actual = fitness.GetFunctionResult(
                             bestChromosome.BuildFunction(),
                             new FunctionBuilderInput(new double[] { 3, 4, 5 }, 60)
                );

            Assert.AreEqual(60.0, actual);
        }
        public void Evolve_ManyGenerations_Fast()
        {
            var selection = new EliteSelection();
            var crossover = new UniformCrossover();
            var mutation = new UniformMutation(true);
            var chromosome = new AutoConfigChromosome();
            var targetChromosome = new TspChromosome(10);
            var targetFitness = new TspFitness(10, 0, 100, 0, 100);            
            var fitness = new AutoConfigFitness(targetFitness, targetChromosome);
            fitness.PopulationMinSize = 20;
            fitness.PopulationMaxSize = 20;
            fitness.Termination = new TimeEvolvingTermination(TimeSpan.FromSeconds(5));
            
            var population = new Population(10, 10, chromosome);

            var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation);
            
            ga.TaskExecutor = new SmartThreadPoolTaskExecutor()
            {
                MinThreads = 10,
                MaxThreads = 20
            };        

            ga.Termination = new GenerationNumberTermination(2);
            ga.Start();

            Assert.NotNull(ga.BestChromosome);            
        }
        public double TrainNeuralNetwork()
        {
            var chromosome = new KasandraChromosome(_fitness.TotalNumberOfWeights);

            var population = new Population(minPopulation, maxPopulation, chromosome);
            _geneticAlgorithm = new GeneticAlgorithm(population, _fitness, new EliteSelection(), new UniformCrossover(), new UniformMutation(true));

            _geneticAlgorithm.MutationProbability = mutationRate;
            _geneticAlgorithm.CrossoverProbability = crossoverProbabilty;
            _geneticAlgorithm.Termination = new GenerationNumberTermination(maxIterations);
            _geneticAlgorithm.TerminationReached += _geneticAlgorithm_TerminationReached;
            _geneticAlgorithm.GenerationRan += _geneticAlgorithm_GenerationRan;

            _geneticAlgorithm.Start();

            return 0;
        }
Example #6
0
        static void Main(string[] args)
        {
            var selection = new EliteSelection();
            var crossover = new OnePointCrossover(0);
            var mutation = new UniformMutation(true);
            var fitness = new Issue1Fitness();
            var chromosome = new Issue1Chromosome();
            var population = new Population(50, 50, chromosome);

            var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation);
            ga.Termination = new GenerationNumberTermination(100);

            Console.WriteLine("GA running...");
            ga.Start();
            Console.WriteLine("GA done in {0} generations.", ga.GenerationsNumber);

            var bestChromosome = ga.BestChromosome as Issue1Chromosome;
            Console.WriteLine("Best solution found is X:{0}, Y:{1} with {2} fitness.", bestChromosome.X, bestChromosome.Y, bestChromosome.Fitness);
		    Console.ReadKey();
        }
Example #7
0
        public void Play2048PatternTest()
        {
            var selection  = new EliteSelection();
            var crossover  = new TwoPointCrossover();
            var mutation   = new PartialShuffleMutation();
            var fitness    = new ClosestToMillion();
            var chromosome = new Play2048Chromosome(20, new List <string>()
            {
                "up", "down", "left", "right"
            });
            var population = new Population(22, 42, chromosome);

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

            ga.Termination = new GenerationNumberTermination(10);

            Console.WriteLine("GA running...");
            ga.GenerationRan += Ga_GenerationRan;
            ga.Population.BestChromosomeChanged += Population_BestChromosomeChanged;
            ga.Start();

            Console.WriteLine((bestChromosome as Play2048Chromosome).Pattern);
            Console.WriteLine("Best solution found has {0} fitness was born in generation {1}", ga.BestChromosome.Fitness, bestGen);
        }
Example #8
0
        static void Main(string[] args)
        {
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WriteLine("GeneticSharp - ConsoleApp");
            Console.ResetColor();
            Console.WriteLine("Select the sample:");
            Console.WriteLine("1) TSP (Travelling Salesman Problem)");
            Console.WriteLine("2) Ghostwriter");
            var sampleNumber = Console.ReadLine();
            ISampleController sampleController = null;

            switch (sampleNumber)
            {
                case "1":
                    sampleController = new TspSampleController(20);
                    break;

                case "2":
                    sampleController = new GhostwriterSampleController();
                    break;

                default:
                    return;
            }

            var selection = new EliteSelection();
            var crossover = new UniformCrossover();
            var mutation = new UniformMutation(true);
            var fitness = sampleController.CreateFitness();
            var population = new Population(50, 70, sampleController.CreateChromosome());

            var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation);
            ga.MutationProbability = 0.4f;
            ga.Termination = new FitnessStagnationTermination(100);

            ga.TaskExecutor = new SmartThreadPoolTaskExecutor()
            {
                MinThreads = 25,
                MaxThreads = 50
            };

            ga.GenerationRan += delegate
            {
                Console.CursorLeft = 0;
                Console.CursorTop = 5;

                var bestChromosome = ga.Population.BestChromosome;
                Console.WriteLine("Generations: {0}", ga.Population.GenerationsNumber);
                Console.WriteLine("Fitness: {0:n4}", bestChromosome.Fitness);
                Console.WriteLine("Time: {0}", ga.TimeEvolving);
                sampleController.Draw(bestChromosome);
            };

            try
            {
                ga.Start();
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine();
                Console.WriteLine("Error: {0}", ex.Message);
                Console.ResetColor();
                Console.ReadKey();
                return;
            }

            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WriteLine();
            Console.WriteLine("Evolved.");
            Console.ResetColor();
            Console.ReadKey();
        }
        public double Evaluate(IChromosome chromosome)
        {
            var autoConfigChromosome = chromosome as AutoConfigChromosome;
            var selection = autoConfigChromosome.Selection;
            var crossover = autoConfigChromosome.Crossover;
            var mutation = autoConfigChromosome.Mutation;
            var population = new Population(PopulationMinSize, PopulationMaxSize, m_targetChromosome);

            var ga = new GeneticAlgorithm(population, m_targetFitness, selection, crossover, mutation);
            ga.Termination = Termination;
            ga.TaskExecutor = TaskExecutor;

            try
            {
                ga.Start();
            }
            catch (Exception)
            {
                return 0;
            }

            return ga.BestChromosome.Fitness.Value;
        }
        private static void Run()
        {
            Console.SetError(TextWriter.Null);
            Console.Clear();
            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WriteLine("GeneticSharp - ConsoleApp");
            Console.ResetColor();
            Console.WriteLine("Select the sample:");

            var sampleNames = TypeHelper.GetDisplayNamesByInterface<ISampleController>();

            for (int i = 0; i < sampleNames.Count; i++)
            {
                Console.WriteLine("{0}) {1}", i + 1, sampleNames[i]);
            }

            int sampleNumber = 0;
            string selectedSampleName = string.Empty;

            try
            {
                sampleNumber = Convert.ToInt32(Console.ReadLine());
                selectedSampleName = sampleNames[sampleNumber - 1];
            }
            catch (Exception)
            {
                Console.WriteLine("Invalid option.");
            }

            var sampleController = TypeHelper.CreateInstanceByName<ISampleController>(selectedSampleName);
            DrawSampleName(selectedSampleName);
            sampleController.Initialize();

            Console.WriteLine("Starting...");

            var selection = sampleController.CreateSelection();
            var crossover = sampleController.CreateCrossover();
            var mutation = sampleController.CreateMutation();
            var fitness = sampleController.CreateFitness();
            var population = new Population(100, 200, sampleController.CreateChromosome());
            population.GenerationStrategy = new PerformanceGenerationStrategy();

            var ga = new GeneticAlgorithm(population, fitness, selection, crossover, mutation);
            ga.Termination = sampleController.CreateTermination();            

            var terminationName = ga.Termination.GetType().Name;

            ga.GenerationRan += delegate
            {
                DrawSampleName(selectedSampleName);

                var bestChromosome = ga.Population.BestChromosome;
                Console.WriteLine("Termination: {0}", terminationName);
                Console.WriteLine("Generations: {0}", ga.Population.GenerationsNumber);
                Console.WriteLine("Fitness: {0,10}", bestChromosome.Fitness);
                Console.WriteLine("Time: {0}", ga.TimeEvolving);
                sampleController.Draw(bestChromosome);
            };

            try
            {
                sampleController.ConfigGA(ga);
                ga.Start();
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.DarkRed;
                Console.WriteLine();
                Console.WriteLine("Error: {0}", ex.Message);
                Console.ResetColor();
                Console.ReadKey();
                return;
            }

            Console.ForegroundColor = ConsoleColor.DarkGreen;
            Console.WriteLine();
            Console.WriteLine("Evolved.");
            Console.ResetColor();
            Console.ReadKey();
            Run();
        }
Example #11
0
 // start the algorithm running/learning
 public void Run()
 {
     ga.Start();
 }
 private string Start(GeneticAlgorithm ga)
 {
     ga.Start();
     return(string.Empty);
 }
        // start the algorithm running/learning
        public void Run()
        {
            ga.Start();

            ga.GenerationRan += Ga_GenerationRan;
        }