-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhybrid_breadth_first.cpp
191 lines (149 loc) · 4.78 KB
/
hybrid_breadth_first.cpp
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
#include <algorithm>
#include <iostream>
#include <sstream>
#include <fstream>
#include <math.h>
#include <vector>
#include "hybrid_breadth_first.h"
using namespace std;
/**
* Initializes HBF
*/
HBF::HBF() {
}
HBF::~HBF() {}
bool HBF::compare_maze_s(const HBF::maze_s & lhs, const HBF::maze_s & rhs) {
return lhs.f < rhs.f;
}
double HBF::heuristic(double x, double y, vector<int> goal){
return fabs(y - goal[0]) + fabs(x - goal[1]); //return grid distance to goal
}
int HBF::theta_to_stack_number(double theta){
/*
Takes an angle (in radians) and returns which "stack" in the 3D configuration space
this angle corresponds to. Angles near 0 go in the lower stacks while angles near
2 * pi go in the higher stacks.
*/
double new_theta = fmod((theta + 2 * M_PI),(2 * M_PI));
int stack_number = (int)(round(new_theta * NUM_THETA_CELLS / (2*M_PI))) % NUM_THETA_CELLS;
return stack_number;
}
int HBF::idx(double float_num) {
/*
Returns the index into the grid for continuous position. So if x is 3.621, then this
would return 3 to indicate that 3.621 corresponds to array index 3.
*/
return int(floor(float_num));
}
vector<HBF::maze_s> HBF::expand(HBF::maze_s state, vector<int> goal) {
int g = state.g;
double x = state.x;
double y = state.y;
double theta = state.theta;
int g2 = g+1;
vector<HBF::maze_s> next_states;
for(double delta_i = -35; delta_i < 40; delta_i+=5)
{
double delta = M_PI / 180.0 * delta_i;
double omega = SPEED / LENGTH * tan(delta);
double theta2 = theta + omega;
if(theta2 < 0)
{
theta2 += 2*M_PI;
}
double x2 = x + SPEED * cos(theta);
double y2 = y + SPEED * sin(theta);
HBF::maze_s state2;
state2.f = g2 + heuristic(x2, y2, goal);
state2.g = g2;
state2.x = x2;
state2.y = y2;
state2.theta = theta2;
next_states.push_back(state2);
}
return next_states;
}
vector< HBF::maze_s> HBF::reconstruct_path(vector< vector< vector<HBF::maze_s> > > came_from, vector<double> start, HBF::maze_s final){
vector<maze_s> path = {final};
int stack = theta_to_stack_number(final.theta);
maze_s current = came_from[stack][idx(final.x)][idx(final.y)];
stack = theta_to_stack_number(current.theta);
double x = current.x;
double y = current.y;
while( x != start[0] || y != start[1] )
{
path.push_back(current);
current = came_from[stack][idx(x)][idx(y)];
x = current.x;
y = current.y;
stack = theta_to_stack_number(current.theta);
}
return path;
}
HBF::maze_path HBF::search(vector< vector<int> > grid, vector<double> start, vector<int> goal) {
/*
Working Implementation of breadth first search. Does NOT use a heuristic
and as a result this is pretty inefficient. Try modifying this algorithm
into hybrid A* by adding heuristics appropriately.
*/
vector< vector< vector<int> > > closed(NUM_THETA_CELLS, vector<vector<int>>(grid[0].size(), vector<int>(grid.size())));
vector< vector< vector<maze_s> > > came_from(NUM_THETA_CELLS, vector<vector<maze_s>>(grid[0].size(), vector<maze_s>(grid.size())));
double theta = start[2];
int stack = theta_to_stack_number(theta);
int g = 0;
maze_s state;
state.g = g;
state.x = start[0];
state.y = start[1];
state.f = g + heuristic(state.x, state.y, goal);
state.theta = theta;
closed[stack][idx(state.x)][idx(state.y)] = 1;
came_from[stack][idx(state.x)][idx(state.y)] = state;
int total_closed = 1;
vector<maze_s> opened = {state};
bool finished = false;
while(!opened.empty())
{
sort(opened.begin(), opened.end(), compare_maze_s);
maze_s current = opened[0]; //grab first elment
opened.erase(opened.begin()); //pop first element
int x = current.x;
int y = current.y;
if(idx(x) == goal[0] && idx(y) == goal[1])
{
cout << "found path to goal in " << total_closed << " expansions" << endl;
maze_path path;
path.came_from = came_from;
path.closed = closed;
path.final = current;
return path;
}
vector<maze_s> next_state = expand(current, goal);
for(int i = 0; i < next_state.size(); i++)
{
int g2 = next_state[i].g;
double x2 = next_state[i].x;
double y2 = next_state[i].y;
double theta2 = next_state[i].theta;
if((x2 < 0 || x2 >= grid.size()) || (y2 < 0 || y2 >= grid[0].size()))
{
//invalid cell
continue;
}
int stack2 = theta_to_stack_number(theta2);
if(closed[stack2][idx(x2)][idx(y2)] == 0 && grid[idx(x2)][idx(y2)] == 0)
{
opened.push_back(next_state[i]);
closed[stack2][idx(x2)][idx(y2)] = 1;
came_from[stack2][idx(x2)][idx(y2)] = current;
total_closed += 1;
}
}
}
cout << "no valid path." << endl;
HBF::maze_path path;
path.came_from = came_from;
path.closed = closed;
path.final = state;
return path;
}