Пример #1
0
        public static float UglyPermutBruteForce(tspclass tsp, out int permutCount)
        {
            float bestDistance = 0;

            permutCount = 0;

            List <int> startPermutList = new List <int>();

            for (int i = 0; i < tsp.Dimension; i++)
            {
                startPermutList.Add(i);
            }

            // foreach (int city in startPermutList)
            // {
            //     Console.Write(" " + city);
            // } Console.WriteLine("  -----\n");

            foreach (int[] test in Utils.Permut(startPermutList.ToArray()))
            {
                List <int> ArrayList = new List <int>(test);
                ArrayList.Add(ArrayList[0]);
                Tour newT = new Tour(tsp.Dimension + 1, ArrayList.ToArray());

                float newDistance = tsp.GetTourDistance(newT);

                if (permutCount == 0)
                {
                    bestDistance = newDistance;
                }

                //Console.WriteLine(newT.ToString() + "::> " + newDistance);

                if (newDistance < bestDistance)
                {
                    bestDistance = newDistance;
                }
                permutCount++;
            }

            return(bestDistance);
        }
Пример #2
0
        static void Main(string[] args)
        {
            ParseTPS  parser    = new ParseTPS();
            tspclass  tspFile   = null;
            Stopwatch stopWatch = new Stopwatch();
            string    fileName;
            int       typeOfSearch = 0;

            List <Tour> ListTours = new List <Tour>();

            //ListTours.Add(new Tour(7,new List<int>{0,1,2,3,4,5,6}.ToArray())); //TODO Change this to a proper Array
            //Tour startTour = new Tour(18, new List<int> { 15, 16, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 15 }.ToArray());

            // for (int i = 0; i < 4; i++)
            // {
            //     List<int> tempList = Utils.GenerateStartList(6);
            //     ListTours.Add(new Tour(6, tempList.ToArray()));
            // }
            // int tc = 0;
            // foreach (Tour tour in ListTours)
            // {
            //     Console.WriteLine(tour.ToString());
            //     Console.Write("TOUR LIST  " + tc + "\n");
            //     tc++;
            // }

            Console.WriteLine("\nPlease enter the name of the .tsp file\n");

            //OTHER FILES JSON
            fileName = Console.ReadLine();
            Console.WriteLine("fileName:  " + fileName);
            Console.ReadKey();

            //fileName = "gr17";

            bool fileExists = File.Exists(@"tspjson/" + fileName + ".json");

            if (fileExists)
            {
                tspFile = JsonConvert.DeserializeObject <tspclass>(File.ReadAllText(@"tspjson/" + fileName + ".json"));
            }
            else
            {
                Console.WriteLine("File do not exist");
            }

            Console.WriteLine("OptDistance  :" + tspFile.OptDistance);
            Console.WriteLine("OptTour  :" + tspFile.OptTour.Length);
            Console.WriteLine("Format  :" + tspFile.Format);

            foreach (int city in tspFile.OptTour)
            {
                Console.Write("  " + city);
            }

            #region Distance Matrix
            //DISTANCE MATRIX
            Console.WriteLine("\nDistance Matrix");
            foreach (int[] row in tspFile.DistanceMatrix)
            {
                foreach (int i in row)
                {
                    Console.Write("  " + i);
                }
                Console.WriteLine();
            }
            #endregion

            Console.WriteLine("SELECIONE O TIPO DE BUSCA\n");
            Console.WriteLine("1 - Iterated Local Search\n");
            Console.WriteLine("2 - Simulated Anneling\n");
            Console.WriteLine("-1 Encerrar o programa\n");

            typeOfSearch = Convert.ToInt32(Console.ReadLine());

            while (typeOfSearch != -1)
            {
                Tour startTour = new Tour(tspFile.Dimension + 1, Utils.GenerateStartList(tspFile.Dimension + 1).ToArray());
                ListTours.Add(startTour);
                Console.WriteLine("RAND");

                Tour startTourFList = new Tour(tspFile.Dimension + 1, Utils.GenerateStartList(tspFile.Dimension + 1).ToArray());//ListTours[0];
                while (startTourFList.Cities[0] != tspFile.OptTour[0])
                {
                    startTourFList = new Tour(tspFile.Dimension + 1, Utils.GenerateStartList(tspFile.Dimension + 1).ToArray());//ListTours[0];
                }

                if (typeOfSearch == 1)
                {
                    Console.WriteLine("\nILS\n");
                    int twoOptBesttIterations  = 0;
                    int twoOptSwaptIterations  = 0;
                    int fourOptSwaptIterations = 0;

                    Tour            twoOptBestTour = new Tour();
                    Utils.ILSLimits Limit1         = new Utils.ILSLimits(100, 50, 4, 30);
                    stopWatch.Start();
                    float twoOptBestDistance = ILS.IteratedLocalSearch(tspFile, startTourFList, out twoOptBesttIterations, out twoOptSwaptIterations, out fourOptSwaptIterations, out twoOptBestTour, Limit1);
                    stopWatch.Stop();
                    Console.WriteLine("TIME: " + Utils.PrintStopwatch(stopWatch.Elapsed));

                    Console.Write("::> " + startTourFList.ToString() + "  START TOUR DISTANCE: " + tspFile.GetTourDistance(startTourFList) + "\n");
                    if (twoOptBesttIterations != -1)
                    {
                        Console.Write("::> " + twoOptBestTour.ToString() + "  BEST TOUR DISTANCE: " + twoOptBestDistance + "\n");
                        Console.Write("::> " + tspFile.PrintOptTour() + "  OPTIMAL TOUR DISTANCE: " + tspFile.OptDistance + "\n");

                        Console.WriteLine();
                        Console.WriteLine("BEST ITERATIONS: " + twoOptBesttIterations);
                        Console.WriteLine("TOTAL FOUR SWAP ITERATIONS: " + fourOptSwaptIterations);
                        Console.WriteLine("TOTAL TWO SWAP ITERATIONS: " + twoOptSwaptIterations);
                    }
                    Console.WriteLine();
                }
                else if (typeOfSearch == 2)
                {
                    Console.WriteLine("\nSA\n");

                    int saBesttIterations     = 0;
                    int saSwaptIterations     = 0;
                    int saFourSwaptIterations = 0;
                    int saTempIterations      = 0;

                    Tour           SABestTour = new Tour();
                    Utils.SALimits Limit2     = new Utils.SALimits(200, 500, 0.994f, 120);
                    stopWatch.Start();
                    float SABestDistance = SA.SimulatedAnneling(tspFile, startTourFList, out saBesttIterations, out saSwaptIterations, out saFourSwaptIterations, out saTempIterations, out SABestTour, Limit2);
                    stopWatch.Stop();
                    Console.WriteLine("TIME: " + Utils.PrintStopwatch(stopWatch.Elapsed) + "  MS: " + stopWatch.ElapsedMilliseconds);

                    Console.Write("::> " + startTourFList.ToString() + "  START TOUR DISTANCE: " + tspFile.GetTourDistance(startTourFList) + "\n");
                    if (saBesttIterations != -1)
                    {
                        Console.Write("::> " + SABestTour.ToString() + "  BEST TOUR DISTANCE: " + SABestDistance + "\n");
                        Console.Write("::> " + tspFile.PrintOptTour() + "  OPTIMAL TOUR DISTANCE: " + tspFile.OptDistance + "\n");

                        Console.WriteLine();
                        Console.WriteLine("BEST ITERATIONS: " + saBesttIterations);
                        Console.WriteLine("TOTAL TEMP SWAP ITERATIONS: " + saTempIterations);
                        Console.WriteLine("TOTAL TWO SWAP ITERATIONS: " + saSwaptIterations);
                    }
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine("SELECIONE O TIPO DE BUSCA\n");
                    Console.WriteLine("1 - Iterated Local Search\n");
                    Console.WriteLine("2 - Simulated Anneling\n");
                    Console.WriteLine("-1 Encerrar o programa\n");

                    typeOfSearch = Convert.ToInt32(Console.ReadLine());
                }

                Console.WriteLine("SELECIONE O TIPO DE BUSCA\n");
                Console.WriteLine("1 - Iterated Local Search\n");
                Console.WriteLine("2 - Simulated Anneling\n");
                Console.WriteLine("-1 - Encerrar o programa\n");

                typeOfSearch = Convert.ToInt32(Console.ReadLine());
            }
        }
