-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathisearch.cpp
More file actions
242 lines (212 loc) · 8.74 KB
/
isearch.cpp
File metadata and controls
242 lines (212 loc) · 8.74 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "isearch.h"
template<typename NodeType>
ISearch<NodeType>::ISearch(const Primitives &MP, bool WithTime)
{
mp = MP;
withTime = WithTime;
}
template<typename NodeType>
ISearch<NodeType>::~ISearch(void) {}
template<typename NodeType>
long long ISearch<NodeType>::T = 0;
template<typename NodeType>
SearchResult ISearch<NodeType>::startSearch(const Map &map, const AgentSet &agentSet,
int start_i, int start_j, int goal_i, int goal_j,
int startAngleId, int goalAngleId, int startTime, int goalTime, int maxTime,
const ConstraintsSet &constraints,
bool withCAT, const ConflictAvoidanceTable &CAT)
{
sresult.pathfound = false;
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
if (goalTime != -1) {
maxTime = goalTime;
}
NodeType cur;
int agentId = -1;
if (agentSet.isOccupied(start_i, start_j)) {
agentId = agentSet.getAgentId(start_i, start_j);
}
if (withCAT) {
open = SearchQueue<NodeType>([](const NodeType &lhs, const NodeType &rhs) {
return std::tuple<int, int, int, int, int>(lhs.F, lhs.conflictsCount, -lhs.g, lhs.i, lhs.j) <
std::tuple<int, int, int, int, int>(rhs.F, rhs.conflictsCount, -rhs.g, rhs.i, rhs.j);
});
}
clearLists();
sresult.numberofsteps = 0;
cur = NodeType(start_i, start_j, nullptr, startTime,
computeHFromCellToCell(start_i, start_j, goal_i, goal_j));
setEndTime(cur, start_i, start_j, startTime, agentId, constraints);
if (mp.withTurns()) {
cur.angleId = startAngleId;
}
addStartNode(cur, map, CAT);
addSuboptimalNode(cur, map, CAT);
while(!checkOpenEmpty()) {
++sresult.numberofsteps;
if (sresult.numberofsteps % 100000 == 0) {
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
int elapsedMilliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
if (elapsedMilliseconds > 10000) {
break;
}
}
cur = getCur(map);
close[cur.convolution(map.getMapWidth(), map.getMapHeight(), withTime)] = cur;
NodeType *curPtr = &(close.find(cur.convolution(map.getMapWidth(), map.getMapHeight(), withTime))->second);
if (cur.i == goal_i && cur.j == goal_j && (goalAngleId == -1 || !mp.withTurns() || cur.angleId == goalAngleId)) {
bool hasFutureConstraint = false;
for (auto cell : mp.covering) {
if (constraints.hasFutureConstraint(cur.i + cell.i, cur.j + cell.j, cur.g, agentId)) {
hasFutureConstraint = true;
break;
}
}
if (!hasFutureConstraint && checkGoal(cur, goalTime, agentId, constraints)) {
sresult.pathfound = true;
break;
} else {
subtractFutureConflicts(cur);
}
}
if (maxTime == -1 || cur.g < maxTime) {
std::list<NodeType> successors;
findSuccessors(successors, cur, map, goal_i, goal_j, agentId, constraints, withCAT, CAT);
for (auto neigh : successors) {
if (close.find(neigh.convolution(map.getMapWidth(), map.getMapHeight(), withTime)) == close.end()) {
neigh.parent = curPtr;
if (!updateFocal(neigh, map)) {
open.insert(map, neigh, withTime);
}
}
}
}
}
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
int elapsedMilliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
sresult.time = static_cast<double>(elapsedMilliseconds) / 1000;
sresult.nodescreated = open.size() + close.size() + getFocalSize();
sresult.nodesexpanded = close.size();
//if (withTime) {
// std::cout << sresult.numberofsteps << std::endl;
//}
//std::cout << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << " " << sresult.nodesexpanded << std::endl;
if (sresult.pathfound) {
sresult.pathlength = cur.g;
sresult.minF = std::min(double(cur.F), getMinFocalF());
sresult.lastNode = cur;
lppath.clear();
hppath.clear();
makePrimaryPath(cur, goalTime == -1 ? -1 : goalTime + 1);
makeSecondaryPath(map);
sresult.hppath = &hppath; //Here is a constant pointer
sresult.lppath = &lppath;
}
return sresult;
}
template<typename NodeType>
void ISearch<NodeType>::findSuccessors(std::list<NodeType> &successors,
const NodeType &curNode, const Map &map,
int goal_i, int goal_j, int agentId,
const ConstraintsSet &constraints,
bool withCAT, const ConflictAvoidanceTable &CAT)
{
std::vector<Primitive> primitives, turns;
mp.getPrimitives(primitives, curNode.i, curNode.j, curNode.angleId, curNode.speed, map);
if (curNode.speed == 0) {
mp.getTurns(turns, curNode.angleId);
primitives.insert(primitives.end(), turns.begin(), turns.end());
}
for (auto pr : primitives) {
pr.setSource(curNode.i, curNode.j);
int newi = pr.target.i;
int newj = pr.target.j;
int newg = curNode.g + pr.intDuration;
double newh = this->computeHFromCellToCell(newi, newj, goal_i, goal_j);
NodeType neigh(newi, newj, nullptr, newg, newh, 0, pr.id, pr.target.angle_id, pr.target.speed);
createSuccessorsFromNode(curNode, neigh, successors, agentId, constraints, CAT,
neigh.i == goal_i && neigh.j == goal_j, pr);
}
}
template<typename NodeType>
void ISearch<NodeType>::clearLists() {
open.clear();
close.clear();
}
template<typename NodeType>
void ISearch<NodeType>::addStartNode(NodeType &node, const Map &map, const ConflictAvoidanceTable &CAT) {
open.insert(map, node, withTime);
}
template<typename NodeType>
bool ISearch<NodeType>::checkOpenEmpty() {
return open.empty();
}
template<typename NodeType>
NodeType ISearch<NodeType>::getCur(const Map& map) {
NodeType cur = open.getFront();
open.erase(map, cur, withTime);
return cur;
}
template<typename NodeType>
bool ISearch<NodeType>::updateFocal(const NodeType& neigh, const Map& map) {
return false;
}
template<typename NodeType>
double ISearch<NodeType>::getMinFocalF() {
return CN_INFINITY;
}
template<typename NodeType>
void ISearch<NodeType>::setEndTime(NodeType& node, int start_i, int start_j, int startTime, int agentId, const ConstraintsSet &constraints) {
return;
}
template<typename NodeType>
int ISearch<NodeType>::getNextConstraintTime(const NodeType& node, const ConstraintsSet &constraints, int agentId) {
return constraints.getFirstConstraintTime(node.i, node.j, node.g, agentId);
}
template<typename NodeType>
bool ISearch<NodeType>::checkGoal(const NodeType &cur, int goalTime, int agentId, const ConstraintsSet &constraints) {
return goalTime == -1 || cur.g == goalTime;
}
template<typename NodeType>
void ISearch<NodeType>::createSuccessorsFromNode(const NodeType &cur, NodeType &neigh, std::list<NodeType> &successors,
int agentId, const ConstraintsSet &constraints,
const ConflictAvoidanceTable &CAT, bool isGoal, Primitive &pr) {
for (auto cell : pr.cells) {
if (constraints.hasNodeConstraint(cell.i, cell.j, cur.g + cell.interval.first,
agentId, cell.interval.second - cell.interval.first + 1)) {
return;
}
}
setHC(neigh, cur, CAT, isGoal);
successors.push_back(neigh);
}
template<typename NodeType>
void ISearch<NodeType>::makePrimaryPath(Node &curNode, int endTime)
{
this->lppath.push_front(curNode);
if (curNode.parent != nullptr) {
makePrimaryPath(*(curNode.parent), curNode.g);
}
}
template<typename NodeType>
void ISearch<NodeType>::makeSecondaryPath(const Map &map)
{
auto it = lppath.begin();
hppath.push_back(*it);
++it;
for (it; it != lppath.end(); ++it) {
auto prevIt = it;
--prevIt;
auto nextIt = it;
++nextIt;
if (nextIt == lppath.end() ||
(it->i - prevIt->i) * (nextIt->j - it->j) != (it->j - prevIt->j) * (nextIt->i - it->i)) {
hppath.push_back(*it);
}
}
}
template class ISearch<Node>;
template class ISearch<SIPPNode>;
template class ISearch<ZeroSCIPPNode>;
template class ISearch<SCIPPNode>;
template class ISearch<FSNode>;