-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAStar.cpp
More file actions
181 lines (171 loc) · 6.76 KB
/
AStar.cpp
File metadata and controls
181 lines (171 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* Authors: Alysha Aul, Nandhitha Krishnan, Paul Rodgers, Nouran Sakr, Chun Yang
* Date: November 7, 2023
* Purpose: Class to calculate the shortest path between start and destination locations using AStar Algorithm.
*/
#include "AStar.h"
#include "Interface.h"
using namespace std;
/**
* Finds the shortest path(s) from starting to destination locations based on map type.
* @return result:The shortest path
*/
pair<vector<pair<int, int>>, vector<pair<int, int>>> AStar::runAlgorithm() {
vector<vector<Block*>> shortestPath;
vector<Block*> pathInstance;
//get start and end points
Block* startBlock = currentBoard->getMap()->getStartPoint();
Block* targetBlock = currentBoard->getMap()->getTargetPoint();
pair<vector<pair<int, int>>, vector<pair<int, int>>> result = findShortestPathOnMap(startBlock, targetBlock);
return result;
}
/**
* Creates a vector containing a list of unvisited blocks to be visited.
* @param node: The current block.
* @param targetLoc: The target block.
*/
void AStar::addNeighboursToUnvisited(Block* node, Block* targetLoc) {
vector<Block*> neighbours = node->getNeighbour();
int parent_g_cost = node->getGCost();
for(Block* neighbour: neighbours){
// if neighbour is not wall
if(!neighbour->visited() && neighbour->getType() != 5) {
int row = neighbour->getRow();
int col = neighbour->getColumn();
bool updated = neighbour->updateGCost(row, col, parent_g_cost);
neighbour->calculateHCost(targetLoc->getRow(), targetLoc->getColumn());
if(updated) neighbour->setPrevBlock(node);
if(!neighbour->isInQueue()) {
neighbour->setInQueue(true);
unvisited.push(neighbour);
} else {
// if this neighbour node is already in priority queue, then we change the min dist and update order
reorderPriorityQueue(node);
}
}
}
}
/**
* Function to reorder the priority queue if the block is already in the queue.
* @param node: The block to be pushed to the priority queue.
*/
void AStar::reorderPriorityQueue(Block* node) {
vector<Block*> temp_array;
Block* curr = unvisited.top();
bool sameCol = node->getColumn() == curr->getColumn();
bool sameRow = node->getRow() == curr->getRow();
bool sameNode = sameCol && sameRow;
while(!sameNode) {
if(!unvisited.empty()){
temp_array.push_back(curr);
unvisited.pop();
curr = unvisited.top();
} else {
break;
}
}
unvisited.pop();
unvisited.push(curr);
for(Block* b: temp_array) {
unvisited.push(b);
}
}
/**
* Finds the shortest distance between the start and target locations using AStar Algorithm.
* @param startLoc: The starting block.
* @param targetLoc: The destination block.
* @return result: The shortest path.
*/
pair<vector<pair<int, int>>, vector<pair<int, int>>> AStar::findShortestPathOnMap(Block* startLoc, Block* targetLoc) {
vector<pair<int, int>> visitedNode;
vector<pair<int, int>> shortestPathNodes;
unvisited.push(startLoc);
Block* curr = unvisited.top();
curr->calculateHCost(targetLoc->getRow(), targetLoc->getColumn());
curr->setGCost(0);
while(!unvisited.empty()){
if(curr->visited()){
unvisited.pop();
continue;
}
unvisited.pop();
curr->setVisit(true);
// add its neighbours to priority queue
addNeighboursToUnvisited(curr, targetLoc);
int x = curr->getRow();
int y = curr->getColumn();
pair<int, int> node(x, y);
visitedNode.push_back(node);
if(curr == targetLoc) {
break;
}
curr = unvisited.top();
}
if(curr == targetLoc) {
int row = curr->getRow();
int col = curr->getColumn();
pair<int, int> pathNode(row, col);
shortestPathNodes.insert(shortestPathNodes.begin(), pathNode);
while(curr->getPrevBlock()) {
int prevRow = curr->getPrevBlock()->getRow();
int prevCol = curr->getPrevBlock()->getColumn();
pathNode = make_pair(prevRow, prevCol);
shortestPathNodes.insert(shortestPathNodes.begin(), pathNode);
curr = curr->getPrevBlock();
}
}
pair<vector<pair<int, int>>, vector<pair<int, int>>> result(visitedNode, shortestPathNodes);
return result;
}
/**
* Method to sort the connection blocks from greatest to least in weight.
* @param connections: the blocks to be sorted.
* @return sortedConnections: the blocks sorted by weight.
*/
vector<Block*> AStar::sortByWeight(vector<Block*> connections) {
vector<Block*> sortedConnections;
int numConnections = connections.size();
int numSortedConnections = 0;
//If there are no connections there is nothing to sort and return empty set of connections.
if(numConnections == 0) {
return connections;
}
//Sorts connection vector from highest to lowest weight and returns sorted vector.
for(int i = 0; i < numConnections; i++) {
Block* tempBlock = connections.at(numConnections-1);
connections.pop_back();
if(numSortedConnections == 0) {
sortedConnections.push_back(tempBlock);
numSortedConnections++;
}
else {
for(int j = numSortedConnections; j < numConnections; j++) {
if(tempBlock->getWeight() >= sortedConnections.at(j)->getWeight()) {
vector<Block*>::iterator iter = sortedConnections.begin() + i;
sortedConnections.insert(iter, tempBlock);
numSortedConnections++;
}
}
}
}
return sortedConnections;
}
/**
* Method to calculate the shortest distance between the starting node and each node in the grid.
* @param currBlock: the node to compute the shortest distance from.
* @param connections: the set of adjacent nodes.
*/
void AStar::calculateShortestDistance(Block* currBlock, vector<Block*> connections)
{
for(int i = 0; i < connections.size(); i++) {
//Get the block's connections and calculate their distance from the starting node.
Block* nextBlock = connections.at(i);
if(nextBlock->visited()) continue;
int dist = nextBlock->getWeight() + currBlock->getMinDistance();
//If the distance found to the block is shorter than the previously stored distance set the smaller distance to the min distance.
if (dist < nextBlock->getMinDistance()) {
nextBlock->setMinDistance(dist);
}
calculateShortestDistance(nextBlock, sortByWeight(currBlock->getNeighbour()));
}
}