-
Notifications
You must be signed in to change notification settings - Fork 1
/
anytime_cbs.cpp
207 lines (177 loc) · 7.35 KB
/
anytime_cbs.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "anytime_cbs.h"
template<typename SearchType>
AnytimeCBS<SearchType>::AnytimeCBS() {}
template<typename SearchType>
AnytimeCBS<SearchType>::AnytimeCBS(ConflictBasedSearch<SearchType>* Search)
{
search = Search;
}
template<typename SearchType>
AnytimeCBS<SearchType>::~AnytimeCBS()
{
if (search)
delete search;
}
template<typename SearchType>
void AnytimeCBS<SearchType>::clear()
{
search->clear();
}
template<typename SearchType>
void AnytimeCBS<SearchType>::setChildren(std::list<CBSNode<SearchType>>& nodeSet) {
for (auto& node : nodeSet) {
node.children.clear();
}
for (auto& node : nodeSet) {
if (node.parent != nullptr) {
node.parent->children.push_back(&node);
}
}
}
template<typename SearchType>
void AnytimeCBS<SearchType>::updateNode(const Map &map, const AgentSet &agentSet, const Config &config,
std::vector<int> &costs,
ConstraintsSet &constraints,
std::vector<std::list<Node>::iterator> &starts,
std::vector<std::list<Node>::iterator> &ends,
ConflictAvoidanceTable &CAT, ConflictSet &conflictSet,
std::vector<MDD> &mdds, std::vector<double> &lb,
std::vector<int> &LLExpansions, std::vector<int> &LLNodes,
CBSNode<SearchType>* node, double focalW)
{
int agentId;
int oldCost;
std::list<Node>::iterator oldStart;
std::list<Node>::iterator oldEnd;
MDD oldMDD;
double oldLb;
double oldSumLb = node->sumLb;
bool isRoot = (node->paths.size() > 1);
bool pathFound = true;
if (!isRoot) {
agentId = node->conflict.id1;
this->search->getState(costs, oldCost, starts, oldStart, ends, oldEnd, mdds, oldMDD, lb, oldLb,
agentId, false, config.withFocalSearch);
constraints.addConstraint(node->constraint);
if (node->hasPositiveConstraint) {
constraints.addConstraint(node->positiveConstraint);
}
node->search.get()->updateFocalW(focalW, map);
this->search->createNode(map, agentSet, config, node->conflict, costs, constraints,
agentId, node->conflict.pos2, node->conflict.pos1, starts, ends,
CAT, conflictSet, mdds, lb, LLExpansions, LLNodes, node->parent, *node, node->search.get(), true);
pathFound = node->pathFound;
}
if (pathFound) {
if (node->children.empty()) {
search->sumLb.erase(search->sumLb.find(oldSumLb));
search->sumLb.insert(node->sumLb);
}
for (CBSNode<SearchType>* child : node->children) {
updateNode(map, agentSet, config, costs, constraints, starts, ends,
CAT, node->conflictSet, mdds, lb, LLExpansions, LLNodes, child, focalW);
}
}
if (!isRoot) {
CAT.removeAgentPath(starts[agentId], ends[agentId]);
CAT.addAgentPath(oldStart, oldEnd);
this->search->setState(costs, oldCost, starts, oldStart, ends, oldEnd, mdds, oldMDD, lb, oldLb,
agentId, false, config.withFocalSearch);
constraints.removeConstraint(node->constraint);
if (node->hasPositiveConstraint) {
constraints.removeLastPositiveConstraint();
}
}
}
template<typename SearchType>
MultiagentSearchResult AnytimeCBS<SearchType>::startSearch(const Map &map, const Config &config, AgentSet &agentSet) {
CBSNode<SearchType>::curId = 0;
int agentCount = agentSet.getAgentCount();
MultiagentSearchResult result(false);
Config curConfig = config;
curConfig.LogParams = nullptr;
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
std::vector<std::vector<Node>> agentsPaths;
if (config.withPerfectHeuristic) {
search->getPerfectHeuristic(map, agentSet);
search->search->setPerfectHeuristic(&search->perfectHeuristic);
}
search->search->updateFocalW(config.focalW, map);
while (curConfig.focalW > 1.0) {
std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
if (std::chrono::duration_cast<std::chrono::milliseconds>(now - begin).count() > config.maxTime) {
break;
}
MultiagentSearchResult newResult = search->startSearch(map, curConfig, agentSet);
if (!newResult.pathfound) {
break;
}
result += newResult;
result.time.back() = (double)std::chrono::duration_cast<std::chrono::milliseconds>(now - begin).count() / 1000;
double minF = std::min(*search->sumLb.begin(), (double)result.cost.back());
result.focalW.back() = result.cost.back() / minF;
agentsPaths = *(result.agentsPaths);
search->agentsPaths.clear();
if (search->open.empty() && search->focal.size() == 1) {
result.focalW.back() = 1.0;
break;
}
curConfig.focalW = result.cost.back() / minF - 0.0001;
if (curConfig.focalW < 1.0) {
curConfig.focalW = 1.0;
}
std::cout << newResult.cost[0] << " " << curConfig.focalW << std::endl;
search->search->updateFocalW(curConfig.focalW, map);
if (config.searchType == CN_ST_AECBS) {
std::list<CBSNode<SearchType>> open;
std::copy(search->open.begin(), search->open.end(), std::back_inserter(open));
std::copy(search->focal.begin(), search->focal.end(), std::back_inserter(open));
search->open.clear();
search->focal.clear();
setChildren(search->close);
setChildren(open);
std::vector<int> costs(agentCount, 0);
ConstraintsSet constraints;
std::vector<std::list<Node>::iterator> starts(agentCount);
std::vector<std::list<Node>::iterator> ends(agentCount);
ConflictAvoidanceTable CAT;
std::vector<MDD> mdds(agentCount);
std::vector<double> lb(agentCount, 0.0);
std::vector<int> LLExpansions;
std::vector<int> LLNodes;
CBSNode<SearchType>* root = &(*search->close.begin());
for (auto it = root->paths.begin(); it != root->paths.end(); ++it) {
int agentId = it->first;
CAT.addAgentPath(it->second.begin(), it->second.end());
search->setState(costs, it->second.size() - 1, starts, it->second.begin(),
ends, it->second.end(), mdds, root->mdds[agentId], lb, root->lb[agentId],
agentId, config.withCardinalConflicts, config.withFocalSearch);
}
updateNode(map, agentSet, curConfig, costs, constraints, starts, ends,
CAT, root->conflictSet, mdds, lb, LLExpansions, LLNodes, root, curConfig.focalW);
for (const auto& node : open) {
if (node.pathFound) {
minF = std::min(minF, double(node.cost));
search->focal.insert(node);
}
}
}
auto it = search->focal.begin();
while (it != search->focal.end()) {
if (it->cost > minF * curConfig.focalW) {
search->open.insert(*it);
it = search->focal.erase(it);
} else {
++it;
}
}
}
search->agentsPaths = agentsPaths;
result.agentsPaths = &search->agentsPaths;
return result;
}
template class AnytimeCBS<Astar<>>;
template class AnytimeCBS<FocalSearch<>>;
template class AnytimeCBS<SIPP<>>;
template class AnytimeCBS<SCIPP<>>;
template class AnytimeCBS<ZeroSCIPP<>>;