fixed dfs with string closed list

This commit is contained in:
dereelatwit
2023-08-09 12:43:20 -04:00
parent 53d3bee642
commit 716995cd92
@@ -201,45 +201,44 @@ public class Pathfinder
// ALTERNATE SEARCH ALGORITHMS // ALTERNATE SEARCH ALGORITHMS
public static Stack<Node> StartDFS(Node initialState, int depth) public static Stack<Node> StartDFS(Node initialState, int depth)
{ {
Debug.Log("DOING DFS"); Debug.Log("DOING DFS");
Stack<Node> solution = new Stack<Node>(); Stack<Node> solution = new Stack<Node>();
Stack<Node> dfsStack = new Stack<Node>(); // Stack for DFS
HashSet<string> visited = new HashSet<string>(); // For avoiding revisiting states
// used to store nodes not visited (generated but not expanded) dfsStack.Push(initialState);
List<Node> open = new List<Node>();
open.Add(initialState);
int cap = 90000; int cap = 90000;
while (true && cap > 0) while (dfsStack.Count > 0 && cap > 0)
{ {
// if open is empty, we have exhausted all of our options and there is no solution Node currentNode = dfsStack.Pop();
if (!open.Any())
return solution;
Node currentNode = open[0]; // If we have already visited this state, skip processing
open.RemoveAt(0); if (visited.Contains(currentNode.getStateString())) continue;
visited.Add(currentNode.getStateString());
// check if agent is in goal state // check if agent is in goal state
if (currentNode.samples.Count == 0) if (currentNode.samples.Count == 0)
{ {
while (currentNode.parent != null) while (currentNode.parent != null)
{ {
solution.Push(currentNode); solution.Push(currentNode);
currentNode = currentNode.parent; currentNode = currentNode.parent;
} }
return solution; return solution;
} }
// otherwise, expand and continue down path List<Node> children = currentNode.expand(-1);
List<Node> children = currentNode.expand(depth); foreach (Node child in children)
foreach (Node child in children) {
{ dfsStack.Push(child);
open.Add(child); }
} cap--;
cap--; }
}
Debug.Log("Too much work :("); Debug.Log("Too much work :(");
return solution; return solution;
} }
public static Stack<Node> StartIDS(Node initialState) public static Stack<Node> StartIDS(Node initialState)
{ {
Debug.Log("RUNNING IDS"); Debug.Log("RUNNING IDS");
@@ -371,7 +370,7 @@ public class Node
public bool isOpen(Vector2Int position) public bool isOpen(Vector2Int position)
{ {
// check that agent is not trying to move into an obstacle // check that agent is not trying to move into an obstacle
if (obstacles.Contains(agent)) if (obstacles.Contains(position))
return false; return false;
if ((position.y < 0) || (position.y > Pathfinder.height - 1) || (position.x < 0) || (position.x > Pathfinder.width - 1)) if ((position.y < 0) || (position.y > Pathfinder.height - 1) || (position.x < 0) || (position.x > Pathfinder.width - 1))
@@ -467,16 +466,26 @@ public class Node
return new State(this.agent, this.samples); return new State(this.agent, this.samples);
} }
// we use to override comparisons for priorityQueues so that our heuristic calculations are actually used public string getStateString()
/* public int CompareTo(Node other) {
{ string stateStr = agent.ToString();
if (this.fn > other.fn) foreach (var obstacle in obstacles)
return 1; stateStr += obstacle.ToString();
else foreach (var sample in samples)
return -1; stateStr += sample.ToString();
}*/ return stateStr;
}
} // we use to override comparisons for priorityQueues so that our heuristic calculations are actually used
/* public int CompareTo(Node other)
{
if (this.fn > other.fn)
return 1;
else
return -1;
}*/
}
// helper class for Node // helper class for Node
/* class NodeComparator : Comparer<Node> /* class NodeComparator : Comparer<Node>