forked from SpaceGroupUCL/depthmapX
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagentparser.cpp
More file actions
351 lines (332 loc) · 12.9 KB
/
Copy pathagentparser.cpp
File metadata and controls
351 lines (332 loc) · 12.9 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
// Copyright (C) 2017 Petros Koutsolampros
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "agentparser.h"
#include "exceptions.h"
#include <cstring>
#include "radiusconverter.h"
#include "runmethods.h"
#include "parsingutils.h"
#include "salalib/entityparsing.h"
#include <sstream>
using namespace depthmapX;
AgentParser::AgentParser() : m_agentMode(AgentMode::NONE)
{}
void AgentParser::parse(int argc, char *argv[])
{
std::vector<std::string> points;
std::string pointFile;
for ( int i = 1; i < argc; )
{
if ( std::strcmp ("-am", argv[i]) == 0)
{
if (m_agentMode != AgentMode::NONE)
{
throw CommandLineException("-am can only be used once, modes are mutually exclusive");
}
ENFORCE_ARGUMENT("-am", i)
if ( std::strcmp(argv[i], "standard") == 0 )
{
m_agentMode = AgentMode::STANDARD;
}
else if ( std::strcmp(argv[i], "los-length") == 0 )
{
m_agentMode = AgentMode::LOS_LENGTH;
}
else if ( std::strcmp(argv[i], "occ-length") == 0 )
{
m_agentMode = AgentMode::OCC_LENGTH;
}
else if ( std::strcmp(argv[i], "occ-any") == 0 )
{
m_agentMode = AgentMode::OCC_ANY;
}
else if ( std::strcmp(argv[i], "occ-group-45") == 0)
{
m_agentMode = AgentMode::OCC_GROUP_45;
}
else if ( std::strcmp(argv[i], "occ-group-60") == 0)
{
m_agentMode = AgentMode::OCC_GROUP_60;
}
else if ( std::strcmp(argv[i], "occ-furthest") == 0)
{
m_agentMode = AgentMode::OCC_FURTHEST;
}
else if ( std::strcmp(argv[i], "bin-far-dist") == 0)
{
m_agentMode = AgentMode::BIN_FAR_DIST;
}
else if ( std::strcmp(argv[i], "bin-angle") == 0)
{
m_agentMode = AgentMode::BIN_ANGLE;
}
else if ( std::strcmp(argv[i], "bin-far-dist-angle") == 0)
{
m_agentMode = AgentMode::BIN_FAR_DIST_ANGLE;
}
else if ( std::strcmp(argv[i], "bin-memory") == 0)
{
m_agentMode = AgentMode::BIN_MEMORY;
}
else
{
throw CommandLineException(std::string("Invalid AGENTS mode: ") + argv[i]);
}
}
else if ( std::strcmp(argv[i], "-ats") == 0 )
{
if (m_totalSystemTimestemps > 0)
{
throw CommandLineException("-ats can only be used once");
}
ENFORCE_ARGUMENT("-ats", i)
if (!has_only_digits(argv[i]))
{
throw CommandLineException(std::string("-ats must be a number >0, got ") + argv[i]);
}
m_totalSystemTimestemps = std::atoi(argv[i]);
if (m_totalSystemTimestemps <= 0)
{
throw CommandLineException(std::string("-ats must be a number >0, got ") + argv[i]);
}
}
else if ( std::strcmp(argv[i], "-arr") == 0 )
{
if (m_releaseRate > 0)
{
throw CommandLineException("-arr can only be used once");
}
ENFORCE_ARGUMENT("-arr", i)
if (!has_only_digits_dots_commas(argv[i]))
{
throw CommandLineException(std::string("-arr must be a number >0, got ") + argv[i]);
}
m_releaseRate = std::atof(argv[i]);
if (m_releaseRate <= 0)
{
throw CommandLineException(std::string("-arr must be a number >0, got ") + argv[i]);
}
}
else if (std::strcmp(argv[i], "-atrails") == 0)
{
if (m_recordTrailsForAgents >= 0)
{
throw CommandLineException("-atrails can only be used once");
}
ENFORCE_ARGUMENT("-atrails", i)
if (!has_only_digits(argv[i]))
{
throw CommandLineException(std::string("-atrails must be a number >=1 or 0 for all (max possible = 50), got ") + argv[i]);
}
m_recordTrailsForAgents = std::atoi(argv[i]);
if (m_recordTrailsForAgents < 0)
{
throw CommandLineException(std::string("-atrails must be a number >=1 or 0 for all (max possible = 50), got ") + argv[i]);
}
}
else if (std::strcmp(argv[i], "-afov") == 0)
{
if (m_agentFOV > 0)
{
throw CommandLineException("-afov can only be used once");
}
ENFORCE_ARGUMENT("-afov", i)
if (!has_only_digits(argv[i]))
{
throw CommandLineException(std::string("-afov must be a number between 1 and 32, got ") + argv[i]);
}
m_agentFOV = std::atoi(argv[i]);
if (m_agentFOV <= 0 || m_agentFOV > 32)
{
throw CommandLineException(std::string("-afov must be a number between 1 and 32, got ") + argv[i]);
}
}
else if (std::strcmp(argv[i], "-asteps") == 0)
{
if (m_agentStepsBeforeTurnDecision > 0)
{
throw CommandLineException("-asteps can only be used once");
}
ENFORCE_ARGUMENT("-asteps", i)
if (!has_only_digits(argv[i]))
{
throw CommandLineException(std::string("-asteps must be a number >0, got ") + argv[i]);
}
m_agentStepsBeforeTurnDecision = std::atoi(argv[i]);
if (m_agentStepsBeforeTurnDecision <= 0)
{
throw CommandLineException(std::string("-asteps must be a number >0, got ") + argv[i]);
}
}
else if (std::strcmp(argv[i], "-alife") == 0)
{
if (m_agentLifeTimesteps > 0)
{
throw CommandLineException("-alife can only be used once");
}
ENFORCE_ARGUMENT("-alife", i)
if (!has_only_digits(argv[i]))
{
throw CommandLineException(std::string("-alife must be a number >0, got ") + argv[i]);
}
m_agentLifeTimesteps = std::atoi(argv[i]);
if (m_agentLifeTimesteps <= 0)
{
throw CommandLineException(std::string("-alife must be a number >0, got ") + argv[i]);
}
}
else if (std::strcmp(argv[i], "-alocseed") == 0)
{
if (!pointFile.empty())
{
throw CommandLineException("-alocseed cannot be used together with -alocfile");
}
if (!points.empty())
{
throw CommandLineException("-alocseed cannot be used together with -aloc");
}
ENFORCE_ARGUMENT("-alocseed", i)
if (!has_only_digits(argv[i]))
{
std::stringstream message;
message << "Invalid starting location seed provided ("
<< argv[i]
<< "). Should only contain digits"
<< std::flush;
throw CommandLineException(message.str().c_str());
}
m_randomReleaseLocationSeed = std::atof(argv[i]);
if (m_randomReleaseLocationSeed < 0 || m_randomReleaseLocationSeed > 10)
{
throw CommandLineException(std::string("-alocseed must be a number between 0 and 10, got ") + argv[i]);
}
}
else if (std::strcmp(argv[i], "-alocfile") == 0)
{
if (!points.empty())
{
throw CommandLineException("-alocfile cannot be used together with -aloc");
}
if (m_randomReleaseLocationSeed > -1)
{
throw CommandLineException("-alocfile cannot be used together with -alocseed");
}
ENFORCE_ARGUMENT("-alocfile", i)
pointFile = argv[i];
}
else if (std::strcmp(argv[i], "-aloc") == 0)
{
if (!pointFile.empty())
{
throw CommandLineException("-aloc cannot be used together with -alocfile");
}
if (m_randomReleaseLocationSeed > -1)
{
throw CommandLineException("-aloc cannot be used together with -alocseed");
}
ENFORCE_ARGUMENT("-aloc", i)
if (!has_only_digits_dots_commas(argv[i]))
{
std::stringstream message;
message << "Invalid starting point provided ("
<< argv[i]
<< "). Should only contain digits dots and commas"
<< std::flush;
throw CommandLineException(message.str().c_str());
}
points.push_back(argv[i]);
}
else if (std::strcmp(argv[i], "-ot") == 0)
{
ENFORCE_ARGUMENT("-ot", i)
if ( std::strcmp(argv[i], "graph") == 0 )
{
if(std::find(m_outputTypes.begin(), m_outputTypes.end(), OutputType::GRAPH) != m_outputTypes.end()) {
throw CommandLineException("Same output type argument (graph) provided twice");
}
m_outputTypes.push_back(OutputType::GRAPH);
}
else if ( std::strcmp(argv[i], "gatecounts") == 0 )
{
if(std::find(m_outputTypes.begin(), m_outputTypes.end(), OutputType::GATECOUNTS) != m_outputTypes.end()) {
throw CommandLineException("Same output type argument (gatecounts) provided twice");
}
m_outputTypes.push_back(OutputType::GATECOUNTS);
}
else if ( std::strcmp(argv[i], "trails") == 0 )
{
if(std::find(m_outputTypes.begin(), m_outputTypes.end(), OutputType::TRAILS) != m_outputTypes.end()) {
throw CommandLineException("Same output type argument (trails) provided twice");
}
m_outputTypes.push_back(OutputType::TRAILS);
}
}
++i;
}
if(m_agentMode == AgentMode::NONE)
{
m_agentMode = AgentMode::STANDARD;
}
if (m_totalSystemTimestemps == 0)
{
throw CommandLineException("Total number of timesteps (-ats <timesteps>) is required");
}
if (m_releaseRate == 0)
{
throw CommandLineException("Release rate (-arr <rate>) is required");
}
if (m_agentFOV == 0)
{
throw CommandLineException("Agent field-of-view (-afov <bins>) is required");
}
if (m_agentStepsBeforeTurnDecision == 0)
{
throw CommandLineException("Agent number of steps before turn decision (-asteps <steps>) is required");
}
if (m_agentLifeTimesteps == 0)
{
throw CommandLineException("Agent life in timesteps (-alife <timesteps>) is required");
}
if (pointFile.empty() && points.empty() && m_randomReleaseLocationSeed == -1)
{
throw CommandLineException("Either -aloc, -alocfile or -alocseed must be given");
}
if(!pointFile.empty())
{
std::ifstream pointsStream(pointFile);
if (!pointsStream)
{
std::stringstream message;
message << "Failed to load file " << pointFile << ", error " << std::strerror(errno) << std::flush;
throw depthmapX::RuntimeException(message.str().c_str());
}
std::vector<Point2f> parsed = EntityParsing::parsePoints(pointsStream, '\t');
m_releasePoints.insert(std::end(m_releasePoints), std::begin(parsed), std::end(parsed));
}
else if(!points.empty())
{
std::stringstream pointsStream;
pointsStream << "x,y";
std::vector<std::string>::iterator iter = points.begin(), end =
points.end();
for ( ; iter != end; ++iter )
{
pointsStream << "\n" << *iter;
}
std::vector<Point2f> parsed = EntityParsing::parsePoints(pointsStream, ',');
m_releasePoints.insert(std::end(m_releasePoints), std::begin(parsed), std::end(parsed));
}
}
void AgentParser::run(const CommandLineParser &clp, IPerformanceSink &perfWriter) const
{
dm_runmethods::runAgentAnalysis(clp, *this, perfWriter);
}