예제 #1
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);
        }
예제 #2
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);
        }