コード例 #1
0
ファイル: GA.cs プロジェクト: uwmyuan/ICITN
 public void Initialize(Parameter Par)
 {
     //PopulationSizeCalculate();
     PopulationSize = 100;
     if (PopulationSize >= 50)
     {
         ApplyElitism = true;
     }
     for (int i = 0; i < PopulationSize; i++)//PopulationSize为染色体数
     {
         GAChromosome newParent = new GAChromosome(Par);
         this.Initializer(ref newParent);
         newParent.FitnessCalculate();
         m_thisGeneration.Add(newParent);
     }
     TotalFitness.Clear();
     RankPopulation(m_thisGeneration);
     TotalFitnessRecord();
 }//初始化种群
コード例 #2
0
ファイル: GA.cs プロジェクト: uwmyuan/ICITN
        }//对染色体按适应度降序排序

        public void CreateNextGeneration(Parameter Par)
        {
            m_nextGeneration.Clear();
            GAChromosome bestChromo = null;

            if (this.ApplyElitism) //最优秀适应度最好是否直接选择加入到新一代群体
            {
                bestChromo = m_thisGeneration[0];
            }//取出最优染色体(已排序)
            for (int i = 0; i < this.PopulationSize; i += 2)
            {
                //Step1选择
                int iDadParent = 0;
                int iMumParent = 0;
                if (this.Selection == SelectionType.Tournment)//竞争法
                {
                    iDadParent = TournamentSelection();
                    iMumParent = TournamentSelection();
                }
                else if (this.Selection == SelectionType.Roullette)//轮赌法
                {
                    iDadParent = RoulletteSelection();
                    iMumParent = RoulletteSelection();
                }
                GAChromosome Dad    = (GAChromosome)m_thisGeneration[iDadParent];
                GAChromosome Mum    = (GAChromosome)m_thisGeneration[iMumParent];
                GAChromosome child1 = new GAChromosome(Par);
                GAChromosome child2 = new GAChromosome(Par);
                //Step2交叉
                if (m_Random.NextDouble() < this.CrossRate)
                {
                    CrossOver(Dad, Mum, ref child1, ref child2);
                }
                else
                {
                    child1 = Dad;
                    child2 = Mum;
                }
                //Step3变异
                if (m_Random.NextDouble() < this.MutationRate)
                {
                    this.Mutation(ref child1);
                    this.Mutation(ref child2);
                }
                //Step4计算适应度
                child1.FitnessCalculate();
                child2.FitnessCalculate();
                m_nextGeneration.Add(Dad);
                m_nextGeneration.Add(Mum);
                m_nextGeneration.Add(child1);
                m_nextGeneration.Add(child2);
            }
            if (null != bestChromo)
            {
                m_nextGeneration.Add(bestChromo); //最优的染色体插入子代群体中
            }
            RankPopulation(m_nextGeneration);     //按照适应度对染色体排序
            m_thisGeneration.Clear();             //用新一代替换当前群体
            for (int j = 0; j < this.PopulationSize; j++)
            {
                m_thisGeneration.Add(m_nextGeneration[j]);
            }
            TotalFitnessRecord();

            //using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\BestFitness_RUE.txt", true))
            //{
            //    file.WriteLine(m_thisGeneration[0].Fitness.ToString());
            //}
            this.GenerationNum++;//进化代计数
        }//产生下一代种群