天天看點

C# GEP基因化程式設計

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Diagnostics;

using System.Collections;

namespace GEP程式設計

{

    class Program

    {

        public static void Main(string[] args)

        {

            Stopwatch wp = new Stopwatch();

            wp.Start();

            //建立A-F所對應的0-5索引數字

            Hashtable wn = new Hashtable();

            wn.Add('A', 0);

            wn.Add('B', 1);

            wn.Add('C', 2);

            wn.Add('D', 3);

            wn.Add('E', 4);

            wn.Add('F', 5);

            //将1-6轉化成A-F的字母

            Hashtable nw = new Hashtable();

            nw.Add(1, 'A');

            nw.Add(2, 'B');

            nw.Add(3, 'C');

            nw.Add(4, 'D');

            nw.Add(5, 'E');

            nw.Add(6, 'F');

            //将字元‘1’-‘6’轉化成對應的1-6

            Hashtable hsCN = new Hashtable();

            hsCN.Add('1', 1);

            hsCN.Add('2', 2);

            hsCN.Add('3', 3);

            hsCN.Add('4', 4);

            hsCN.Add('5', 5);

            hsCN.Add('6', 6);

            //PrintHashTable(wn);

            #region 建立初始化數組表

            //建立參照矩陣表

            int[,] cankao = new int[6, 6];

            cankao[0, 0] = 5;

            cankao[0, 1] = 6;

            cankao[0, 2] = 9;

            cankao[0, 3] = 7;

            cankao[0, 4] = 4;

            cankao[0, 5] = 6;

            cankao[1, 0] = 8;

            cankao[1, 1] = 3;

            cankao[1, 2] = 5;

            cankao[1, 3] = 4;

            cankao[1, 4] = 6;

            cankao[1, 5] = 7;

            cankao[2, 0] = 6;

            cankao[2, 1] = 2;

            cankao[2, 2] = 4;

            cankao[2, 3] = 7;

            cankao[2, 4] = 8;

            cankao[2, 5] = 9;

            cankao[3, 0] = 9;

            cankao[3, 1] = 7;

            cankao[3, 2] = 6;

            cankao[3, 3] = 8;

            cankao[3, 4] = 4;

            cankao[3, 5] = 5;

            cankao[4, 0] = 7;

            cankao[4, 1] = 4;

            cankao[4, 2] = 3;

            cankao[4, 3] = 6;

            cankao[4, 4] = 8;

            cankao[4, 5] = 9;

            cankao[5, 0] = 5;

            cankao[5, 1] = 7;

            cankao[5, 2] = 8;

            cankao[5, 3] = 9;

            cankao[5, 4] = 6;

            cankao[5, 5] = 4;

            #endregion

            int[,] xuliehua = new int[6, 6];

            int n = 0;

            for (int p = 0; p < 6; p++)

            {

                for (int q = 0; q < 6; q++)

                {

                    xuliehua[p, q] = n;

                    n++;

                }

            }

            //PrintArray(xuliehua);

            Dictionary<int, int> refer = new Dictionary<int, int>();

            for (int i = 0; i < 6; i++)

                for (int j = 0; j < 6; j++)

                    int p = cankao[i, j];

                    int c = xuliehua[i, j];

                    refer.Add(c, p);

           // PrintDic(refer);

            //在此處寫出20代循環,但要将随機生成進行處理

            int jishu=1;

            List<int> s1 = new List<int>(); //一個個體的前六位

            List<int> s2 = new List<int>();//一個個體的後六位

            List<int> s3 = new List<int>();

            StringBuilder sb = new StringBuilder();

            Dictionary<string, int> dicEntity = new Dictionary<string, int>();  //建立20個個體和對應的詞典

            //存儲子代hs(int,List<int>)

            Hashtable hs1 = new Hashtable();    

            Hashtable hs2 = new Hashtable();

            //循環生成第一代的20個不同的個體

            for (int h = 1; h <= 20; h++)

                //sb.Clear();

                //s1.Clear();

                //s2.Clear();

                //s3.Clear();

                ////輸出參考數組中[0,0]的參考值

                //Console.Write(refer[0]);

                //PrintArray(cankao);

                //生成兩個随機字元串

                Console.WriteLine("***************************************************************************");

                Console.WriteLine("         第" + jishu + "代個體群:\n");

                int count = 1;

                if (jishu == 1)

                    #region 初代循環産生不同的20個個體

                    while (dicEntity.Count < 20)

                    {

                        sb.Clear();

                        s1.Clear();

                        s2.Clear();

                        s3.Clear();

                        //List<int> s4 = new List<int>();

                        randList(s1);

                        randList(s2);

                        //生成的随機一組字元串

                        //Console.WriteLine("生成的随機一組字元串:");

                        //Console.WriteLine("s1的數字串為:");

                        for (int i = 0; i < 6; i++)

                        {

                            //Console.Write(s1[i] + "  ");

                            sb.Append(s1[i].ToString());

                        }

                        //Console.WriteLine("\ns2的數字串為:");

                            //Console.Write(s2[i] + "  ");

                            char c = (char)nw[s2[i]];

                            //Console.Write(c + "  ");//将1-6轉化成A-F的字母

                            sb.Append(c.ToString());

                            s3.Add(Convert.ToInt32(wn[c]));//将字母轉化成0-5的索引号并指派給s3

                        //Console.Write(sb);

                        //Console.Write("\n這種情況的下的基因變異程度:  ");

                        int sum = 0;

                            int s = s1[i] - 1;

                            int p = s3[i];

                            //sum+=cankao[s,p];

                            //Console.Write(s + "    ");

                            //Console.WriteLine(p);

                            sum += cankao[s, p];

                        //Console.WriteLine(sum);

                        if (!dicEntity.ContainsKey(sb.ToString()))

                            dicEntity.Add(sb.ToString(), sum);

                            Console.Write("第" + count.ToString() + "個個體是:    ");

                            Console.WriteLine(sb.ToString());

                            Console.Write("它的變異度為:    ");

                            Console.WriteLine(sum);

                            Console.WriteLine("");

                            count++;

                    }

                    #endregion

                else

                    for (int shN = 0; shN < 20; shN++)

                        s1 = (List<int>)hs1[shN];

                        s2 = (List<int>)hs2[shN];

                        //string ss = "";

                        //string ss1 = "";

                        //string ss2 = "";

                        ////變異選擇子代

                        //List<string> strNext = new List<string>();

                        //strNext = bianyi(strnext);

                        //for (int g = 0; g < 20; g++)

                        //{

                        //    ss = strNext[g];

                        //    ss2 = ss.Substring(6);

                        //    ss1 = ss.Substring(0, 6);

                        //    for (int f = 0; f < 6; f++)

                        //    {

                        //        //char cc = ss1[f];

                        //        //Console.Write(cc);

                        //        s1.Add((int)hsCN[ss1[f]]);

                        //        s2.Add((int)wn[ss2[f]] + 1);

                        //    }

                        //}

                        dicEntity.Add(sb.ToString(), sum);

                        Console.Write("第" + count.ToString() + "個個體是:    ");

                        Console.WriteLine(sb.ToString());

                        Console.Write("它的變異度為:    ");

                        Console.WriteLine(sum);

                        Console.WriteLine("");

                        count++;

                //選出變異度最小的那個個體

                Dictionary<string, int> dicmin = new Dictionary<string, int>();

                dicmin = DicMin(dicEntity);

                string good="";

                int min = 0;

                foreach (KeyValuePair<string, int> kv in dicmin)

                    string key = kv.Key;

                    int value = kv.Value;

                    min = value;

                    good=key;

                    Console.WriteLine("變異度最小的那個個體是:" + key + "     它的适應度為:" + value.ToString());

                //列印輸出20個個體以及各自所對應的适應率

                Dictionary<string, double> dicStrDoubP = new Dictionary<string, double>();

                dicStrDoubP = dicStrDoubleP(dicEntity,min);

                int l = 1;

                Console.WriteLine("\n         20個個體各自所對應的适應率\n");

                foreach (KeyValuePair<string,double> kc in dicStrDoubP)

                    string key = kc.Key;

                    double value = kc.Value;

                    Console.WriteLine("\n第"+l+"個體是:" + key + "     它的适應率為:" + value.ToString());

                    l++;

                //列印輸出20個個體及其适應度

                Dictionary<string, double> dictable = new Dictionary<string, double>();

                dictable = dicTable(dicStrDoubP);

                List<string> lists = new List<string>();

                List<double> listd = new List<double>();

                int r = 1;

                Console.WriteLine("\n         20個個體各自所對應的适應度\n");

                foreach (KeyValuePair<string, double> kc in dictable)

                    lists.Add(key);

                    listd.Add(value);

                    Console.WriteLine("\n第" + r + "個體是:" + key + "     它的适應度為:" + value.ToString());

                    r++;

                //随機生成20個4位小數

                Console.WriteLine("           随機生成19個4位小數");

                List<double> randdouble = new List<double>();

                string str="";

                randdouble = randDouble();

                foreach (double b in randdouble)

                    Console.Write(b + "  ");

                Console.WriteLine("\n");

                //判斷每個4位小數是否所在的區域并且生成下一代

                List<string> strnext = new List<string>();

                strnext.Add(good);  //選擇上一代最好的那個直接遺傳到下一代

                for (int N = 0; N < 19; N++)   //比較19次  最優的那個個體直接遺傳到下一代

                    for (int M = 0; M < 20; M++)

                        if (randdouble[N] <= listd[M])

                            str = lists[M];

                            strnext.Add(str);

                            break;

                //經過選擇變異過的下一代

                List<string> strNext = new List<string>();

                strNext = bianyi(strnext);

                ////Console.WriteLine("***************************************************************************");

                ////Console.WriteLine("           第2代個體群");

                ////foreach (string s in strNext)

                ////{

                ////    Console.WriteLine(s);

                ////}

                //再将新個體群分成List<int>s1和List<int>s2

                //将二代複制給初代的變量,以便循環

                for (int g = 0; g < 20; g++)

                    s1.Clear();

                    s2.Clear();

                    s3.Clear();

                    sb.Clear();

                    string ss = "";

                    string ss1 = "";

                    string ss2 = "";

                    ss = strNext[g];

                    ss2 = ss.Substring(6);

                    ss1 = ss.Substring(0, 6);

                    for (int f = 0; f < 6; f++)

                        //char cc = ss1[f];

                        //Console.Write(cc);

                        s1.Add((int)hsCN[ss1[f]]);

                        s2.Add((int)wn[ss2[f]] + 1);

                    hs1.Add(g, s1);

                    hs2.Add(g, s2);

                    //s4.AddRange(s1);

                    //s5.AddRange(s2);

                    //for (int i = 0; i < 6; i++)

                    //{

                    //    //Console.Write(s1[i] + "  ");

                    //    sb.Append(s1[i].ToString());

                    //}

                    ////Console.WriteLine("\ns2的數字串為:");

                    //    //Console.Write(s2[i] + "  ");

                    //    char c = (char)nw[s2[i]];

                    //    //Console.Write(c + "  ");//将1-6轉化成A-F的字母

                    //    sb.Append(c.ToString());

                    //    s3.Add(Convert.ToInt32(wn[c]));//将字母轉化成0-5的索引号并指派給s3

                    ////Console.Write(sb);

                    ////Console.Write("\n這種情況的下的基因變異程度:  ");

                    //int sum = 0;

                    //    int s = s1[i] - 1;

                    //    int p = s3[i];

                    //    //sum+=cankao[s,p];

                    //    //Console.Write(s + "    ");

                    //    //Console.WriteLine(p);

                    //    sum += cankao[s, p];

                    //dicEntity.Add(sb.ToString(), sum);

                    //Console.Write("第" + count.ToString() + "個個體是:    ");

                    //Console.WriteLine(sb.ToString());

                    //Console.Write("它的變異度為:    ");

                    //Console.WriteLine(sum);

                    //Console.WriteLine("");

                    //count++;

                //        //for (int i = 0; i < 6; i++)

                //        //{

                //        //    //Console.Write(s1[i] + "  ");

                //        //    sb.Append(s1[i].ToString());

                //        //}

                //        ////Console.WriteLine("\ns2的數字串為:");

                //        //    //Console.Write(s2[i] + "  ");

                //        //    char c = (char)nw[s2[i]];

                //        //    //Console.Write(c + "  ");//将1-6轉化成A-F的字母

                //        //    sb.Append(c.ToString());

                //        //    s3.Add(Convert.ToInt32(wn[c]));//将字母轉化成0-5的索引号并指派給s3

                //        ////Console.Write(sb);

                //        ////Console.Write("\n這種情況的下的基因變異程度:  ");

                //        //int sum = 0;

                //        //    int s = s1[i] - 1;

                //        //    int p = s3[i];

                //        //    //sum+=cankao[s,p];

                //        //    //Console.Write(s + "    ");

                //        //    //Console.WriteLine(p);

                //        //    sum += cankao[s, p];

                //        //dicEntity.Add(sb.ToString(), sum);

                //        //Console.Write("第" + count.ToString() + "個個體是:    ");

                //        //Console.WriteLine(sb.ToString());

                //        //Console.Write("它的變異度為:    ");

                //        //Console.WriteLine(sum);

                //        //Console.WriteLine("");

                //        //count++;

                //int count = 1;

                //foreach (KeyValuePair<string, int> keyvaluepair in dicEntity)

                //{

                //    Console.Write("第" + count.ToString() + "個個體是:    ");

                //    Console.WriteLine(keyvaluepair.Key.ToString());

                //    Console.Write("它的變異度為:  ");

                //    Console.WriteLine(keyvaluepair.Value.ToString());

                //    count++;

                //}    

                jishu++;

            wp.Stop();

            Console.WriteLine("***************************************************************************");

            Console.WriteLine("\n程式用時:  " + wp.Elapsed);

            Console.Read();

        }

