-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrategy.cpp
More file actions
196 lines (170 loc) · 5.28 KB
/
strategy.cpp
File metadata and controls
196 lines (170 loc) · 5.28 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include "strategy.h"
namespace NAVIGATION
{
void delay(const int& time)
{
clock_t now = clock();
while (clock() - now < time){}
}
AStar::AStar(const int& _dist_type, const int& _algo_type) : dist_type(_dist_type), algo_type(_algo_type)
{
}
bool AStar::search(const unique_ptr<GridMapBase>& map, vector<Node*>& result_nodes)
{
// init start
open_queue.emplace(*map->start_node);
open_set.insert(map->start_node);
Node* current_node = map->start_node; // ptr to start
// set move direction
vector<pair<int, int>> move_step = // use pair for direction
{
pair <int, int>(0, 1),
pair <int, int>(0, -1),
pair <int, int>(1, 0),
pair <int, int>(-1, 0),
pair <int, int>(-1, 1),
pair <int, int>(-1, -1),
pair <int, int>(1, -1),
pair <int, int>(1, 1),
};
// search
while (!open_queue.empty())
{
// current node
int r = open_queue.top().row;
int c = open_queue.top().col;
current_node = &map->grid_map[r][c];
cout << "CURRENT NODE: f = " << current_node->f << ", g = " << current_node->g << ", h = " << current_node->h << endl;
// draw close
setfillcolor(YELLOW);
drawNodeStep(map->start_node, map->end_node, current_node, SEARCH_DELAY);
open_queue.pop();
open_set.erase(current_node);
close_set.insert(current_node);
// reach end node
if (current_node == map->end_node)
{
path_len = current_node->g;
// store to result_nodes from current to start
while (current_node->parent_node) // start has empty parent_node
{
current_node->node_type = NodeType::way_pt;
result_nodes.emplace_back(current_node);
current_node = current_node->parent_node;
}
reverse(result_nodes.begin(), result_nodes.end());
cout << "Successful!" << endl;
// recover start and end node type
map->start_node->node_type = NodeType::start;
map->end_node->node_type = NodeType::end;
// print
printResult(result_nodes);
map->printMap();
drawResult(map->start_node, map->end_node, result_nodes);
return true;
}
// search
for (auto i = move_step.begin(); i != move_step.end(); i++)
{
Node temp_node(r + i->first, c + i->second);
if (map->outOfMap(temp_node)) // skip if out of bound
{
continue;
}
Node* next_node = &map->grid_map[temp_node.row][temp_node.col]; // create next node if node out of bound
if (close_set.find(next_node) != close_set.end() // if is in close_set (already visited)
|| map->obs_nodes.find(next_node) != map->obs_nodes.end()) // or in obs
{
continue; // skip
}
step_len = abs(i->first) == abs(i->second) ? XLEN : LEN; // either side or diagonal
if (open_set.find(next_node) != open_set.end()) // already in open
{
if (current_node->g + step_len < next_node->g)
{
calcCost(*current_node, *next_node, *map->end_node);
next_node->parent_node = current_node;
}
}
else // not in open, add to open
{
calcCost(*current_node, *next_node, *map->end_node);
next_node->parent_node = current_node;
open_queue.emplace(*next_node);
open_set.insert(next_node);
// draw open
setfillcolor(LIGHTCYAN);
drawNodeStep(map->start_node, map->end_node, current_node, SEARCH_DELAY);
}
}
}
// fail to search
cout << "Failed!" << endl;
drawResult(map->start_node, map->end_node, result_nodes);
return false;
}
int AStar::heuristicDist(const Node& node1, const Node& node2)
{
if (dist_type = DistType::Euclidean)
{
return hypot(node1.col - node2.col, node1.row - node2.row) * LEN;
}
else if (dist_type = DistType::Manhattan)
{
return(abs(node1.col - node2.col) + abs(node1.row - node2.row)) * LEN;
}
else if (dist_type == DistType::Chebyshev)
{
int len_col = abs(node1.col - node2.col);
int len_row = abs(node1.row - node2.row);
int x_step = min(len_col, len_row);
int res_step = abs(len_col - len_row);
return x_step * XLEN + res_step * LEN;
}
return 0;
}
void AStar::calcCost(const Node& current_node, Node& next_node, const Node& end_node)
{
next_node.g = current_node.g + step_len;
next_node.h = heuristicDist(next_node, end_node);
if (algo_type == AlgoType::Dijkstra)
{
next_node.h = 0;
}
else if (algo_type == AlgoType::BFS)
{
next_node.g = 0;
}
next_node.f = next_node.g + next_node.h;
cout << "COST: f = " << next_node.f << ", g = " << next_node.g << ", h = " << next_node.h << endl;
}
void AStar::printResult(const vector<Node*>& result_nodes)
{
cout << "num of waypoints: " << result_nodes.size() <<endl;
cout << "num of search: " << close_set.size() <<endl;
cout << "path len: " << path_len <<endl;
for (const auto& result_node : result_nodes)
{
cout << "(" << result_node->row << ", " << result_node->col << ")" << endl;
cout << "f = " << result_node->f << ", g = " << result_node->g << ", h = " << result_node->h << endl;
}
}
void AStar::drawNodeStep(Node* const start, Node* const end, Node* const node, const int& time)
{
if (node != start && node != end)
{
BeginBatchDraw();
node->drawNode();
delay(time);
EndBatchDraw();
}
}
void AStar::drawResult(Node* const start, Node* const end, const vector<Node*>& result_nodes)
{
setfillcolor(GREEN);
for (const auto& result_node : result_nodes)
{
drawNodeStep(start, end, result_node, RESULT_DELAY);
}
}
}