예제 #1
0
        /// <summary>
        /// 种群较优解交配
        /// </summary>
        private void AntCross()
        {
            double length1;
            double length2;

            int[] tempPath1_1 = new int[Common.N_CITY_COUNT * 2];
            int[] tempPath2_1 = new int[Common.N_CITY_COUNT * 2];
            int[] tempPath1   = new int[Common.N_CITY_COUNT];
            int[] tempPath2   = new int[Common.N_CITY_COUNT];
            int   loopCount   = 0;

            while (loopCount < 100)
            {
                int ramdPoint  = Common.rnd(10, 50);
                int ChromCount = ramdPoint;

                for (int i = ramdPoint, j = 0; i < Common.N_CITY_COUNT + ramdPoint; i++)
                {
                    tempPath1_1[i] = m_cBestAnt_1.m_nPath[i - ramdPoint];
                    tempPath2_1[i] = m_cBestAnt_2.m_nPath[i - ramdPoint];
                }

                for (int i = 0; i < ramdPoint; i++)
                {
                    tempPath1_1[i] = m_cBestAnt_2.m_nPath[i];
                    tempPath2_1[i] = m_cBestAnt_1.m_nPath[i];
                    tempPath1[i]   = m_cBestAnt_2.m_nPath[i];
                    tempPath2[i]   = m_cBestAnt_1.m_nPath[i];
                }

                int count;
                for (int i = ramdPoint; i < Common.N_CITY_COUNT + ramdPoint; i++)
                {
                    count = 0;
                    bool flag = true; //默认为没有相同的值

                    //确认是否有相同的值
                    while (count < ramdPoint)
                    {
                        if (tempPath1_1[count] == tempPath1_1[i])
                        {
                            flag = false;
                            break;
                        }
                        count++;
                    }

                    if (flag)
                    {
                        tempPath1[ChromCount] = tempPath1_1[i];
                        ChromCount++;
                    }
                }

                length1 = GetLength(tempPath1);
                if (length1 < m_cBestAnt_1.m_dbPathLength)
                {
                    m_cBestAnt_1.m_dbPathLength = length1;
                    m_cBestAnt_1.m_nPath        = (int[])INTSergesion.DeepClone(tempPath1);
                    //Console.WriteLine("Use");
                    //throw new Exception("Success");
                }

                ChromCount = ramdPoint;
                for (int i = ramdPoint; i < Common.N_CITY_COUNT + ramdPoint; i++)
                {
                    count = 0;
                    bool flag = true; //默认为没有相同的值

                    //确认是否有相同的值
                    while (count < ramdPoint)
                    {
                        if (tempPath2_1[count] == tempPath2_1[i])
                        {
                            flag = false;
                            break;
                        }
                        count++;
                    }

                    if (flag)
                    {
                        tempPath2[ChromCount] = tempPath2_1[i];
                        ChromCount++;
                    }
                }

                length2 = GetLength(tempPath2);
                if (length2 < m_cBestAnt_2.m_dbPathLength)

                {
                    m_cBestAnt_2.m_dbPathLength = length2;
                    m_cBestAnt_2.m_nPath        = (int[])INTSergesion.DeepClone(tempPath2);
                    //Console.WriteLine("Use");
                    //throw new Exception("Success");
                }
                loopCount++;
            }
        }
