0% found this document useful (0 votes)
54 views10 pages

Assignment No A-03 Aim

The document describes implementing the A* pathfinding algorithm. It includes: 1. Defining the problem as finding the optimal path between a start and end node on a grid, using the A* algorithm which combines uniform cost search and heuristics. 2. Explaining the mathematical model and components of A* including open and closed lists to track nodes, and the cost formula f(n) = g(n) + h(n). 3. Providing pseudocode for the main steps of the A* search algorithm to find the shortest path.

Uploaded by

sagar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views10 pages

Assignment No A-03 Aim

The document describes implementing the A* pathfinding algorithm. It includes: 1. Defining the problem as finding the optimal path between a start and end node on a grid, using the A* algorithm which combines uniform cost search and heuristics. 2. Explaining the mathematical model and components of A* including open and closed lists to track nodes, and the cost formula f(n) = g(n) + h(n). 3. Providing pseudocode for the main steps of the A* search algorithm to find the shortest path.

Uploaded by

sagar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Assignment No A-03

Aim
Implement A* approach for any suitable application.

Pre-requisite
1. Data Structure.
2. Programming language basics.

Objective
1. To understand idea of best first search methods.
2. To implement path finding problem using A* algorithm.

Problem Statement
Implement path finding application using best first search technique A*.

Hardware / Software Used


1. Openjdk.
2. Eclipse IDE.

Mathematical Model
M = { s, e, X, Y, Fme , DD, N DD, Fsuccess , Ff ailure , M emshared , CP UCoreCount }

1. s: Start State - Identify Start and Goal nodes.


2. e: End State - Shortest path between Start and Goal Node found ( If Available ).
3. X: Input - Grid structure indicating total available points, Start, Goal Nodes, Obstacles.
4. Y: Output - Path is found.
5. DD: Deterministic Data - Nodes, Obstacles on grid and their positions.
6. NDD: Non-Deterministic Data - Positions of Obstacles on grid.
7. F me = AstarManager(startNode, endNode, width, height, nodesize);
8. Mem shared = No shared Memory used.
9. Success: Path found.
10. Failure: Path not found or not available.
11. CPUCoreCount : 1.

Theory
A* Algorithm
A star algorithm is used to finding path, graph traversal, plotting of traversal path between
multiple point. The A star algorithm has combination of uniform-cost search and heuristic
search to compute optimal solution.
In A star algorithm the cost associated with a node is f(n) = g(n) + h(n),
Where,
g(n) is cost to reach node n.
h(n) is the heuristic estimate or the cost to reach the goal node from node n.
f(n) estimates the lowest total cost of solution path going through node n.
A star
algorithm finds the optimal path to a goal if heuristic function h(n) is admissible i.e., it never
over estimate actual cost.
The sequence nodes expanded by A star using graph search is in, increasing order of f(n).
Hence, the first goal node selected for expansion must be optimal solution because all latter
nodes will either having same value of f(n) or greater value of f(n) than currently expanded
node.
To implement such a graph-search procedure, we will need to use two lists of node:
1.

OPEN: It is a priority queue which keeps a track of those nodes which had heuristic
function applied to them and that nodes are need to be examined.

2.

CLOSED:It is a priority queue which keeps a track of nodes that have already examined.

Steps Of Algorithm
1. Put the start node s on OPEN.
2. If OPEN is empty, exit with failure.
3. Remove from OPEN and place on CLOSED a node n having minimum f. If n is a goal
node exit successfully with a solution path obtained by tracing back the pointers from n
to s.
4. Otherwise, expand n generating its children and directing pointers from each child node
to n.
5. Go to step 2.

Procedure
Compiling Program: javac MainWindow.java
Run The Program: java MainWindow

Conclusion
Hence, we studied the A* algorithm and implemented it.
3

Program
================================================
GROUP A
Assignment No : A3
Title : Implement A* approach for any suitable application.
Roll No :
Batch : B
Class : BE ( Computer )
================================================