        #region 随機生成一段六位字元數字串

        /// <summary>

        /// 随機生成一段六位字元數字串

        /// </summary>

        /// <param name="list1">參數List</param>

        /// <returns>傳回List</returns>

        public static List<int> randList(List<int> list1)

            do

                Random a = new Random();

                int result = a.Next(1, 7);

                if (!list1.Contains(result))

                    list1.Add(result);

            } while (list1.Count < 6);

            return list1;

        #endregion

        #region 列印輸出參考數組(二維)

        /// 列印輸出參考數組

        /// <param name="array">數組參數</param>

        public static void PrintArray(int[,] array)

            Console.WriteLine("參照數組為:");

                    int p = array[i, j];

                    Console.Write(p + "  ");

                Console.WriteLine("");

        #region 列印輸出HashTable

        /// 列印輸出HashTable

        /// <param name="ht">參數HashTable</param>

        public static void PrintHashTable(Hashtable ht)

            Console.WriteLine("對應的HashTable為:");

            foreach (DictionaryEntry de in ht) //fileht為一個Hashtable執行個體

                Console.Write(de.Key + "   ");//de.Key對應于keyvalue鍵值對key

                Console.WriteLine(de.Value);//de.Key對應于keyvalue鍵值對value

        #region 列印輸出Dic<int,int>

