Skip to content

Commit c033c60

Browse files
committed
Adjust a* heuristics based on grid connectivity
1 parent d047cdf commit c033c60

File tree

5 files changed

+31
-9
lines changed

5 files changed

+31
-9
lines changed

figures/img0.png

-30.4 KB
Loading

include/States/Algorithms/GraphBased/ASTAR/ASTAR.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ class ASTAR : public BFS {
3737
std::priority_queue<std::shared_ptr<Node>, std::vector<std::shared_ptr<Node>>,
3838
MinimumDistanceASTAR>
3939
frontier_;
40+
41+
bool use_manhattan_heuristics_{true};
4042
};
4143

4244
} // namespace graph_based

include/States/Algorithms/GraphBased/Utils.h

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,23 @@ namespace path_finding_visualizer {
66
namespace graph_based {
77
namespace utils {
88

9-
inline double L1_Distance(const std::shared_ptr<Node> &n1,
10-
const std::shared_ptr<Node> &n2) {
11-
return fabs(n1->getPos().x - n2->getPos().x) +
12-
fabs(n1->getPos().y - n2->getPos().y);
9+
inline double distanceCost(const std::shared_ptr<Node> &n1,
10+
const std::shared_ptr<Node> &n2) {
11+
return std::sqrt(
12+
(n1->getPos().x - n2->getPos().x) * (n1->getPos().x - n2->getPos().x) +
13+
(n1->getPos().y - n2->getPos().y) * (n1->getPos().y - n2->getPos().y));
14+
}
15+
16+
inline double costToGoHeuristics(const std::shared_ptr<Node> &n1,
17+
const std::shared_ptr<Node> &n2,
18+
bool use_manhattan = false) {
19+
if (use_manhattan)
20+
return fabs(n1->getPos().x - n2->getPos().x) +
21+
fabs(n1->getPos().y - n2->getPos().y);
22+
23+
return std::sqrt(
24+
(n1->getPos().x - n2->getPos().x) * (n1->getPos().x - n2->getPos().x) +
25+
(n1->getPos().y - n2->getPos().y) * (n1->getPos().y - n2->getPos().y));
1326
}
1427

1528
inline void addNeighbours(std::vector<std::shared_ptr<Node>> &nodes,

src/States/Algorithms/GraphBased/ASTAR/ASTAR.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,12 @@ void ASTAR::initAlgorithm() {
1717
while (!frontier_.empty()) {
1818
frontier_.pop();
1919
}
20+
21+
use_manhattan_heuristics_ = (grid_connectivity_ == 0) ? true : false;
22+
2023
nodeStart_->setGDistance(0.0);
21-
nodeStart_->setFDistance(utils::L1_Distance(nodeStart_, nodeEnd_));
24+
nodeStart_->setFDistance(utils::costToGoHeuristics(
25+
nodeStart_, nodeEnd_, use_manhattan_heuristics_));
2226
frontier_.push(nodeStart_);
2327
}
2428

@@ -65,15 +69,18 @@ void ASTAR::solveConcurrently(
6569
}
6670

6771
double dist = nodeCurrent->getGDistance() +
68-
utils::L1_Distance(nodeCurrent, nodeNeighbour);
72+
utils::costToGoHeuristics(nodeCurrent, nodeNeighbour,
73+
use_manhattan_heuristics_);
6974

7075
if (dist < nodeNeighbour->getGDistance()) {
7176
nodeNeighbour->setParentNode(nodeCurrent);
7277
nodeNeighbour->setGDistance(dist);
7378

7479
// f = g + h
75-
double f_dist = nodeCurrent->getGDistance() +
76-
utils::L1_Distance(nodeNeighbour, s_nodeEnd);
80+
double f_dist =
81+
nodeCurrent->getGDistance() +
82+
utils::costToGoHeuristics(nodeNeighbour, s_nodeEnd,
83+
use_manhattan_heuristics_);
7784
nodeNeighbour->setFDistance(f_dist);
7885
nodeNeighbour->setFrontier(true);
7986
frontier_.push(nodeNeighbour);

src/States/Algorithms/GraphBased/DIJKSTRA/DIJKSTRA.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ void DIJKSTRA::solveConcurrently(
6363
}
6464

6565
double dist = nodeCurrent->getGDistance() +
66-
utils::L1_Distance(nodeCurrent, nodeNeighbour);
66+
utils::distanceCost(nodeCurrent, nodeNeighbour);
6767

6868
if (dist < nodeNeighbour->getGDistance()) {
6969
nodeNeighbour->setParentNode(nodeCurrent);

0 commit comments

Comments
 (0)