2016-11-27 05:39:50 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace SpriteCompiler.AI
|
|
|
|
|
{
|
2016-12-05 05:14:51 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
2016-11-27 05:39:50 +00:00
|
|
|
|
public abstract class InformedNodeExpander<A, S, T, C> : INodeExpander<A, S, T, C>
|
|
|
|
|
where T : HeuristicSearchNode<A, S, T, C>
|
2016-11-29 06:20:29 +00:00
|
|
|
|
where C : IPathCost<C>, new()
|
2016-11-27 05:39:50 +00:00
|
|
|
|
{
|
|
|
|
|
public abstract T CreateNode(T parent, S state);
|
|
|
|
|
|
|
|
|
|
public IEnumerable<T> Expand(ISearchProblem<A, S, C> problem, T node)
|
|
|
|
|
{
|
2016-12-05 05:14:51 +00:00
|
|
|
|
var successors = problem.Successors(node.State);
|
|
|
|
|
|
|
|
|
|
// Debug
|
|
|
|
|
Console.WriteLine(String.Format("There are {0} successors for {1}", successors.Count(), node));
|
|
|
|
|
Console.WriteLine(String.Format("This node has a current path cost of {0}", node.PathCost));
|
|
|
|
|
|
|
|
|
|
foreach (var successor in successors)
|
2016-11-27 05:39:50 +00:00
|
|
|
|
{
|
2016-11-30 22:13:43 +00:00
|
|
|
|
var action = successor.Item1;
|
|
|
|
|
var state = successor.Item2;
|
2016-11-27 05:39:50 +00:00
|
|
|
|
var next = CreateNode(node, state);
|
2016-12-05 05:14:51 +00:00
|
|
|
|
|
|
|
|
|
next.Action = action;
|
2016-11-27 05:39:50 +00:00
|
|
|
|
next.StepCost = problem.StepCost(node.State, action, state);
|
|
|
|
|
next.Heuristic = problem.Heuristic(state);
|
|
|
|
|
|
2016-12-05 05:14:51 +00:00
|
|
|
|
Console.WriteLine(" Action = " + next.Action + ", g(n') = " + next.PathCost + ", h(n') = " + next.Heuristic);
|
|
|
|
|
|
2016-11-27 05:39:50 +00:00
|
|
|
|
yield return next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|