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
       

 

    }
}

 源码:http://download.csdn.net/detail/s10141303/6028561

时间: 2024-11-03 08:20:57

C# GEP基因化编程的相关文章

基因表达式编程的任务指派问题求解算法设计与实现

朱明放1,2,叶飞跃1,2,丁小未2  (1.江苏理工学院云计算与智能信息处理常州市重点实验室,江苏常州,213001: 2.江苏理工学院计算机学院,江苏常州,213001) 摘要:任务指派问题是典型的组合优化问题,得到了广泛的研究.基于基因表达式编程的思想,设计了任务指派问题求解的算法,并用C#实现了该算法.结合人力资源任务分配的实例进行了实验分析和研究,获得了人员与岗位配置的最优解.实验表明算法设计是正确性和有效性,因而为企业人员安排提供参考依据. 关键词:TAP问题:基因表达式编程:逆串算

ExtJS4 组件化编程,动态加载,面向对象,Direct_extjs

ExtJS4推荐定义类的时候均使用Ext.define,利用xtype动态加载 修改了以前的一个登陆窗口,感觉用官方推荐的方法还是很不错的 但还有一些问题没有想得非常清楚,先把代码贴出来一起研究下.请看代码中的注释~~ 使用Ext+.Net,用Direct模式传递数据 Default.aspx: 复制代码 代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3

进化计算简介和遗传算法的实现--AForge.NET框架的使用(六)

原文:进化计算简介和遗传算法的实现--AForge.NET框架的使用(六) 开学了,各种忙起来了- 上一篇介绍了AForge.NET在人工神经网络上的一点点使用,但是老觉不过瘾.matlab用着实在不习惯,就又琢磨了一下进化计算. 进化计算简介 进化计算算不上新的方法了,已经有大量研究人员作出了努力,这导致了大量的进化计算算法出现.他们不仅研究算法本身,还致力于扩大算法的应用范围. 众所周知,现实世界存在大量复杂问题,它们中一部分无法用常规方法在合理的时间内获得精确解,而另一部分甚至没有行之有效

如何文档化你的PHP类

你已经阅读过关于:面向对象编程可以帮助你管理你的大型web项目,并且你已经开始使用PHP来进行面向对象编程了吗?如果你已经编写了几个类应用在网站上并且你是一个有条理的人的话,那么你应该已经编写了关于它们的一些文档.但是如果你是一个象我一样的不拘小节的人,你只是会在类的源代码中加一些注释而没有别的文档.没有文档就很难记住方法的名字和它们的使用方法(参数和含义).解决这种情况最典型的办法就是打开源代码文件,从成百上千的语句中查找. 类似Javadoc的文档 应该有一种好的方法----如果你曾经使用过

如何文档化你的PHP类(一)

如何文档化你的PHP类(一) 作者:stefano Locati 翻译:limodou   你已经阅读过关于:面向对象编程可以帮助你管理你的大型web项目,并且你已经开始使用PHP来进行面向对象编程了吗?如果你已经编写了几个类应用在网站上并且你是一个有条理的人的话,那么你应该已经编写了关于它们的一些文档.但是如果你是一个象我一样的不拘小节的人,你只是会在类的源代码中加一些注释而没有别的文档.没有文档就很难记住方法的名字和它们的使用方法(参数和含义).解决这种情况最典型的办法就是打开源代码文件,从

PHP的面向对象编程:开发大型PHP项目的方法(三)(转载)

编程|对象|项目 PHP的面向对象编程:开发大型PHP项目的方法(三) 作者:Luis Argerich 译者:limodou 重载(与覆盖不同)在PHP中不支持.在OOP中,你可以重载一个方法来实现两个或重多的方法具有相同 的名字,但是有不同数量或类型的参数(这要看语言).PHP 是一种松散类型的语言,所以通过类型重载不 起作用,然而通过参数的个数不同来重载也不起作用. 有时在OOP中重载构造函数非常好,这样你可以通过不同的方法创建对象(传递不同数量的参数).在PHP 中实现它的技巧是: --

PHP开发利器-PRADO 1.6编程

ado|编程   第2章 用PRADO编程 现在让我们更深入的了解一下PRADO框架.这一章我们会介绍一些框架的基本概念和如何使用现有的PRADO组件来开发一个PRADO应用. 2.1  组件 根据Clemens Szyperski的说法, ``软件组件是一个具有特定接口的组合单元,可以独立存在,参与组成其它组建.'' 在PRADO中, 组件是 TComponent 或者其子类的实例.PRADO框架中已经包括了 TComponent 类,它主要实现了组件的属性和事件机制. 属性可以被认为是描述组

PHP的面向对象编程:开发大型PHP项目的方法(三)

重载(与覆盖不同)在PHP中不支持.在OOP中,你可以重载一个方法来实现两个或重多的方法具有相同 的名字,但是有不同数量或类型的参数(这要看语言).PHP 是一种松散类型的语言,所以通过类型重载不 起作用,然而通过参数的个数不同来重载也不起作用. 有时在OOP中重载构造函数非常好,这样你可以通过不同的方法创建对象(传递不同数量的参数).在PHP 中实现它的技巧是: ------------------------------------------------------------------

Python中的函数式编程

虽然人们总把Python当作过程化的,面向对象的语言,但是他实际上包含了函数化编程中,你需要的任何东西.这篇文章主要讨论函数化编程的一般概念,并说明用Python来函数化编程的技术. 我们最好从艰难的问题开始出发:"到底什么是函数化编程呢?"其中一个答案可能是这样的,函数化编程就是你在使用Lisp这样的语言时所做的(还有Scheme,Haskell,ML,OCAML,Mercury,Erlang和其他一些语言).这是一个保险的回答,但是它解释得并不清晰.不幸的是对于什么是函数化编程,很