0% found this document useful (0 votes)
18 views3 pages

Question Ai

This assignment involves implementing the A* pathfinding algorithm in JavaScript. Students are provided with starter code including classes for TileNodes, a GameMap, and a PriorityQueue. The GameMap class contains method signatures for manhattanDistance(), astar(), and backtrack() that need to be implemented. A* finds the lowest-cost path between start and end nodes by estimating total cost as the sum of path cost so far and a heuristic distance estimate to the goal. Manhattan distance is used as the heuristic. The algorithm maintains open and closed lists to track explored nodes.

Uploaded by

ejaknon
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)
18 views3 pages

Question Ai

This assignment involves implementing the A* pathfinding algorithm in JavaScript. Students are provided with starter code including classes for TileNodes, a GameMap, and a PriorityQueue. The GameMap class contains method signatures for manhattanDistance(), astar(), and backtrack() that need to be implemented. A* finds the lowest-cost path between start and end nodes by estimating total cost as the sum of path cost so far and a heuristic distance estimate to the goal. Manhattan distance is used as the heuristic. The algorithm maintains open and closed lists to track explored nodes.

Uploaded by

ejaknon
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/ 3

Assignment 2 – A* (20%)

Due: Feb 15th. Upload to Brightspace by 11:59pm NST.


Compress the folder containing your source files as a .zip for submission.
You do not have to include: node_modules, package.json, and package_lock.json

The goal of this assignment is to implement the A* pathfinding algorithm.

Introduction

A* is designed for point-to-point pathfinding, it always returns a single path from source to goal.
Given a graph (directed non-negative weighted) and two nodes in that graph (called start and
end), we would like to generate a path such that the total path cost of that path is minimal
among all possible paths from start to end.

The A* algorithm works similarly to Optimized Dijkstra (Lecture 11). However, rather than always
first looking at the unvisited node with the lowest cost value, A* looks at the most promising
node to lead to the shortest overall path. We choose which node is most promising by
estimating the total cost to get to the end from each node we encounter, using the following
formula.

f(n) = g(n) + h(n)

Where,
f(n) = total cost estimation.
g(n) = the current lowest cost to get from the start node to node n.
h(n) = an estimation to get from node n to the end node.

While g(n) can be calculated in our search of the graph, h(n) uses a heuristic to estimate the
remaining cost to our end node. Since we are using a graph with edges in the 4 cardinal
directions (N,S,E,W), we will use the Manhattan Distance between two nodes as our heuristic.

A* Algorithm

1. Keep track of two lists, an open list and closed list


2. Add the starting node to the open list
3. Repeat the following:
a. current node = the lowest f cost node on the open list
b. Add the current node to the closed list
c. For each of the edge nodes from the current node,
i. if the edge node is in the closed list, ignore it,
Otherwise, do the following,
ii. if it is not in the open list, add it to the open list, set the parent node to the
current node, and record the f, g, and h costs of the node
iii.
If it is in the open list, check to see if this path to the edge node is better
than what is in the open list, using the g cost. If so, change the parent of
the edge node to the current node, and recalculate the g and f costs of
the edge node. **If the open list is sorted by the f score, resort the open
list
d. Stop when:
i. The end node is added to the closed list (which means the path has been
found)
ii. There are no more open nodes to look at, and a path has not been found
4. Generate the path by working backwards from the end node. Go from each node to its
parent until you arrive at the start node.

Task

In the starter code, you will be given an open three.js scene with a pre-generated map. The goal
of this assignment is to implement the astar(), manhattanDistance(), and backtrack() methods,
as presented in the GameMap class.

You will be given the following .js files, with certain methods already implemented:

TileNode.js

This class is for the nodes in your graph. The TileNode class holds the node’s id (equivalent to
its position in the graph), a list of its edges, and a type. Types are defined as an Enum within
this class, and can be Ground or Obstacle.

GameMap.js

This class is essentially the GameMap class we created in class. It holds the graph (and
associated logic), as well as a MapRenderer to create a visualization of the game map to
display in the browser window. The graph is generated within the initGraph() method (which
should not be edited). This class contains a localize function, which returns a Vector3 for where
the node will be located on the screen. Method signatures are provided for required
implementation: manhattanDistance(node, end), astar(startIndex, endIndex), and
backtrack(start, end, parents).

PriorityQueue.js

The PriorityQueue class is given to simplify the logic for selecting the most promising node. This
class implements a PriorityQueue as a min heap. It contains essential PriorityQueue methods
including enqueue, which takes in a node and a cost as arguments, dequeue which returns the
lowest cost node in the queue, and remove which removes a node from the queue. This class
should not be edited.
MapRenderer.js

MapRenderer is used to draw the map to the screen. This class should not be edited.

main.js

The main class is where the three.js scene, camera, and renderer are instantiated (as well as
optional OrbitControls). The setup() method sets up the scene, containing the same code as
was presented in Lecture 11, with the exception of astar being called instead of dijkstra. This
class should not need to be edited.

Hints

- Save the open nodes as a PriorityQueue. This will ease finding the most promising node
to search next.
- If you need to change a cost value for a node in the PriorityQueue, just remove the node
from the PriorityQueue and reinsert it. This will handle the resorting.
- The source code from Lecture 11 should prove useful.

If you have an issue getting the code to run, ensure that you have the following in your directory:
node_modules (folder), package_lock.json, and package.json

If you do not have these files, follow the instructions on


https://threejs.org/docs/#manual/en/introduction/Installation to install via npm.
If you have the files and are receiving errors, try deleting them and reinstalling via npm.

You might also like