From 4046a86cd110bb978c5ab3c1bf05fcab0fbd41a9 Mon Sep 17 00:00:00 2001 From: Simon O'Shea Date: Wed, 9 Aug 2023 09:48:55 -0400 Subject: [PATCH] astar now supports multiple consecutive runs --- .../Assets/Scripts/Pathfinder.cs | 48 +++++++++++-------- 1 file changed, 27 insertions(+), 21 deletions(-) diff --git a/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs b/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs index 556abdb..d19ba73 100644 --- a/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs +++ b/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs @@ -11,12 +11,12 @@ public class Pathfinder public static List closed = new List(); LogicGrid world; - public static Vector2Int agent; - public static List obstacles; - public static List samples; + public Vector2Int agent; + public List obstacles; + public List samples; - public static int algorithm; // 0 = A* | 1 = dfs | 2 = bfs - public static int heuristic; // 0 = h0 | 1 = h1 | 2 = h2 + public int algorithm; // 0 = A* | 1 = dfs | 2 = bfs + public int heuristic; // 0 = h0 | 1 = h1 | 2 = h2 public static int height; public static int width; @@ -26,6 +26,8 @@ public class Pathfinder public Pathfinder(LogicGrid world, Vector2Int a, List o, List s, int algo, int heu) { + closed = new List(); + this.world = world; height = world.GetHeight(); width = world.GetWidth(); @@ -59,31 +61,32 @@ public class Pathfinder public void StartWork() { - Node initialState = new Node(null, agent, samples, '0', 0, 0); + Node initialState = new Node(null, agent, obstacles, samples, '0', 0, 0); result = new Stack(); // Run AStar if (algorithm == 0) { - result = StartAStar(initialState); + result = StartAStar(initialState, heuristic); } } - public static Stack StartAStar(Node initialState) + public static Stack StartAStar(Node initialState, int heuristic) { - Debug.Log("GO!"); - Stack solution = new Stack(); List open = new List(); - - open.Add(initialState); + Debug.Log("GO!"); + + open.Add(initialState); int cap = 1000; while (true && cap > 0) { if (!open.Any()) - return solution; + { + return solution; + } open = open.OrderBy(Node => Node.fn).ToList(); @@ -201,7 +204,9 @@ public class Node public static int col = 1; public Node parent; + public Vector2Int agent; + public List obstacles; public List samples; public char lastMove; // for output/animation @@ -211,10 +216,11 @@ public class Node //bool canSample; // for expansion // construct node :) - public Node(Node parent, Vector2Int agent, List samples, char lastMove, int distanceTraveled, double heuristic) + public Node(Node parent, Vector2Int agent, List obstacles, List samples, char lastMove, int distanceTraveled, double heuristic) { this.parent = parent; this.agent = agent; + this.obstacles = new List(obstacles); this.samples = new List(samples); this.lastMove = lastMove; this.distanceTraveled = distanceTraveled; @@ -250,7 +256,7 @@ public class Node if (isOpen(up)) { // if move is valid, create that new node/state and document - Node child = new Node(this, up, this.samples, 'U', this.distanceTraveled + 1, this.heuristic); + Node child = new Node(this, up, this.obstacles, this.samples, 'U', this.distanceTraveled + 1, this.heuristic); //SampleWorld.nodesGenerated++; // make sure that we have not already made that move @@ -261,7 +267,7 @@ public class Node // same idea but for the different potential moves if (isOpen(down)) { - Node child = new Node(this, down, this.samples, 'D', this.distanceTraveled + 1, this.heuristic); + Node child = new Node(this, down, this.obstacles, this.samples, 'D', this.distanceTraveled + 1, this.heuristic); //SampleWorld.nodesGenerated++; if (!child.getState().inClosed()) @@ -270,7 +276,7 @@ public class Node if (isOpen(left)) { - Node child = new Node(this, left, this.samples, 'L', this.distanceTraveled + 1, this.heuristic); + Node child = new Node(this, left, this.obstacles, this.samples, 'L', this.distanceTraveled + 1, this.heuristic); //SampleWorld.nodesGenerated++; if (!child.getState().inClosed()) @@ -279,7 +285,7 @@ public class Node if (isOpen(right)) { - Node child = new Node(this, right, this.samples, 'R', this.distanceTraveled + 1, this.heuristic); + Node child = new Node(this, right, this.obstacles, this.samples, 'R', this.distanceTraveled + 1, this.heuristic); //SampleWorld.nodesGenerated++; if (!child.getState().inClosed()) @@ -290,7 +296,7 @@ public class Node onSample = CanSample(); if (onSample.x == 1) { - Node child = new Node(this, this.agent, this.samples, 'S', this.distanceTraveled + 1, this.heuristic); + Node child = new Node(this, this.agent, this.obstacles, this.samples, 'S', this.distanceTraveled + 1, this.heuristic); child.samples.RemoveAt(onSample.y); @@ -305,9 +311,9 @@ public class Node public bool isOpen(Vector2Int position) { // check that agent is not trying to move into an obstacle - for (int i = 0; i < Pathfinder.obstacles.Count(); i++) + for (int i = 0; i < obstacles.Count(); i++) { - if (Pathfinder.obstacles[i].x.Equals(position.x) && Pathfinder.obstacles[i].y.Equals(position.y)) + if (obstacles[i].x.Equals(position.x) && obstacles[i].y.Equals(position.y)) return false; }