}         //End_Method

        /// <summary>
        /// 二点交叉で作っていく
        /// </summary>
        /// <param name="obj1"></param>
        /// <param name="obj2"></param>
        /// <returns></returns>
        private GeneticObj[] CrossOverDoublePoint(GeneticObj obj1, GeneticObj obj2)
        {
            //次の世代のゲノム
            var childGenom1 = new int[obj1.GenomList.Length];
            var childGenom2 = new int[obj1.GenomList.Length];

            //obj1の切り抜く範囲  obj1[0:n] + obj2[n:m] + obj1[m:length]
            int range1 = G.Rnd.Next(0, childGenom1.Length - 2);
            int range2 = G.Rnd.Next(range1, childGenom1.Length - 1);
            int range3 = G.Rnd.Next(range2, childGenom1.Length);

            for (int i = 0; i < childGenom1.Length; ++i)
            {
                //0~range1
                if (i <= range1)
                {
                    childGenom1[i] = obj1.GenomList[i];
                    childGenom2[i] = obj2.GenomList[i];
                }                 //range1~range2
                else if (i <= range2)
                {
                    childGenom1[i] = obj2.GenomList[i];
                    childGenom2[i] = obj1.GenomList[i];
                }                 //range2~length
                else
                {
                    childGenom1[i] = obj1.GenomList[i];
                    childGenom2[i] = obj2.GenomList[i];
                }         //End_IfElse
            }             //End_For

            var children = new GeneticObj[2];

            children[0] = new GeneticObj(childGenom1);
            children[1] = new GeneticObj(childGenom2);
            return(children);
        }         //End_Method
        }         //End_Method

        /// <summary>
        /// 確率1/2で一様交叉
        /// </summary>
        /// <param name="obj1"></param>
        /// <param name="obj2"></param>
        /// <returns></returns>
        private GeneticObj[] CrossOver(GeneticObj obj1, GeneticObj obj2)
        {
            //次の世代のゲノム
            var childGenom = new int[obj1.GenomList.Length];

            //乱数が0ならobj1,2ならobj2の遺伝子を受け継ぐ
            for (int i = 0; i < childGenom.Length; ++i)
            {
                if (G.Rnd.Next(2) == 0)
                {
                    childGenom[i] = obj1.GenomList[i];
                }
                else
                {
                    childGenom[i] = obj2.GenomList[i];
                }
            }             //End_For

            //子オブジェクトを作成
            var child = new GeneticObj[1];

            child[0] = new GeneticObj(childGenom);
            return(child);
        }         //End_Method
Пример #3
0
        }         //End_Class

        //-----------------------publicメソッド-----------------------
        /// <summary>
        /// DeepCopy
        /// </summary>
        /// <param name="obj"></param>
        public void CopyTo(GeneticObj obj)
        {
            Array.Copy(obj.GenomList, this.GenomList, obj.GenomList.Length);
        }         //End_Method