Пример #3
0
        public static float IteratedLocalSearch(tspclass tsp, Tour StartTour, out int newBestIterations, out int TwoSwapIterations, out int FourSwapIterations, out Tour bestTour, Utils.ILSLimits Limit)
        {
            newBestIterations  = 0;
            FourSwapIterations = 0;
            TwoSwapIterations  = 0;

            int limitTwoOpt = 0;

            int limitFourOpt = 0;

            int ILS = 0;

            bestTour = null;
            Stopwatch   stopWatch  = new Stopwatch();
            List <Tour> twoOptList = new List <Tour>();


            if (tsp.Dimension + 1 != StartTour.Size)
            {
                newBestIterations = -1;
                bestTour          = null;
                Console.WriteLine("Something is wrong with your tour mate");
                return(-1); //Start tour have the wrong size for this particular tsp
            }


            float bestDistance = tsp.GetTourDistance(StartTour);
            Tour  swapedTour   = StartTour;

            Console.WriteLine("ILS RUNNING -- TIME LIMIT: " + Limit.Time + " TWOOPT: " + Limit.TwoOpt);
            stopWatch.Start();
            while (ILS < Limit.ILS && bestDistance != tsp.OptDistance && stopWatch.ElapsedMilliseconds <= (Limit.Time * 1000f))//(bestDistance != tsp.OptDistance)
            {
                if (ILS != 0)
                {
                    while (limitFourOpt <= Limit.FourOpt && bestDistance != tsp.OptDistance)
                    {
                        //if(limitFourOpt%50 == 0) Console.WriteLine("4OPT" + limitFourOpt);
                        List <int> fourOptRandom = Utils.GenerateRandomSet(4, tsp.Dimension);
                        // foreach (int i in fourOptRandom)
                        // {
                        //     Console.Write(" " + i);
                        // } Console.WriteLine();
                        swapedTour = FourOptSwap.GetFourOptSwap(swapedTour, fourOptRandom[0], fourOptRandom[1], fourOptRandom[2], fourOptRandom[3]);
                        FourSwapIterations++;
                        float newDistance = tsp.GetTourDistance(swapedTour);
                        if (newDistance >= bestDistance)
                        {
                            limitFourOpt++;
                            continue;
                        }
                        else
                        {
                            bestDistance = newDistance;
                            bestTour     = swapedTour;
                            newBestIterations++;
                            limitFourOpt++;

                            break;
                        }
                    }
                    limitTwoOpt  = 0;
                    limitFourOpt = 0;
                }

                while (limitTwoOpt <= Limit.TwoOpt && bestDistance != tsp.OptDistance)
                {
                    for (var i = 1; i < StartTour.Size - 1; i++)
                    {
                        for (var k = i + 1; k < StartTour.Size - 1; k++)
                        {
                            TwoSwapIterations++;
                            swapedTour = TwoOptSwap.GetTwoOptSwap(swapedTour, i, k);

                            // if(twoOptList.FindIndex( x => (x == swapedTour))
                            // {
                            //     Console.WriteLine(swapedTour.ToString());
                            //     Console.WriteLine("You're meh");
                            //     Environment.Exit(-1);
                            // }
                            twoOptList.Add(swapedTour);
                            float newDistance = tsp.GetTourDistance(swapedTour);



                            if (newDistance >= bestDistance)
                            {
                                continue;
                            }
                            else //if (newDistance < bestDistance)
                            {
                                //We have a new winner
                                //Console.WriteLine("NEW BEST: " + newBestIterations + "  DISTANCE: " + newDistance + "\n");
                                //Console.WriteLine("L: " + limit + " SI: " + swapIterations + " BI: " + newBestIterations + "  NEW: " + newDistance + "  BEST: " + bestDistance);

                                bestDistance = newDistance;
                                bestTour     = swapedTour;
                                newBestIterations++;
                                break;
                            }
                        }
                    }
                    if (limitTwoOpt % 2000 == 0)
                    {
                        //Utils.ClearCurrentConsoleLine();
                        //Console.WriteLine("  L: " + limitTwoOpt);
                    }
                    limitTwoOpt++;
                }
                // Console.WriteLine("AI: " + swapedTour);
                ILS++;
                // Utils.ClearCurrentConsoleLine();
                // Utils.ClearCurrentConsoleLine();
                if (ILS % 10 == 0 || stopWatch.ElapsedMilliseconds % 10000 == 0)
                {
                    Console.WriteLine("-----------ILS: " + ILS);
                }
                // Console.WriteLine();
            }
            stopWatch.Stop();
            Console.WriteLine("IT'S OVER: " + Utils.PrintStopwatch(stopWatch.Elapsed));
            return(bestDistance);
        }
