Exemple #1
0
        public void SolveOneStep()
        {
            _nextPopulation = new IntegerPopulation
            {
                ChromosomesType       = _solverInfo.ChromosomesType,
                SelectionType         = _solverInfo.SelectionType,
                CrossOverType         = _solverInfo.CrossOverType,
                MutationType          = _solverInfo.MutationType,
                MutationRate          = _solverInfo.MutationRate,
                InitialPopulationSize = _solverInfo.InitialPopulationSize,
                GeneLength            = _solverInfo.GeneLength,
                GenerationNum         = _currentPopulation.GenerationNum + 1,
            };

            // 適用度の計算
            _currentPopulation.CalcAllFittness();

            // 選択
            for (int j = 0; j < _solverInfo.InitialPopulationSize; j++)
            {
                _nextPopulation.AddGene(_currentPopulation.GetGeneAt(_currentPopulation.Selection()).Chromosomes, _solverInfo.CalcFunc);
            }

            for (int i = 0; i < _solverInfo.InitialPopulationSize; i++)
            {
                // 交叉
                var newChromosomes = _currentPopulation.CrossOver();

                // 新しい遺伝子の追加
                foreach (var newChromosome in newChromosomes)
                {
                    _nextPopulation.AddGene(newChromosome, _solverInfo.CalcFunc);
                }
            }

            // 突然変異
            for (int j = 0; j < _nextPopulation.PopulationSize; j++)
            {
                _nextPopulation.Mutation(j);
            }

            // 適用度の計算
            _nextPopulation.CalcAllFittness();

            // 淘汰
            _nextPopulation.DecreasePopulation();

            _currentPopulation = _nextPopulation;
        }
Exemple #2
0
        /// <summary>
        /// 初期化
        /// </summary>
        public void Initialize()
        {
            switch (_solverInfo.ChromosomesType)
            {
            case ChromosomesType.Binary:
            case ChromosomesType.Permutaion:
                _currentPopulation = new IntegerPopulation
                {
                    ChromosomesType       = _solverInfo.ChromosomesType,
                    SelectionType         = _solverInfo.SelectionType,
                    CrossOverType         = _solverInfo.CrossOverType,
                    MutationType          = _solverInfo.MutationType,
                    MutationRate          = _solverInfo.MutationRate,
                    InitialPopulationSize = _solverInfo.InitialPopulationSize,
                    GeneLength            = _solverInfo.GeneLength,
                    CalcFunc = _solverInfo.CalcFunc,
                };
                break;
            }


            _currentPopulation.Initialize();
        }