Esempio n. 1
0
        public ArrayList BranchAndBound(City[] Cities, ArrayList Route, double BSSF)
        {
            Node startNode = new Node(Cities);
            startNode.initStaticMembers();
            PriorityQueue pQ = new PriorityQueue(startNode);
            ArrayList bbRoute = null;
            Stopwatch timer = new Stopwatch();
            timer.Start();

            //while ((pQ.Size() > 0) && (pQ.Peek() < BSSF) && (timer.Elapsed.TotalMinutes < 10))
            while ((pQ.Size() > 0) && (pQ.Peek() < BSSF) && (timer.Elapsed.TotalSeconds < 30))
            {
                // Keep track of the largest size of the queue
                if (pQ.Size() > Node.maxNodesCreated)
                {
                    Node.maxNodesCreated = pQ.Size();
                }

                startNode = pQ.DeleteMinimum();
                //startNode.matrix2Table();

                // Check for a solution
                if (startNode.includedEdges == Cities.Length)
                {
                    ArrayList tempRoute = new ArrayList();

                    if (!startNode.exited.Contains(-1))
                    {
                        int index = 0;

                        while (tempRoute.Count < Cities.Length)
                        {
                            tempRoute.Add(Cities[startNode.exited[index]]);
                            index = startNode.exited[index];
                        }

                        BSSF = startNode.lowerBound;
                        bbRoute = tempRoute;
                        Node.numSolutions++;
                    }
                }

                Node incNode = new Node(startNode);
                Node excNode = new Node(startNode);
                Node maxInclude = null;
                Node maxExclude = null;

                double maxDiff = -1;
                double diff = 0;
                int maxRow = 0;
                int maxCol = 0;

                for (int row = 0; row < startNode.matrixSize; row++)
                {
                    for (int col = 0; col < startNode.matrixSize; col++)
                    {
                        if (startNode.rcMatrix[row, col] == 0)
                        {
                            Node includeNode = new Node(incNode, true, row, col);
                            Node excludeNode = new Node(excNode, false, row, col);
                            diff = excludeNode.lowerBound - includeNode.lowerBound;
                            Node.numStatesCreated += 2;

                            if (diff > maxDiff)
                            {
                                maxDiff = diff;
                                maxRow = row;
                                maxCol = col;
                                maxInclude = new Node(includeNode);
                                maxExclude = new Node(excludeNode);
                            }
                        }
                    }
                }

                if (maxInclude != null && maxInclude.lowerBound < BSSF)
                {
                    pQ.Insert(maxInclude);
                }
                else
                {
                    Node.pruneCount++;
                }

                if (maxExclude != null && maxExclude.lowerBound < BSSF)
                {
                    pQ.Insert(maxExclude);
                }
                else
                {
                    Node.pruneCount++;
                }
            }

            timer.Stop();
            Node.timeElapsed = timer.ElapsedMilliseconds;

            if (bbRoute == null)
            {
                return Route;
            }
            else
            {
                // Add the rest of the queue to the pruned count
                Node.pruneCount += pQ.Size();
                return bbRoute;
            }
        }