Пример #1
0
        public void GenerateChildren(ref List <NodeState> queue, ref int MaxNodeStateAmount, ref int BSSFupdates, ref int TotalNodeStates, ref int TotalNodeStatesPrunned, int startCity)
        {
            int cityCount = Cities.Length;
            //Get the first node in the priority
            NodeState currentNodeState = DeletePriorityNode(ref queue);

            //If the currentNodeState should be prunned
            if (currentNodeState.GetLowerBound() >= bssf.costOfRoute())
            {
                currentNodeState = PruneTheQueue(ref queue, currentNodeState, ref TotalNodeStatesPrunned);
            }
            //Generate a child for each city the currentNodeState can go to.
            for (int i = 0; i < cityCount; i++)
            {
                if (!currentNodeState.AlreadyVisited(i))
                {
                    //Make a child
                    NodeState child = MakeChild(ref currentNodeState, i);
                    child.ReduceMatrixAndUpdateLowerBound(cityCount);
                    // Add to node states because a child was generated
                    TotalNodeStates++;

                    //If LB is less BSSF then add to queue, TNS++
                    if (child.GetLowerBound() < bssf.costOfRoute())
                    {
                        //Before adding to the queue, see if you have a new BSSF
                        if (BetterBSSFExists(ref child, startCity))
                        {
                            bssf = new TSPSolution(child.GetRoute());
                            BSSFupdates++;
                        }
                        else
                        {
                            //add node to the queue
                            if (!child.HasVisitedAllCities())
                            {
                                //Add
                                AddNodeToPriorityQueue(ref queue, child);
                            }
                        }
                    }
                    else
                    {
                        //Prune. We don't need to even visit it.
                        TotalNodeStatesPrunned++;
                    }
                }
            }

            //Check the MaxNodeStates
            CheckQueueSize(ref MaxNodeStateAmount, queue.Count - 1);
        }
Пример #2
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];
            Stopwatch timer   = new Stopwatch();

            timer.Start();
            //Obtain a BSSF by using a GREEDY approach.
            int startCity = 0;

            bssf = new TSPSolution(GenerateInitialBSSF(ref startCity));
            //Make an initial Matrix
            int cityCount = Cities.Length;

            double[,] initialMatrix = GetInitialMatrix(cityCount);
            //Put start city in list
            ArrayList startingRoute = new ArrayList();

            startingRoute.Add(Cities[startCity]);
            //Make Start node
            int         lowerBound    = 0;
            List <bool> citiesVisited = InitializeCitiesVisitedList(startCity, cityCount);
            NodeState   startNode     = new NodeState(initialMatrix, lowerBound, startingRoute, startCity, citiesVisited);

            startNode.ReduceMatrixAndUpdateLowerBound(cityCount);
            Console.WriteLine("Initial BSSF: " + bssf.costOfRoute().ToString());

            List <NodeState> priorityQueue = new List <NodeState>();

            priorityQueue.Add(null);
            priorityQueue.Add(startNode);

            //Now we are ready to execute the branch and bound algorithm
            int MaxNodeStateAmount     = 1;
            int BSSFupdates            = 0;
            int TotalNodeStates        = 1;
            int TotalNodeStatesPrunned = 0;

            BranchAndBound(ref priorityQueue, ref MaxNodeStateAmount, ref BSSFupdates, ref TotalNodeStates, ref TotalNodeStatesPrunned, startCity, ref timer);

            timer.Stop();

            Console.WriteLine("Max NodeState Amount: " + MaxNodeStateAmount.ToString());
            Console.WriteLine("Total Node States: " + TotalNodeStates.ToString());
            Console.WriteLine("Total Node States Prunned: " + TotalNodeStatesPrunned.ToString() + "\r\n");
            results[COST]  = bssf.costOfRoute().ToString();
            results[TIME]  = timer.Elapsed.ToString();
            results[COUNT] = BSSFupdates.ToString();

            return(results);
        }