Exemplo n.º 1
0
        public BranchAndBound(City[] cities, double bssf)
        {
            _cities = cities;
            _bssf   = bssf;
            int[] defaultArray = new int[_cities.Length];
            Populate(defaultArray, -1);
            State state = GenerateReducedMatrix(new State(new Matrix(_cities), -1, defaultArray, defaultArray, 0));

            pq.Add(0, state);
        }
Exemplo n.º 2
0
        /// <summary>
        ///  solve the problem.  This is the entry point for the solver when the run button is clicked
        /// right now it just picks a simple solution.
        /// </summary>
        public void solveProblem()
        {
            stopwatch = new Stopwatch();
            stopwatch.Start();
            bssf = generateInitialBSSF();
            State s = initState();

            agenda = new PriorityQueue <double, State>();
            agenda.Add(new KeyValuePair <double, State>(s.boundValue / s.path.Count, s));

            maxSize = 0;
            while (!agenda.IsEmpty && stopwatch.ElapsedMilliseconds < 60000 && bssf.costOfRoute() != s.boundValue)
            {
                if (agenda.Count > maxSize)
                {
                    maxSize = agenda.Count;
                }
                State u = agenda.Dequeue().Value;
                if (u.boundValue < bssf.costOfRoute())
                {
                    if (stopwatch.ElapsedMilliseconds % 5000 == 0)
                    {
                        updateUI();
                    }
                    ArrayList children = generateChildren(u);
                    foreach (State w in children)
                    {
                        if (w.boundValue < bssf.costOfRoute())
                        {
                            if (w.path.Count == Cities.Length)
                            {
                                bssf   = bssfFromIntArray(w.path);
                                myBssf = (int[])w.path.ToArray(System.Type.GetType("System.Int32"));
                                while (updateBssf(myBssf))
                                {
                                    ;
                                }
                                updateUI();
                            }
                            else
                            {
                                agenda.Add(new KeyValuePair <double, State>(w.boundValue / w.path.Count, w));
                            }
                        }
                    }
                }
            }
            updateUI();
        }
Exemplo n.º 3
0
        public int BBSolution(Stopwatch timer)
        {
            // Stats
            int maxStates    = 0;
            int totalStates  = 0;
            int prunedStates = 0;

            int           count             = 0;
            PriorityQueue q                 = new PriorityQueue();
            SubProblem    initialSubProblem = new SubProblem(
                this.CalculateCostMatrix(),
                this.Cities.Length,
                0,
                new List <int>()
                );

            initialSubProblem.path.Add(0);
            q.Add(initialSubProblem);

            while (!q.Empty() && timer.ElapsedMilliseconds < this.time_limit)
            {
                // Update statistics
                if (q.Size() > maxStates)
                {
                    maxStates = q.Size();
                }
                ++totalStates;

                SubProblem subProblem = q.Pop();


                if (subProblem.path.Count == this.Cities.Length &&
                    subProblem.path[subProblem.GetCurrentCityIndex()] != 0)
                {
                    // The subproblem is a valid solution!
                    this.bssf = new TSPSolution(PathToRoute(subProblem.path));
                    count++;
                }
                else
                {
                    if (subProblem.HasMorePaths())
                    {
                        for (int col = 0; col < this.Cities.Length; ++col)
                        {
                            if (subProblem.reducedMatrix[subProblem.GetCurrentCityIndex(), col] != double.PositiveInfinity)
                            {
                                if (subProblem.reducedMatrix[subProblem.GetCurrentCityIndex(), col] + subProblem.lowerBound < this.costOfBssf())
                                {
                                    List <int> pathClone  = new List <int>(subProblem.path);
                                    SubProblem newProblem = new SubProblem(
                                        (double[, ])subProblem.reducedMatrix.Clone(),
                                        this.Cities.Length,
                                        subProblem.lowerBound,
                                        pathClone
                                        );
                                    newProblem.path.Add(col);
                                    newProblem.BlockPaths(
                                        subProblem.GetCurrentCityIndex(),
                                        newProblem.GetCurrentCityIndex()
                                        );
                                    newProblem.ReduceCostMatrix();
                                    q.Add(newProblem);
                                }
                                else
                                {
                                    ++prunedStates;
                                }
                            }
                        }
                    }
                }
            }

            // Print stats
            Console.WriteLine("Max # of states: {0}", maxStates);
            Console.WriteLine("Total # number of states: {0}", totalStates);
            Console.WriteLine("# of states pruned: {0}", prunedStates);

            return(count);
        }