示例#1
0
        public static List <Point> SolveTSP(List <Point> lstLocs,
                                            int ants,
                                            double alpha,
                                            double beta,
                                            double q0,
                                            double tao0)
        {
            Dictionary <KeyValuePair <Point, Point>, double> dicPhTrails =
                InitPheromoneTrails(lstLocs);

            const int ITERATIONS = 5;

            List <List <Point> > lstSolutions          = new List <List <Point> >();
            List <Point>         lstReallyBestSolution = null;

            for (int nIter = 0; nIter < ITERATIONS; nIter++)
            {
                lstSolutions.Clear();
                for (int nAnt = 0; nAnt < ants; nAnt++)
                {
                    lstSolutions.Add(TSPAnt.FindSolution(dicPhTrails, beta, q0));
                    if (TSPwACO.s_actFinishedPercentageHandler != null)
                    {
                        TSPwACO.s_actFinishedPercentageHandler.Invoke((nIter * ants + nAnt + 1) / (double)(ITERATIONS * ants),
                                                                      dicPhTrails);
                    }
                }

                // Local trail updating
                foreach (List <Point> lstSol in lstSolutions)
                {
                    TSPwACO.TrailUpdating(dicPhTrails, lstSol, alpha, tao0);
                }

                // Get best solution
                double dBestSolLength =
                    (from sol in lstSolutions
                     select Form1.GetPathTotalDistance(sol)).Min();
                List <Point> lstBestSolution =
                    (from sol in lstSolutions
                     where Form1.GetPathTotalDistance(sol) == dBestSolLength
                     select sol).First();

                // Global updating
                TSPwACO.TrailUpdating(dicPhTrails, lstBestSolution, alpha, 1 / dBestSolLength);

                // Save best solution
                if (lstReallyBestSolution == null || dBestSolLength < Form1.GetPathTotalDistance(lstReallyBestSolution))
                {
                    lstReallyBestSolution = lstBestSolution;
                }
            }

            return(lstReallyBestSolution);
        }
示例#2
0
        public static List <List <Point> > SolveSingleVRP(List <Point> lstLocs, Point pCenter, int vehicles)
        {
            lstLocs = new List <Point>(lstLocs);
            List <List <Point> > lstSolution = new List <List <Point> >();
            List <List <Point> > lstSections = new List <List <Point> >();
            List <Point>         lstCurrLocs = new List <Point>();

            lstLocs.Sort(new Comparison <Point>((p1, p2) =>
            {
                double dAng1 = Math.Atan2(p1.Y - pCenter.Y, p1.X - pCenter.X);
                double dAng2 = Math.Atan2(p2.Y - pCenter.Y, p2.X - pCenter.X);
                return(dAng1.CompareTo(dAng2));
            }));

            if (vehicles > lstLocs.Count)
            {
                vehicles = lstLocs.Count;
            }

            int nLocsPerVehicle    = lstLocs.Count / vehicles;
            int nExtraVehicleCount = lstLocs.Count % vehicles;
            int nVehicleIndex      = 0;
            int nCurrLocIndex      = 0;
            int nTempCurrLocIndex;

            while (nCurrLocIndex < lstLocs.Count)
            {
                lstCurrLocs = new List <Point>();
                lstCurrLocs.Add(pCenter);
                nTempCurrLocIndex = nCurrLocIndex;
                for (int nLoc = 0; nLoc < Math.Min(nLocsPerVehicle + (nExtraVehicleCount > nVehicleIndex ? 1 : 0),
                                                   lstLocs.Count - nTempCurrLocIndex); nLoc++)
                {
                    lstCurrLocs.Add(lstLocs[nCurrLocIndex++]);
                }

                lstSections.Add(lstCurrLocs);
                nVehicleIndex++;
            }

            Parallel.ForEach(lstSections, sect =>
            {
                lstSolution.Add(Form1.TwoOptimization(TSPwACO.SolveTSP(sect,
                                                                       10,
                                                                       0.1,
                                                                       2,
                                                                       0.9,
                                                                       sect.Count / Form1.GetPathTotalDistance(Form1.NearestNeighbour(sect)))));
                lstSolution.Last().Add(lstSolution.Last()[0]);
            });

            return(lstSolution);
        }
示例#3
0
        private List <Point> SolveTSP(List <Point> lstLocs)
        {
            TSPwACO.OnFinishedPercentageUpdated += (d, ph) =>
            {
                //this.label1.Text = (d * 100).ToString() + "%";
                this.ShowPhTrails(ph);
                Application.DoEvents();
            };
            List <Point> lstResult =
                TSPwACO.SolveTSP(lstLocs,
                                 10,
                                 0.1,
                                 2,
                                 0.9,
                                 lstLocs.Count / GetPathTotalDistance(NearestNeighbour(lstLocs)));

            this.CreateGraphics().Clear(this.BackColor);
            lstResult = TwoOptimization(lstResult);
            lstResult.Add(lstResult[0]);
            return(lstResult);
        }