示例#1
0
        internal static Node <TFactor, TStep> Run <TFactor, TStep>(this HeuristicSearchBase <TFactor, TStep> source)
        {
            Debug.WriteLine($"Searching path between {source.From} and {source.To} with {source.AlgorithmName}...");

            var lastNode        = default(Node <TFactor, TStep>);
            var observerFactory = source.AlgorithmObserverFactory;
            var observer        = observerFactory != null?observerFactory.Create(source) : null;

            switch (source.AlgorithmName)
            {
            case nameof(AStar):
                lastNode = observer == null?AStar.Run(source) : AStar.Run(source, observer);

                break;

            case nameof(BestFirstSearch):
                lastNode = observer == null?BestFirstSearch.Run(source) : BestFirstSearch.Run(source, observer);

                break;

            case nameof(IterativeDeepeningAStar):
                lastNode = observer == null?IterativeDeepeningAStar.Run(source) : IterativeDeepeningAStar.Run(source, observer);

                break;

            case nameof(RecursiveBestFirstSearch):
                lastNode = observer == null?RecursiveBestFirstSearch.Run(source) : RecursiveBestFirstSearch.Run(source, observer);

                break;

            default:
                var algorithm = HeuristicSearch.RegisteredAlgorithms[source.AlgorithmName](source.AlgorithmName);

                if (algorithm is IObservableAlgorithm && observer != null)
                {
                    lastNode = (algorithm as IObservableAlgorithm).Run(source, observer);
                }
                else
                {
                    lastNode = algorithm.Run(source);
                }
                break;
            }

            return(lastNode);
        }
示例#2
0
        public override IEnumerator <TFactor> GetEnumerator()
        {
            Debug.WriteLine($"Searching path between {From} and {To} with {AlgorithmName}...");

            var lastNode = default(Node <TFactor, TStep>);

            switch (AlgorithmName)
            {
            case nameof(AStar):
                lastNode = AStar.Run(this);
                break;

            case nameof(BestFirstSearch):
                lastNode = BestFirstSearch.Run(this);
                break;

            case nameof(IterativeDeepeningAStar):
                lastNode = IterativeDeepeningAStar.Run(this);
                break;

            case nameof(RecursiveBestFirstSearch):
                lastNode = RecursiveBestFirstSearch.Run(this);
                break;

            default:
                lastNode = HeuristicSearch.RegisteredAlgorithms[AlgorithmName](AlgorithmName).Run(this);
                break;
            }
            if (lastNode == null) // Solution not found
            {
                return(Enumerable.Empty <TFactor>().GetEnumerator());
            }

            if (IsReversed)
            {
                return(lastNode.EnumerateReverseFactors().GetEnumerator());
            }
            else
            {
                return(lastNode.TraceBack().EnumerateFactors().GetEnumerator());
            }
        }
        public override IEnumerator <TResult> GetEnumerator()
        {
            Debug.WriteLine($"Searching path between {From} and {To} with {AlgorithmName}...");

            switch (AlgorithmName)
            {
            case nameof(AStar):
                return(AStar.Run(this).GetEnumerator());

            case nameof(BestFirstSearch):
                return(BestFirstSearch.Run(this).GetEnumerator());

            case nameof(RecursiveBestFirstSearch):
                return(RecursiveBestFirstSearch.Run(this).GetEnumerator());

            case nameof(IterativeDeepeningAStar):
                return(IterativeDeepeningAStar.Run(this).GetEnumerator());
            }
            return(base.GetEnumerator());
        }