⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 crossover.cs

📁 c#编写
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;

namespace GA_RPS.GAKernel
{
    class Crossover
    {
        public Population[] crossOp(Population[] p)
        {
            Population[] newP = new Population[p.Length];
            // Random rand = new Random();
            Roulette rand = new Roulette();
            Roulette roul = new Roulette();
            roul.resetBallot(p.Length);

            /*
            bool[] ballot = new bool[p.Length];
            int chosen;
            Random r = new Random();
            for (int i = 0; i < p.Length / 2; i++)  // chose n/2 pairs of parents
            {
                // choose parents in pairs to crossover
                int[] parent =new int[2];
                for (int j = 0; j < 2; j++)
                {
                    chosen = r.Next(p.Length - i * 2 - j);
                    int count = 0;                  // none chosen ones visit count
                    int k = -1;                     // visit count
                    do
                    {
                        if (!ballot[++k]) count++;
                    } while (count < chosen+1 && k < p.Length-1);
                    ballot[k] = true;
                    parent[j] = k;
                }
             */

            for (int i = 0; i < p.Length / 2; i++)  // chose n/2 pairs of parents
            {
                // choose parents in pairs to crossover
                int[] parent = new int[2];
                for (int j = 0; j < 2; j++)
                {
                    parent[j] = roul.lot();
                }


                newP[i * 2] = new Population();
                newP[i * 2 + 1] = new Population();

                //String s = "";

                // use crossover mask to exchange gene
                for (int j = 0; j < GAControl.POP_SIZE; j++)
                {

                    int p0GA = p[parent[0]].GaArray[j];
                    int p0PL = p[parent[0]].PlayerArray[j];
                    int p1GA = p[parent[1]].GaArray[j];
                    int p1PL = p[parent[1]].PlayerArray[j];

                    int ranNum  = rand.randomize(2);
                    //s += Convert.ToString(ranNum);
                    if (ranNum == 1)
                    {
                        //System.Windows.Forms.MessageBox.Show(Convert.ToString(p[parent[1]].GaArray[j]) + "," + Convert.ToString(p[parent[1]].PlayerArray[j]));
                        newP[i * 2].addMember(p0GA,p0PL);
                        newP[i * 2 + 1].addMember(p1GA,p1PL);
                    }
                    else
                    {
                        //System.Windows.Forms.MessageBox.Show(Convert.ToString(p[parent[1]].GaArray[j])+","+Convert.ToString(p[parent[1]].PlayerArray[j]));
                        newP[i * 2].addMember(p1GA,p1PL);
                        newP[i * 2 + 1].addMember(p0GA,p0PL);
                    }
                }
                //System.Windows.Forms.MessageBox.Show(s);
            }
            return newP;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -