diff --git a/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs b/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs index 8f6304b..2f10505 100644 --- a/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs +++ b/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs @@ -68,6 +68,11 @@ public class Pathfinder if (algorithm == 0) { result = StartAStar(initialState, heuristic); + } + // Run DFS + if (algorithm == 1) + { + result = StartDFS(initialState, -1); } } @@ -76,10 +81,10 @@ public class Pathfinder Stack solution = new Stack(); List open = new List(); + open.Add(initialState); Debug.Log("GO!"); - open.Add(initialState); int cap = 200000; while (true && cap > 0) { @@ -95,7 +100,7 @@ public class Pathfinder // check whether we are in goal state - if (currentNode.samples.Count() == 0) + if (currentNode.samples.Count == 0) { Debug.Log("Gottem"); @@ -112,11 +117,11 @@ public class Pathfinder List children = currentNode.expand(-1); if (heuristic == 0) - h2(children);/* + h2(children); else if (heuristic == 1) h1(children); else if (heuristic == 2) - h2(children);*/ + h2(children); foreach (Node child in children) { @@ -187,6 +192,63 @@ public class Pathfinder return children; } + + // ALTERNATE SEARCH ALGORITHMS + public static Stack StartDFS(Node initialState, int depth) + { + Debug.Log("DOING DFS"); + Stack solution = new Stack(); + + // used to store nodes not visited (generated but not expanded) + List open = new List(); + open.Add(initialState); + + int cap = 100000; + while (true && cap > 0) + { + // if open is empty, we have exhausted all of our options and there is no solution + if (!open.Any()) + return solution; + + Node currentNode = open[0]; + + // check if agent is in goal state + if (currentNode.samples.Count == 0) + { + while (currentNode.parent != null) + { + solution.Push(currentNode); + currentNode = currentNode.parent; + } + return solution; + } + + // otherwise, expand and continue down path + List children = currentNode.expand(depth); + foreach (Node child in children) + { + open.Add(child); + } + cap--; + } + Debug.Log("Too much work :("); + return solution; + } + public static Stack ids(Node initialState) + { + Debug.Log("RUNNING IDS"); + Stack result = new Stack(); + int depth = 1; + + while (true) + { + result = StartDFS(initialState, depth); + if (result.Count != 0) + return result; + depth++; + closed.Clear(); + } + } } public class Node