public int CompareTo(Node other) { int compare = EstimatedTotalCost.CompareTo(other.EstimatedTotalCost); if (compare == 0) { compare = HeuristicCost.CompareTo(other.HeuristicCost); } return(-compare); }
/// <summary> /// Finds the path in some problem space from a starting state until a goal condition is satified. <para/> /// A common application of a heuristic search is implementing path finding. /// </summary> /// <remarks> /// This search implements the famous A* algorithm. /// </remarks> /// <typeparam name="T">Type of the state elements.</typeparam> /// <param name="start">Starting state</param> /// <param name="goalCondition">Search predicate</param> /// <param name="getSuccessors">Returns sucessors</param> /// <param name="cost">Cost function between two states.</param> /// <param name="heuristic">Estimate cost between state and goal state.</param> /// <returns>A sequence of states from start (inclusive) to goal (inclusive).</returns> public static IReadOnlyList <T> HeuristicSearch <T>(T start, Func <T, bool> goalCondition, Func <T, IEnumerable <T> > getSuccessors, ActualCost <T> cost, HeuristicCost <T> heuristic) { // var nodes = new Dictionary <T, Node <T> >(); // var frontier = new Heap <Node <T> > { new Node <T>(start) { State = start, FScore = heuristic(start), GScore = 0, } }; Node <T> GetNode(T state) { if (!nodes.TryGetValue(state, out var node)) { node = new Node <T>(state); nodes[state] = node; } return(node); }
/// <summary> /// Finds the path in some problem space from a starting state until a goal state has been reached. <para/> /// A common application of a heuristic search is implementing path finding. /// </summary> /// <remarks> /// This search implements the famous A* algorithm. /// </remarks> /// <typeparam name="T">Type of the state elements.</typeparam> /// <param name="start">Starting state</param> /// <param name="goal">Terminating state.</param> /// <param name="getSuccessors">Returns sucessors</param> /// <param name="cost">Cost function between two states.</param> /// <param name="heuristic">Estimate cost between state and goal state.</param> /// <returns>A sequence of states from start (inclusive) to goal (inclusive).</returns> public static IReadOnlyList <T> HeuristicSearch <T>(T start, T goal, Func <T, IEnumerable <T> > getSuccessors, ActualCost <T> cost, HeuristicCost <T> heuristic) { return(HeuristicSearch(start, x => Equals(x, goal), getSuccessors, cost, heuristic)); }