Ejemplo n.º 1
0
        internal HeuristicSearchBase(HeuristicSearchBase <TFactor, TStep> source)
            : this(source.AlgorithmName, source.From, source.To, source.StepComparer, source.NodeComparer, source.Converter, source.Expander)
        {
            _source = source;

            IsReversed = source.IsReversed;
            AlgorithmObserverFactory = source.AlgorithmObserverFactory;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Determines whether the solution can be found.
        /// </summary>
        /// <typeparam name="TFactor">The type of factor used to evaluate with heuristic function. The type is projected from <typeparamref name="TStep"/>.</typeparam>
        /// <typeparam name="TStep">The type of step of the problem.</typeparam>
        /// <param name="source">The current instance.</param>
        /// <returns>true if the solution can be found; otherwise, false.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
        /// <exception cref="InvalidOperationException">
        /// The orderby clause is missing or <typeparamref name="TFactor"/> does not implement <see cref="IComparable{TFactor}"/> interface.
        /// </exception>
        public static bool Any <TFactor, TStep>(this HeuristicSearchBase <TFactor, TStep> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            return(source.Run() != null);
        }
Ejemplo n.º 3
0
        public HeuristicSearchSelect(HeuristicSearchBase <TSource, TStep> source, Func <TSource, int, TFactor> selector)
            : base(source.AlgorithmName, source.From, source.To, source.StepComparer, null, null, source.Expander)
        {
            _source   = source;
            _selector = selector;

            IsReversed = source.IsReversed;
            AlgorithmObserverFactory = source.AlgorithmObserverFactory;
        }
        internal HeuristicSearchSelectMany(HeuristicSearchBase <TSource, TStep> source,
                                           Func <TSource, int, IEnumerable <TCollection> > collectionSelector, Func <TSource, TCollection, TFactor> factorSelector)
            : base(source.AlgorithmName, source.From, source.To, source.StepComparer, null, null, source.Expander)
        {
            _source             = source;
            _collectionSelector = collectionSelector;
            _factorSelector     = factorSelector;

            IsReversed = source.IsReversed;
            AlgorithmObserverFactory = source.AlgorithmObserverFactory;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Inverts the order of solution from <see cref="HeuristicSearchBase{TFactor, TStep}.From"/> to <see cref="HeuristicSearchBase{TFactor, TStep}.To"/>
        /// to <see cref="HeuristicSearchBase{TFactor, TStep}.To"/> to <see cref="HeuristicSearchBase{TFactor, TStep}.From"/>.
        /// </summary>
        /// <typeparam name="TFactor">The type of factor used to evaluate with heuristic function. The type is projected from <typeparamref name="TStep"/>.</typeparam>
        /// <typeparam name="TStep">The type of step of the problem.</typeparam>
        /// <param name="source">The current instance.</param>
        /// <returns>An instance with type <typeparamref name="TFactor"/> as factor.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
        public static HeuristicSearchBase <TFactor, TStep> Reverse <TFactor, TStep>(this HeuristicSearchBase <TFactor, TStep> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            source.IsReversed = !source.IsReversed;

            return(source);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Returns the number of steps in solution, in <see cref="Int64"/>.
        /// </summary>
        /// <typeparam name="TFactor">The type of factor used to evaluate with heuristic function. The type is projected from <typeparamref name="TStep"/>.</typeparam>
        /// <typeparam name="TStep">The type of step of the problem.</typeparam>
        /// <param name="source">The current instance.</param>
        /// <returns>The number of steps in solution.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
        /// <exception cref="InvalidOperationException">
        /// The orderby clause is missing or <typeparamref name="TFactor"/> does not implement <see cref="IComparable{TFactor}"/> interface.
        /// </exception>
        public static long LongCount <TFactor, TStep>(this HeuristicSearchBase <TFactor, TStep> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var lastNode = source.Run();

            return(lastNode == null ? 0 : lastNode.Level + 1);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Apply filter to current instance.
        /// </summary>
        /// <typeparam name="TFactor">The type of factor used to evaluate with heuristic function. The type is projected from <typeparamref name="TStep"/>.</typeparam>
        /// <typeparam name="TStep">The type of step of the problem.</typeparam>
        /// <param name="source">The current instance.</param>
        /// <param name="predicate">A function to test each source element and its index for a condition.</param>
        /// <returns>The instance with appied filter.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="predicate"/> is null.</exception>
        public static HeuristicSearchBase <TFactor, TStep> Where <TFactor, TStep>(this HeuristicSearchBase <TFactor, TStep> source, Func <TFactor, int, bool> predicate)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (predicate == null)
            {
                throw new ArgumentNullException(nameof(predicate));
            }

            return(new HeuristicSearchWhere <TFactor, TStep>(source, predicate));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Apply black-listing filter to current instance by giving a <typeparamref name="TStep"/> collection.
        /// </summary>
        /// <typeparam name="TFactor">The type of factor used to evaluate with heuristic function. The type is projected from <typeparamref name="TStep"/>.</typeparam>
        /// <typeparam name="TStep">The type of step of the problem.</typeparam>
        /// <param name="source">The current instance.</param>
        /// <param name="collection">The collection as filter condition.</param>
        /// <returns>The instance with appied filter.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="collection"/> is null.</exception>
        public static HeuristicSearchBase <TFactor, TStep> Except <TFactor, TStep>(this HeuristicSearchBase <TFactor, TStep> source, IEnumerable <TFactor> collection)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (collection == null)
            {
                throw new ArgumentNullException(nameof(collection));
            }

            return(new HeuristicSearchExcept <TFactor, TStep>(source, collection, null));
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Apply white-listing  filter to current instance by giving a <typeparamref name="TStep"/> collection and specific comparer.
        /// </summary>
        /// <typeparam name="TFactor">The type of factor used to evaluate with heuristic function. The type is projected from <typeparamref name="TStep"/>.</typeparam>
        /// <typeparam name="TStep">The type of step of the problem.</typeparam>
        /// <param name="source">The current instance.</param>
        /// <param name="collection">The collection as filter condition.</param>
        /// <param name="comparer">The specific comparer.</param>
        /// <returns>The instance with appied filter.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="collection"/> is null.</exception>
        public static HeuristicSearchBase <TFactor, TStep> Contains <TFactor, TStep>(this HeuristicSearchBase <TFactor, TStep> source, IEnumerable <TFactor> collection, IEqualityComparer <TFactor> comparer)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (collection == null)
            {
                throw new ArgumentNullException(nameof(collection));
            }

            return(new HeuristicSearchContains <TFactor, TStep>(source, collection, comparer));
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Returns last step of the solution, or default value of <typeparamref name="TFactor"/> if solution is not found.
        /// </summary>
        /// <typeparam name="TFactor">The type of factor used to evaluate with heuristic function. The type is projected from <typeparamref name="TStep"/>.</typeparam>
        /// <typeparam name="TStep">The type of step of the problem.</typeparam>
        /// <param name="source">The current instance.</param>
        /// <returns>The last step of the solution.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
        /// <exception cref="InvalidOperationException">
        /// The orderby clause is missing or <typeparamref name="TFactor"/> does not implement <see cref="IComparable{TFactor}"/> interface.
        /// </exception>
        public static TFactor LastOrDefault <TFactor, TStep>(this HeuristicSearchBase <TFactor, TStep> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var lastNode = source.Run();

            if (lastNode == null)
            {
                return(default(TFactor));
            }

            return(source.IsReversed ? lastNode.TraceBack().Factor : lastNode.Factor);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Returns last step of the solution.
        /// </summary>
        /// <typeparam name="TFactor">The type of factor used to evaluate with heuristic function. The type is projected from <typeparamref name="TStep"/>.</typeparam>
        /// <typeparam name="TStep">The type of step of the problem.</typeparam>
        /// <param name="source">The current instance.</param>
        /// <returns>The last step of the solution.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> is null.</exception>
        /// <exception cref="InvalidOperationException">Solution is not found.</exception>
        public static TFactor Last <TFactor, TStep>(this HeuristicSearchBase <TFactor, TStep> source)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var lastNode = source.Run();

            if (lastNode == null)
            {
                throw new InvalidOperationException("Sequence contains no elements.");
            }

            return(source.IsReversed ? lastNode.TraceBack().Factor : lastNode.Factor);
        }
Ejemplo n.º 12
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);
        }
Ejemplo n.º 13
0
 internal HeuristicSearchExcept(HeuristicSearchBase <TFactor, TStep> source, IEnumerable <TFactor> collection, IEqualityComparer <TFactor> comparer)
     : base(source)
 {
     _collection = collection;
     _comparer   = comparer;
 }
Ejemplo n.º 14
0
 internal HeuristicSearchOrderBy(HeuristicSearchBase <TFactor, TStep> source, INodeComparer <TFactor, TStep> nodeComparer)
     : base(source.AlgorithmName, source.From, source.To, source.StepComparer, nodeComparer, source.Converter, source.Expander)
 {
     IsReversed = source.IsReversed;
     AlgorithmObserverFactory = source.AlgorithmObserverFactory;
 }
        IProgress <AlgorithmState <TFactor, TStep> > IAlgorithmObserverFactory <TStep> .Create <TFactor>(HeuristicSearchBase <TFactor, TStep> source)
        {
            var progress = new AlgorithmProgress <TFactor, TStep>();

            OnCreated(progress);

            return(progress);
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Select one or more factors used to evaluate with heuristic functions.
        /// </summary>
        /// <typeparam name="TSource">The source type of factor used to evaluate with heuristic function.</typeparam>
        /// <typeparam name="TCollection">The type of the intermediate elements collected by the function represented by <paramref name="collectionSelector"/>.</typeparam>
        /// <typeparam name="TFactor">The type of factor used to evaluate with heuristic function. The type is projected from <typeparamref name="TStep"/>.</typeparam>
        /// <typeparam name="TStep">The type of step of the problem.</typeparam>
        /// <param name="source">The current instance.</param>
        /// <param name="collectionSelector">A projection function to apply to each element of the source.</param>
        /// <param name="factorSelector">The selector to select factor from current instance.</param>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="collectionSelector"/> or <paramref name="factorSelector"/> is null.</exception>
        /// <returns>An instance with type <typeparamref name="TFactor"/> as factor.</returns>
        /// <remarks>Only the factor with lowest cost estimated by heuristic function will be considerd.</remarks>
        public static HeuristicSearchBase <TFactor, TStep> SelectMany <TSource, TCollection, TFactor, TStep>(this HeuristicSearchBase <TSource, TStep> source, Func <TSource, IEnumerable <TCollection> > collectionSelector, Func <TSource, TCollection, TFactor> factorSelector)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (collectionSelector == null)
            {
                throw new ArgumentNullException(nameof(collectionSelector));
            }
            if (factorSelector == null)
            {
                throw new ArgumentNullException(nameof(factorSelector));
            }

            return(new HeuristicSearchSelectMany <TSource, TCollection, TFactor, TStep>(source, (s, i) => collectionSelector(s), factorSelector));
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Select one or more factors used to evaluate with heuristic functions.
        /// </summary>
        /// <typeparam name="TSource">The source type of factor used to evaluate with heuristic function.</typeparam>
        /// <typeparam name="TFactor">The type of factor used to evaluate with heuristic function. The type is projected from <typeparamref name="TStep"/>.</typeparam>
        /// <typeparam name="TStep">The type of step of the problem.</typeparam>
        /// <param name="source">The current instance.</param>
        /// <param name="selector">The selector with index argument to select factor from current instance.</param>
        /// <returns>An instance with type <typeparamref name="TFactor"/> as factor.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
        /// <remarks>Only the factor with lowest cost estimated by heuristic function will be considerd.</remarks>
        public static HeuristicSearchBase <TFactor, TStep> SelectMany <TSource, TFactor, TStep>(this HeuristicSearchBase <TSource, TStep> source, Func <TSource, int, IEnumerable <TFactor> > selector)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (selector == null)
            {
                throw new ArgumentNullException(nameof(selector));
            }

            return(new HeuristicSearchSelectMany <TSource, TFactor, TStep>(source, selector));
        }
Ejemplo n.º 18
0
 internal HeuristicSearchWhere(HeuristicSearchBase <TFactor, TStep> source, Func <TFactor, int, bool> predicate)
     : base(source)
 {
     _predicate = predicate;
 }
Ejemplo n.º 19
0
        /// <summary>
        /// Select the factor used to evaluate with heuristic functions.
        /// </summary>
        /// <typeparam name="TSource">The source type of factor used to evaluate with heuristic function.</typeparam>
        /// <typeparam name="TFactor">The type of factor used to evaluate with heuristic function. The type is projected from <typeparamref name="TStep"/>.</typeparam>
        /// <typeparam name="TStep">The type of step of the problem.</typeparam>
        /// <param name="source">The current instance.</param>
        /// <param name="selector">The selector to select factor from current instance.</param>
        /// <returns>An instance with type <typeparamref name="TFactor"/> as factor.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="selector"/> is null.</exception>
        public static HeuristicSearchBase <TFactor, TStep> Select <TSource, TFactor, TStep>(this HeuristicSearchBase <TSource, TStep> source, Func <TSource, TFactor> selector)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }
            if (selector == null)
            {
                throw new ArgumentNullException(nameof(selector));
            }

            return(new HeuristicSearchSelect <TSource, TFactor, TStep>(source, (s, i) => selector(s)));
        }