来点C#代码吧

解决方案 »

  1.   

     FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.Create);
    byte[] b = Encoding.GetEncoding("GB2312").GetBytes(textBox1.Text);
     StreamWriter sw=new StreamWriter (fs);
     textBox2.Text = saveFileDialog1.FileName;
    fs.Write(b, 0, b.Length);
     fs.Close();
      

  2.   

    蚁群算法(ant colony optimization, ACO),又称蚂蚁算法,是一种用来在图中寻找优化路径的机率型算法。它由Marco Dorigo于1992年在他的博士论文中提出,其灵感来源于蚂蚁在寻找食物过程中发现路径的行为。蚁群算法是一种模拟进化算法,初步的研究表明该算法具有许多优良的性质.针对PID控制器参数优化设计问题,将蚁群算法设计的结果与遗传算法设计的结果进行了比较,数值仿真结果表明,蚁群算法具有一种新的模拟进化优化方法的有效性和应用价值。
      

  3.   

    同样的问题,lz对启发式算法族一点概念都没有。蚁群算法、遗传算法、退火算法都是一类算法的统称,并非某个具体的算法。lz的问题好比,来个人类给我看看。
      

  4.   

    以前搜集的一个例子,给你看看using System;
    using System.Collections.Generic;
    using System.Text;
    namespace AntSystem
    {
        public class AA
         {
            /**//// <summary>
            /// 对信息量的重视程度
            /// </summary>
            private int alpha;
            /**//// <summary>
            /// 启发式信息的受重视程度
            /// </summary>
            private int beta;
            /**//// <summary>
            /// 信息素的挥发速度
            /// </summary>
            private double lo;
            /**//// <summary>
            /// 城市距离矩阵
            /// </summary>
            private double[,] City;
            /**//// <summary>
            /// 信息素矩阵
            /// </summary>
            private double[,] Message;
            /**//// <summary>
            /// opneList用于存放下一步可行城市
            /// </summary>
            private Queue<int> openList=new Queue<int> ();
            /**//// <summary>
            /// closedList用于存放已经访问过的城市
            /// </summary>
            private Queue<int> closedList=new Queue<int> ();
            /**//// <summary>
            /// 储存较好的路径
            /// </summary>
            private Queue <int> BestList=new Queue<int> ();
            private int Pro_time = 0;
            /**//////////////////////////////////////////////////////////
            /// <summary>
            /// 构造函数:形成城市距离和信息素矩阵
            /// </summary>
            /// <param name="city">城市距离矩阵</param>
            /// <param name="Lo"> 信息素的挥发速度</param>
            public AA(double[,] city,double Lo,int Alpha,int Beta)
             {
                alpha = Alpha;
                beta = Beta;
                lo=Lo;
                int temp = Convert.ToInt32( Math.Sqrt(city.Length));
                City=new double [temp,temp];
                Message=new double [temp,temp];
                for (int i = 0; i < temp; i++)
                 {
                    for (int j = 0; j < temp; j++)
                    {
                        City[i, j] = city[i, j];
                    }
                }
                //初始化信息素矩阵
                for (int i = 0; i < temp; i++)
                 {
                    for (int j = 0; j < temp; j++)
                     {
                        if (i != j)
                         {
                            Message[i, j] = (double)1 / (temp * temp - temp);
                         }
                    }
                }
            }
            /**/////////////////////////////////////////////////////////////
            /// <summary>
            /// 改变信息素矩阵,closed_list为较好的路径
            /// </summary>
            /// <param name="closed_list"></param>
            private void Change_Message(Queue<int> closed_list)
             {
                lock (this)
                 {
                    int[] temp_Array = new int[closed_list.Count];
                    temp_Array = closed_list.ToArray();
                    for (int i = 0; i < closed_list.Count - 1; i++)
                     {
                        Message[temp_Array[i], temp_Array[i + 1]] = Message[temp_Array[i], temp_Array[i + 1]] + lo / ((1 - lo) *Convert.ToInt32(Get_Weight(closed_list)+1));
                    }
                    Message[temp_Array[temp_Array.Length - 1], temp_Array[0]] = Message[temp_Array[temp_Array.Length - 1], temp_Array[0]] + lo / ((1 - lo) *Convert.ToInt32(Get_Weight(closed_list)));
                    for (int i = 0; i < closed_list.Count; i++)
                     {
                        for (int j = 0; j < closed_list.Count; j++)
                         {
                            Message[i, j] = (1 - lo) * Message[i, j];
                        }
                    }
                }
            }
            /**////////////////////////////////////////////////////////////////
            /// <summary>
            /// 输入一个链表,计算出其对应的总路径
            /// </summary>
            /// <param name="closed_list"></param>
            /// <returns></returns>
            public double Get_Weight(Queue <int> closed_list)
             {
                lock (this)
                 {
                    double sum = 0;
                    int[] temp_Array = new int[closed_list.Count];
                    temp_Array = closed_list.ToArray();
                    for (int i = 0; i < Convert.ToInt32(temp_Array.Length) - 1; i++)
                     {
                        sum = sum + City[temp_Array[i], temp_Array[i + 1]];
                    }
                    sum = sum + City[temp_Array[temp_Array.Length - 1], temp_Array[0]];
                    return sum;
                }
            }
            /**///////////////////////////////////////////////////////////////
            /// <summary>
            /// 产生到i城市后,下一个可走城市的集合。并将城市编号加入到openList中。
            /// 产生的城市不可以已经存在closedList中
            /// </summary>
            /// <param name="i"></param>
            private void NextCity()
             {
                openList.Clear();
                int temp_int=Convert.ToInt32(Math.Sqrt(City.Length));
                for (int i = 0; i < temp_int; i++)
                 {
                    if (closedList.Contains(i) ==false)
                     {
                        openList.Enqueue(i);
                    }
                }
            }
            /**///////////////////////////////////////////////////////////////
            /// <summary>
            /// 选择应该走那条路,选择完路A后,清空openList,再把A加入到openList中
            /// </summary>
            /// <returns></returns>
            private int choiceRoute()
             {
                int index = 0;//记录选择的城市
                Random random = new Random();
                double random_value =(double) random.NextDouble();//随机选择的概率
                int[] temp_Array=new int [openList.Count];
                temp_Array=openList.ToArray();
                double sum_Message = 0;//openList所有节点的总信息量
                for (int i = 0; i < openList.Count; i++)
                 {
                    double eta = 1 / City[Pro_time, temp_Array[i]];
                    sum_Message = sum_Message +Math.Pow(Message[Pro_time, temp_Array[i]],alpha)*Math.Pow (eta,beta);
                }
                double temp=0;
                for (int j = 0; j < openList.Count; j++)
                 {
                    double eta = 1 / City[Pro_time, temp_Array[j]];
                    temp=temp+Math.Pow(Message[Pro_time,temp_Array[j]],alpha)*Math.Pow(eta,beta)/sum_Message;
                    if (temp > random_value)
                     {
                        index = temp_Array [j];
                        break;
                    }
                }
                openList.Clear();
                openList.Enqueue(index);
                return index;
            }
            /**//////////////////////////////////////////////////////////////
            public Queue<int> Main_DW()
              {
                BestList.Clear();
                /**////共循环20次
                for (int i = 0; i < 4; i++)
                 {
                    /**////共有n只蚂蚁n=City'number Convert.ToInt32(Math.Sqrt(City.Length))
                    for (int j = 0; j < Convert.ToInt32(Math.Sqrt(City.Length)); j++)
                     {
                        openList.Enqueue(0);
                        closedList.Clear();
                        while (openList.Count != 0 && closedList.Count != Convert.ToInt32(Math.Sqrt(City.Length)))
                         {
                            int temp = openList.Dequeue();
                            Pro_time = temp;
                            closedList.Enqueue(temp);
                            if (openList.Count == 0 && closedList.Count == Convert.ToInt32(Math.Sqrt(City.Length)))
                             {
                                if (BestList.Count == 0)
                                 {
                                    int[] temp_Array = new int[Convert.ToInt32(Math.Sqrt(City.Length))];
                                    temp_Array = closedList.ToArray();
                                    for (int k = 0; k < Convert.ToInt32(Math.Sqrt(City.Length)); k++)
                                     {
                                        BestList.Enqueue(temp_Array[k]);
                                    }
                                }
                                if (Get_Weight(BestList) > Get_Weight(closedList))
                                 {
                                    BestList.Clear();
                                    int[] temp_Array = new int[Convert.ToInt32(Math.Sqrt(City.Length))];
                                    temp_Array = closedList.ToArray();
                                    for (int k = 0; k < Convert.ToInt32(Math.Sqrt(City.Length)); k++)
                                    {
                                        BestList.Enqueue(temp_Array[k]);
                                    }
                                }
                            }
                            NextCity();
                            choiceRoute();
                        }
                    }
                    Change_Message(BestList);//修改信息量
                }
                return BestList;
            }
        }

      

  5.   

    我觉得AStar算法就有点像蚁群算法。在我的下载资源里有得下载C#_AStar DEMO。可以直接运行看结果。(WinForm)
      

  6.   

    这是求最短路的using System;
    using System.Collections.Generic;
    using System.Text;
    namespace AntSystem
    {
        public class AA
         {
            /**//// <summary>
            /// 对信息量的重视程度
            /// </summary>
            private int alpha;
            /**//// <summary>
            /// 启发式信息的受重视程度
            /// </summary>
            private int beta;
            /**//// <summary>
            /// 信息素的挥发速度
            /// </summary>
            private double lo;
            /**//// <summary>
            /// 城市距离矩阵
            /// </summary>
            private double[,] City;
            /**//// <summary>
            /// 信息素矩阵
            /// </summary>
            private double[,] Message;
            /**//// <summary>
            /// opneList用于存放下一步可行城市
            /// </summary>
            private Queue<int> openList=new Queue<int> ();
            /**//// <summary>
            /// closedList用于存放已经访问过的城市
            /// </summary>
            private Queue<int> closedList=new Queue<int> ();
            /**//// <summary>
            /// 储存较好的路径
            /// </summary>
            private Queue <int> BestList=new Queue<int> ();
            private int Pro_time = 0;
            /**//////////////////////////////////////////////////////////
            /// <summary>
            /// 构造函数:形成城市距离和信息素矩阵
            /// </summary>
            /// <param name="city">城市距离矩阵</param>
            /// <param name="Lo"> 信息素的挥发速度</param>
            public AA(double[,] city,double Lo,int Alpha,int Beta)
             {
                alpha = Alpha;
                beta = Beta;
                lo=Lo;
                int temp = Convert.ToInt32( Math.Sqrt(city.Length));
                City=new double [temp,temp];
                Message=new double [temp,temp];
                for (int i = 0; i < temp; i++)
                 {
                    for (int j = 0; j < temp; j++)
                    {
                        City[i, j] = city[i, j];
                    }
                }
                //初始化信息素矩阵
                for (int i = 0; i < temp; i++)
                 {
                    for (int j = 0; j < temp; j++)
                     {
                        if (i != j)
                         {
                            Message[i, j] = (double)1 / (temp * temp - temp);
                        }
                    }
                }
            }
            /**/////////////////////////////////////////////////////////////
            /// <summary>
            /// 改变信息素矩阵,closed_list为较好的路径
            /// </summary>
            /// <param name="closed_list"></param>
            private void Change_Message(Queue<int> closed_list)
             {
                lock (this)
                 {
                    int[] temp_Array = new int[closed_list.Count];
                    temp_Array = closed_list.ToArray();
                    for (int i = 0; i < closed_list.Count - 1; i++)
                     {
                        Message[temp_Array[i], temp_Array[i + 1]] = Message[temp_Array[i], temp_Array[i + 1]] + lo / ((1 - lo) *Convert.ToInt32(Get_Weight(closed_list)+1));
                    }
                    Message[temp_Array[temp_Array.Length - 1], temp_Array[0]] = Message[temp_Array[temp_Array.Length - 1], temp_Array[0]] + lo / ((1 - lo) *Convert.ToInt32(Get_Weight(closed_list)));
                    for (int i = 0; i < closed_list.Count; i++)
                     {
                        for (int j = 0; j < closed_list.Count; j++)
                         {
                            Message[i, j] = (1 - lo) * Message[i, j];
                        }
                    }
                }
            }
            /**////////////////////////////////////////////////////////////////
            /// <summary>
            /// 输入一个链表,计算出其对应的总路径
            /// </summary>
            /// <param name="closed_list"></param>
            /// <returns></returns>
            public double Get_Weight(Queue <int> closed_list)
             {
                lock (this)
                 {
                    double sum = 0;
                    int[] temp_Array = new int[closed_list.Count];
                    temp_Array = closed_list.ToArray();
                    for (int i = 0; i < Convert.ToInt32(temp_Array.Length) - 1; i++)
                     {
                        sum = sum + City[temp_Array[i], temp_Array[i + 1]];
                    }
                    sum = sum + City[temp_Array[temp_Array.Length - 1], temp_Array[0]];
                    return sum;
                }
            }
            /**///////////////////////////////////////////////////////////////
            /// <summary>
            /// 产生到i城市后,下一个可走城市的集合。并将城市编号加入到openList中。
            /// 产生的城市不可以已经存在closedList中
            /// </summary>
            /// <param name="i"></param>
            private void NextCity()
             {
                openList.Clear();
                int temp_int=Convert.ToInt32(Math.Sqrt(City.Length));
                for (int i = 0; i < temp_int; i++)
                 {
                    if (closedList.Contains(i) ==false)
                     {
                        openList.Enqueue(i);
                    }
                }
            }
            /**///////////////////////////////////////////////////////////////
            /// <summary>
            /// 选择应该走那条路,选择完路A后,清空openList,再把A加入到openList中
            /// </summary>
            /// <returns></returns>
            private int choiceRoute()
             {
                int index = 0;//记录选择的城市
                Random random = new Random();
                double random_value =(double) random.NextDouble();//随机选择的概率
                int[] temp_Array=new int [openList.Count];
                temp_Array=openList.ToArray();
                double sum_Message = 0;//openList所有节点的总信息量
                for (int i = 0; i < openList.Count; i++)
                 {
                    double eta = 1 / City[Pro_time, temp_Array[i]];
                    sum_Message = sum_Message +Math.Pow(Message[Pro_time, temp_Array[i]],alpha)*Math.Pow (eta,beta);
                }
                double temp=0;
                for (int j = 0; j < openList.Count; j++)
                 {
                    double eta = 1 / City[Pro_time, temp_Array[j]];
                    temp=temp+Math.Pow(Message[Pro_time,temp_Array[j]],alpha)*Math.Pow(eta,beta)/sum_Message;
                    if (temp > random_value)
                     {
                        index = temp_Array [j];
                        break;
                    }
                }
                openList.Clear();
                openList.Enqueue(index);
                return index;
            }
            /**//////////////////////////////////////////////////////////////
            public Queue<int> Main_DW()
              {
                BestList.Clear();
                /**////共循环20次
                for (int i = 0; i < 4; i++)
                 {
                    /**////共有n只蚂蚁n=City'number Convert.ToInt32(Math.Sqrt(City.Length))
                    for (int j = 0; j < Convert.ToInt32(Math.Sqrt(City.Length)); j++)
                     {
                        openList.Enqueue(0);
                        closedList.Clear();
                        while (openList.Count != 0 && closedList.Count != Convert.ToInt32(Math.Sqrt(City.Length)))
                         {
                            int temp = openList.Dequeue();
                            Pro_time = temp;
                            closedList.Enqueue(temp);
                            if (openList.Count == 0 && closedList.Count == Convert.ToInt32(Math.Sqrt(City.Length)))
                             {
                                if (BestList.Count == 0)
                                 {
                                    int[] temp_Array = new int[Convert.ToInt32(Math.Sqrt(City.Length))];
                                    temp_Array = closedList.ToArray();
                                    for (int k = 0; k < Convert.ToInt32(Math.Sqrt(City.Length)); k++)
                                     {
                                        BestList.Enqueue(temp_Array[k]);
                                    }
                                }
                                if (Get_Weight(BestList) > Get_Weight(closedList))
                                 {
                                    BestList.Clear();
                                    int[] temp_Array = new int[Convert.ToInt32(Math.Sqrt(City.Length))];
                                    temp_Array = closedList.ToArray();
                                    for (int k = 0; k < Convert.ToInt32(Math.Sqrt(City.Length)); k++)
                                     {
                                        BestList.Enqueue(temp_Array[k]);
                                    }
                                }
                            }
                            NextCity();
                            choiceRoute();
                        }
                    }
                    Change_Message(BestList);//修改信息量
                }
                return BestList;
            }
        }