Exemple #1
0
        //当前种群进化
        public void Evolve(SelectionType selectionType)
        {
            Population parents = null;

            if (selectionType == SelectionType.Elite)
            {
                parents = EliteSelect(this);
            }
            else if (selectionType == SelectionType.Roulette)
            {
                parents = RouletteSelect(this);
            }
            else if (selectionType == SelectionType.Hybrid)
            {
                parents = HybridSelect(this);
            }

            //crossover 得到子女种群
            if (parents != null)
            {
                var children = Crossover(Chromosomes.Count - parents.Chromosomes.Count);
                Chromosomes.Clear();
                //将选择出来的所有父母染色体,及他们 crossover 得到的子女,作为新的种群
                Chromosomes.AddRange(parents.Chromosomes);
                Chromosomes.AddRange(children.Chromosomes);
            }

            //变异
            Mutate();
        }
 /// <summary>
 /// 初始化,设置染色体
 /// </summary>
 private static void Init()
 {
     Chromosomes.Clear();
     for (int i = 0; i < ChromosomeLength; i++)
     {
         int[,] valueLimit = new int[GenesLength, 2];
         for (int ii = 0; ii < GenesLength; ii++)
         {
             valueLimit[ii, 0] = variateInfo[ii].MinValue;
             valueLimit[ii, 1] = variateInfo[ii].MaxValue;
         }
         Chromosome chromosome = new Chromosome {
             ValueLimit = valueLimit,
         };
         chromosome.Genes = IniVariate(chromosome);
         Chromosomes.Add(chromosome);
     }
 }