Astar.java
package astar;
import java.util.ArrayList;
public class Astar
{
private ArrayList<Node> open;
private ArrayList<Node> close;
private ArrayList<Node> path;
private Node endNode;
private Node startNode;
private Node closeNode;
private Grid grid;
private int straightCost = 1;
private int diagCost = 2;
public boolean findPath(Grid grid)
{
this.grid = grid;
this.open = new ArrayList<Node>();
this.close = new ArrayList<Node>();
this.startNode = grid.getStartNode();
this.endNode = grid.getEndNode();
startNodeInit();
return search();
}
public boolean search()
{
Node node = startNode;
while (!node.equalsNodesPosition(endNode))
{
int startX = Math.max(0, node.x - 1);
int startY = Math.max(0, node.y - 1);
int endX = Math.min(grid.getNumCols() - 1, node.x + 1);
int endY = Math.min(grid.getNumRows() - 1, node.y + 1);
for (int i = startX; i <= endX; i++)
4

{
for (int j = startY; j <= endY; j++)
{
Node test = grid.getNode(i, j);
if (test.equalsNodesPosition(node) ||
!test.walkable ||
!grid.getNode(node.x, test.y).walkable ||
!grid.getNode(test.x, node.y).walkable)
{
continue;
}
int cost = straightCost;
if (!((node.x == test.x) || (node.y == test.y)))
{
cost = diagCost;
}
int g = node.g + cost; // * test.costMultiplier;
int h = euclidean(test);
int f = g + h;
if (isOpen(test) || isClosed(test))
{
if (test.f > f)
{
test.f = f;
test.h = h;
test.g = g;
test.parent = node;
}
}
else
{
test.f = f;
test.h = h;
test.g = g;
test.parent = node;
open.add(test);
}
}
}
close.add(node);
if (open.size() == 0)
{
System.out.println("path not found");
break;
} else
{
node = sortArray(open);
open.remove(node);
5

closeNode = node;
}
}
buildPath();
return true;
}
public void buildPath()
{
path = new ArrayList<Node>();
path.add(closeNode);
while (!(closeNode.equalsNodesPosition(startNode)))
{
closeNode = closeNode.parent;
path.add(closeNode);
}
}
public boolean isOpen(Node node)
{
for (int i = 0; i <= open.size() - 1; i++)
{
if (node.equalsNodesPosition((open.get(i))))
{
return true;
}
}
return false;
}
public boolean isClosed(Node node)
{
for (int i = 0; i <= close.size() - 1; i++)
{
if (node.equalsNodesPosition((close.get(i))))
{
return true;
}
}
return false;
}
public Node sortArray(ArrayList<Node> array)
{
Node fMin = array.get(0);
for (int i = 1; i <= array.size() - 1; i++)
{
Node node = array.get(i);
if (node.f < fMin.f)
{
fMin = node;
}
}
return fMin;
6

}
public int manhattan(Node node)
{
return (Math.abs(node.x - endNode.x) * straightCost + Math.abs(node.y - end
}
public int euclidean(Node node)
{
int dx = node.x - endNode.x;
int dy = node.y - endNode.y;
return (int) Math.sqrt(dx * dx + dy * dy) * straightCost;
}
public int diagonal(Node node)
{
int dx = Math.abs(node.x - endNode.x);
int dy = Math.abs(node.y - endNode.y);
int diag = Math.min(dx, dy);
int straight = dx + dy;
return diagCost * diag + straightCost * (straight - 2 * diag);
}
public ArrayList<Node> getVisited()
{
ArrayList<Node> array = new ArrayList<Node>();
array.addAll(open);
array.addAll(close);
return array;
}
public ArrayList<Node> getPath()
{
return path;
}
private void startNodeInit()
{
startNode.g = 0;
startNode.h = euclidean(startNode);
startNode.f = startNode.g + startNode.h;
}
}

Grid.java
package astar;
public class Grid
{
private Node startNode;
private Node endNode;
private Node nodes[][];
private int numRows;
private int numCols;
public Grid(int numCols, int numRows)
7

{
this.numRows = numRows;
this.numCols = numCols;
nodes = new Node[numCols][numRows];
for (int i = 0; i <= numCols - 1; i++)
{
for (int j = 0; j <= numRows - 1; j++)
{
nodes[i][j] = new Node(i, j);
}
}
}
public void setStartNode(int x, int y)
{
startNode = new Node(x, y);
}
public void setEndNode(int x, int y)
{
endNode = new Node(x, y);
}
public void setWalkable(int x, int y, boolean value)
{
nodes[x][y].walkable = value;
}
public Node getNode(int x, int y)
{
return nodes[x][y];
}
public Node getEndNode()
{
return endNode;
}
public Node getStartNode()
{
return startNode;
}
public int getNumRows()
{
return numRows;
}
public int getNumCols()
{
return numCols;
}
}

Node.java
package astar;
8

public class Node


{
public Node parent;
public int f;
public int g;
public int h;
public int x;
public int y;
public int costMultiplier = 1;
public boolean walkable = true;
public Node(int x, int y)
{
this.x = x;
this.y = y;
}
public boolean equalsNodesPosition(Node node)
{
return x == node.x && y == node.y;
}
}

Output

*
Figure 1: Output

Figure 2: Output

Plagiarism Score

10

You might also like