-
Notifications
You must be signed in to change notification settings - Fork 10
/
plans.cc
2209 lines (2082 loc) · 77.7 KB
/
plans.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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// 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
#include "plans.h"
#include <algorithm>
#include <limits>
#include <queue>
#include <typeinfo>
#include "bindings.h"
#include "debug.h"
#include "domains.h"
#include "formulas.h"
#include "heuristics.h"
#include "parameters.h"
#include "predicates.h"
#include "problems.h"
#include "refcount.h"
#include "terms.h"
#include "types.h"
#include "src/timer.h"
/*
* Mapping of predicate names to achievers.
*/
struct PredicateAchieverMap : public std::map<Predicate, ActionEffectMap> {
};
/* Planning parameters. */
static const Parameters* params;
/* Domain of problem currently being solved. */
static const Domain* domain = NULL;
/* Problem currently being solved. */
static const Problem* problem = NULL;
/* Planning graph. */
static const PlanningGraph* planning_graph;
/* The goal action. */
static Action* goal_action;
/* Maps predicates to actions. */
static PredicateAchieverMap achieves_pred;
/* Maps negated predicates to actions. */
static PredicateAchieverMap achieves_neg_pred;
/* Whether last flaw was a static predicate. */
static bool static_pred_flaw;
/* ====================================================================== */
/* Link */
/* Constructs a causal link. */
Link::Link(size_t from_id, StepTime effect_time,
const OpenCondition& open_cond)
: from_id_(from_id), effect_time_(effect_time), to_id_(open_cond.step_id()),
condition_(open_cond.literal()), condition_time_(open_cond.when()) {
Formula::register_use(condition_);
}
/* Constructs a causal link. */
Link::Link(const Link& l)
: from_id_(l.from_id_), effect_time_(l.effect_time_), to_id_(l.to_id_),
condition_(l.condition_), condition_time_(l.condition_time_) {
Formula::register_use(condition_);
}
/* Deletes this causal link. */
Link::~Link() {
Formula::unregister_use(condition_);
}
/* ====================================================================== */
/* Plan */
/*
* Less than function object for plan pointers.
*/
namespace std {
template<>
struct less<const Plan*>
: public binary_function<const Plan*, const Plan*, bool> {
/* Comparison function operator. */
bool operator()(const Plan* p1, const Plan* p2) const {
return *p1 < *p2;
}
};
}
/*
* A plan queue.
*/
struct PlanQueue : public std::priority_queue<const Plan*> {
};
/* Id of goal step. */
const size_t Plan::GOAL_ID = std::numeric_limits<size_t>::max();
/* Adds goal to chain of open conditions, and returns true if and only
if the goal is consistent. */
static bool add_goal(const Chain<OpenCondition>*& open_conds,
size_t& num_open_conds, BindingList& new_bindings,
const Formula& goal, size_t step_id,
bool test_only = false) {
if (goal.tautology()) {
return true;
} else if (goal.contradiction()) {
return false;
}
std::vector<const Formula*> goals(1, &goal);
while (!goals.empty()) {
const Formula* goal = goals.back();
goals.pop_back();
const Literal* l;
FormulaTime when;
const TimedLiteral* tl = dynamic_cast<const TimedLiteral*>(goal);
if (tl != NULL) {
l = &tl->literal();
when = tl->when();
} else {
l = dynamic_cast<const Literal*>(goal);
when = AT_START;
}
if (l != NULL) {
if (!test_only
&& !(params->strip_static_preconditions()
&& PredicateTable::static_predicate(l->predicate()))) {
open_conds =
new Chain<OpenCondition>(OpenCondition(step_id, *l, when),
open_conds);
}
num_open_conds++;
} else {
const Conjunction* conj = dynamic_cast<const Conjunction*>(goal);
if (conj != NULL) {
const FormulaList& gs = conj->conjuncts();
for (FormulaList::const_iterator fi = gs.begin();
fi != gs.end(); fi++) {
if (params->random_open_conditions) {
size_t pos = size_t((goals.size() + 1.0)*rand()/(RAND_MAX + 1.0));
if (pos == goals.size()) {
goals.push_back(*fi);
} else {
const Formula* tmp = goals[pos];
goals[pos] = *fi;
goals.push_back(tmp);
}
} else {
goals.push_back(*fi);
}
}
} else {
const Disjunction* disj = dynamic_cast<const Disjunction*>(goal);
if (disj != NULL) {
if (!test_only) {
open_conds =
new Chain<OpenCondition>(OpenCondition(step_id, *disj),
open_conds);
}
num_open_conds++;
} else {
const BindingLiteral* bl = dynamic_cast<const BindingLiteral*>(goal);
if (bl != NULL) {
bool is_eq = (typeid(*bl) == typeid(Equality));
new_bindings.push_back(Binding(bl->variable(),
bl->step_id1(step_id),
bl->term(),
bl->step_id2(step_id), is_eq));
#ifdef BRANCH_ON_INEQUALITY
const Inequality* neq = dynamic_cast<const Inequality*>(bl);
if (params->domain_constraints
&& neq != NULL && bl.term().variable()) {
/* Both terms are variables, so handle specially. */
if (!test_only) {
open_conds =
new Chain<OpenCondition>(OpenCondition(step_id, *neq),
open_conds);
}
num_open_conds++;
new_bindings.pop_back();
}
#endif
} else {
const Exists* exists = dynamic_cast<const Exists*>(goal);
if (exists != NULL) {
if (params->random_open_conditions) {
size_t pos =
size_t((goals.size() + 1.0)*rand()/(RAND_MAX + 1.0));
if (pos == goals.size()) {
goals.push_back(&exists->body());
} else {
const Formula* tmp = goals[pos];
goals[pos] = &exists->body();
goals.push_back(tmp);
}
} else {
goals.push_back(&exists->body());
}
} else {
const Forall* forall = dynamic_cast<const Forall*>(goal);
if (forall != NULL) {
const Formula& g = forall->universal_base(
std::map<Variable, Term>(), *problem);
if (params->random_open_conditions) {
size_t pos =
size_t((goals.size() + 1.0)*rand()/(RAND_MAX + 1.0));
if (pos == goals.size()) {
goals.push_back(&g);
} else {
const Formula* tmp = goals[pos];
goals[pos] = &g;
goals.push_back(tmp);
}
} else {
goals.push_back(&g);
}
} else {
throw std::logic_error("unknown kind of goal");
}
}
}
}
}
}
}
return true;
}
/* Returns a set of achievers for the given literal. */
static const ActionEffectMap* literal_achievers(const Literal& literal) {
if (params->ground_actions) {
return planning_graph->literal_achievers(literal);
} else if (typeid(literal) == typeid(Atom)) {
PredicateAchieverMap::const_iterator pai =
achieves_pred.find(literal.predicate());
return (pai != achieves_pred.end()) ? &(*pai).second : NULL;
} else {
PredicateAchieverMap::const_iterator pai =
achieves_neg_pred.find(literal.predicate());
return (pai != achieves_neg_pred.end()) ? &(*pai).second : NULL;
}
}
/* Finds threats to the given link. */
static void link_threats(const Chain<Unsafe>*& unsafes, size_t& num_unsafes,
const Link& link, const Chain<Step>* steps,
const Orderings& orderings,
const Bindings& bindings) {
StepTime lt1 = link.effect_time();
StepTime lt2 = end_time(link.condition_time());
for (const Chain<Step>* sc = steps; sc != NULL; sc = sc->tail) {
const Step& s = sc->head;
if (orderings.possibly_not_after(link.from_id(), lt1,
s.id(), StepTime::AT_END)
&& orderings.possibly_not_before(link.to_id(), lt2,
s.id(), StepTime::AT_START)) {
const EffectList& effects = s.action().effects();
for (EffectList::const_iterator ei = effects.begin();
ei != effects.end(); ei++) {
const Effect& e = **ei;
if (!problem->durative() && e.link_condition().contradiction()) {
continue;
}
StepTime et = end_time(e);
if (!(s.id() == link.to_id() && et >= lt2)
&& orderings.possibly_not_after(link.from_id(), lt1, s.id(), et)
&& orderings.possibly_not_before(link.to_id(), lt2, s.id(), et)) {
if (typeid(link.condition()) == typeid(Negation)
|| !(link.from_id() == s.id() && lt1 == et)) {
if (bindings.affects(e.literal(), s.id(),
link.condition(), link.to_id())) {
unsafes = new Chain<Unsafe>(Unsafe(link, s.id(), e), unsafes);
num_unsafes++;
}
}
}
}
}
}
}
/* Finds the threatened links by the given step. */
static void step_threats(const Chain<Unsafe>*& unsafes, size_t& num_unsafes,
const Step& step, const Chain<Link>* links,
const Orderings& orderings,
const Bindings& bindings) {
const EffectList& effects = step.action().effects();
for (const Chain<Link>* lc = links; lc != NULL; lc = lc->tail) {
const Link& l = lc->head;
StepTime lt1 = l.effect_time();
StepTime lt2 = end_time(l.condition_time());
if (orderings.possibly_not_after(l.from_id(), lt1,
step.id(), StepTime::AT_END)
&& orderings.possibly_not_before(l.to_id(), lt2,
step.id(), StepTime::AT_START)) {
for (EffectList::const_iterator ei = effects.begin();
ei != effects.end(); ei++) {
const Effect& e = **ei;
if (!problem->durative() && e.link_condition().contradiction()) {
continue;
}
StepTime et = end_time(e);
if (!(step.id() == l.to_id() && et >= lt2)
&& orderings.possibly_not_after(l.from_id(), lt1, step.id(), et)
&& orderings.possibly_not_before(l.to_id(), lt2, step.id(), et)) {
if (typeid(l.condition()) == typeid(Negation)
|| !(l.from_id() == step.id() && lt1 == et)) {
if (bindings.affects(e.literal(), step.id(),
l.condition(), l.to_id())) {
unsafes = new Chain<Unsafe>(Unsafe(l, step.id(), e), unsafes);
num_unsafes++;
}
}
}
}
}
}
}
/* Finds the mutex threats by the given step. */
static void mutex_threats(const Chain<MutexThreat>*& mutex_threats,
const Step& step, const Chain<Step>* steps,
const Orderings& orderings,
const Bindings& bindings) {
const EffectList& effects = step.action().effects();
for (const Chain<Step>* sc = steps; sc != NULL; sc = sc->tail) {
const Step& s = sc->head;
bool ss, se, es, ee;
if (orderings.possibly_concurrent(step.id(), s.id(), ss, se, es, ee)) {
const EffectList& effects2 = s.action().effects();
for (EffectList::const_iterator ei = effects.begin();
ei != effects.end(); ei++) {
const Effect& e = **ei;
if (e.when() == Effect::AT_START) {
if (!ss && !se) {
continue;
}
} else if (!es && !ee) {
continue;
}
for (EffectList::const_iterator ej = effects2.begin();
ej != effects2.end(); ej++) {
const Effect& e2 = **ej;
if (e.when() == Effect::AT_START) {
if (e2.when() == Effect::AT_START) {
if (!ss) {
continue;
}
} else if (!se) {
continue;
}
} else {
if (e2.when() == Effect::AT_START) {
if (!es) {
continue;
}
} else if (!ee) {
continue;
}
}
if (bindings.unify(e.literal().atom(), step.id(),
e2.literal().atom(), s.id())) {
mutex_threats = new Chain<MutexThreat>(MutexThreat(step.id(), e,
s.id(), e2),
mutex_threats);
}
}
}
}
}
}
/* Returns binding constraints that make the given steps fully
instantiated, or NULL if no consistent binding constraints can be
found. */
static const Bindings* step_instantiation(const Chain<Step>* steps, size_t n,
const Bindings& bindings) {
if (steps == NULL) {
return &bindings;
} else {
const Step& step = steps->head;
const ActionSchema* as = dynamic_cast<const ActionSchema*>(&step.action());
if (as == NULL || as->parameters().size() <= n) {
return step_instantiation(steps->tail, 0, bindings);
} else {
const Variable& v = as->parameters()[n];
if (v != bindings.binding(v, step.id())) {
return step_instantiation(steps, n + 1, bindings);
} else {
const Type& t = TermTable::type(v);
const std::vector<Object>& arguments =
problem->terms().compatible_objects(t);
for (std::vector<Object>::const_iterator oi = arguments.begin();
oi != arguments.end(); oi++) {
BindingList bl;
bl.push_back(Binding(v, step.id(), *oi, 0, true));
const Bindings* new_bindings = bindings.add(bl);
if (new_bindings != NULL) {
const Bindings* result = step_instantiation(steps, n + 1,
*new_bindings);
if (result != new_bindings) {
delete new_bindings;
}
if (result != NULL) {
return result;
}
}
}
return NULL;
}
}
}
}
/* Returns the initial plan representing the given problem, or NULL
if initial conditions or goals of the problem are inconsistent. */
const Plan* Plan::make_initial_plan(const Problem& problem) {
/*
* Create goal of problem.
*/
if (params->ground_actions) {
goal_action = new GroundAction("", false);
const Formula& goal_formula =
problem.goal().instantiation(std::map<Variable, Term>(), problem);
goal_action->set_condition(goal_formula);
} else {
goal_action = new ActionSchema("", false);
goal_action->set_condition(problem.goal());
}
/* Chain of open conditions. */
const Chain<OpenCondition>* open_conds = NULL;
/* Number of open conditions. */
size_t num_open_conds = 0;
/* Bindings introduced by goal. */
BindingList new_bindings;
/* Add goals as open conditions. */
if (!add_goal(open_conds, num_open_conds, new_bindings,
goal_action->condition(), GOAL_ID)) {
/* Goals are inconsistent. */
RCObject::ref(open_conds);
RCObject::destructive_deref(open_conds);
return NULL;
}
/* Make chain of mutex threat place holder. */
const Chain<MutexThreat>* mutex_threats =
new Chain<MutexThreat>(MutexThreat(), NULL);
/* Make chain of initial steps. */
const Chain<Step>* steps =
new Chain<Step>(Step(0, problem.init_action()),
new Chain<Step>(Step(GOAL_ID, *goal_action), NULL));
size_t num_steps = 0;
/* Variable bindings. */
const Bindings* bindings = &Bindings::EMPTY;
/* Step orderings. */
const Orderings* orderings;
if (problem.durative()) {
const TemporalOrderings* to = new TemporalOrderings();
/*
* Add steps for timed initial literals.
*/
for (TimedActionTable::const_iterator ai = problem.timed_actions().begin();
ai != problem.timed_actions().end(); ai++) {
num_steps++;
steps = new Chain<Step>(Step(num_steps, *(*ai).second), steps);
const TemporalOrderings* tmp = to->refine((*ai).first, steps->head);
delete to;
if (tmp == NULL) {
RCObject::ref(open_conds);
RCObject::destructive_deref(open_conds);
RCObject::ref(steps);
RCObject::destructive_deref(steps);
return NULL;
}
to = tmp;
}
orderings = to;
} else {
orderings = new BinaryOrderings();
}
/* Return initial plan. */
return new Plan(steps, num_steps, NULL, 0, *orderings, *bindings,
NULL, 0, open_conds, num_open_conds, mutex_threats, NULL);
}
/* Returns plan for given problem. */
const Plan* Plan::plan(const Problem& problem, const Parameters& p,
bool last_problem) {
Timer<> timer;
/* Set planning parameters. */
params = &p;
/* Set current domain. */
domain = &problem.domain();
::problem = &problem;
/*
* Initialize planning graph and maps from predicates to actions.
*/
bool need_pg = (params->ground_actions || params->domain_constraints
|| params->heuristic.needs_planning_graph());
for (size_t i = 0; !need_pg && i < params->flaw_orders.size(); i++) {
if (params->flaw_orders[i].needs_planning_graph()) {
need_pg = true;
}
}
if (need_pg) {
planning_graph = new PlanningGraph(problem, *params);
} else {
planning_graph = NULL;
}
if (!params->ground_actions) {
achieves_pred.clear();
achieves_neg_pred.clear();
for (std::map<std::string, const ActionSchema*>::const_iterator ai =
domain->actions().begin();
ai != domain->actions().end(); ai++) {
const ActionSchema* as = (*ai).second;
for (EffectList::const_iterator ei = as->effects().begin();
ei != as->effects().end(); ei++) {
const Literal& literal = (*ei)->literal();
if (typeid(literal) == typeid(Atom)) {
achieves_pred[literal.predicate()].insert(std::make_pair(as, *ei));
} else {
achieves_neg_pred[literal.predicate()].insert(std::make_pair(as,
*ei));
}
}
}
const GroundAction& ia = problem.init_action();
for (EffectList::const_iterator ei = ia.effects().begin();
ei != ia.effects().end(); ei++) {
const Literal& literal = (*ei)->literal();
achieves_pred[literal.predicate()].insert(std::make_pair(&ia, *ei));
}
for (TimedActionTable::const_iterator ai = problem.timed_actions().begin();
ai != problem.timed_actions().end(); ai++) {
const GroundAction& action = *(*ai).second;
for (EffectList::const_iterator ei = action.effects().begin();
ei != action.effects().end(); ei++) {
const Literal& literal = (*ei)->literal();
if (typeid(literal) == typeid(Atom)) {
achieves_pred[literal.predicate()].insert(std::make_pair(&action,
*ei));
} else {
achieves_neg_pred[literal.predicate()].insert(std::make_pair(&action,
*ei));
}
}
}
}
static_pred_flaw = false;
/* Number of visited plan. */
size_t num_visited_plans = 0;
/* Number of generated plans. */
size_t num_generated_plans = 0;
/* Number of static preconditions encountered. */
size_t num_static = 0;
/* Number of dead ends encountered. */
size_t num_dead_ends = 0;
/* Generated plans for different flaw selection orders. */
std::vector<size_t> generated_plans(params->flaw_orders.size(), 0);
/* Queues of pending plans. */
std::vector<PlanQueue> plans(params->flaw_orders.size(), PlanQueue());
/* Dead plan queues. */
std::vector<PlanQueue*> dead_queues;
/* Construct the initial plan. */
const Plan* initial_plan = make_initial_plan(problem);
if (initial_plan != NULL) {
initial_plan->id_ = 0;
}
/* Variable for progress bar (number of generated plans). */
size_t last_dot = 0;
/* Variable for progress bar (time). */
std::chrono::minutes next_hash(1);
/*
* Search for complete plan.
*/
size_t current_flaw_order = 0;
size_t flaw_orders_left = params->flaw_orders.size();
size_t next_switch = 1000;
const Plan* current_plan = initial_plan;
generated_plans[current_flaw_order]++;
num_generated_plans++;
if (verbosity > 1) {
std::cerr << "using flaw order " << current_flaw_order << std::endl;
}
float f_limit;
if (current_plan != NULL
&& params->search_algorithm == Parameters::IDA_STAR) {
f_limit = current_plan->primary_rank();
} else {
f_limit = std::numeric_limits<float>::infinity();
}
do {
float next_f_limit = std::numeric_limits<float>::infinity();
while (current_plan != NULL && !current_plan->complete()) {
/* Do a little amortized cleanup of dead queues. */
for (size_t dq = 0; dq < 4 && !dead_queues.empty(); dq++) {
PlanQueue& dead_queue = *dead_queues.back();
delete dead_queue.top();
dead_queue.pop();
if (dead_queue.empty()) {
dead_queues.pop_back();
}
}
const auto elapsed_time = timer.ElapsedTime();
if (elapsed_time >= params->time_limit) {
/* Time limit exceeded. */
break;
}
/*
* Visiting a new plan.
*/
num_visited_plans++;
if (verbosity == 1) {
while (num_generated_plans - num_static - last_dot >= 1000) {
std::cerr << '.';
last_dot += 1000;
}
while (elapsed_time >= next_hash) {
std::cerr << '#';
++next_hash;
}
}
if (verbosity > 1) {
std::cerr << std::endl << (num_visited_plans - num_static) << ": "
<< "!!!!CURRENT PLAN (id " << current_plan->id_ << ")"
<< " with rank (" << current_plan->primary_rank();
for (size_t ri = 1; ri < current_plan->rank_.size(); ri++) {
std::cerr << ',' << current_plan->rank_[ri];
}
std::cerr << ")" << std::endl << *current_plan << std::endl;
}
/* List of children to current plan. */
PlanList refinements;
/* Get plan refinements. */
current_plan->refinements(refinements,
params->flaw_orders[current_flaw_order]);
/* Add children to queue of pending plans. */
bool added = false;
for (PlanList::const_iterator pi = refinements.begin();
pi != refinements.end(); pi++) {
const Plan& new_plan = **pi;
/* N.B. Must set id before computing rank, because it may be used. */
new_plan.id_ = num_generated_plans;
if (new_plan.primary_rank() != std::numeric_limits<float>::infinity()
&& (generated_plans[current_flaw_order]
< params->search_limits[current_flaw_order])) {
if (params->search_algorithm == Parameters::IDA_STAR
&& new_plan.primary_rank() > f_limit) {
next_f_limit = std::min(next_f_limit, new_plan.primary_rank());
delete &new_plan;
continue;
}
if (!added && static_pred_flaw) {
num_static++;
}
added = true;
plans[current_flaw_order].push(&new_plan);
generated_plans[current_flaw_order]++;
num_generated_plans++;
if (verbosity > 2) {
std::cerr << std::endl << "####CHILD (id " << new_plan.id_ << ")"
<< " with rank (" << new_plan.primary_rank();
for (size_t ri = 1; ri < new_plan.rank_.size(); ri++) {
std::cerr << ',' << new_plan.rank_[ri];
}
std::cerr << "):" << std::endl << new_plan << std::endl;
}
} else {
delete &new_plan;
}
}
if (!added) {
num_dead_ends++;
}
/*
* Process next plan.
*/
bool limit_reached = false;
if ((limit_reached = (generated_plans[current_flaw_order]
>= params->search_limits[current_flaw_order]))
|| generated_plans[current_flaw_order] >= next_switch) {
if (verbosity > 1) {
std::cerr << "time to switch ("
<< generated_plans[current_flaw_order] << ")" << std::endl;
}
if (limit_reached) {
flaw_orders_left--;
/* Discard the rest of the plan queue. */
dead_queues.push_back(&plans[current_flaw_order]);
}
if (flaw_orders_left > 0) {
do {
current_flaw_order++;
if (verbosity > 1) {
std::cerr << "use flaw order "
<< current_flaw_order << "?" << std::endl;
}
if (current_flaw_order >= params->flaw_orders.size()) {
current_flaw_order = 0;
next_switch *= 2;
}
} while ((generated_plans[current_flaw_order]
>= params->search_limits[current_flaw_order]));
if (verbosity > 1) {
std::cerr << "using flaw order " << current_flaw_order
<< std::endl;
}
}
}
if (flaw_orders_left > 0) {
if (generated_plans[current_flaw_order] == 0) {
current_plan = initial_plan;
generated_plans[current_flaw_order]++;
num_generated_plans++;
} else {
if (current_plan != initial_plan) {
delete current_plan;
}
if (plans[current_flaw_order].empty()) {
/* Problem lacks solution. */
current_plan = NULL;
} else {
current_plan = plans[current_flaw_order].top();
plans[current_flaw_order].pop();
}
}
/*
* Instantiate all actions if the plan is otherwise complete.
*/
bool instantiated = params->ground_actions;
while (current_plan != NULL && current_plan->complete()
&& !instantiated) {
const Bindings* new_bindings =
step_instantiation(current_plan->steps(), 0,
*current_plan->bindings_);
if (new_bindings != NULL) {
instantiated = true;
if (new_bindings != current_plan->bindings_) {
const Plan* inst_plan =
new Plan(current_plan->steps(), current_plan->num_steps(),
current_plan->links(), current_plan->num_links(),
current_plan->orderings(), *new_bindings,
NULL, 0, NULL, 0, NULL, current_plan);
delete current_plan;
current_plan = inst_plan;
}
} else if (plans[current_flaw_order].empty()) {
/* Problem lacks solution. */
current_plan = NULL;
} else {
current_plan = plans[current_flaw_order].top();
plans[current_flaw_order].pop();
}
}
} else {
if (next_f_limit != std::numeric_limits<float>::infinity()) {
current_plan = NULL;
}
break;
}
}
if (current_plan != NULL && current_plan->complete()) {
break;
}
f_limit = next_f_limit;
if (f_limit != std::numeric_limits<float>::infinity()) {
/* Restart search. */
if (current_plan != NULL && current_plan != initial_plan) {
delete current_plan;
}
current_plan = initial_plan;
}
} while (f_limit != std::numeric_limits<float>::infinity());
if (verbosity > 0) {
/*
* Print statistics.
*/
std::cerr << std::endl << "Plans generated: " << num_generated_plans;
if (num_static > 0) {
std::cerr << " [" << (num_generated_plans - num_static) << "]";
}
std::cerr << std::endl << "Plans visited: " << num_visited_plans;
if (num_static > 0) {
std::cerr << " [" << (num_visited_plans - num_static) << "]";
}
std::cerr << std::endl << "Dead ends encountered: " << num_dead_ends
<< std::endl;
}
/*
* Discard the rest of the plan queue and some other things, unless
* this is the last problem in which case we can save time by just
* letting the operating system reclaim the memory for us.
*/
if (!last_problem) {
if (current_plan != initial_plan) {
delete initial_plan;
}
for (size_t i = 0; i < plans.size(); i++) {
while (!plans[i].empty()) {
delete plans[i].top();
plans[i].pop();
}
}
}
/* Return last plan, or NULL if problem does not have a solution. */
return current_plan;
}
/* Cleans up after planning. */
void Plan::cleanup() {
if (planning_graph != NULL) {
delete planning_graph;
planning_graph = NULL;
}
if (goal_action != NULL) {
delete goal_action;
goal_action = NULL;
}
}
/* Constructs a plan. */
Plan::Plan(const Chain<Step>* steps, size_t num_steps,
const Chain<Link>* links, size_t num_links,
const Orderings& orderings, const Bindings& bindings,
const Chain<Unsafe>* unsafes, size_t num_unsafes,
const Chain<OpenCondition>* open_conds, size_t num_open_conds,
const Chain<MutexThreat>* mutex_threats, const Plan* parent)
: steps_(steps), num_steps_(num_steps),
links_(links), num_links_(num_links),
orderings_(&orderings), bindings_(&bindings),
unsafes_(unsafes), num_unsafes_(num_unsafes),
open_conds_(open_conds), num_open_conds_(num_open_conds),
mutex_threats_(mutex_threats) {
RCObject::ref(steps);
RCObject::ref(links);
Orderings::register_use(&orderings);
Bindings::register_use(&bindings);
RCObject::ref(unsafes);
RCObject::ref(open_conds);
RCObject::ref(mutex_threats);
#ifdef DEBUG
depth_ = (parent != NULL) ? parent->depth() + 1 : 0;
#endif
}
/* Deletes this plan. */
Plan::~Plan() {
RCObject::destructive_deref(steps_);
RCObject::destructive_deref(links_);
Orderings::unregister_use(orderings_);
Bindings::unregister_use(bindings_);
RCObject::destructive_deref(unsafes_);
RCObject::destructive_deref(open_conds_);
RCObject::destructive_deref(mutex_threats_);
}
/* Returns the bindings of this plan. */
const Bindings* Plan::bindings() const {
return params->ground_actions ? NULL : bindings_;
}
/* Checks if this plan is complete. */
bool Plan::complete() const {
return unsafes() == NULL && open_conds() == NULL && mutex_threats() == NULL;
}
/* Returns the primary rank of this plan, where a lower rank
signifies a better plan. */
float Plan::primary_rank() const {
if (rank_.empty()) {
params->heuristic.plan_rank(rank_, *this, params->weight, *domain,
planning_graph);
}
return rank_[0];
}
/* Returns the serial number of this plan. */
size_t Plan::serial_no() const {
return id_;
}
/* Returns the next flaw to work on. */
const Flaw& Plan::get_flaw(const FlawSelectionOrder& flaw_order) const {
const Flaw& flaw = flaw_order.select(*this, *problem, planning_graph);
if (!params->ground_actions) {
const OpenCondition* open_cond = dynamic_cast<const OpenCondition*>(&flaw);
static_pred_flaw = (open_cond != NULL && open_cond->is_static());
}
return flaw;
}
/* Returns the refinements for the next flaw to work on. */
void Plan::refinements(PlanList& plans,
const FlawSelectionOrder& flaw_order) const {
const Flaw& flaw = get_flaw(flaw_order);
if (verbosity > 1) {
std::cerr << std::endl << "handle ";
flaw.print(std::cerr, *bindings_);
std::cerr << std::endl;
}
const Unsafe* unsafe = dynamic_cast<const Unsafe*>(&flaw);
if (unsafe != NULL) {
handle_unsafe(plans, *unsafe);
} else {
const OpenCondition* open_cond = dynamic_cast<const OpenCondition*>(&flaw);
if (open_cond != NULL) {
handle_open_condition(plans, *open_cond);
} else {
const MutexThreat* mutex_threat =
dynamic_cast<const MutexThreat*>(&flaw);
if (mutex_threat != NULL) {
handle_mutex_threat(plans, *mutex_threat);
} else {
throw std::logic_error("unknown kind of flaw");
}
}
}
}
/* Counts the number of refinements for the given threat, and returns
true iff the number of refinements does not exceed the given
limit. */
bool Plan::unsafe_refinements(int& refinements, int& separable,
int& promotable, int& demotable,
const Unsafe& unsafe, int limit) const {
if (refinements >= 0) {
return refinements <= limit;
} else {
int ref = 0;
BindingList unifier;
const Link& link = unsafe.link();
StepTime lt1 = link.effect_time();
StepTime lt2 = end_time(link.condition_time());
StepTime et = end_time(unsafe.effect());
if (orderings().possibly_not_after(link.from_id(), lt1,
unsafe.step_id(), et)
&& orderings().possibly_not_before(link.to_id(), lt2,
unsafe.step_id(), et)
&& bindings_->affects(unifier, unsafe.effect().literal(),
unsafe.step_id(),
link.condition(), link.to_id())) {
PlanList dummy;
if (separable < 0) {
separable = separate(dummy, unsafe, unifier, true);
}
ref += separable;
if (ref <= limit) {
if (promotable < 0) {
promotable = promote(dummy, unsafe, true);
}
ref += promotable;
if (ref <= limit) {
if (demotable < 0) {