/
vhpop.cc
406 lines (381 loc) · 12.7 KB
/
vhpop.cc
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
// Copyright (C) 2002--2005 Carnegie Mellon University
// Copyright (C) 2019 Google Inc
//
// This file is part of VHPOP.
//
// VHPOP 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 2 of the License, or
// (at your option) any later version.
//
// VHPOP 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 VHPOP; if not, write to the Free Software Foundation,
// Inc., #59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// Main program.
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <limits>
#include "debug.h"
#include "domains.h"
#include "heuristics.h"
#include "parameters.h"
#include "plans.h"
#include "problems.h"
#include "src/timer.h"
#if HAVE_GETOPT_LONG
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <getopt.h>
#endif
/* The parse function. */
extern int yyparse();
/* File to parse. */
extern FILE* yyin;
/* Name of current file. */
std::string current_file;
/* Level of warnings. */
int warning_level;
/* Verbosity level. */
int verbosity;
/* Program options. */
static struct option long_options[] = {
{ "action-cost", required_argument, NULL, 'a' },
{ "domain-constraints", optional_argument, NULL, 'd' },
{ "flaw-order", required_argument, NULL, 'f' },
{ "ground-actions", no_argument, NULL, 'g' },
{ "help", no_argument, NULL, 'H' },
{ "heuristic", required_argument, NULL, 'h' },
{ "limit", required_argument, NULL, 'l' },
{ "random-open-conditions", no_argument, NULL, 'r' },
{ "search-algorithm", required_argument, NULL, 's' },
{ "seed", required_argument, NULL, 'S' },
{ "time-limit", required_argument, NULL, 'T' },
{ "tolerance", required_argument, NULL, 't' },
{ "version", no_argument, NULL, 'V' },
{ "verbose", optional_argument, NULL, 'v' },
{ "warnings", optional_argument, NULL, 'W' },
{ "weight", required_argument, NULL, 'w' },
{ 0, 0, 0, 0 }
};
static const char OPTION_STRING[] = "a:d::f:gHh:l:rS:s:T:t:Vv::W::w:";
/* Displays help. */
static void display_help() {
std::cout << "usage: " << PACKAGE << " [options] [file ...]" << std::endl
<< "options:" << std::endl
<< " -a a, --action-cost=a" << std::endl
<< "\t\t\tuse action cost a" << std::endl
<< " -d[k], --domain-constraints=[k]" << std::endl
<< "\t\t\tuse parameter domain constraints;" << std::endl
<< "\t\t\t if k is 0, static preconditions are pruned;"
<< std::endl
<< "\t\t\t otherwise (default) static preconditions are kept"
<< std::endl
<< " -f f, --flaw-order=f\t"
<< "use flaw selection order f" << std::endl
<< " -g, --ground-actions" << std::endl
<< "\t\t\tuse ground actions" << std::endl
<< " -H --help\t\t"
<< "display this help and exit" << std::endl
<< " -h h, --heuristic=h\t"
<< "use heuristic h to rank plans" << std::endl
<< " -l l, --limit=l\t"
<< "search no more than l plans" << std::endl
<< " -r, --random-open-conditions" << std::endl
<< "\t\t\tadd open conditions in random order"
<< std::endl
<< " -S s, --seed=s\t"
<< "uses s as seed for random number generator" << std::endl
<< " -s s, --search-algorithm=s" << std::endl
<< "\t\t\tuse search algorithm s" << std::endl
<< " -T t, --time-limit=t\t"
<< "limit search to t minutes" << std::endl
<< " -t t, --tolerance=t\t"
<< "use tolerance t with durative actions;" << std::endl
<< "\t\t\t time stamps less than t appart are considered"
<< std::endl
<< "\t\t\t indistinguishable (default is 0.01)" << std::endl
<< " -v[n], --verbose[=n]\t"
<< "use verbosity level n;" << std::endl
<< "\t\t\t n is a number from 0 (verbose mode off) and up;"
<< std::endl
<< "\t\t\t default level is 1 if optional argument is left out"
<< std::endl
<< " -V, --version\t"
<< "display version information and exit" << std::endl
<< " -W[n], --warnings[=n]\t"
<< "determines how warnings are treated;" << std::endl
<< "\t\t\t 0 supresses warnings; 1 displays warnings;"
<< std::endl
<< "\t\t\t 2 treats warnings as errors" << std::endl
<< " -w, --weight=w\t"
<< "weight to use with heuristic (default is 1)" << std::endl
<< " file ...\t\t"
<< "files containing domain and problem descriptions;" << std::endl
<< "\t\t\t if none, descriptions are read from standard input"
<< std::endl
<< std::endl
<< "Report bugs to <" PACKAGE_BUGREPORT ">." << std::endl;
}
/* Displays version information. */
static void display_version() {
std::cout << PACKAGE_STRING << std::endl
<< "Copyright (C) 2002--2005 Carnegie Mellon University"
<< std::endl << "Copyright (C) 2019 Google Inc" << std::endl
<< PACKAGE_NAME
<< " comes with NO WARRANTY, to the extent permitted by law."
<< std::endl
<< "For information about the terms of redistribution,"
<< std::endl
<< "see the file named COPYING in the " PACKAGE_NAME
<< " distribution." << std::endl
<< std::endl
<< "Written by Haakan Younes." << std::endl;
}
/* Parses the given file, and returns true on success. */
static bool read_file(const char* name) {
yyin = fopen(name, "r");
if (yyin == NULL) {
std::cerr << PACKAGE << ':' << name << ": " << strerror(errno)
<< std::endl;
return false;
} else {
current_file = name;
bool success = (yyparse() == 0);
fclose(yyin);
return success;
}
}
/* Cleanup function. */
static void cleanup() {
Problem::clear();
Domain::clear();
}
/* The main program. */
int main(int argc, char* argv[]) {
atexit(cleanup);
const bool free_all_memory = getenv("VHPOP_FREE_ALL_MEMORY");
/* Default planning parameters. */
Parameters params;
bool no_flaw_order = true;
bool no_search_limit = true;
/* Set default verbosity. */
verbosity = 0;
/* Set default warning level. */
warning_level = 1;
/*
* Get command line options.
*/
while (1) {
int option_index = 0;
#if HAVE_GETOPT_LONG
int c = getopt_long(argc, argv, OPTION_STRING, long_options, &option_index);
#else
int c = getopt(argc, argv, OPTION_STRING);
#endif
if (c == -1) {
break;
}
switch (c) {
case 'a':
try {
params.set_action_cost(optarg);
} catch (const InvalidActionCost& e) {
std::cerr << PACKAGE ": " << e.what() << std::endl
<< "Try `" PACKAGE " --help' for more information."
<< std::endl;
return -1;
}
break;
case 'd':
params.domain_constraints = true;
params.keep_static_preconditions = (optarg == NULL || atoi(optarg) != 0);
break;
case 'f':
try {
if (no_flaw_order) {
params.flaw_orders.clear();
no_flaw_order = false;
}
params.flaw_orders.push_back(FlawSelectionOrder(optarg));
} catch (const InvalidFlawSelectionOrder& e) {
std::cerr << PACKAGE << ": " << e.what() << std::endl
<< "Try `" << PACKAGE << " --help' for more information."
<< std::endl;
return -1;
}
break;
case 'g':
params.ground_actions = true;
break;
case 'H':
display_help();
return 0;
case 'h':
try {
params.heuristic = optarg;
} catch (const InvalidHeuristic& e) {
std::cerr << PACKAGE ": " << e.what() << std::endl
<< "Try `" PACKAGE " --help' for more information."
<< std::endl;
return -1;
}
break;
case 'l':
if (no_search_limit) {
params.search_limits.clear();
no_search_limit = false;
}
if (optarg == std::string("unlimited")) {
params.search_limits.push_back(std::numeric_limits<unsigned int>::max());
} else {
params.search_limits.push_back(atoi(optarg));
}
break;
case 'r':
params.random_open_conditions = true;
break;
case 'S':
srand(atoi(optarg));
break;
case 's':
try {
params.set_search_algorithm(optarg);
} catch (const InvalidSearchAlgorithm& e) {
std::cerr << PACKAGE ": " << e.what() << std::endl
<< "Try `" PACKAGE " --help' for more information."
<< std::endl;
return -1;
}
break;
case 'T':
params.time_limit = std::chrono::minutes(atoi(optarg));
break;
case 't':
if (optarg == std::string("unlimited")) {
Orderings::threshold = std::numeric_limits<unsigned int>::max();
} else {
Orderings::threshold = atof(optarg);
}
break;
case 'V':
display_version();
return 0;
case 'v':
verbosity = (optarg != NULL) ? atoi(optarg) : 1;
break;
case 'W':
warning_level = (optarg != NULL) ? atoi(optarg) : 1;
break;
case 'w':
params.weight = atof(optarg);
break;
case ':':
default:
std::cerr << "Try `" PACKAGE " --help' for more information."
<< std::endl;
return -1;
}
}
for (size_t i = 0;
i < params.flaw_orders.size() - params.search_limits.size(); i++) {
params.search_limits.push_back(params.search_limits.back());
}
try {
/*
* Read pddl files.
*/
if (optind < argc) {
/*
* Use remaining command line arguments as file names.
*/
while (optind < argc) {
if (!read_file(argv[optind++])) {
return -1;
}
}
} else {
/*
* No remaining command line argument, so read from standard input.
*/
yyin = stdin;
if (yyparse() != 0) {
return -1;
}
}
if (verbosity > 1) {
/*
* Display domains and problems.
*/
std::cerr << "----------------------------------------"<< std::endl
<< "domains:" << std::endl;
for (Domain::DomainMap::const_iterator di = Domain::begin();
di != Domain::end(); di++) {
std::cerr << std::endl << *(*di).second << std::endl;
}
std::cerr << "----------------------------------------"<< std::endl
<< "problems:" << std::endl;
for (Problem::ProblemMap::const_iterator pi = Problem::begin();
pi != Problem::end(); pi++) {
std::cerr << std::endl << *(*pi).second << std::endl;
}
std::cerr << "----------------------------------------"<< std::endl;
}
std::cerr.setf(std::ios::unitbuf);
/*
* Solve the problems.
*/
for (Problem::ProblemMap::const_iterator pi = Problem::begin();
pi != Problem::end(); ) {
const Problem& problem = *(*pi).second;
pi++;
std::cout << ';' << problem.name() << std::endl;
Timer<> timer;
const Plan* plan =
Plan::plan(problem, params, !free_all_memory && pi == Problem::end());
if (plan != NULL) {
if (plan->complete()) {
if (verbosity > 0) {
#ifdef DEBUG
std::cerr << "Depth of solution: " << plan->depth() << std::endl;
#endif
std::cerr << "Number of steps: " << plan->num_steps() << std::endl;
}
std::cout << *plan << std::endl;
} else {
std::cout << "no plan" << std::endl;
std::cout << ";Search limit reached." << std::endl;
}
} else {
std::cout << "no plan" << std::endl;
std::cout << ";Problem has no solution." << std::endl;
}
if (free_all_memory || pi != Problem::end()) {
if (plan != NULL) {
delete plan;
}
Plan::cleanup();
}
/* Planning time. */
const auto elapsed_millis =
std::chrono::duration_cast<std::chrono::milliseconds>(
timer.ElapsedTime());
std::cout << "Time: " << elapsed_millis.count() << std::endl;
}
} catch (const std::exception& e) {
std::cerr << PACKAGE ": " << e.what() << std::endl;
return -1;
} catch (...) {
std::cerr << PACKAGE ": fatal error" << std::endl;
return -1;
}
return 0;
}