fixed dfs with string closed list

This commit is contained in:
dereelatwit
2023-08-09 12:43:20 -04:00
parent 53d3bee642
commit 716995cd92
@@ -203,20 +203,19 @@ public class Pathfinder
{ {
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)
@@ -229,14 +228,14 @@ public class Pathfinder
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)
{ {
open.Add(child); dfsStack.Push(child);
} }
cap--; cap--;
} }
Debug.Log("Too much work :("); Debug.Log("Too much work :(");
return solution; return solution;
} }
@@ -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,6 +466,16 @@ public class Node
return new State(this.agent, this.samples); return new State(this.agent, this.samples);
} }
public string getStateString()
{
string stateStr = agent.ToString();
foreach (var obstacle in obstacles)
stateStr += obstacle.ToString();
foreach (var sample in samples)
stateStr += sample.ToString();
return stateStr;
}
// we use to override comparisons for priorityQueues so that our heuristic calculations are actually used // we use to override comparisons for priorityQueues so that our heuristic calculations are actually used
/* public int CompareTo(Node other) /* public int CompareTo(Node other)
{ {