Пример #1
0
 /// <summary>
 /// 概率进行个体的变异
 /// </summary>
 private void Mutation()
 {
     //概率执行变异
     ProbabilityExcute.Excute(mutationRate, delegate()
     {
         if (nextGenomeList.Count <= 1)
         {
             return;
         }
         nextGenomeList[ProbabilityExcute.globleRand.Next(1, nextGenomeList.Count)].Mutate();
     });
 }
Пример #2
0
        /// <summary>
        /// 繁衍子代
        /// </summary>
        private void Breed()
        {
            //概率执行繁衍
            ProbabilityExcute.Excute(crossoverRate, delegate()
            {
                //随机两个父代
                Genome parent1 = GenomeList[ProbabilityExcute.globleRand.Next(0, GenomeList.Count / 2)];

                Genome parent2 = GenomeList[ProbabilityExcute.globleRand.Next(0, GenomeList.Count / 2)];

                //生成子代,并将子代填充到容器中
                Genome[] children = parent1.Crossover(parent2);

                foreach (Genome child in children)
                {
                    nextGenomeList.Add(child);
                }
            });
        }
Пример #3
0
        /// <summary>
        /// 创建基因用的函数
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        private Point[] createGensHandle(int count)
        {
            List <Point> returnList = new List <Point>();
            Point        point      = Conf.NullPoint;

            for (int i = 0; i < count; i++)
            {
                while (true)
                {
                    point = ProbabilityExcute.CreateRandGene();

                    //如果随机的棋子为空,没有重复生成点坐标,并周围1个单元格内有其他棋子.则停止循环
                    if (!returnList.Contains(point) && Chess.Find(point).Type == ChessType.NULL && checkPoint(point, 2))
                    {
                        returnList.Add(point);
                        break;
                    }
                }
            }
            return(returnList.ToArray());
        }
Пример #4
0
        /// <summary>
        /// 生成选择遗传的概率,并采用"轮盘赌"模型执行选择操作
        /// 分配更大的"可能被复制"给适应性更高的个体
        /// </summary>
        private void Selection()
        {
            //更新整个种群的适应度和
            UpdateTotalFitness();
            //创建下一代种群容器
            nextGenomeList = new List <Genome>();

            ////最优个体保存原则
            //nextGenomeList.Add(GenomeList[GenomeList.Count - 1]);

            //更新每个个体被选中的概率---排除个体最优
            for (int i = 0; i < GenomeList.Count; i++)
            {
                //概率执行复制操作
                ProbabilityExcute.Excute((Math.Abs(GenomeList[i].Fitness) / this.totalFitness), delegate()
                {
                    //复制到下一代
                    nextGenomeList.Add(GenomeList[i]);
                });
            }
        }