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

📄 selection.cs

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

namespace GA_RPS.GAKernel
{
    class Selection
    {
        public Population[] selectOp(Population[] p)
        {
            Population[] newP = new Population[p.Length];
            int fitnessSum = 0;
		    foreach (Population c in p)
            {
			    fitnessSum += involution(c.Fitness,GAControl.SEL_SCALAR);
            }
		    double[] proportion = new double[p.Length];
            int i=0;
		    foreach (Population c in p)
            {
                proportion[i] = (double)involution(c.Fitness, GAControl.SEL_SCALAR) / (double)fitnessSum;
                i++;
		    }
            
            //String show = "";
            //for (i = 0; i < p.Length; i++)
            //{
            //    show += Convert.ToString(proportion[i]) + "\n";
            //}
            //MessageBox.Show(show);
            
            Roulette r = new Roulette();
            int[] chosenPos = r.roll(p.Length,proportion);
		    for (i=0;i<p.Length;i++)
            {
                newP[i] = p[chosenPos[i]];
		    }

            String show = "";
            for (int t = 0; t < GAControl.POP_NUM; t++)
            {
                show += "the " + t
                        + " position of the new population is "
                        + (chosenPos[t] + 1) + "\n";
            }
            MessageBox.Show(show);

            return newP;
        }

        private int involution(int origin, int power)
        {
            int ret = 1;
            for (int i = 0; i < power; i++)
            {
                ret *= origin;
            }
            return ret;
        }
    }
}

⌨️ 快捷键说明

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