Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

All things DONE!!! #7

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 162 additions & 0 deletions Pathfinder/Pathfinder/JavaScript/Astar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
//A* Implementation :)

var check = false;

//Algorithm Starts here
async function Astar() {

//Creating a priority_queue for storing the unvisited cells
pqueue = new priority_queue();
check = false;

//Re-initialising the property of each cell to its default value
for (var i = 0; i < col; i++) {
for (var j = 0; j < row; j++) {

grid[i][j].g = Infinity;
grid[i][j].f = Infinity;

grid[i][j].visited = false;
grid[i][j].camefrom = null;
grid[i][j].neighbours = [];
grid[i][j].showyou(color(255));
}
}

strt.g = 0;
strt.f = dist(str.i, str.j, end.i, end.j); //Math.abs(strt.i - end.i) + Math.abs(strt.j - end.j);

//Inserting the source cell
pqueue.enqueue(strt.i, strt.j);

//For keeping the Visited Cell
var closedset = [];

//Adding reachable neighbours of every cell.Why?.because the user may have added a new wall before running algo.
for (var i = 0; i < col; i++) {
for (var j = 0; j < row; j++) {
grid[i][j].addneighbours(grid);
}
}

//While all the cells have been traversed or the END has been found
while (!pqueue.isEmpty()) {

//if the user clicks Cancel Search,abort=true
if (abort) {
abort = false;
for (var i = 0; i < col; i++) {
for (var j = 0; j < row; j++)
grid[i][j].showyou(color(255));
}
strt.showyou(color(0, 255, 0));
end.showyou(color(255, 0, 0));
break;
}

var current = pqueue.front().element;

//Hurrah!! We reached our destination
if (current === end) {
check = true;

var path = [];
var temp = current;
path.push(current);

//Extract the minimum cost path
while (temp.camefrom) {
path.push(temp.camefrom);
temp = temp.camefrom;
}

//if enough battery is available
if (battery - (0.5 * (path.length - 1)) >= 0) {
noFill();
stroke(255, 245, 102);
strokeWeight(w / 5);
beginShape();
for (var i = 0; i < path.length; i++) {
vertex(path[i].i * w + w / 2, path[i].j * h + h / 2);
}
endShape();
var c = path.length;
c--;
battery -= 0.5 * c;
success(c);
display_battery();
} else {
battery_low();
}
break;
} else {
pqueue.dequeue();

closedset.push(current);

var neigh = current.neighbours;

//Traversing all neighbours
for (var i = 0; i < neigh.length; i++) {

var neighbor = neigh[i];
//If that neighbour is not visited
if (!neighbor.visited) {

neighbor.visited = true;
//new g value of neighbor
var tempG = current.g + 1;

//if new g value is samller than previos g value,update it
if (tempG < neighbor.g) {
neighbor.g = tempG;
neighbor.camefrom = current;
}

//Heuristic Value.Used Euclidean here
var temph = dist(neighbor.i, neighbor.j, end.i, end.j); //Math.abs(neighbor.i - end.i) + Math.abs(neighbor.j - end.j);
//new cost
var newcost = neighbor.g + temph;
//if new cost is smaller than previos cell's cost

if (newcost < neighbor.f) {
neighbor.h = temph;
neighbor.f = newcost;
pqueue.enqueue(neighbor.i, neighbor.j);
}
}
}
}

//If path has not been found keep colouring the grid
if (!check) {
for (var i = 0; i < pqueue.items.length; i++) {
pqueue.items[i].element.showyou(color(177, 250, 82));
}

for (var i = 0; i < closedset.length; i++) {
closedset[i].showyou(color(74, 247, 244));
}

strt.showyou(color(0, 255, 0));
end.showyou(color(255, 0, 0));
await sleep(15);
}
}

//Path not found
if (!check && pqueue.isEmpty()) {
fail();
strt.showyou(color(0, 255, 0));
end.showyou(color(255, 0, 0));
}

//Enabling of Clear and Start button after Search completes.
document.getElementById("clr").disabled = false;
document.getElementById("strt").disabled = false;

//Disabling of Cancel Search button after Search completes
document.getElementById("can").disabled = true;
first_time = 3;
}
//end of the A* implementation
151 changes: 151 additions & 0 deletions Pathfinder/Pathfinder/JavaScript/Dijkstra.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
//Dijkstra ALgorithm Implementation :)

