Ejemplo n.º 1
0
        /// <summary>
        /// performs a Branch and Bound search of the state space of partial tours
        /// stops when time limit expires and uses BSSF as solution
        /// </summary>
        /// <returns>results array for GUI that contains three ints: cost of solution, time spent to find solution, number of solutions found during search (not counting initial BSSF estimate)</returns>
        public string[] bBSolveProblem()
        {
            Stopwatch timer = new Stopwatch();

            timer.Start();
            initialBSSF = true;
            greedySolveProblem();
            string[] results = new string[3];
            int      n       = Cities.Length;

            double[,] baseMatrix = new double[n, n];
            int           count;
            ReducedMatrix currentCity;

            queue = new PriorityQueue();
            ReducedMatrix reduced;

            for (int i = 0; i < n; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    if (i == j)
                    {
                        baseMatrix[i, j] = Double.PositiveInfinity;
                    }
                    else
                    {
                        baseMatrix[i, j] = Cities[i].costToGetTo(Cities[j]);
                    }
                }
            }
            reduced = new ReducedMatrix(baseMatrix, Cities.Length, Cities[0]);
            reduced.reduce();
            queue.add(reduced);
            count       = 0;
            totalStates = 1;
            while (true)
            {
                if (queue.getSize() > maxStroedStates)
                {
                    maxStroedStates = queue.getSize();
                }
                if (timer.ElapsedMilliseconds > time_limit)
                {
                    break;
                }
                currentCity = queue.remove();
                if (currentCity == null)
                {
                    break;
                }
                if (currentBssf < currentCity.bound)
                {
                    break;
                }
                for (int i = 1; i < n; i++)
                {
                    if (currentCity.matrix[currentCity.cityNumber, i] != INFINITY)
                    {
                        ReducedMatrix newMatrix = new ReducedMatrix(currentCity, Cities[i], i);
                        newMatrix.cancel(currentCity.cityNumber, i);
                        newMatrix.reduce();
                        if (newMatrix.Route.Count == Cities.Length)
                        {
                            if (newMatrix.bound < currentBssf)
                            {
                                numBSSFUpdates++;
                                bssf        = new TSPSolution(newMatrix.Route);
                                currentBssf = newMatrix.bound;
                            }
                            count++;
                            break;
                        }
                        if (newMatrix.bound < currentBssf)
                        {
                            queue.add(newMatrix);
                            totalStates++;
                        }
                        else
                        {
                            numStatesPruned++;
                        }
                    }
                }
            }

            timer.Stop();
            numStatesPruned += queue.getSize();

            String track;

            track  = "MAX #of stored states " + maxStroedStates + "\n";
            track += "#of BSSF updates " + numBSSFUpdates + "\n";
            track += "Total states created " + totalStates + "\n";
            track += "Total states pruned " + numStatesPruned + "\n";

            MessageBox.Show(track);


            results[COST]  = bssf.costOfRoute().ToString();   // load results into array here, replacing these dummy values
            results[TIME]  = timer.Elapsed.ToString();
            results[COUNT] = count.ToString();

            return(results);
        }