        public static void PrintDic(Dictionary<int, int> dic)

            Console.WriteLine("輸出的dic: ");

            foreach (KeyValuePair<int, int> kv in dic)

                int p = (int)kv.Key;

                int q = (int)kv.Value;

                Console.WriteLine(p + "  " + q);

        } 

        #region 求出二十個個體中,變異度最小的那個個體,傳回dic<string,int>

        public static Dictionary<string, int> DicMin(Dictionary<string, int> olddic)

            int min = 0;

            string s = "";

            Dictionary<string, int> dicMin = new Dictionary<string, int>();

            List<int> listInt = new List<int>();

            List<string> listStr = new List<string>();

            foreach (KeyValuePair<string, int> kv in olddic)

                string key = (string)kv.Key;

                int value = (int)kv.Value;

                listInt.Add(value);

                listStr.Add(key);

            min = listInt.Min();

                if (value == min)

                    s = key;

            dicMin.Add(s, min);

            return dicMin;

        #region 将dic<string,int>轉化成機率dic<string,double>

        /// 将dic<string,int>轉化成機率dic<string,double>

        /// <param name="olddic">原來的個體和對應的值的dic</param>

        /// <param name="min">變異度的最小值</param>

        /// <returns></returns>

        public static Dictionary<string, double> dicStrDoubleP(Dictionary<string, int> olddic,int min)

            Dictionary<string, double> newdic = new Dictionary<string, double>();

            Dictionary<string, int> dic = new Dictionary<string, int>();

            List<int> listInt = new List<int>();//存數變異度的內插補點

            int sub = 0;

                sub=value - min + 1;

                listInt.Add(sub);

                dic.Add(key, sub);

            foreach (KeyValuePair<string,int> kc in dic)

                string key = (string)kc.Key;

                int value = (int)kc.Value;

                double v = (double)value;

                int sum=listInt.Sum();

                double p = Math.Round((v / sum),4);

                newdic.Add(key, p);

            return newdic;

        #region 随機生成19個4位小數

        public static List<double> randDouble()

            List<double> randomlist = new List<double>();

            double x = 0;

            Random r = new Random();

            for (int i = 0; i < 19; i++)

                x = Math.Round((double)r.NextDouble(), 4);

                randomlist.Add(x);

            return randomlist;

        #region 将機率變異率轉化成對應的表dic<string,double>

        public static Dictionary<string, double> dicTable(Dictionary<string, double> dicP)

            Dictionary<string, double> dictable = new Dictionary<string, double>();

            List<double> listD = new List<double>();

            List<string> listS = new List<string>();

            foreach (KeyValuePair<string, double> kv in dicP)

                double value = (double)kv.Value;

                listD.Add(value);

                listS.Add(key);

            for (int i = 0; i < 20; i++)

                double p = 0;

                for (int j = 0; j <= i; j++)

                    p =p+ listD[j];

                dictable.Add(listS[i],p);

            return dictable;

        #region 将一個字元串逆串操作

        public static string changeover(string s, int i, int j)

            string str = "";

            string ss = "";

            StringBuilder SB = new StringBuilder();

            int count = 0;

            if (i <= j)

                count = j - i;

                str = s.Substring(i, count);

                for (int k = str.Length - 1; k >= 0; k--)

                    SB.Append(str[k]);

                ss = s.Substring(0, i) + SB + s.Substring(j);

            else

                count = i - j;

                str = s.Substring(j, count);

                ss = s.Substring(0, j) + SB + s.Substring(i);

            return ss;

        #region list<string>是否發生基因突變

        public static List<string> bianyi(List<string> list)

            List<string> listBianYi = new List<string>();

            int e = 0;

            foreach (string s in list)

                e = r.Next(0, 3);                //三分之一的可能性變異

                if (e == 0)

                    int j = r.Next(0, 2);     //選擇左邊變異還是右邊變異

                    if (j == 0)     //左邊變異

                        int ll = r.Next(0, 6);

                        int rr = r.Next(0, 6);

                        str = changeover(s, ll, rr);

                        listBianYi.Add(str);

                    else          //右邊變異

                        int ll = r.Next(6, 12);

                        int rr = r.Next(6, 12);

                else                //三分之二的可能性不變異

                    listBianYi.Add(s);

            return listBianYi;

    }

}

本文轉自蓬萊仙羽51CTO部落格,原文連結:http://blog.51cto.com/dingxiaowei/1366633,如需轉載請自行聯系原作者

繼續閱讀