Question Ai
Question Ai
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.
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
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