コード例 #1
0
ファイル: QwopGA.cs プロジェクト: bpgeck/QwopStyle
        public void GenerateNewMembers()
        {
            SelectMembers();
            OldRunners = new QwopRunner[popSize]; // deep copy that shit
            for (int i = 0; i < popSize; i++)
            {
                OldRunners[i] = Runners[i];
            }

            Random            rn        = new Random();
            List <QwopRunner> available = Runners.ToList <QwopRunner>();

            QwopRunner[] newRunners = new QwopRunner[popSize];
            for (int i = 0; i < popSize; i += 2)
            {
                // choose the two mates based on fittest local neighbors (cellular GA)
                QwopRunner mateOne      = available[0];
                QwopRunner mateTwo      = available[1];
                int        mateTwoIndex = 1;

                //foreach (QwopRunner r in available) Console.WriteLine(r.ToString());
                for (int j = 2; j <= numNeighbors && j < available.Count; j++)
                {
                    if (available[j].Fitness > mateTwo.Fitness)
                    {
                        mateTwo      = available[j];
                        mateTwoIndex = j;
                    }
                }
                available.RemoveAt(0);
                available.RemoveAt(mateTwoIndex - 1); // every member can only have one mate, so remove the used members

                // time to perform some crossover
                int    spliceLocOne  = rn.Next(0, mateOne.InitSeq.Length);
                int    spliceLocTwo  = rn.Next(0, mateTwo.InitSeq.Length);
                string newInitSeqOne = mateOne.InitSeq.Substring(0, spliceLocOne) + mateTwo.InitSeq.Substring(spliceLocTwo, mateTwo.InitSeq.Length - spliceLocTwo);
                string newInitSeqTwo = mateTwo.InitSeq.Substring(0, spliceLocTwo) + mateOne.InitSeq.Substring(spliceLocOne, mateOne.InitSeq.Length - spliceLocOne);

                spliceLocOne = rn.Next(0, mateOne.ComSeq.Length);
                spliceLocTwo = rn.Next(0, mateTwo.ComSeq.Length);
                string newComSeqOne = mateOne.ComSeq.Substring(0, spliceLocOne) + mateTwo.ComSeq.Substring(spliceLocTwo, mateTwo.ComSeq.Length - spliceLocTwo);
                string newComSeqTwo = mateTwo.ComSeq.Substring(0, spliceLocTwo) + mateOne.ComSeq.Substring(spliceLocOne, mateOne.ComSeq.Length - spliceLocOne);

                // store the new population members accordingly
                newRunners[i]     = new QwopRunner(newInitSeqOne, newComSeqOne, mateOne, mateTwo);
                newRunners[i + 1] = new QwopRunner(newInitSeqTwo, newComSeqTwo, mateOne, mateTwo);

                if (rn.NextDouble() < mutationRate)
                {
                    newRunners[i].InitSeq = MutateCode(newRunners[i].InitSeq);
                }
                if (rn.NextDouble() < mutationRate)
                {
                    newRunners[i].ComSeq = MutateCode(newRunners[i].ComSeq);
                }
                if (rn.NextDouble() < mutationRate)
                {
                    newRunners[i + 1].InitSeq = MutateCode(newRunners[i].InitSeq);
                }
                if (rn.NextDouble() < mutationRate)
                {
                    newRunners[i + 1].ComSeq = MutateCode(newRunners[i].ComSeq);
                }

                // Console.WriteLine(mateOne.ToString() + " + \n" + mateTwo.ToString() + " = \n" + newRunners[i].ToString() + " and \n" + newRunners[i + 1].ToString());
            }

            Runners = newRunners;
        }