//function for producing delay in execution
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

//Algorithm Starts here
async function Dijkstra() {

//hold cells that are yet to be visited
var que = new Queue();

var ok = false;

//Every time algorithm runs,the cells are loaded with default values
for (var i = 0; i < col; i++) {
for (var j = 0; j < row; j++) {

grid[i][j].showyou(color(255));
grid[i][j].camefrom = null;
grid[i][j].visited = false;
grid[i][j].neighbours = [];
}
}

//Adding reachable neighbours of every cell.Why?.because the user may have added a new wall before running algo.
for (var i = 0; i < col; i++) {
for (var j = 0; j < row; j++) {
grid[i][j].addneighbours(grid);
}
}

//Inserting the source cell in the queue
que.enqueue(new QItem(strt.i, strt.j, 0));

//Array for keeping the visited cell
var closedSet = [];

var check = false;

//While all the cells have been traversed or the END has been found
while (!que.isEmpty()) {

//if the user clicks Cancel Search,abort=true
if (abort) {
abort = false;
for (var i = 0; i < col; i++) {
for (var j = 0; j < row; j++)
grid[i][j].showyou(color(255));
}
strt.showyou(color(0, 255, 0));
end.showyou(color(255, 0, 0));
break;
}

var p = que.front();
que.dequeue();
closedSet.push(p);

//Hurrah!! We reached our destination
if (grid[p.row][p.col] === end) {

check = true;
var x = grid[p.row][p.col];

var path = [];
var temp = x;
path.push(x);

//Extracting the minimum cost path
while (true) {

path.push(temp.camefrom);
temp = temp.camefrom;
if (temp == strt || x == null) {
path.push(temp);
break;
}
}

//if enough battery is available,then traverse
if (battery - (0.5 * (path.length - 2)) >= 0) {

noFill();
stroke(255, 245, 102);
strokeWeight(w / 7);
beginShape();

for (var i = 0; i < path.length; i++) {
vertex(path[i].i * w + w / 2, path[i].j * h + h / 2);
}
success(path.length - 2);
endShape();
battery -= 0.5 * (path.length - 2);
display_battery();
} else {
battery_low();
}
break;

} else {

var neigh = grid[p.row][p.col].neighbours;

//traversing through current cell's neighbours
for (var i = 0; i < neigh.length; i++) {

var neighbor = neigh[i];

//if not visited,visit it
if (!neighbor.visited) {
que.enqueue(new QItem(neighbor.i, neighbor.j, p.dist + 1));
neighbor.visited = true;
neighbor.camefrom = grid[p.row][p.col];
}
}
}

//If path has not been found keep colouring the grid
if (!check) {

for (var i = 0; i < que.items.length; i++)
grid[que.items[i].row][que.items[i].col].showyou(color(177, 250, 82));

for (var i = 0; i < closedSet.length; i++)
grid[cSet[i].row][cSet[i].col].showyou(color(74, 247, 244));

strt.showyou(color(0, 255, 0));
end.showyou(color(255, 0, 0));
await sleep(5);
}
}

//If no path is found
if (!check && que.isEmpty()) {
fail();
strt.showyou(color(0, 255, 0));
end.showyou(color(255, 0, 0));
}

//Enabling of Clear and Start button after Search completes.
document.getElementById("clr").disabled = false;
document.getElementById("strt").disabled = false;

//Disabling of Cancel Search button after Search completes
document.getElementById("can").disabled = true;
first_time = 3;

//end of Dijkstra Implementation :)
}
Loading