Exemplo n.º 1
0
        public ProblemAndSolver.TSPSolution Solve()
        {
            //Tests show that heap is better for more cities, array is better for less
            queue           = new HeapPriorityQueue();
            nodesCreated    = 0;
            prunedNodes     = 0;
            averageDistance = 0;
            numUpdates      = 0;
            var timer = new Stopwatch();

            timer.Start();
            //Build the mother node state
            //Build a starting matrix. time O(n^2) space O(n^2)
            int count = 0;

            double[,] motherMatrix = new double[cities.Length, cities.Length];
            for (int row = 0; row < cities.Length; row++)
            {
                for (int col = 0; col < cities.Length; col++)
                {
                    double cost = cities[row].costToGetTo(cities[col]);
                    motherMatrix[row, col] = row == col ? double.PositiveInfinity : cost;
                    if (!double.IsPositiveInfinity(cost))
                    {
                        averageDistance += cost;
                        count++;
                    }
                }
            }
            averageDistance = averageDistance / count;
            //Reduce the matrix & get the bound. time O(n^2)
            double    bound = ReduceMatrix(motherMatrix);
            ArrayList route = new ArrayList();

            route.Add(cities[0]);
            //Put them in a new state
            State motherState = new State(motherMatrix, route, bound, 0);

            //Expand the mother node state. time O(n^3) space O(n^3)
            ExpandState(motherState);
            //While the priority queue is not empty. Worst case Time O(n^3*b^n+log(n!)), with b > 1
            while (!queue.IsEmpty() && timer.ElapsedMilliseconds < timeLimit)
            {
                //pop off the top state and expand it. time O(n^3) space O(n^3)
                //DeleteMin could potentially be as bad as time O(logn!)
                ExpandState(queue.DeleteMin());
            }
            timer.Stop();

            //When the timer goes off, update results and return bssf
            results[ProblemAndSolver.COST]  = costOfBssf().ToString();
            results[ProblemAndSolver.TIME]  = timer.Elapsed.ToString();
            results[ProblemAndSolver.COUNT] = numUpdates.ToString();
            Console.WriteLine("Max Stored States: " + queue.LargestSize());
            Console.WriteLine("States Created: " + nodesCreated);
            Console.WriteLine("States Pruned: " + prunedNodes);
            return(bssf);
        }