Beispiel #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()
        {
            string[]              results            = new string[3];
            int                   solutionCount      = 0;
            Stopwatch             timer              = new Stopwatch();
            PriorityQueue <State> pQueue             = new PriorityQueue <State>();
            int                   maxStoredStates    = 0;
            int                   bssfUpdates        = 0;
            int                   totalStatesCreated = 0;
            int                   totalStatesPruned  = 0;

            timer.Start();
            string[] intialResults = defaultSolveProblem();
            double   bestCostSoFar = Int32.Parse(intialResults[COST]);

            Console.WriteLine("BestCostSoFar: " + bestCostSoFar);

            State initialState = initializeState();

            this.reduceState(ref initialState);
            pQueue.insert(initialState, (int)initialState.lowerBound);

            while (!pQueue.isEmpty() && timer.Elapsed.Seconds < 60)             // while queue is not empty or has not passed 60 seconds
            {
                State parentState = pQueue.deleteMin();
                if (parentState.lowerBound >= bestCostSoFar)                 // trim queue if state has worse bound than bssf
                {
                    totalStatesPruned++;
                    continue;
                }
                for (int i = 0; i < this.Cities.Length; i++)
                {
                    if (!parentState.visited.Contains(i))
                    {
                        // createChild
                        totalStatesCreated++;
                        State childState = parentState.getCopy();
                        this.addCityToRoute(ref childState, i);
                        this.reduceState(ref childState);
                        if (childState.route.Count == this.Cities.Count())                         // if route is complete
                        {
                            TSPSolution newSolution = new TSPSolution(childState.route);
                            double      cost        = newSolution.costOfRoute();
                            if (cost < bestCostSoFar)
                            {
                                bssfUpdates++;
                                bestCostSoFar = cost;
                                bssf.Route    = childState.route;
                            }
                        }
                        else                                           // route is not complete
                        {
                            if (childState.lowerBound < bestCostSoFar) // don't insert in queue if state has worse bound than bssf
                            {
                                pQueue.insert(childState, (int)childState.lowerBound);
                                if (pQueue.getCount() > maxStoredStates)
                                {
                                    maxStoredStates = pQueue.getCount();
                                }
                            }
                            else
                            {
                                totalStatesPruned++;
                            }
                        }
                    }
                }
            }

            timer.Stop();

            results[COST]  = costOfBssf().ToString();
            results[TIME]  = timer.Elapsed.ToString();
            results[COUNT] = solutionCount.ToString();


            Console.WriteLine("===================================");
            Console.WriteLine("StatesCreated: " + totalStatesCreated);
            Console.WriteLine("StatesPruned: " + totalStatesPruned);
            Console.WriteLine("bssfUpdates: " + bssfUpdates);
            Console.WriteLine("MaxStoredStates: " + maxStoredStates);


            return(results);
        }