예제 #2
0
        /// <summary>
        /// 开始搜索
        /// </summary>
        public void Search()
        {
            //char cBuf[256]; //打印信息用
            string strInfo = "";

            bool blFlag = false;

            //Mant = new Thread(MantStart);
            //在迭代次数内进行循环
            for (int i = 0; i < Common.N_IT_COUNT; i++)
            {
                MantStart();

                //蚁群交叉
                if (BestPathLengthList.Count > 2)
                {
                    double ramd = Common.rnd(0.9, 1);
                    if (ramd < BestPathLengthList[BestPathLengthList.Count - 1]
                        / BestPathLengthList[BestPathLengthList.Count - 2])
                    {
                        AntCross();
                    }
                }

                if (i % 10 == 0)
                {
                    UpdateBestTrial();
                    //AntCross();
                }

                localBest_2.m_dbPathLength = Common.DB_MAX;
                for (int a = 0; a < Common.N_ANT_COUNT; a++)
                {
                    if (m_cAntAry_2[a].m_dbPathLength < localBest_2.m_dbPathLength)
                    {
                        localBest_2.m_dbPathLength = m_cAntAry_2[a].m_dbPathLength;
                    }
                }
                Common.CAnt_2List.Add(localBest_2.m_dbPathLength);

                localBest_1.m_dbPathLength = Common.DB_MAX;
                for (int b = 0; b < Common.N_ANT_COUNT; b++)
                {
                    if (m_cAntAry_1[b].m_dbPathLength < localBest_1.m_dbPathLength)
                    {
                        localBest_1.m_dbPathLength = m_cAntAry_1[b].m_dbPathLength;
                        localBest_1.m_nPath        = m_cAntAry_1[b].m_nPath;
                    }
                }
                Common.CAnt_1List.Add(localBest_1.m_dbPathLength);

                //Common.CAnt_1List.Add(m_cBestAnt_1.m_dbPathLength);
                //Common.CAnt_2List.Add(m_cBestAnt_2.m_dbPathLength);

                if (m_cBestAnt.m_dbPathLength > m_cBestAnt_2.m_dbPathLength)
                {
                    //m_cBestAnt.m_nPath = m_cBestAnt_2.m_nPath;
                    m_cBestAnt.m_nPath        = (int[])INTSergesion.DeepClone(m_cBestAnt_2.m_nPath);
                    m_cBestAnt.m_dbPathLength = m_cBestAnt_2.m_dbPathLength;
                    blFlag = true;
                }
                if (m_cBestAnt.m_dbPathLength > m_cBestAnt_1.m_dbPathLength)
                {
                    m_cBestAnt.m_nPath        = (int[])INTSergesion.DeepClone(m_cBestAnt_1.m_nPath);
                    m_cBestAnt.m_dbPathLength = m_cBestAnt_1.m_dbPathLength;
                    blFlag = true;
                }

                //BestPathLengthList.Add(m_cBestAnt.m_dbPathLength);
                //if (BestPathLengthList.Count > 2
                //    && BestPathLengthList[BestPathLengthList.Count - 1] != BestPathLengthList[BestPathLengthList.Count - 2])
                //{
                //    ballance = 1 - (BestPathLengthList[BestPathLengthList.Count - 1] / BestPathLengthList[BestPathLengthList.Count - 2]);
                //    //TrialBallance();
                //}
                //AntCross();
                if (BestPathLengthList.Count == 0)
                {
                    BestPathLengthList.Add(m_cBestAnt.m_dbPathLength);
                }
                else
                {
                    if (BestPathLengthList[BestPathLengthList.Count - 1] != m_cBestAnt.m_dbPathLength)
                    {
                        BestPathLengthList.Add(m_cBestAnt.m_dbPathLength);
                    }
                }

                if (blFlag)
                {
                    //Console.WriteLine("\nall:[{0}]: {1}", i, m_cBestAnt.m_dbPathLength);
                    //for (int j = 0; j < Common.N_CITY_COUNT; j++)
                    //{
                    //    Console.Write("{0}, ", m_cBestAnt.m_nPath[j] + 1);
                    //}
                    //Console.WriteLine();
                    blFlag = false;
                }
            }

            //Console.WriteLine("\nbest path length = {0} ", m_cBestAnt.m_dbPathLength);
            //for (int i = 0; i < Common.N_CITY_COUNT; i++)
            //{
            //    strInfo = String.Format("{0} ", m_cBestAnt.m_nPath[i] + 1);
            //    Console.Write(strInfo);
            //}

            //Console.WriteLine("\nbest_1 path length = {0} ", m_cBestAnt_1.m_dbPathLength);
            //for (int i = 0; i < Common.N_CITY_COUNT; i++)
            //{
            //    strInfo = String.Format("{0} ", m_cBestAnt_1.m_nPath[i] + 1);
            //    Console.Write(strInfo);
            //}

            //Console.WriteLine();

            //Console.WriteLine("best_2 path length = {0} ", m_cBestAnt_2.m_dbPathLength);
            //for (int i = 0; i < Common.N_CITY_COUNT; i++)
            //{
            //    strInfo = String.Format("{0} ", m_cBestAnt_2.m_nPath[i] + 1);
            //    Console.Write(strInfo);
            //}
        }