Skip to content

Commit a83fe33

Browse files
committed
updating types
1 parent e8810c7 commit a83fe33

File tree

9 files changed

+354
-308
lines changed

9 files changed

+354
-308
lines changed
Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,51 @@
1-
import { PriorityQueue } from './datastructures/priorityQueue';
2-
import { getNeighbors, heuristic, exists } from './helper';
1+
import { PriorityQueue } from "./datastructures/priorityQueue";
2+
import { getNeighbors, heuristic, exists } from "./helper";
33

4-
export function ASTAR(grid, start, destination) {
5-
let priorityQueue = new PriorityQueue(); // prioirty queue with lowest cost priority
6-
priorityQueue.enqueue({ node: start, path: [start], cost: heuristic(start, destination), gcost: 0}); // add start node with cost being heuristic value
7-
let expanded = []; // expanded nodes (nodes that weve checked all neighbors)
4+
export function ASTAR(grid: any[][], start: number[], destination: number[]) {
5+
let priorityQueue = new PriorityQueue(); // prioirty queue with lowest cost priority
6+
priorityQueue.enqueue({
7+
node: start,
8+
path: [start],
9+
cost: heuristic(start, destination),
10+
gcost: 0,
11+
}); // add start node with cost being heuristic value
12+
let expanded = []; // expanded nodes (nodes that weve checked all neighbors)
813

9-
while (!priorityQueue.isEmpty()) {
10-
let { node, path, cost, gcost } = priorityQueue.dequeue(); // get the node, its path, and cost from the front of the priority queue
11-
if (node.toString() === destination.toString()) { // if node is destination
12-
return { status: "found", path, cost, expanded }; // return found, path, cost, and expanded.
13-
}
14+
while (!priorityQueue.isEmpty()) {
15+
let { node, path, cost, gcost } = priorityQueue.dequeue(); // get the node, its path, and cost from the front of the priority queue
16+
if (node.toString() === destination.toString()) {
17+
// if node is destination
18+
return { status: "found", path, cost, expanded }; // return found, path, cost, and expanded.
19+
}
1420

15-
if (exists(expanded, node)){ // if we have already expanded this node continue
16-
continue;
17-
}
21+
if (exists(expanded, node)) {
22+
// if we have already expanded this node continue
23+
continue;
24+
}
1825

19-
let neighbors = getNeighbors(grid, node); // get neighbors of current node
20-
for (let i = 0; i < neighbors.length; i++){ // for all neighbors
21-
let neighbor = neighbors[i]; // select neighbor
22-
if (grid[neighbor[0]][neighbor[1]] !== "w") { // if not a wall
23-
let costToNeighbor = 1; // set cost to neighbor as 1
24-
if (grid[neighbor[0]][neighbor[1]] === 'e'){ // if weighted
25-
costToNeighbor = 5; // set weight cost 5
26-
}
27-
let g = gcost + costToNeighbor; // add up total cost actual to neighbor node (cost to current node + cost of neighbor)
28-
let f = g + heuristic(neighbor, destination); // f = g (total cost to node) + heuristic
29-
priorityQueue.enqueue({ node: neighbor, path: path.concat([neighbor]), cost: f, gcost: g}); // add neihhbor, path, fcost, and gcost
30-
}
26+
let neighbors = getNeighbors(grid, node); // get neighbors of current node
27+
for (let i = 0; i < neighbors.length; i++) {
28+
// for all neighbors
29+
let neighbor = neighbors[i]; // select neighbor
30+
if (grid[neighbor[0]][neighbor[1]] !== "w") {
31+
// if not a wall
32+
let costToNeighbor = 1; // set cost to neighbor as 1
33+
if (grid[neighbor[0]][neighbor[1]] === "e") {
34+
// if weighted
35+
costToNeighbor = 5; // set weight cost 5
3136
}
32-
expanded.push(node); // add to expanded list
33-
37+
let g = gcost + costToNeighbor; // add up total cost actual to neighbor node (cost to current node + cost of neighbor)
38+
let f = g + heuristic(neighbor, destination); // f = g (total cost to node) + heuristic
39+
priorityQueue.enqueue({
40+
node: neighbor,
41+
path: path.concat([neighbor]),
42+
cost: f,
43+
gcost: g,
44+
}); // add neihhbor, path, fcost, and gcost
45+
}
3446
}
47+
expanded.push(node); // add to expanded list
48+
}
3549

36-
return { status: "not found", path: [], cost: 0 };
37-
}
50+
return { status: "not found", path: [], cost: 0 };
51+
}
Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
1-
import { getNeighbors } from './helper';
1+
import { getNeighbors } from "./helper";
22

3-
export function BFS(grid, start, destination) {
4-
let queue = [{ node: start, path: [start] }];
5-
let visited = {};
6-
let expanded = [];
3+
export function BFS(grid: any[][], start: number[], destination: number[]) {
4+
let queue: any[] = [{ node: start, path: [start] }];
5+
let visited: Record<string, boolean> = {};
6+
let expanded = [];
77

8-
while (queue) {
9-
let { node, path } = queue.shift(); // get the node and its path from the front of the queue
10-
let current_node_key = node.toString(); // to string for comparison
8+
while (queue) {
9+
let { node, path } = queue.shift(); // get the node and its path from the front of the queue
10+
let current_node_key = node.toString(); // to string for comparison
1111

12-
if (current_node_key === destination.toString()) { // check if we have arrived at the solution
13-
return { status: "found", path , expanded};
14-
}
12+
if (current_node_key === destination.toString()) {
13+
// check if we have arrived at the solution
14+
return { status: "found", path, expanded };
15+
}
16+
17+
if (visited.hasOwnProperty(current_node_key)) {
18+
// if we have not been to this node, add it
19+
continue;
20+
}
21+
visited[current_node_key] = true; // mark as visited
1522

16-
if (visited.hasOwnProperty(current_node_key)) { // if we have not been to this node, add it
17-
continue;
18-
}
19-
visited[current_node_key] = true; // mark as visited
20-
21-
let neighbors = getNeighbors(grid, node); // get neighbors of the current node
22-
for (let i = 0; i < neighbors.length; i++) {
23-
let neighbor = neighbors[i];
24-
if (!(grid[neighbor[0]][neighbor[1]] === "w")) { // if there is no wall
25-
queue.push({ node: neighbor, path: path.concat([neighbor]) }); // add to end of queue
26-
expanded.push(neighbor); // add the expanded node to the list
27-
}
28-
29-
}
23+
let neighbors = getNeighbors(grid, node); // get neighbors of the current node
24+
for (let i = 0; i < neighbors.length; i++) {
25+
let neighbor = neighbors[i];
26+
if (!(grid[neighbor[0]][neighbor[1]] === "w")) {
27+
// if there is no wall
28+
queue.push({ node: neighbor, path: path.concat([neighbor]) }); // add to end of queue
29+
expanded.push(neighbor); // add the expanded node to the list
30+
}
3031
}
32+
}
3133

32-
return { status: "not found", path: [], expanded};
33-
}
34+
return { status: "not found", path: [], expanded };
35+
}
Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
1-
import { getNeighbors } from './helper';
1+
import { getNeighbors } from "./helper";
22

3-
export function DFS(grid, start, destination) {
4-
let stack = [{ node: start, path: [start] }];
5-
let visited = {};
6-
let expanded = [];
3+
export function DFS(grid: any[][], start: number[], destination: number[]) {
4+
let stack: any[] = [{ node: start, path: [start] }];
5+
let visited: Record<string, boolean> = {};
6+
let expanded = [];
77

8-
while (stack) {
9-
let { node, path } = stack.shift(); // get the node and its path from the front of the stack
10-
let current_node_key = node.toString(); // to string for comparison
8+
while (stack) {
9+
let { node, path } = stack.shift(); // get the node and its path from the front of the stack
10+
let current_node_key = node.toString(); // to string for comparison
1111

12-
if (current_node_key === destination.toString()) { // check if we have arrived at the solution
13-
return { status: "found", path , expanded};
14-
}
12+
if (current_node_key === destination.toString()) {
13+
// check if we have arrived at the solution
14+
return { status: "found", path, expanded };
15+
}
1516

16-
if (visited.hasOwnProperty(current_node_key)) { // if we have not been to this node, add it
17-
continue;
18-
}
19-
visited[current_node_key] = true; // mark as visited
17+
if (visited.hasOwnProperty(current_node_key)) {
18+
// if we have not been to this node, add it
19+
continue;
20+
}
21+
visited[current_node_key] = true; // mark as visited
2022

21-
let neighbors = getNeighbors(grid, node); // get neighbors of the current node
22-
for (let i = 0; i < neighbors.length; i++) {
23-
let neighbor = neighbors[i];
24-
if (!(grid[neighbor[0]][neighbor[1]] === "w")) { // if there is no wall
25-
stack.unshift({ node: neighbor, path: path.concat([neighbor]) }); // add to beggining of stack
26-
expanded.push(neighbor); // add the expanded node to the list
27-
}
28-
}
29-
23+
let neighbors = getNeighbors(grid, node); // get neighbors of the current node
24+
for (let i = 0; i < neighbors.length; i++) {
25+
let neighbor = neighbors[i];
26+
if (!(grid[neighbor[0]][neighbor[1]] === "w")) {
27+
// if there is no wall
28+
stack.unshift({ node: neighbor, path: path.concat([neighbor]) }); // add to beggining of stack
29+
expanded.push(neighbor); // add the expanded node to the list
30+
}
3031
}
32+
}
3133

32-
return { status: "not found", path: [], expanded};
33-
}
34+
return { status: "not found", path: [], expanded };
35+
}
Lines changed: 55 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,77 @@
1-
function isValidIndex(matrix, i, j) {
2-
return i >= 0 && i < matrix.length && j >= 0 && j < matrix[0].length;
1+
function isValidIndex(matrix: any[][], i: number, j: number) {
2+
return i >= 0 && i < matrix.length && j >= 0 && j < matrix[0].length;
33
}
44

5+
export function getNeighbors(matrix: any[][], current_node: number[]) {
6+
let neighbors = [];
7+
let i = current_node[0];
8+
let j = current_node[1];
59

6-
export function getNeighbors(matrix, current_node) {
7-
let neighbors = []
8-
let i = current_node[0];
9-
let j = current_node[1];
10-
11-
// top neihgbor
12-
if (isValidIndex(matrix, i - 1, j)) {
13-
neighbors.push([i - 1, j]);
14-
}
10+
// top neihgbor
11+
if (isValidIndex(matrix, i - 1, j)) {
12+
neighbors.push([i - 1, j]);
13+
}
1514

16-
// bottom neighbor
17-
if (isValidIndex(matrix, i + 1, j)) {
18-
neighbors.push([i + 1, j]);
19-
}
15+
// bottom neighbor
16+
if (isValidIndex(matrix, i + 1, j)) {
17+
neighbors.push([i + 1, j]);
18+
}
2019

21-
// left neighbor
22-
if (isValidIndex(matrix, i, j - 1)) {
23-
neighbors.push([i, j-1]);
24-
}
20+
// left neighbor
21+
if (isValidIndex(matrix, i, j - 1)) {
22+
neighbors.push([i, j - 1]);
23+
}
2524

26-
// right neighbor
27-
if (isValidIndex(matrix, i, j + 1)) {
28-
neighbors.push([i, j+1]);
29-
}
25+
// right neighbor
26+
if (isValidIndex(matrix, i, j + 1)) {
27+
neighbors.push([i, j + 1]);
28+
}
3029

31-
return neighbors;
30+
return neighbors;
3231
}
3332

34-
35-
// function to check for conflict
36-
export function conflict(i, j, maze){
37-
// index checking
38-
if (i < 0 || i >= maze.length || j < 0 || j >= maze[0].length) {
39-
return false;
40-
}
41-
42-
43-
if (maze[i][j] === 'w' || maze[i][j] === 'e' || maze[i][j] === 'o' || maze[i][j] === 's'){
44-
return true;
45-
}
46-
47-
// if objective is surronded by obstacles
48-
49-
if (
50-
(i > 0 && maze[i - 1][j] === "w") ||
51-
(i < maze.length - 1 && maze[i + 1][j] === "w") ||
52-
(j > 0 && maze[i][j - 1] === "w") ||
53-
(j < maze[0].length - 1 && maze[i][j + 1] === "w")
54-
) {
55-
return true;
56-
}
57-
33+
// function to check for conflict
34+
export function conflict(i: number, j: number, maze: any[][]) {
35+
// index checking
36+
if (i < 0 || i >= maze.length || j < 0 || j >= maze[0].length) {
5837
return false;
5938
}
6039

40+
if (
41+
maze[i][j] === "w" ||
42+
maze[i][j] === "e" ||
43+
maze[i][j] === "o" ||
44+
maze[i][j] === "s"
45+
) {
46+
return true;
47+
}
48+
49+
// if objective is surronded by obstacles
50+
if (
51+
(i > 0 && maze[i - 1][j] === "w") ||
52+
(i < maze.length - 1 && maze[i + 1][j] === "w") ||
53+
(j > 0 && maze[i][j - 1] === "w") ||
54+
(j < maze[0].length - 1 && maze[i][j + 1] === "w")
55+
) {
56+
return true;
57+
}
6158

62-
export function heuristic(node, destination){
63-
return (Math.abs(node[0]-destination[0]) + Math.abs(node[1]-destination[1]));
59+
return false;
6460
}
6561

62+
export function heuristic(node: number[], destination: number[]) {
63+
return (
64+
Math.abs(node[0] - destination[0]) + Math.abs(node[1] - destination[1])
65+
);
66+
}
6667

6768
export function exists(list_, node) {
68-
const nodeString = JSON.stringify(node);
69-
return list_.some(item => JSON.stringify(item) === nodeString);
69+
const nodeString = JSON.stringify(node);
70+
return list_.some((item) => JSON.stringify(item) === nodeString);
7071
}
7172

72-
export function randInts(min, max) {
73+
export function randInts(min: number, max: number) {
7374
min = Math.ceil(min);
7475
max = Math.floor(max);
7576
return Math.floor(Math.random() * (max - min + 1)) + min;
76-
}
77+
}

0 commit comments

Comments
 (0)