Пример #4
0
        public static float SimulatedAnneling(tspclass tsp, Tour StartTour, out int newBestIterations, out int TwoSwapIterations, out int FourSwapIterations, out int TempIterations, out Tour bestTour, Utils.SALimits Limit)
        {
            newBestIterations  = 0;
            FourSwapIterations = 0;
            TwoSwapIterations  = 0;

            TempIterations = 0;

            float currTemp;
            float tempMult = Limit.TempMult;

            int SA = 0;

            float bestDistance = tsp.GetTourDistance(StartTour);
            float currentDist, nextDist;
            float deltaDiff;

            Tour currentTour = StartTour;
            Tour nextTour    = null;

            List <int> RandomSet  = new List <int>();
            List <int> RandomSet2 = new List <int>();

            bestTour = null;
            Stopwatch stopWatch = new Stopwatch();

            Console.WriteLine("START TEMP: " + Limit.StartTemp + " START MULT: " + Limit.TempMult);
            stopWatch.Start();
            while (bestDistance != tsp.OptDistance && stopWatch.ElapsedMilliseconds <= (Limit.Time * 1000f) && SA < Limit.SA)
            {
                currTemp = Limit.StartTemp; //* ((SA/10) +1);

                //Console.WriteLine(currTemp *= tempMult);

                // RandomSet2 = Utils.GenerateRandomSet(4, tsp.Dimension);
                // currentTour = FourOptSwap.GetFourOptSwap(currentTour, RandomSet2[0], RandomSet2[1], RandomSet2[2], RandomSet2[3]);

                while (currTemp > 0.0013f)
                {
                    RandomSet = Utils.GenerateRandomSetNotZero(2, tsp.Dimension);
                    nextTour  = TwoOptSwap.GetTwoOptSwap(currentTour, RandomSet[0], RandomSet[1]);

                    //RandomSet2 = Utils.GenerateRandomSet(4, tsp.Dimension);
                    //nextTour = FourOptSwap.GetFourOptSwap(currentTour, RandomSet2[0], RandomSet2[1], RandomSet2[2], RandomSet2[3]);

                    //Console.WriteLine(nextTour.ToString());
                    TwoSwapIterations++;
                    currentDist = tsp.GetTourDistance(currentTour);
                    nextDist    = tsp.GetTourDistance(nextTour);

                    deltaDiff = currentDist - nextDist;
                    if (deltaDiff > 0)
                    {
                        currentTour = nextTour;
                    }
                    else
                    {
                        if (Utils.GetNextRand() <= Math.Exp(deltaDiff / currTemp))
                        {
                            currentTour = nextTour;
                        }
                    }

                    if (currentDist < bestDistance)
                    {
                        bestDistance = currentDist;
                        bestTour     = currentTour;
                        newBestIterations++;
                    }

                    currTemp *= tempMult;
                    //Console.WriteLine(currTemp);
                    TempIterations++;
                    if (TempIterations % 50000 == 0)
                    {
                        Console.WriteLine("Current Temp: " + currTemp + "  :::>Current Best Tour Distance " + bestDistance);
                    }
                }
                SA++;
            }

            return(bestDistance);
        }