From c49365523bcd8680a915a4ff7cc861bae93bdb81 Mon Sep 17 00:00:00 2001 From: Simon O'Shea Date: Tue, 8 Aug 2023 18:35:53 -0400 Subject: broken AStar has been implemented need to fix sorting problem because the returned order of movements does not make any sense --- .../Assets/Scripts/Pathfinder.cs | 38 +++++++++++++--------- 1 file changed, 23 insertions(+), 15 deletions(-) (limited to 'Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs') diff --git a/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs b/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs index 884a05a..a3fe8e0 100644 --- a/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs +++ b/Pathfinding Visualizer/Assets/Scripts/Pathfinder.cs @@ -4,7 +4,7 @@ using UnityEngine; using System.Linq; -public class Pathfinder : MonoBehaviour +public class Pathfinder { // used to store all unique states that agent has visited (holds agent and sample coords) @@ -58,45 +58,53 @@ public class Pathfinder : MonoBehaviour public void StartWork() { Node initialState = new Node(null, agent, samples, '0', 0, 0); - Stack result = new Stack(); + List result = new List(); if (algorithm == 0) { result = StartAStar(initialState); - + Debug.Log(" "); + for(int i = 0; i < result.Count; i++) + { + Node n = result[i]; + Debug.Log(n.lastMove); + } } } - public static Stack StartAStar(Node initialState) + public static List StartAStar(Node initialState) { - Stack solution = new Stack(); + List solution = new List(); - Queue open = new Queue(); + List open = new List(); //PriorityQueue open = new PriorityQueue(); - open.Enqueue(initialState); - int cap = 10000; + open.Add(initialState); + int cap = 1000; while (true && cap > 0) { if (!open.Any()) return solution; - Node currentNode = open.Dequeue(); + open = open.OrderBy(Node => Node.fn).ToList(); + + Node currentNode = open[0]; + open.RemoveAt(0); + // check whether we are in goal state if (currentNode.samples.Count() == 0) { - - var orderedList = open.OrderBy(Node => Node.fn); - while (currentNode.parent != null) { - solution.Push(currentNode); + Debug.Log("Agent at: " + currentNode.agent.x + ", " + currentNode.agent.y); + solution.Add(currentNode); currentNode = currentNode.parent; } + return solution; } @@ -112,12 +120,12 @@ public class Pathfinder : MonoBehaviour foreach (Node child in children) { - open.Enqueue(child); + open.Add(child); } cap--; } - + return solution; } // HEURISTICS FOR ASTAR public static List h0(List children) -- cgit v1.3.1-4-g156e