zoukankan      html  css  js  c++  java
  • 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");
                    Console.WriteLine("***************************************************************************");


                    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的数字串为:");
                            for (int i = 0; i < 6; i++)
                            {
                                //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;
                            for (int i = 0; i < 6; i++)
                            {
                                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.Clear();
                            s2.Clear();
                            s3.Clear();
                            sb.Clear();
                            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);
                            //    }
                            //}
                            for (int i = 0; i < 6; i++)
                            {
                                //Console.Write(s1[i] + "  ");
                                sb.Append(s1[i].ToString());
                            }

                            //Console.WriteLine("\ns2的数字串为:");
                            for (int i = 0; i < 6; i++)
                            {
                                //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;
                            for (int i = 0; i < 6; i++)
                            {
                                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++;
                        }
                    }


                    //选出变异度最小的那个个体
                    Console.WriteLine("***************************************************************************");
                    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());
                    }
                    Console.WriteLine("***************************************************************************");
                    //打印输出20个个体以及各自所对应的适应率
                    Dictionary<string, double> dicStrDoubP = new Dictionary<string, double>();
                    dicStrDoubP = dicStrDoubleP(dicEntity,min);
                    int l = 1;
                    Console.WriteLine("***************************************************************************");
                    Console.WriteLine("\n         20个个体各自所对应的适应率\n");
                    Console.WriteLine("***************************************************************************");
                    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("***************************************************************************");
                    Console.WriteLine("\n         20个个体各自所对应的适应度\n");
                    Console.WriteLine("***************************************************************************");
                    foreach (KeyValuePair<string, double> kc in dictable)
                    {
                        string key = kc.Key;
                        double value = kc.Value;
                        lists.Add(key);
                        listd.Add(value);
                        Console.WriteLine("\n第" + r + "个体是:" + key + "     它的适应度为:" + value.ToString());
                        r++;
                    }

                    //随机生成20个4位小数
                    Console.WriteLine("***************************************************************************");
                    Console.WriteLine("           随机生成19个4位小数");
                    Console.WriteLine("***************************************************************************");
                    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代个体群");
                    ////Console.WriteLine("***************************************************************************");
                    ////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的数字串为:");
                        //for (int i = 0; i < 6; i++)
                        //{
                        //    //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;
                        //for (int i = 0; i < 6; i++)
                        //{
                        //    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的数字串为:");
                    //        //for (int i = 0; i < 6; i++)
                    //        //{
                    //        //    //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;
                    //        //for (int i = 0; i < 6; i++)
                    //        //{
                    //        //    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.WriteLine("***************************************************************************");
                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 打印输出参考数组(二维)
            /// <summary>
            /// 打印输出参考数组
            /// </summary>
            /// <param name="array">数组参数</param>
            public static void PrintArray(int[,] array)
            {
                Console.WriteLine("参照数组为:");
                for (int i = 0; i < 6; i++)
                {
                    for (int j = 0; j < 6; j++)
                    {
                        int p = array[i, j];
                        Console.Write(p + "  ");
                    }
                    Console.WriteLine("");
                }
            }
            #endregion

            #region 打印输出HashTable
            /// <summary>
            /// 打印输出HashTable
            /// </summary>
            /// <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
                }
            }
            #endregion

            #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);
                }
            }
            #endregion

            #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();
                foreach (KeyValuePair<string, int> kv in olddic)
                {
                    string key = (string)kv.Key;
                    int value = (int)kv.Value;
                    if (value == min)
                    {
                        s = key;
                    }
                }
                dicMin.Add(s, min);
                return dicMin;
            }
            #endregion

            #region 将dic<string,int>转化成概率dic<string,double>
            /// <summary>
            /// 将dic<string,int>转化成概率dic<string,double>
            /// </summary>
            /// <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>();//存数变异度的差值
                List<string> listStr = new List<string>();
                int sub = 0;
                foreach (KeyValuePair<string, int> kv in olddic)
                {
                    string key = (string)kv.Key;
                    int value = (int)kv.Value;
                    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;
            }
            #endregion

            #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;
            }
            #endregion

            #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)
                {
                    string key = (string)kv.Key;
                    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;
            }
            #endregion

           
            #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);
                    for (int k = str.Length - 1; k >= 0; k--)
                    {
                        SB.Append(str[k]);
                    }
                    ss = s.Substring(0, j) + SB + s.Substring(i);
                }
                return ss;
            }
            #endregion

            #region list<string>是否发生基因突变
            public static List<string> bianyi(List<string> list)
            {
                List<string> listBianYi = new List<string>();
                Random r = new Random();

                string str = "";
                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);
                            str = changeover(s, ll, rr);
                            listBianYi.Add(str);
                        }
                    }

                    else                //三分之二的可能性不变异
                    {
                        listBianYi.Add(s);
                    }
                }
                return listBianYi;
            }
            #endregion
           

        }
    }

  • 相关阅读:
    快排算法的一点思考
    imglab .xml 标签格式转coco .json格式
    Ubuntu18.04 编译 sparse-ncnet
    Detectron2 keypoint_rcnn 网络c++版本部署
    技术部斗争
    我的程序人生
    关于ddd落地体验
    DevOps关于制定适合自身生产环境的redis集群
    从前端到后端的跨域攻击与防御
    DKIM对发送邮件的好处及使用方法
  • 原文地址:https://www.cnblogs.com/java20130723/p/3211550.html
Copyright © 2011-2022 走看看