iigs-sprite-compiler/SpriteCompiler/AI/GraphSearch.cs

39 lines
1.3 KiB
C#

namespace SpriteCompiler.AI
{
using System.Collections.Generic;
using Queue;
public class GraphSearch<A, S, T, C> : AbstractSearchStrategy<A, S, T, C>
where T : ISearchNode<A, S, T, C>
where C : ICost<C>
{
private readonly ISet<S> closed = new HashSet<S>();
public GraphSearch(INodeExpander<A, S, T, C> expander)
: base(expander)
{
}
/// <summary>
/// Generic graph search. See page 83 in Russell and Norvig. This only works in informed
/// search if the heuristic is admissible. However, if a heuristic is not admissible and
/// you still want to use, that means you should know enough to extend this class or write
/// your own Search class.
/// </summary>
public override IEnumerable<T> Search(ISearchProblem<A, S, C> problem, IQueue<T> fringe, S initialState)
{
closed.Clear();
return base.Search(problem, fringe, initialState);
}
protected override void AddNodes(IQueue<T> fringe, T node, ISearchProblem<A, S, C> problem)
{
if (!closed.Contains(node.State))
{
closed.Add(node.State);
fringe.AddRange(Expander.Expand(problem, node));
}
}
}
}