コード例 #1
0
        public void Start()
        {
            if (_executor == null)
            {
                throw new Exception("Executor was not initialized");
            }

            //create the population
            IList <IChromosome> list = new List <IChromosome>();

            GeneFactory.Initialize(_config.Genes);
            for (int i = 0; i < _config.PopulationSize; i++)
            {
                list.Add(new Chromosome(true, GeneFactory.Config));
            }

            int max = _config.PopulationSizeMaximum < _config.PopulationSize ? _config.PopulationSize * 2 : _config.PopulationSizeMaximum;

            _population = new PreloadPopulation(_config.PopulationSize, max, list);
            _population.GenerationStrategy = new PerformanceGenerationStrategy();

            //create the GA itself
            var ga = new GeneticAlgorithm(_population, _fitness, new TournamentSelection(),
                                          _config.OnePointCrossover ? new OnePointCrossover() : new TwoPointCrossover(), new UniformMutation(true));

            //subscribe to events
            ga.GenerationRan       += GenerationRan;
            ga.TerminationReached  += TerminationReached;
            ga.TaskExecutor         = _executor;
            ga.Termination          = new OrTermination(new FitnessStagnationTermination(_config.StagnationGenerations), new GenerationNumberTermination(_config.Generations));
            ga.Reinsertion          = new ElitistReinsertion();
            ga.MutationProbability  = _config.MutationProbability;
            ga.CrossoverProbability = _config.CrossoverProbability;
            //run the GA
            ga.Start();
        }
コード例 #2
0
        public void Start()
        {
            _executor = new SmartThreadPoolTaskExecutor()
            {
                MinThreads = 1
            };
            _executor.MaxThreads = _config.MaxThreads > 0 ? _config.MaxThreads : 8;

            //create the population
            IList <IChromosome> list = new List <IChromosome>();
            var geneConfig           = GeneFactory.Load();

            for (int i = 0; i < _config.PopulationSize; i++)
            {
                list.Add(new Chromosome(true, geneConfig));
            }

            int max = _config.PopulationSizeMaximum < _config.PopulationSize ? _config.PopulationSize * 2 : _config.PopulationSizeMaximum;

            _population = new PreloadPopulation(_config.PopulationSize, max, list);
            _population.GenerationStrategy = new PerformanceGenerationStrategy();

            //create the GA itself
            var ga = new GeneticAlgorithm(_population, _fitness, new TournamentSelection(),
                                          _config.OnePointCrossover ? new OnePointCrossover() : new TwoPointCrossover(), new UniformMutation(true));

            //subscribe to events
            ga.GenerationRan      += GenerationRan;
            ga.TerminationReached += TerminationReached;
            ga.TaskExecutor        = _executor;
            ga.Termination         = new OrTermination(new FitnessStagnationTermination(_config.StagnationGenerations), new GenerationNumberTermination(_config.Generations));
            ga.Reinsertion         = new ElitistReinsertion();

            //run the GA
            ga.Start();
        }
コード例 #3
0
        public override IChromosome CreateNew()
        {
            var config = GeneFactory.Load();

            return(new Chromosome(false, config));
        }
コード例 #4
0
        public override Gene GenerateGene(int geneIndex)
        {
            var item = _config[geneIndex];

            return(GeneFactory.Generate(item, _isActual));
        }