/// <summary> /// checks if a list has contains a node /// used to check if node has aready checked a node /// </summary> /// <param name="nodes">the list of nodes</param> /// <param name="node">the node to check against</param> /// <returns>whether the node is in the list</returns> protected bool ContainsNode(List <Node> nodes, Node node) { foreach (Node a in nodes) { if (node.EqualsPos(a)) { return(true); } } return(false); }
private SearchResult RecursiveSearch(SearchResult node, int limit) { // checks if node is at goal if (node.Result.Cell == CellTypes.GOAL) { return(node); } List <SearchResult> score = new List <SearchResult>(); // give a score to each node foreach (Node child in node.Result.Children) { // checks node is not in the current path Node parent = node.Result; bool inCurPath = false; while (parent.Parent is Node) { parent = parent.Parent; if (parent.EqualsPos(child)) { inCurPath = true; } } // jump out of cycle if in current path if (!inCurPath) { score.Add(new SearchResult(child, MovePortential(child.X, child.Y) + NodeCost(child))); Count++; } } while (score.Count != 0) { SearchResult best = score.OrderBy(n => n.CostLimit).First(); if (best.CostLimit > limit || best.CostLimit == int.MaxValue) { return(new SearchResult(null, best.CostLimit)); } SearchResult result = RecursiveSearch(best, (score.Count > 1) ? Math.Min(limit, score.OrderBy(n => n.CostLimit).ElementAt(1).CostLimit) : limit); best.CostLimit = result.CostLimit; if (result.Result is Node) { return(result); } //score.Remove(best); } return(new SearchResult(null, int.MaxValue)); }
private SearchResult RecursiveSearch(Node node, int depth) { // checks if node is at goal if (depth == 0) { if (node.Cell == CellTypes.GOAL) { return(new SearchResult(node, true)); } else { return(new SearchResult(null, true)); } } bool remaining = false; foreach (Node child in node.Children) { // checks node is not in the current path Node parent = node; bool inCurPath = false; while (parent.Parent is Node) { parent = parent.Parent; if (parent.EqualsPos(child)) { inCurPath = true; } } // jump out of cycle if in current path if (!inCurPath) { Count++; SearchResult result = RecursiveSearch(child, depth - 1); if (result.Goal is Node) { return(result); } if (result.Remaining) { remaining = true; } } } return(new SearchResult(null, remaining)); }
public override void Update() { if (state == State.IN) { // checks if node is at goal if (focus[0].Result.Cell == CellTypes.GOAL) { finished = true; return; } state = State.CONTINUE; score.Insert(0, new List <SearchResult>()); foreach (Node child in focus[0].Result.Children) { if (!ContainsNode(CheckedNodes, child)) { CheckedNodes.Add(child); } // checks node is not in the current path Node parent = focus[0].Result; bool inCurPath = false; while (parent.Parent is Node) { parent = parent.Parent; if (parent.EqualsPos(child)) { inCurPath = true; } } if (!inCurPath) { score[0].Add(new SearchResult(child, MovePortential(child.X, child.Y) + NodeCost(child))); } } if (score[0].Count == 0) { returnValue = int.MaxValue; state = State.OUT; score.RemoveAt(0); } return; } if (state == State.CONTINUE) { SearchResult best = score[0].OrderBy(n => n.CostLimit).First(); if (best.CostLimit > limit[0] || best.CostLimit == int.MaxValue) { returnValue = best.CostLimit; state = State.OUT; score.RemoveAt(0); return; } // enter into next depth focus.Insert(0, best); limit.Insert(0, (score[0].Count > 1) ? Math.Min(limit[0], score[0].OrderBy(n => n.CostLimit).ElementAt(1).CostLimit) : limit[0]); state = State.IN; return; } if (state == State.OUT) { state = State.CONTINUE; focus[0].CostLimit = returnValue; focus.RemoveAt(0); limit.RemoveAt(0); } }