-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmission.cpp
More file actions
326 lines (299 loc) · 12.4 KB
/
mission.cpp
File metadata and controls
326 lines (299 loc) · 12.4 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include "mission.h"
#include "astar.h"
#include "dijkstra.h"
#include "xmllogger.h"
#include "gl_const.h"
#include <iostream>
#include <numeric>
Mission::Mission()
{
logger = nullptr;
multiagentSearch = nullptr;
mapFile = nullptr;
}
Mission::Mission (const char* MapFile)
{
mapFile = MapFile;
logger = nullptr;
multiagentSearch = nullptr;
}
Mission::~Mission()
{
if (logger)
delete logger;
if (multiagentSearch)
delete multiagentSearch;
}
bool Mission::getMap()
{
return map.getMap(mapFile, config.scale);
}
bool Mission::getAgents(const char* agentsFile)
{
agentSet.clear();
return agentSet.readAgents(agentsFile, config.scale);
}
bool Mission::getConfig()
{
return config.getConfig(mapFile);
}
bool Mission::createLog()
{
if (logger != NULL) delete logger;
logger = new XmlLogger(config.LogParams[CN_LP_LEVEL]);
return logger->getLog(mapFile, config.LogParams);
}
/*void Mission::createEnvironmentOptions()
{
if (config.SearchParams[CN_SP_ST] == CN_SP_ST_BFS || config.SearchParams[CN_SP_ST] == CN_SP_ST_DIJK)
options = EnvironmentOptions(config.SearchParams[CN_SP_AS], config.SearchParams[CN_SP_AD],
config.SearchParams[CN_SP_CC]);
else
options = EnvironmentOptions(config.SearchParams[CN_SP_AS], config.SearchParams[CN_SP_AD],
config.SearchParams[CN_SP_CC], config.SearchParams[CN_SP_MT]);
}*/
/*void Mission::createSearch()
{
if (config.lowLevel == CN_SP_ST_SCIPP) {
searchType = CN_SP_ST_SCIPP;
//search = new SCIPP<>(config.focalW);
} else if (config.withFocalSearch) {
searchType = CN_SP_ST_FOCAL_SEARCH;
//search = new FocalSearch<>(true, config.focalW);
} else if (config.lowLevel == CN_SP_ST_ASTAR) {
if (config.searchType == CN_ST_CBS || config.searchType == CN_ST_PP) {
searchType = CN_SP_ST_TIME_ASTAR;
} else {
searchType = CN_SP_ST_ASTAR;
}
//search = new Astar<>(config.searchType == CN_ST_CBS || config.searchType == CN_ST_PP);
} else {
searchType = CN_SP_ST_SIPP;
//search = new SIPP<>();
}
}*/
void Mission::createAlgorithm()
{
if (config.searchType == CN_ST_CBS) {
if (config.lowLevel == CN_SP_ST_ASTAR) {
multiagentSearch = new ConflictBasedSearch<Astar<>>(new Astar<>(config.mp, true));
} else if (config.lowLevel == CN_SP_ST_SIPP) {
multiagentSearch = new ConflictBasedSearch<SIPP<>>(new SIPP<>(config.mp));
} else if (config.lowLevel == CN_SP_ST_ZSCIPP) {
multiagentSearch = new ConflictBasedSearch<ZeroSCIPP<>>(new ZeroSCIPP<>(config.mp,
config.focalW,
config.genSuboptFromOpt));
} else if (config.lowLevel == CN_SP_ST_FS) {
multiagentSearch = new ConflictBasedSearch<FocalSearch<>>(new FocalSearch<>(config.mp,
true, config.focalW));
} else if (config.lowLevel == CN_SP_ST_SCIPP) {
multiagentSearch = new ConflictBasedSearch<SCIPP<>>(new SCIPP<>(config.mp, config.focalW));
}
} else if (config.searchType == CN_ST_PP) {
if (config.lowLevel == CN_SP_ST_ASTAR) {
multiagentSearch = new PrioritizedPlanning<Astar<>>(new Astar<>(config.mp, true));
} else if (config.lowLevel == CN_SP_ST_SIPP) {
multiagentSearch = new PrioritizedPlanning<SIPP<>>(new SIPP<>(config.mp));
} else if (config.lowLevel == CN_SP_ST_SCIPP) {
multiagentSearch = new PrioritizedPlanning<SCIPP<>>(new SCIPP<>(config.mp, config.focalW));
} else if (config.lowLevel == CN_SP_ST_ZSCIPP) {
multiagentSearch = new PrioritizedPlanning<ZeroSCIPP<>>(new ZeroSCIPP<>(config.mp, config.focalW, config.genSuboptFromOpt));
}
}
}
bool Mission::checkAgentsCorrectness(const std::string &agentsFile) {
if (config.maxAgents != -1 && agentSet.getAgentCount() < config.maxAgents) {
std::cout << "Warning: not enough agents in " << agentsFile <<
" agents file. This file will be ignored" << std::endl;
return false;
}
for (int i = 0; i < agentSet.getAgentCount(); ++i) {
Agent agent = agentSet.getAgent(i);
Node start = agent.getStartPosition(), goal = agent.getGoalPosition();
if (!map.CellOnGrid(start.i, start.j) || !map.CellOnGrid(goal.i, goal.j) ||
map.CellIsObstacle(start.i, start.j) || map.CellIsObstacle(goal.i, goal.j)) {
std::cout << "Warning: start or goal position of agent " << agent.getId() << " in " << agentsFile <<
" agents file is incorrect. This file will be ignored" << std::endl;
return false;
}
}
for (int i = 0; i < agentSet.getAgentCount(); ++i) {
for (int j = i + 1; j < agentSet.getAgentCount(); ++j) {
if (agentSet.getAgent(i).getStartPosition() == agentSet.getAgent(j).getStartPosition()) {
std::cout << "Warning: start positions of agents " << i << " and " << j <<
" in " << agentsFile << " are in the same cell. This file will be ignored" << std::endl;
return false;
} else if (agentSet.getAgent(i).getGoalPosition() == agentSet.getAgent(j).getGoalPosition()) {
std::cout << "Warning: goal positions of agents " << i << " and " << j <<
" in " << agentsFile << " are in the same cell. This file will be ignored" << std::endl;
return false;
}
}
}
return true;
}
void Mission::startSearch(const std::string &agentsFile)
{
int minAgents = config.singleExecution ? config.maxAgents : config.minAgents;
int maxAgents = config.maxAgents == -1 ? agentSet.getAgentCount() : config.maxAgents;
TestingResults res;
for (int i = minAgents; i <= maxAgents; i += config.agentsStep) {
AgentSet curAgentSet;
for (int j = 0; j < i; ++j) {
Agent agent = agentSet.getAgent(j);
curAgentSet.addAgent(agent.getStart_i(), agent.getStart_j(),
agent.getGoal_i(), agent.getGoal_j(),
agent.getStartAngleId(), agent.getGoalAngleId());
}
multiagentSearch->clear();
sr = multiagentSearch->startSearch(map, config, curAgentSet);
if (!sr.pathfound) {
std::cout << "Failed to find solution for " << i << " agents" << std::endl;
if (config.singleExecution) {
std::cout << "Log will not be created" << std::endl;
}
break;
}
agentsPaths = *(sr.agentsPaths);
std::pair<double, double> costs = getCosts();
double makespan = costs.first;
double flowtime = costs.second;
res.data[CNS_TAG_ATTR_MAKESPAN][i] = makespan;
res.data[CNS_TAG_ATTR_FLOWTIME][i] = flowtime;
res.data[CNS_TAG_ATTR_TIME][i] = sr.time;
res.data[CNS_TAG_ATTR_HLE][i] = sr.HLExpansions;
res.data[CNS_TAG_ATTR_HLN][i] = sr.HLNodes;
res.data[CNS_TAG_ATTR_LLE][i] = sr.AvgLLExpansions;
res.data[CNS_TAG_ATTR_LLN][i] = sr.AvgLLNodes;
if (config.singleExecution) {
saveAgentsPathsToLog(agentsFile, sr.time, makespan, flowtime,
sr.HLExpansions, sr.HLNodes, sr.AvgLLExpansions, sr.AvgLLNodes);
}
if (!checkCorrectness()) {
std::cout << "Search returned incorrect results!" << std::endl;
break;
}
std::cout << "Found solution for " << i << " agents. Time: " <<
sr.time << ", flowtime: " << flowtime << ", makespan: " << makespan << std::endl;
}
testingResults.push_back(res);
}
std::pair<double, double> Mission::getCosts() {
double makespan = 0, flowtime = 0;
for (int i = 0; i < agentsPaths.size(); ++i) {
double time = double(agentsPaths[i].back().g) / config.resolution;
makespan = std::max(makespan, time);
flowtime += time;
}
return std::make_pair(makespan, flowtime);
}
bool Mission::checkCorrectness() {
size_t agentCount = agentsPaths.size();
size_t solutionSize = 0;
for (int j = 0; j < agentCount; ++j) {
solutionSize = std::max(solutionSize, agentsPaths[j].size());
}
std::vector<std::list<Node>> lists;
std::vector<std::list<Node>::iterator> starts, ends;
for (int j = 0; j < agentCount; ++j) {
if (agentsPaths[j][0] != agentSet.getAgent(j).getStartPosition()) {
std::cout << "Incorrect result: agent path starts in wrong position!" << std::endl;
return false;
}
if (agentsPaths[j].back() != agentSet.getAgent(j).getGoalPosition()) {
std::cout << "Incorrect result: agent path ends in wrong position!" << std::endl;
return false;
}
lists.push_back(std::list<Node>(agentsPaths[j].begin(), agentsPaths[j].end()));
starts.push_back(lists.back().begin());
ends.push_back(lists.back().end());
auto pr = *std::prev(ends.back());
int q = 0;
}
for (int i = 0; i < solutionSize; ++i) {
for (int j = 0; j < agentCount; ++j) {
if (i >= agentsPaths[j].size()) {
continue;
}
if (map.CellIsObstacle(agentsPaths[j][i].i, agentsPaths[j][i].j)) {
std::cout << "Incorrect result: agent path goes through obstacle!" << std::endl;
return false;
}
}
}
/*ConflictSet conflictSet;
if (config.lowLevel == CN_SP_ST_TKN) {
conflictSet = multiagentSearch->findConflict(starts, ends);
} else {
ConflictBasedSearch<> cbs;
conflictSet = cbs.findConflict(starts, ends);
}
if (!conflictSet.empty()) {
Conflict conflict = conflictSet.getBestConflict();
if (conflict.edgeConflict) {
std::cout << "Incorrect result: two agents swap positions!" << std::endl;
} else {
std::cout << "Incorrect result: two agents occupy the same node!" << std::endl;
}
return false;
}*/
return true;
}
void Mission::saveSeparateResultsToLog() {
for (int i = 0; i < testingResults.size(); ++i) {
std::map<int, int> successCount;
for (auto pair : testingResults[i].data[CNS_TAG_ATTR_TIME]) {
successCount[pair.first] = 1;
}
logger->writeToLogAggregatedResults(successCount, testingResults[i],
config.agentsFile + "-" + std::to_string(i + 1));
logger->saveLog();
}
}
void Mission::saveAggregatedResultsToLog() {
std::map<int, int> successCounts;
TestingResults aggRes;
std::vector<std::string> keys = testingResults[0].getKeys();
for (int i = config.minAgents; i <= config.maxAgents; ++i) {
std::map<std::string, double> sums;
for (auto key : keys) {
sums[key] = 0.0;
}
int successCount = 0;
for (auto res : testingResults) {
if (res.data[CNS_TAG_ATTR_TIME].find(i) != res.data[CNS_TAG_ATTR_TIME].end()) {
for (auto key : keys) {
sums[key] += res.data[key][i];
}
++successCount;
}
}
if (successCount == 0) {
break;
}
successCounts[i] = successCount;
for (auto key : keys) {
aggRes.data[key][i] = sums[key] / successCount;
}
}
logger->writeToLogAggregatedResults(successCounts, aggRes);
logger->saveLog();
}
void Mission::saveAgentsPathsToLog(const std::string &agentsFile, double time,
double makespan, double flowtime,
int HLExpansions, int HLNodes,
double LLExpansions, double LLNodes) {
logger->writeToLogAgentsPaths(agentSet, agentsPaths, agentsFile, time, makespan, flowtime,
HLExpansions, HLNodes, LLExpansions, LLNodes, config.scale,
config.breakPrimitives, config.timeStep, config.mp);
logger->saveLog();
}
int Mission::getTasksCount() {
return config.tasksCount;
}
std::string Mission::getAgentsFile() {
return config.agentsFile;
}
bool Mission::getSingleExecution() {
return config.singleExecution;
}