pathfinding class now interprets grid properly

can successfully read in the user-generated world!
This commit is contained in:
Simon O'Shea
2023-08-08 17:17:45 -04:00
parent 2dcb6ea96d
commit ddc02784c2
3 changed files with 48 additions and 67 deletions
+8 -16
View File
@@ -14,9 +14,9 @@ public class Main : MonoBehaviour
// Coordinates for pathfinding // Coordinates for pathfinding
public Vector2 agent = new Vector2(); public Vector2Int agent = new Vector2Int();
public List<Vector2> obstacles = new List<Vector2>(); public List<Vector2Int> obstacles = new List<Vector2Int>();
public List<Vector2> samples = new List<Vector2>(); public List<Vector2Int> samples = new List<Vector2Int>();
// Start is called before the first frame update // Start is called before the first frame update
void Start() void Start()
@@ -71,8 +71,8 @@ public class Main : MonoBehaviour
{ {
StartAlgorithm(); StartAlgorithm();
} }
//////////////////////////
//////////////////////////
// Update Cell: // Update Cell:
// Place value in cell based on selected placementValue with left click // Place value in cell based on selected placementValue with left click
@@ -85,11 +85,6 @@ public class Main : MonoBehaviour
{ {
world.SetValue(CodeMonkey.Utils.UtilsClass.GetMouseWorldPosition(), 0); world.SetValue(CodeMonkey.Utils.UtilsClass.GetMouseWorldPosition(), 0);
} }
// Make functionality for when "start" button is pressed.
// should call some sort of function that will take in the world
// and perform the selected algorithm
} }
public void SetModeObstacle() public void SetModeObstacle()
@@ -111,7 +106,7 @@ public class Main : MonoBehaviour
public void StartAlgorithm() public void StartAlgorithm()
{ {
Vector2 coord = new Vector2(); Vector2Int coord = new Vector2Int();
// Collect information about world // Collect information about world
for (int x = 0; x < world.GetWidth(); x++) for (int x = 0; x < world.GetWidth(); x++)
@@ -125,8 +120,6 @@ public class Main : MonoBehaviour
// Add coordinate to obstacle array // Add coordinate to obstacle array
if (value == 1) if (value == 1)
{ {
//coord = new int[2][];
coord.x = x; coord.x = x;
coord.y = y; coord.y = y;
@@ -136,18 +129,17 @@ public class Main : MonoBehaviour
// Add coordinate to sample array // Add coordinate to sample array
if (value == 2) if (value == 2)
{ {
//coord = new int[2][];
coord.x = x; coord.x = x;
coord.y = y; coord.y = y;
if (!samples.Contains(coord))
samples.Add(coord); samples.Add(coord);
} }
// Add coordinate to obstacle array // Add coordinate to obstacle array
if (value == 3) if (value == 3)
{ {
agent[0] = x; agent.x = x;
agent[1] = y; agent.y = y;
} }
} }
@@ -11,8 +11,8 @@ using UnityEngine;
public static int col = 1; public static int col = 1;
Node parent; Node parent;
int[] agent; Vector2Int agent;
List<int[]> samples; List<Vector2Int> samples;
char lastMove; // for output/animation char lastMove; // for output/animation
int distanceTraveled; // for calculating f(n) value int distanceTraveled; // for calculating f(n) value
double heuristic; double heuristic;
@@ -20,11 +20,11 @@ using UnityEngine;
bool canSample; // for expansion bool canSample; // for expansion
// construct node :) // construct node :)
public Node(Node parent, int[] agent, List<int[]> samples, char lastMove, int distanceTraveled, double heuristic) public Node(Node parent, Vector2Int agent, List<Vector2Int> samples, char lastMove, int distanceTraveled, double heuristic)
{ {
this.parent = parent; this.parent = parent;
this.agent = agent; this.agent = agent;
this.samples = new List<int[]>(samples); this.samples = samples;
this.lastMove = lastMove; this.lastMove = lastMove;
this.distanceTraveled = distanceTraveled; this.distanceTraveled = distanceTraveled;
this.heuristic = heuristic; this.heuristic = heuristic;
@@ -40,7 +40,7 @@ using UnityEngine;
// document expansion of node and get ready to collect this node's children // document expansion of node and get ready to collect this node's children
// SampleWorld.expansions++; // SampleWorld.expansions++;
int[] onSample = new int[2]; Vector2Int onSample = new Vector2Int();
// since this state is being currently visited (expanded), put in closed list // since this state is being currently visited (expanded), put in closed list
Pathfinder.closed.Add(this.getState()); Pathfinder.closed.Add(this.getState());
@@ -50,10 +50,10 @@ using UnityEngine;
//////////////////////////////////// ////////////////////////////////////
// store coordinates for all potential moves to be checked // store coordinates for all potential moves to be checked
int[] up = { this.agent[row] - 1, this.agent[col] }; Vector2Int up = new Vector2Int( this.agent.y - 1, this.agent.x );
int[] down = { this.agent[row] + 1, this.agent[col] }; Vector2Int down = new Vector2Int( this.agent.y + 1, this.agent.x );
int[] left = { this.agent[row], this.agent[col] - 1 }; Vector2Int left = new Vector2Int( this.agent.y, this.agent.x - 1 );
int[] right = { this.agent[row], this.agent[col] + 1 }; Vector2Int right = new Vector2Int( this.agent.y, this.agent.x + 1 );
// make sure going up doesn't go outside world-bounds or into obstacle // make sure going up doesn't go outside world-bounds or into obstacle
if (isOpen(up)) if (isOpen(up))
@@ -97,12 +97,12 @@ using UnityEngine;
// CHECK IF CAN SAMPLE // CHECK IF CAN SAMPLE
onSample = CanSample(); onSample = CanSample();
if (onSample[0] == 1) 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.samples, 'S', this.distanceTraveled + 1, this.heuristic);
// PROBLEM? // PROBLEM?
child.samples.RemoveAt(onSample[1]); child.samples.RemoveAt(onSample.y);
//SampleWorld.nodesGenerated++; //SampleWorld.nodesGenerated++;
children.Add(child); children.Add(child);
@@ -112,33 +112,29 @@ using UnityEngine;
} }
// helper for expand, verifies whether potential move is legal // helper for expand, verifies whether potential move is legal
public bool isOpen(int[] 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
for (int i = 0; i < Pathfinder.obstacles.Count(); i++) for (int i = 0; i < Pathfinder.obstacles.Count(); i++)
{ {
int[] coord = new int[2]; if (Pathfinder.obstacles[i].x == position.x && Pathfinder.obstacles[i].y == position.y)
coord[0] = Pathfinder.obstacles[i][0][0];
coord[1] = Pathfinder.obstacles[i][1][0];
if (Pathfinder.obstacles[i][0][0] == position[0] && Pathfinder.obstacles[i][1][0] == position[1])
return false; return false;
} }
// check that agent is not stepping out of the world // check that agent is not stepping out of the world
if (!((position[row] >= 0) && (position[row] <= Pathfinder.height - 1) && (position[col] >= 0) && (position[col] <= Pathfinder.width - 1))) if (!((position.y >= 0) && (position.y <= Pathfinder.height - 1) && (position.x >= 0) && (position.x <= Pathfinder.width - 1)))
return false; return false;
return true; return true;
} }
// returns the coordinates of the sample closest to the agent's current location // returns the coordinates of the sample closest to the agent's current location
public int[] nearestSample() public Vector2Int nearestSample()
{ {
if (samples.Count == 0) if (samples.Count == 0)
return agent; return agent;
// PROBLEM? Vector2Int s = samples[0];
int[] s = samples[0];
double lowest = distance(agent, samples[0]); double lowest = distance(agent, samples[0]);
double dist; double dist;
@@ -154,10 +150,10 @@ using UnityEngine;
return s; return s;
} }
// helper function for nearestSample() // helper function for nearestSample()
public static double distance(int[] a, int[] b) public static double distance(Vector2Int a, Vector2Int b)
{ // _________________________ { // _________________________
// distance between 2D points = √(y2 - y1)^2 + (x2 - x1)^2 // distance between 2D points = √(y2 - y1)^2 + (x2 - x1)^2
double total = (((b[row] - a[row]) * (b[row] - a[row])) + ((b[col] - a[col]) * (b[col] - a[col]))); double total = (((b.y - a.y) * (b.y - a.y)) + ((b.x - a.x) * (b.x - a.x)));
total = Math.Sqrt(total); total = Math.Sqrt(total);
return total; return total;
@@ -168,17 +164,16 @@ using UnityEngine;
// returns two pieces of information if true: // returns two pieces of information if true:
// [0] is 1 to indicate agent can sample // [0] is 1 to indicate agent can sample
// [1] is the index of the valid sample in the list // [1] is the index of the valid sample in the list
public int[] CanSample() public Vector2Int CanSample()
{ {
// default to false // default to false
int[] result = new int[2]; Vector2Int result = new Vector2Int();
result[0] = 0; result.x = 0;
result[1] = -1; result.y = -1;
for (int i = 0; i < this.samples.Count; i++) for (int i = 0; i < this.samples.Count; i++)
{ {
// PROBLEM? if ((this.agent.y == samples[i].y) && (this.agent.x == samples[i].x))
if ((this.agent[row] == samples[i][row]) && (this.agent[col] == samples[i][col]))
{ {
result[0] = 1; result[0] = 1;
result[1] = i; result[1] = i;
@@ -221,21 +216,19 @@ using UnityEngine;
// helper class for Node // helper class for Node
public class State public class State
{ {
int[] agent; Vector2Int agent;
int[][] samples; List<Vector2Int> samples;
public State(int[] agent, List<int[]> samples) public State(Vector2Int agent, List<Vector2Int> samples)
{ {
// PROBLEM?
this.agent = agent; this.agent = agent;
this.samples = new int[samples.Count][]; this.samples = samples;
// convert List<int[]> into 2D array of ints [][] // convert List<int[]> into 2D array of ints [][]
for (int i = 0; i < samples.Count; i++) /*for (int i = 0; i < samples.Count; i++)
{ {
// PROBLEM??
this.samples[i] = samples[i]; this.samples[i] = samples[i];
} }*/
} }
public bool inClosed() public bool inClosed()
@@ -252,21 +245,17 @@ using UnityEngine;
public bool equals(State other) public bool equals(State other)
{ {
if (this.samples.Length != other.samples.Length) if (this.samples.Count != other.samples.Count)
return false; return false;
// PROBLEM? if (!(this.agent.x.Equals(other.agent.x) && this.agent.y.Equals(other.agent.y)))
if (!this.agent[0].Equals(other.agent[0]) && !this.agent[1].Equals(other.agent[1]))
return false; return false;
for (int i = 0; i < this.samples.Length; i++) for (int i = 0; i < this.samples.Count; i++)
{ {
//if (Arrays.compare(this.samples[i], other.samples[i]) != 0) //if (Arrays.compare(this.samples[i], other.samples[i]) != 0)
// PROBLEM? if (this.samples.SequenceEqual(other.samples))
if (!(this.samples.Rank == other.samples.Rank) &&
!(Enumerable.Range(0, this.samples.Rank).All(dimension => this.samples.GetLength(dimension) == other.samples.GetLength(dimension))) &&
!(this.samples.Cast<double>().SequenceEqual(other.samples.Cast<double>())))
return false; return false;
} }
@@ -9,9 +9,9 @@ public class Pathfinder : MonoBehaviour
public static List<State> closed = new List<State>(); public static List<State> closed = new List<State>();
LogicGrid world; LogicGrid world;
public static int[] agent; public static Vector2Int agent;
public static List<int[][]> obstacles; public static List<Vector2Int> obstacles;
public static List<int[][]> samples; public static List<Vector2Int> samples;
public static int algorithm; // 0 = A* | 1 = dfs | 2 = bfs public static int algorithm; // 0 = A* | 1 = dfs | 2 = bfs
public static int heuristic; // 0 = h0 | 1 = h1 | 2 = h2 public static int heuristic; // 0 = h0 | 1 = h1 | 2 = h2
@@ -20,7 +20,7 @@ public class Pathfinder : MonoBehaviour
public static int width; public static int width;
public Pathfinder(LogicGrid world, int[] a, List<int[][]> o, List<int[][]> s, int algo, int heu) public Pathfinder(LogicGrid world, Vector2Int a, List<Vector2Int> o, List<Vector2Int> s, int algo, int heu)
{ {
this.world = world; this.world = world;
height = world.GetHeight(); height = world.GetHeight();
@@ -41,12 +41,12 @@ public class Pathfinder : MonoBehaviour
for(int i = 0; i < obstacles.Count; i++) for(int i = 0; i < obstacles.Count; i++)
{ {
Debug.Log("Obstacle " + i + ": " + obstacles[i][0][0] + ", " + obstacles[i][0][1]); Debug.Log("Obstacle " + i + ": " + obstacles[i].x + ", " + obstacles[i].y);
} }
for (int i = 0; i < samples.Count; i++) for (int i = 0; i < samples.Count; i++)
{ {
Debug.Log("Sample " + i + ": " + samples[i][0][0] + ", " + samples[i][0][1]); Debug.Log("Sample " + i + ": " + samples[i].x + ", " + samples[i].y);
} }
} }