/// <summary> /// Searches for a solution to the provided problem using the algorithm of the derived class. /// </summary> /// <param name="problem">The <see cref="SearchProblem"/> to be searched.</param> /// <param name="heuristic">The <see cref="IHeuristic"/> to use when evaluating states.</param> /// <returns>An <see cref="Array"/> of <see cref="State"/>s that will lead to the solution or null if no solution is possible.</returns> public State[] Search(SearchProblem problem, IHeuristic heuristic = null) { if (heuristic == null) { heuristic = new NullHeuristic(); } this.StartStopwatch(); State[] result = this.RunSearch(problem, heuristic); this.StopStopwatch(); return(result); }
static public List <Action> AStar(SearchProblem <State, Action> problem) { int iterationNum = 0; Priority_Queue.SimplePriorityQueue <(State, List <Action>)> fringe = new Priority_Queue.SimplePriorityQueue <(State, List <Action>)>(); HashSet <State> closedSet = new HashSet <State>(); fringe.Enqueue((problem.GetStartState(), new List <Action>()), 0); while (fringe.Count > 0) { (State state, List <Action> actions)next = fringe.Dequeue(); if (problem.IsGoalState(next.state)) { return(next.actions); } if (!closedSet.Contains(next.state)) { closedSet.Add(next.state); List <(State, Action)> successors = problem.GetSuccessors(next.state); if (++iterationNum > 100) { //Debug.Log("A* is iterating too much"); break; } foreach ((State state, Action action)successor in successors) { List <Action> newActions = new List <Action>(next.actions); newActions.Add(successor.action); float priority = problem.GetCost(newActions) + problem.Heuristic(successor.state); fringe.Enqueue((successor.state, newActions), priority); } } } return(new List <Action>()); }
/// <summary> /// Algorithm specific implementation for searching. /// </summary> /// <param name="problem">The <see cref="SearchProblem"/> to be searched.</param> /// <param name="heuristic">The <see cref="IHeuristic"/> to use when evaluating states.</param> /// <returns>An <see cref="Array"/> of <see cref="State"/>s that will lead to the solution or null if no solution is possible.</returns> protected override State[] RunSearch(SearchProblem problem, IHeuristic heuristic) { /* * Implement the Breadth First Search algorithm here. * * The SearchProblem class contains useful methods for implementing the algorithm: * SearchProblem.GetStartingState() * SearchProblem.IsGoalState(State state) * SearchProblem.GetSuccessors(State state) * * The SearchAlgorithm class also contains a way to time the execution of your implementation: * The SearchAlgorithm.Duration property will contain a TimeSpan of your code's execution time * * The IHeuristic interface contains a method for evaluating a state, given a problem: * IHeuristic.EvaluateState(SearchProblem problem, State state) * * .NET contains built in data structures for implementing some of the search algorithms * - Queue<T> * - Stack<T> * * The PriorityQueue<T> class is an implementation of the priority queue structure. See the Uility.Classes.PriorityQueue.cs file. * * Examples: * SearchProblem Examples: * problem.GetStartingState(); // gets the starting state of the problem * * Heuristic Examples: * heuristic.EvaluateState(problem, state); // returns a double with the expected value of this state in terms of reaching the goal state * * PriorityQueue<T> Examples: * PriorityQueue<State> pQueue = new PriorityQueue<State>(); // creates a new priority queue instance for State objects * pQueue.push(state, 4.3); // places the State object "state" in queue with a priority of 4.3 * State next = pQueue.Top; // gets the State object in queue with the smallest priority value * pQueue.Pop(); // removes the State object with the smallest priority value from the queue */ return(null); }
public double EvaluateState(SearchProblem problem, State state) { return(0d); }
/// <summary> /// Algorithm specific implementation for searching. /// </summary> /// <param name="problem">The <see cref="SearchProblem"/> to be searched.</param> /// <param name="heuristic">The <see cref="IHeuristic"/> to use when evaluating states.</param> /// <returns>An <see cref="Array"/> of <see cref="State"/>s that will lead to the solution or null if no solution is possible.</returns> protected abstract State[] RunSearch(SearchProblem problem, IHeuristic heuristic);
public double EvaluateState(SearchProblem problem, State state) { return((this.p_Function.ContainsKey(state.GetHashCode())) ? this.p_Function[state.GetHashCode()] : 0d); }