コード例 #1
0
        // TODO: FIX THIS METHOD TO MATCH THE EDGE.ROW && EDGE.COLUMN!!
        private LbDifferenceResult GetLbDifference(int row, int col, State state)
        {
            State includeState = new State(state);
            State excludeState = new State(state);

            _createdStates += 2;

            includeState.Matrix = SetupIncludeMatrix(includeState.Matrix, row, col);
            excludeState.Matrix = SetupExcludeMatrix(excludeState.Matrix, row, col);

            includeState = DeleteEdges(includeState, row, col);

            includeState = GenerateReducedMatrix(includeState);
            excludeState = GenerateReducedMatrix(excludeState);

            double lbDifference = excludeState.GetLowerBound() - includeState.GetLowerBound();

            return(new LbDifferenceResult(includeState, excludeState, lbDifference, row, col));
        }
コード例 #2
0
        public PathCalculation CalculatePath()
        {
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            while (pq.Count != 0)
            {
                if (stopwatch.Elapsed.Seconds >= 30)
                {
                    break;
                }

                State state = pq.RemoveMin();

                if (state.GetLowerBound() < _bssf)
                {
                    LbDifferenceResult greatestDifferenceResult = CalculateGreatestLBDifference(state);
                    State[]            children = new State[2]
                    {
                        greatestDifferenceResult.IncludeState, greatestDifferenceResult.ExcludeState
                    };

                    foreach (State child in children)
                    {
                        if (IsSolution(child))
                        {
                            bssfUpdates++;
                            _bssf           = child.GetLowerBound();
                            _bestStateSoFar = child;
                        }
                        else if (child.GetLowerBound() < _bssf)
                        {
                            // Dividing by the number cities in the solution is a great way to prioritize those solutions
                            // that are further along. This helps the algorithm "dig deeper" into the search.
                            pq.Add((int)child.GetLowerBound() / (child.GetCitiesInSolution() == 0 ? 1 : child.GetCitiesInSolution()), child);

                            if (pq.Count > _maxStatesStored)
                            {
                                _maxStatesStored = pq.Count;
                            }
                        }
                        else
                        {
                            _prunedSates++;
                        }
                    }
                }
                else
                {
                    _prunedSates++;
                }
            }

            stopwatch.Stop();

            Console.WriteLine("----------------------------------");
            Console.WriteLine("Elapsed Time: " + stopwatch.ElapsedMilliseconds);
            Console.WriteLine("Problem Size: " + _cities.Length);
            Console.WriteLine("Max States: " + _maxStatesStored);
            Console.WriteLine("Total Created: " + _createdStates);
            Console.WriteLine("States Pruned: " + _prunedSates);
            Console.WriteLine("----------------------------------");

            return(GeneratePathResult(_bestStateSoFar, _cities, stopwatch.ElapsedMilliseconds));
        }