-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfc_planner.cpp
1932 lines (1659 loc) · 68 KB
/
fc_planner.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
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
#include "fc_planner.h"
#include <queue>
#include <cmath>
#include <time.h>
#include "formula.h"
#include "predicate_manager.h"
#include "action_manager.h"
#include "type_manager.h"
#include "term_manager.h"
#include "parser_utils.h"
#include "heuristics/dtg_reachability.h"
#include "heuristics/equivalent_object_group.h"
//#define MYPOP_FORWARD_CHAIN_PLANNER_COMMENTS
#include "type_manager.h"
#include "heuristics/fact_set.h"
#include "coloured_graph.h"
//#define FC_PLANNER_SAFE_MEMORY
namespace MyPOP {
StateHeuristicListener::StateHeuristicListener(std::vector<State*>& found_states, const State& current_state, HEURISTICS::HeuristicInterface& heuristic, const std::vector<const GroundedAtom*>& initial_facts, const std::vector<const GroundedAtom*>& goal_facts, const TermManager& term_manager, bool find_helpful_actions, bool allow_new_goals_to_be_added)
: found_states_(&found_states), current_state_(¤t_state), heuristic_(&heuristic), initial_facts_(&initial_facts), goal_facts_(&goal_facts), term_manager_(&term_manager), find_helpful_actions_(find_helpful_actions), allow_new_goals_to_be_added_(allow_new_goals_to_be_added), found_better_state_(false)
{
}
StateHeuristicListener::~StateHeuristicListener()
{
}
void StateHeuristicListener::addNewState(State& state)
{
//heuristic_->setHeuristicForState(state, *goal_facts_, *term_manager_, find_helpful_actions_, allow_new_goals_to_be_added_);
heuristic_->setHeuristicForState(state, *initial_facts_, *goal_facts_, *term_manager_, false, allow_new_goals_to_be_added_);
/* if (find_helpful_actions_ && state.getHeuristic() < current_state_->getHeuristic())
{
found_better_state_ = true;
for (std::vector<State*>::const_iterator ci = found_states_->begin(); ci != found_states_->end(); ++ci)
{
delete *ci;
}
found_states_->clear();
}*/
found_states_->push_back(&state);
}
bool StateHeuristicListener::continueSearching()
{
return !found_better_state_;
}
StateStoreListener::StateStoreListener(std::vector<State*>& found_states)
: found_states_(&found_states)
{
}
StateStoreListener::~StateStoreListener()
{
}
void StateStoreListener::addNewState(State& state)
{
found_states_->push_back(&state);
}
bool StateStoreListener::continueSearching()
{
return true;
}
std::vector<const GroundedAction*> GroundedAction::instantiated_grounded_actions_;
std::vector<const GroundedAtom*> GroundedAtom::instantiated_grounded_atoms_;
const GroundedAction& GroundedAction::getGroundedAction(const Action& action, const Object** variables)
{
for (std::vector<const GroundedAction*>::const_iterator ci = instantiated_grounded_actions_.begin(); ci != instantiated_grounded_actions_.end(); ci++)
{
const GroundedAction* instantiated_action = *ci;
if (instantiated_action->action_ != &action) continue;
bool variables_match = true;
for (unsigned int i = 0; i < action.getVariables().size(); i++)
{
if (variables[i] != instantiated_action->variables_[i])
{
variables_match = false;
break;
}
}
if (variables_match) return *instantiated_action;
}
const Object** copied_variables = new const Object*[action.getVariables().size()];
for (unsigned int i = 0; i < action.getVariables().size(); ++i)
{
copied_variables[i] = variables[i];
}
GroundedAction* new_grounded_action = new GroundedAction(action, copied_variables);
instantiated_grounded_actions_.push_back(new_grounded_action);
return *new_grounded_action;
}
void GroundedAction::removeInstantiatedGroundedActions(std::vector<const GroundedAction*>::const_iterator begin, std::vector<const GroundedAction*>::const_iterator end)
{
for (std::vector<const GroundedAction*>::reverse_iterator ri = instantiated_grounded_actions_.rbegin(); ri != instantiated_grounded_actions_.rend(); ++ri)
{
const GroundedAction* grounded_action = *ri;
if (std::find(begin, end, grounded_action) == end)
{
delete *ri;
instantiated_grounded_actions_.erase(ri.base() - 1);
}
}
}
void GroundedAction::removeInstantiatedGroundedActions(const State& state)
{
std::vector<const GroundedAction*> not_remove_list;
const State* parent = &state;
while (parent != NULL)
{
not_remove_list.push_back(parent->getAchievingAction());
parent = parent->getParent();
}
removeInstantiatedGroundedActions(not_remove_list.begin(), not_remove_list.end());
}
void GroundedAction::removeInstantiatedGroundedActions()
{
for (std::vector<const GroundedAction*>::const_iterator ci = instantiated_grounded_actions_.begin(); ci != instantiated_grounded_actions_.end(); ++ci)
{
delete *ci;
}
instantiated_grounded_actions_.clear();
}
unsigned int GroundedAction::numberOfGroundedActions()
{
return instantiated_grounded_actions_.size();
}
GroundedAction::GroundedAction(const Action& action, const Object** variables)
: action_(&action), variables_(variables)
{
}
GroundedAction::~GroundedAction()
{
delete[] variables_;
}
void GroundedAction::applyTo(std::vector<const GroundedAtom*>& facts) const
{
/*
std::cout << "Apply: " << *this << std::endl;
for (std::vector<const GroundedAtom*>::const_iterator ci = facts.begin(); ci != facts.end(); ++ci)
{
std::cout << "* " << **ci << std::endl;
}
*/
std::vector<const GroundedAtom*> to_add;
for (unsigned int effect_index = 0; effect_index < action_->getEffects().size(); ++effect_index)
{
const Atom* effect = action_->getEffects()[effect_index];
const Object** effect_variables = new const Object*[effect->getArity()];
for (unsigned int term_index = 0; term_index < effect->getArity(); term_index++)
{
unsigned int variable_index = action_->getActionVariable(effect_index, term_index);
effect_variables[term_index] = variables_[variable_index];
}
const GroundedAtom& grounded_effect = GroundedAtom::getGroundedAtom(effect->getPredicate(), effect_variables);
if (effect->isNegative())
{
// std::cout << "Remove: " << grounded_effect << std::endl;
// Normalise we assume that effect can only delete facts which are mentioned in the preconditions. But some domains
// like Satellite do not conform to this assumption.
std::vector<const GroundedAtom*>::iterator ci = std::find(facts.begin(), facts.end(), &grounded_effect);
if (ci != facts.end())
{
facts.erase(ci);
}
}
else
{
// std::cout << "Add: " << grounded_effect << std::endl;
to_add.push_back(&grounded_effect);
}
}
facts.insert(facts.end(), to_add.begin(), to_add.end());
}
std::ostream& operator<<(std::ostream& os, const GroundedAction& grounded_action)
{
os << "(" << grounded_action.action_->getPredicate();
for (unsigned int i = 0; i < grounded_action.action_->getVariables().size(); i++)
{
os << " " << *grounded_action.variables_[i];
}
os << ")";
return os;
}
void GroundedAtom::removeInstantiatedGroundedAtom()
{
for (std::vector<const GroundedAtom*>::const_iterator ci = instantiated_grounded_atoms_.begin(); ci != instantiated_grounded_atoms_.end(); ++ci)
{
const GroundedAtom* grounded_atom = *ci;
delete grounded_atom;
}
instantiated_grounded_atoms_.clear();
}
void GroundedAtom::removeInstantiatedGroundedAtom(const std::vector<const GroundedAtom*>& exceptions)
{
for (std::vector<const GroundedAtom*>::reverse_iterator ri = instantiated_grounded_atoms_.rbegin(); ri != instantiated_grounded_atoms_.rend(); ++ri)
{
const GroundedAtom* grounded_atom = *ri;
if (std::find(exceptions.begin(), exceptions.end(), grounded_atom) == exceptions.end())
{
delete grounded_atom;
instantiated_grounded_atoms_.erase(ri.base() - 1);
}
}
}
const GroundedAtom& GroundedAtom::getGroundedAtom(const Predicate& predicate, const Object** variables)
{
for (std::vector<const GroundedAtom*>::const_iterator ci = instantiated_grounded_atoms_.begin(); ci != instantiated_grounded_atoms_.end(); ++ci)
{
const GroundedAtom* grounded_atom = *ci;
if (grounded_atom->getPredicate().getArity() != predicate.getArity() ||
grounded_atom->getPredicate().getName() != predicate.getName())
{
continue;
}
bool variable_domain_match = true;
for (unsigned int i = 0; i < grounded_atom->getPredicate().getArity(); ++i)
{
if (&grounded_atom->getObject(i) != variables[i])
{
variable_domain_match = false;
break;
}
}
if (variable_domain_match)
{
delete[] variables;
return *grounded_atom;
}
}
GroundedAtom* new_grounded_atom = new GroundedAtom(predicate, variables);
instantiated_grounded_atoms_.push_back(new_grounded_atom);
return *new_grounded_atom;
}
void GroundedAtom::generateGroundedAtoms(std::vector<const GroundedAtom*>& grounded_objects, const PredicateManager& predicate_manager, const TermManager& term_manager)
{
for (std::vector<Predicate*>::const_iterator ci = predicate_manager.getManagableObjects().begin(); ci != predicate_manager.getManagableObjects().end(); ++ci)
{
const Predicate* predicate = *ci;
unsigned int counter[predicate->getArity()];
memset(&counter[0], 0, sizeof(unsigned int) * predicate->getArity());
unsigned int max_counter[predicate->getArity()];
for (unsigned int i = 0; i < predicate->getArity(); ++i)
{
std::vector<const Object*> objects;
term_manager.getTypeManager().getObjectsOfType(objects, *predicate->getTypes()[i]);
max_counter[i] = objects.size();
}
bool done = false;
while (!done)
{
done = true;
const Object** objects = new const Object*[predicate->getArity()];
for (unsigned int i = 0; i < predicate->getArity(); ++i)
{
std::vector<const Object*> objects_of_type;
term_manager.getTypeManager().getObjectsOfType(objects_of_type, *predicate->getTypes()[i]);
objects[i] = objects_of_type[counter[i]];
}
const GroundedAtom& grounded_atom = getGroundedAtom(*predicate, objects);
//std::cout << grounded_atom << std::endl;
grounded_objects.push_back(&grounded_atom);
// Update the counters.
for (unsigned int i = 0; i < predicate->getArity(); ++i)
{
if (counter[i] + 1 == max_counter[i])
{
counter[i] = 0;
}
else
{
done = false;
counter[i] = counter[i] + 1;
break;
}
}
}
}
}
/*
const GroundedAtom& GroundedAtom::getGroundedAtom(const SAS_Plus::BoundedAtom& bounded_atom, const Bindings& bindings)
{
const Object** variables = new const Object*[bounded_atom.getAtom().getArity()];
for (unsigned int i = 0; i < bounded_atom.getAtom().getArity(); ++i)
{
variables[i] = bounded_atom.getVariableDomain(i, bindings)[0];
}
return getGroundedAtom(bounded_atom.getAtom(), variables);
}
*/
unsigned int GroundedAtom::numberOfGroundedAtoms()
{
return instantiated_grounded_atoms_.size();
}
GroundedAtom::GroundedAtom(const Predicate& predicate, const Object** variables)
: predicate_(&predicate), variables_(variables)
{
// std::cout << "New Grounded atom: " << *this << std::endl;
}
/*
GroundedAtom::GroundedAtom(const SAS_Plus::BoundedAtom& bounded_atom, const Bindings& bindings)
: atom_(&bounded_atom.getAtom())
{
variables_ = new const Object*[bounded_atom.getAtom().getArity()];
for (unsigned int i = 0; i < bounded_atom.getAtom().getArity(); i++)
{
const std::vector<const Object*>& variable_domain = bounded_atom.getVariableDomain(i, bindings);
assert (variable_domain.size() == 1);
variables_[i] = variable_domain[0];
}
// std::cout << "New Grounded atom: " << *this << std::endl;
}
*/
GroundedAtom::~GroundedAtom()
{
delete[] variables_;
}
bool GroundedAtom::operator==(const GroundedAtom& rhs) const
{
if (predicate_->getName() != rhs.predicate_->getName() ||
predicate_->getArity() != rhs.predicate_->getArity()) return false;
for (unsigned int i = 0; i < predicate_->getArity(); i++)
{
if (variables_[i] != rhs.variables_[i]) return false;
}
return true;
}
bool GroundedAtom::operator!=(const GroundedAtom& rhs) const
{
return !(*this == rhs);
}
std::ostream& operator<<(std::ostream& os, const GroundedAtom& grounded_atom)
{
os << "(" << grounded_atom.getPredicate().getName();
for (unsigned int i = 0; i < grounded_atom.getPredicate().getArity(); i++)
{
os << " " << grounded_atom.getObject(i);
}
os << ")";
return os;
}
/*
State::State(const std::vector<const GroundedAtom*>& facts, bool created_by_helpful_action)
: parent_(NULL), achieving_action_(NULL), facts_(facts), distance_to_goal_(0), distance_from_start_(0), created_by_helpful_action_(created_by_helpful_action)
{
assert (facts_.size() == facts.size());
std::sort(facts_.begin(), facts_.end());
//checkSanity();
}
*/
State::State(bool created_by_helpful_action)
: parent_(NULL), achieving_action_(NULL), distance_to_goal_(0), distance_from_start_(0), created_by_helpful_action_(created_by_helpful_action)
{
}
State::State(const State& rhs, const GroundedAction& grounded_action, bool created_by_helpful_action)
: parent_(&rhs), achieving_action_(&grounded_action)/*, facts_(rhs.facts_)*/, distance_to_goal_(rhs.distance_to_goal_), distance_from_start_(rhs.distance_from_start_ + 1), created_by_helpful_action_(created_by_helpful_action)
{
//achievers_.insert(achievers_.end(), rhs.achievers_.begin(), rhs.achievers_.end());
//achievers_.push_back(&grounded_action);
// std::sort(facts_.begin(), facts_.end());
//checkSanity();
}
/*
void State::checkSanity() const
{
for (unsigned int i = 0; i < facts_.size(); ++i)
{
for (unsigned int j = 0; j < facts_.size(); ++j)
{
if (i == j)
{
continue;
}
if (facts_[i] == facts_[j])
{
assert (false);
std::cerr << "£";
}
}
}
}
*/
State::~State()
{
/*
for (std::vector<std::pair<const REACHABILITY::AchievingTransition*, const std::vector<HEURISTICS::VariableDomain*>* > >::const_iterator ci = helpful_actions_.begin(); ci != helpful_actions_.end(); ++ci)
{
const std::vector<HEURISTICS::VariableDomain*>* variable_domains = (*ci).second;
for (std::vector<HEURISTICS::VariableDomain*>::const_iterator ci = variable_domains->begin(); ci != variable_domains->end(); ++ci)
{
delete *ci;
}
delete variable_domains;
}
*/
/*
for (std::vector<const REACHABILITY::AchievingTransition*>::const_iterator ci = helpful_actions_.begin(); ci != helpful_actions_.end(); ++ci)
{
delete *ci;
}
*/
}
//bool State::areSymmetrical(const State& state, REACHABILITY::EquivalentObjectGroupManager& eog_manager, const std::vector<const GroundedAtom*>& goal_facts, const TermManager& term_manager) const
bool State::areSymmetrical(const State& state, const HEURISTICS::HeuristicInterface& heuristic, const std::vector<const GroundedAtom*>& initial_facts, const std::vector<const GroundedAtom*>& goal_facts, const TermManager& term_manager) const
{
std::multimap<const Object*, const Object*> lhs_symmetrical_groups;
std::multimap<const Object*, const Object*> rhs_symmetrical_groups;
// std::cout << "Are symmetrical?" << std::endl;
// std::cout << *this << std::endl;
// std::cout << " *** " << std::endl;
// std::cout << state << std::endl;
heuristic.getFunctionalSymmetricSets(lhs_symmetrical_groups, *this, initial_facts, goal_facts, term_manager);
heuristic.getFunctionalSymmetricSets(rhs_symmetrical_groups, state, initial_facts, goal_facts, term_manager);
// getSymmetricalObjects(lhs_reachable_facts, eog_manager, goal_facts, term_manager, lhs_symmetrical_groups);
// state.getSymmetricalObjects(rhs_reachable_facts, eog_manager, goal_facts, term_manager, rhs_symmetrical_groups);
ColouredGraph lhs_cg(lhs_symmetrical_groups);
ColouredGraph rhs_cg(rhs_symmetrical_groups);
std::vector<const GroundedAtom*> state_facts;
getFacts(initial_facts, state_facts);
//for (std::vector<const GroundedAtom*>::const_iterator ci = getFacts().begin(); ci != getFacts().end(); ++ci)
for (std::vector<const GroundedAtom*>::const_iterator ci = state_facts.begin(); ci != state_facts.end(); ++ci)
{
const GroundedAtom* fact = *ci;
std::vector<const Object*> objects;
for (unsigned int i = 0; i < fact->getPredicate().getArity(); ++i)
{
objects.push_back(&fact->getObject(i));
}
for (unsigned int i = 0; i < fact->getPredicate().getArity(); ++i)
{
ColouredGraphNodePredicates& cgnp = lhs_cg.createPredicateNode(fact->getPredicate(), i, objects);
ColouredGraphNodeObjects* node = lhs_cg.getNode(*objects[i]);
assert (node != NULL);
node->addEdge(cgnp);
}
}
std::vector<const GroundedAtom*> other_state_facts;
state.getFacts(initial_facts, other_state_facts);
//for (std::vector<const GroundedAtom*>::const_iterator ci = state.getFacts().begin(); ci != state.getFacts().end(); ++ci)
for (std::vector<const GroundedAtom*>::const_iterator ci = other_state_facts.begin(); ci != other_state_facts.end(); ++ci)
{
const GroundedAtom* fact = *ci;
std::vector<const Object*> objects;
for (unsigned int i = 0; i < fact->getPredicate().getArity(); ++i)
{
objects.push_back(&fact->getObject(i));
}
for (unsigned int i = 0; i < fact->getPredicate().getArity(); ++i)
{
ColouredGraphNodePredicates& cgnp = rhs_cg.createPredicateNode(fact->getPredicate(), i, objects);
ColouredGraphNodeObjects* node = rhs_cg.getNode(*objects[i]);
assert (node != NULL);
node->addEdge(cgnp);
}
}
for (std::vector<const GroundedAtom*>::const_iterator ci = goal_facts.begin(); ci != goal_facts.end(); ++ci)
{
const GroundedAtom* grounded_atom = *ci;
ColouredGraphNodeGoal& cgng_lhs = lhs_cg.createGoalNode(**ci);
ColouredGraphNodeGoal& cgng_rhs = rhs_cg.createGoalNode(**ci);
for (unsigned int i = 0; i < grounded_atom->getPredicate().getArity(); ++i)
{
ColouredGraphNodeObjects* lhs_node = lhs_cg.getNode(grounded_atom->getObject(i));
assert (lhs_node != NULL);
lhs_node->addEdge(cgng_lhs);
ColouredGraphNodeObjects* rhs_node = rhs_cg.getNode(grounded_atom->getObject(i));
assert (rhs_node != NULL);
rhs_node->addEdge(cgng_rhs);
}
}
if (lhs_cg.isSymmetricalTo(rhs_cg))
{
return true;
}
return false;
}
/*
void State::getSymmetricalObjects(std::vector<REACHABILITY::ReachableFact*>& state_reachable_facts, REACHABILITY::EquivalentObjectGroupManager& eog_manager, const std::vector<const GroundedAtom*>& goal_facts, const TermManager& term_manager, std::multimap<const Object*, const Object*>& symmetrical_groups) const
{
eog_manager.reset();
//std::vector<REACHABILITY::ReachableFact*> reachable_facts;
for (std::vector<const GroundedAtom*>::const_iterator ci = getFacts().begin(); ci != getFacts().end(); ci++)
{
const GroundedAtom* grounded_atom = *ci;
state_reachable_facts.push_back(&REACHABILITY::ReachableFact::createReachableFact(*grounded_atom, eog_manager));
}
#ifdef MYPOP_FORWARD_CHAIN_PLANNER_COMMENTS
std::cout << " *** CALCULATE THE HEURISTIC FOR *** " << std::endl;
for (std::vector<REACHABILITY::ReachableFact*>::const_iterator ci = state_reachable_facts.begin(); ci != state_reachable_facts.end(); ci++)
{
std::cout << **ci << std::endl;
}
#endif
eog_manager.initialise(state_reachable_facts);
eog_manager.updateEquivalences(0);
#ifdef MYPOP_FORWARD_CHAIN_PLANNER_COMMENTS
std::cout << "Found equivalence relationships: " << std::endl;
std::cout << *eog_manager << std::endl;
#endif
// Find which objects are equivalent in the initial state.
std::map<const Object*, std::vector<const Object*>* > initial_symmetrical_groups;
std::vector<std::vector<const Object*>* > delete_list;
for (std::vector<REACHABILITY::EquivalentObjectGroup*>::const_iterator ci = eog_manager.getEquivalentObjectGroups().begin(); ci != eog_manager.getEquivalentObjectGroups().end(); ++ci)
{
REACHABILITY::EquivalentObjectGroup* eog = *ci;
if (!eog->isRootNode())
{
continue;
}
std::vector<const Object*>* equivalent_objects = new std::vector<const Object*>();
delete_list.push_back(equivalent_objects);
for (std::vector<REACHABILITY::EquivalentObject*>::const_iterator ci = eog->begin(0); ci != eog->end(0); ++ci)
{
REACHABILITY::EquivalentObject* eo = *ci;
equivalent_objects->push_back(&eo->getObject());
initial_symmetrical_groups.insert(std::make_pair(&eo->getObject(), equivalent_objects));
}
}
// Do the same for the goal.
std::map<const Object*, std::vector<const Object*>* > goal_symmetrical_groups;
eog_manager.reset();
//reachable_facts.clear();
std::vector<REACHABILITY::ReachableFact*> reachable_facts;
for (std::vector<const GroundedAtom*>::const_iterator ci = goal_facts.begin(); ci != goal_facts.end(); ci++)
{
const GroundedAtom* grounded_atom = *ci;
reachable_facts.push_back(&REACHABILITY::ReachableFact::createReachableFact(*grounded_atom, eog_manager));
}
#ifdef MYPOP_FORWARD_CHAIN_PLANNER_COMMENTS
std::cout << " *** CALCULATE THE HEURISTIC FOR *** " << std::endl;
for (std::vector<REACHABILITY::ReachableFact*>::const_iterator ci = reachable_facts.begin(); ci != reachable_facts.end(); ci++)
{
std::cout << **ci << std::endl;
}
#endif
eog_manager.initialise(reachable_facts);
eog_manager.updateEquivalences(0);
#ifdef MYPOP_FORWARD_CHAIN_PLANNER_COMMENTS
std::cout << "Found equivalence relationships: " << std::endl;
std::cout << eog_manager << std::endl;
#endif
// Find which objects are equivalent in the initial state.
for (std::vector<REACHABILITY::EquivalentObjectGroup*>::const_iterator ci = eog_manager.getEquivalentObjectGroups().begin(); ci != eog_manager.getEquivalentObjectGroups().end(); ++ci)
{
REACHABILITY::EquivalentObjectGroup* eog = *ci;
if (!eog->isRootNode())
{
continue;
}
std::vector<const Object*>* equivalent_objects = new std::vector<const Object*>();
delete_list.push_back(equivalent_objects);
for (std::vector<REACHABILITY::EquivalentObject*>::const_iterator ci = eog->begin(0); ci != eog->end(0); ++ci)
{
REACHABILITY::EquivalentObject* eo = *ci;
equivalent_objects->push_back(&eo->getObject());
goal_symmetrical_groups.insert(std::make_pair(&eo->getObject(), equivalent_objects));
}
}
/// Split the objects such that any set of equivalent objects from the initial state does not overlap with two
/// equivalent objects from the goal state.
for (std::vector<const Object*>::const_iterator ci = term_manager.getAllObjects().begin(); ci != term_manager.getAllObjects().end(); ++ci)
{
const Object* object = *ci;
std::vector<const Object*>* init_objects = initial_symmetrical_groups[object];
std::vector<const Object*>* goal_objects = goal_symmetrical_groups[object];
if (init_objects == NULL || goal_objects == NULL)
{
continue;
}
// Take the intersection.
for (std::vector<const Object*>::const_iterator init_ci = init_objects->begin(); init_ci != init_objects->end(); ++init_ci)
{
const Object* i_object = *init_ci;
for (std::vector<const Object*>::const_iterator goal_ci = goal_objects->begin(); goal_ci != goal_objects->end(); ++goal_ci)
{
const Object* g_object = *goal_ci;
if (i_object == g_object)
{
symmetrical_groups.insert(std::make_pair(object, i_object));
break;
}
}
}
}
for (std::vector<std::vector<const Object*> *>::const_iterator ci = delete_list.begin(); ci != delete_list.end(); ++ci)
{
delete *ci;
}
}
*/
//void State::getSuccessors(NewStateReachedListener& listener, const std::multimap<const Object*, const Object*>& symmetrical_groups, const ActionManager& action_manager, const TypeManager& type_manager, bool prune_unhelpful_actions, const std::vector<const State*>& all_states) const
//void State::getSuccessors(NewStateReachedListener& listener, const std::multimap<const Object*, const Object*>& symmetrical_groups, const ActionManager& action_manager, const TypeManager& type_manager, bool prune_unhelpful_actions, const std::vector<const State*>& all_states, const TermManager& term_manager, const std::vector<const GroundedAtom*>& goals, const HEURISTICS::HeuristicInterface& heuristic) const
void State::getSuccessors(NewStateReachedListener& listener, const std::multimap<const Object*, const Object*>& symmetrical_groups, const ActionManager& action_manager, const TypeManager& type_manager, bool prune_unhelpful_actions, const std::vector<const GroundedAtom*>& initial_facts, const std::vector<std::pair<const REACHABILITY::AchievingTransition*, const std::vector<HEURISTICS::VariableDomain*>* > >& helpful_actions) const
{
#ifdef MYPOP_FORWARD_CHAIN_PLANNER_COMMENTS
std::cout << "Find successors of" << std::endl << *this << std::endl;
#endif
/*
// Instantiate all the helpful actions and try to find preconditions to match them.
//if (helpful_actions_.size() > 0 && false)
if (false)
{
std::vector<const GroundedAction*> already_tried_actions;
// TODO: Maybe shuffle? std::shuffle().
for (std::vector<std::pair<const REACHABILITY::AchievingTransition*, const std::vector<HEURISTICS::VariableDomain*>* > >::const_iterator ci = helpful_actions_.begin(); ci != helpful_actions_.end(); ++ci)
{
const REACHABILITY::AchievingTransition* transition = (*ci).first;
const std::vector<HEURISTICS::VariableDomain*>* variable_domains = (*ci).second;
const Action& action = transition->getAchiever()->getTransition().getAction();
unsigned int counter[action.getVariables().size()];
memset(&counter[0], 0, sizeof(unsigned int) * action.getVariables().size());
const Object* assigned_variables[action.getVariables().size()];
std::vector<const Atom*> preconditions;
std::vector<const Equality*> equalities;
Utility::convertFormula(preconditions, equalities, &action.getPrecondition());
bool done = false;
while (!done)
{
done = true;
memset(assigned_variables, 0, sizeof(Object*) * action.getVariables().size());
for (unsigned int action_variable_index = 0; action_variable_index < action.getVariables().size(); ++action_variable_index)
{
//HEURISTICS::VariableDomain* vd = transition->getVariableAssignments()[action_variable_index];
HEURISTICS::VariableDomain* vd = (*variable_domains)[action_variable_index];
assigned_variables[action_variable_index] = vd->getVariableDomain()[counter[action_variable_index]];
}
// Ground this action.
const GroundedAction& grounded_action = GroundedAction::getGroundedAction(action, assigned_variables);
// Make sure a symmetrical precondition has not been used.
bool is_symmetrical = false;
for (std::vector<const GroundedAction*>::const_iterator ci = already_tried_actions.begin(); ci != already_tried_actions.end(); ++ci)
{
const GroundedAction* old_action = *ci;
if (old_action->getAction().getVariables().size() != grounded_action.getAction().getVariables().size() ||
old_action->getAction().getPredicate() != grounded_action.getAction().getPredicate())
{
continue;
}
bool terms_are_symmetrical = true;
for (unsigned int term_index = 0; term_index < old_action->getAction().getVariables().size(); ++term_index)
{
std::pair<std::multimap<const Object*, const Object*>::const_iterator, std::multimap<const Object*, const Object*>::const_iterator> eo_ci = symmetrical_groups.equal_range(&old_action->getVariablesAssignment(term_index));
bool found_symmetrical_object = false;
for (std::multimap<const Object*, const Object*>::const_iterator ci = eo_ci.first; ci != eo_ci.second; ++ci)
{
const Object* symmetrical_object = (*ci).second;
if (symmetrical_object == &grounded_action.getVariablesAssignment(term_index))
{
found_symmetrical_object = true;
break;
}
}
if (!found_symmetrical_object)
{
terms_are_symmetrical = false;
break;
}
}
// If the terms are symmetrical than we do not need to persue this branch further!
if (terms_are_symmetrical)
{
// std::cerr << *fact << " == " << *grounded_atom << std::endl;
is_symmetrical = true;
break;
}
}
if (is_symmetrical)
{
continue;
}
//if (all_precondition_satisfied)
{
already_tried_actions.push_back(&grounded_action);
// Create a new state!
State* new_state = new State(*this, grounded_action, true);
#ifdef MYPOP_FORWARD_CHAIN_PLANNER_COMMENTS
// std::cout << "Copy the old state: " << *new_state << std::endl;
#endif
listener.addNewState(*new_state);
}
for (unsigned int action_variable_index = 0; action_variable_index < action.getVariables().size(); ++action_variable_index)
{
//if (counter[action_variable_index] + 1 != transition->getVariableAssignments()[action_variable_index]->getVariableDomain().size())
if (counter[action_variable_index] + 1 != (*variable_domains)[action_variable_index]->getVariableDomain().size())
{
done = false;
counter[action_variable_index] = counter[action_variable_index] + 1;
break;
}
else
{
counter[action_variable_index] = 0;
}
}
}
}
}
else*/
{
for (std::vector<Action*>::const_iterator ci = action_manager.getManagableObjects().begin(); ci != action_manager.getManagableObjects().end(); ci++)
{
const Action* action = *ci;
std::vector<const Atom*> preconditions;
std::vector<const Equality*> equalities;
Utility::convertFormula(preconditions, equalities, &action->getPrecondition());
// Construct all grounded variants of this action which are applicable in this state.
const Object* assigned_variables[action->getVariables().size()];
memset(assigned_variables, 0, sizeof(Object*) * action->getVariables().size());
instantiateAndExecuteAction(listener, symmetrical_groups, *action, preconditions, equalities, 0, assigned_variables, type_manager, prune_unhelpful_actions, initial_facts, helpful_actions);
}
}
#ifdef MYPOP_FORWARD_CHAIN_PLANNER_COMMENTS
std::cout << "Found: " << action_manager.getManagableObjects().size() << " successors states for: " << std::endl << *this << std::endl;
#endif
}
bool State::isSuperSetOf(const std::vector<const GroundedAtom*>& initial_facts, const std::vector<const GroundedAtom*>& facts) const
{
for (std::vector<const GroundedAtom*>::const_iterator ci = facts.begin(); ci != facts.end(); ci++)
{
const GroundedAtom* goal_fact = *ci;
bool is_satisfied = false;
std::vector<const GroundedAtom*> state_facts;
getFacts(initial_facts, state_facts);
//for (std::vector<const GroundedAtom*>::const_iterator ci = facts_.begin(); ci != facts_.end(); ci++)
for (std::vector<const GroundedAtom*>::const_iterator ci = state_facts.begin(); ci != state_facts.end(); ++ci)
{
if (**ci == *goal_fact)
{
is_satisfied = true;
break;
}
}
if (!is_satisfied) return false;
}
return true;
}
void State::getFacts(const std::vector<const GroundedAtom*>& initial_facts, std::vector<const GroundedAtom*>& facts) const
{
if (parent_ != NULL)
{
parent_->getFacts(initial_facts, facts);
achieving_action_->applyTo(facts);
}
// If there is no parent then we are at the initial state.
else
{
facts.insert(facts.end(), initial_facts.begin(), initial_facts.end());
}
std::sort(facts.begin(), facts.end());
}
/*
void State::getSymmetricalObjects(std::map<const Object*, std::vector<const Object*>*>& symmetrical_object_mappings, const std::vector<const GroundedAtom*>& goal_facts, REACHABILITY::EquivalentObjectManager& eog_manager) const
{
std::vector<ReachableFact*> facts;
for (const GroundedAtom* grounded_atom : facts_)
{
REACHABILITY::ReachableFact& fact = REACHABILITY::ReachableFact::createReachableFact(*grounded_atom, eog_manager);
facts.push_back(&fact);
}
eog_manager.reset();
eog_manager.initialise(facts);
eog_manager.updateEquivalences(0);
// Find which objects are equivalent in the initial state.
for (REACHABILITY::EquivalentObjectGroup* eog : eog_manager.getEquivalentObjectGroups())
{
if (!eog->isRootNode())
{
continue;
}
std::vector<const Object*>* equivalent_objects = new std::vector<const Object*>();
for (std::vector<REACHABILITY::EquivalentObject*>::const_iterator ci = eog->begin(0); ci != eog->end(0); ++ci)
{
REACHABILITY::EquivalentObject* eo = *ci;
equivalent_objects->push_back(&eo->getObject());
symmetrical_object_mappings.insert(std::make_pair(&eo->getObject(), equivalent_objects));
}
}
facts.empty();
for (const GroundedAtom* grounded_atom : goal_facts)
{
REACHABILITY::ReachableFact& fact = REACHABILITY::ReachableFact::createReachableFact(*grounded_atom, eog_manager);
facts.push_back(&fact);
}
// Check if the same holds for the goal sstate.
eog_manager.reset();
eog_manager.initialise(facts);
eog_manager.updateEquivalences(0);
for (REACHABILITY::EquivalentObjectGroup* eog : eog_manager.getEquivalentObjectGroups())
{
if (!eog->isRootNode())
{
continue;
}
std::vector<const Object*>* equivalent_objects = new std::vector<const Object*>();
for (std::vector<REACHABILITY::EquivalentObject*>::const_iterator ci = eog->begin(0); ci != eog->end(0); ++ci)
{
REACHABILITY::EquivalentObject* eo = *ci;
equivalent_objects->push_back(&eo->getObject());
symmetrical_object_mappings.insert(std::make_pair(&eo->getObject(), equivalent_objects));
}
for (Object* object : equivalent_objects)
{
std::map<const Object*, std::vector<const Object*>*>::const_iterator map_ci = symmetrical_object_mappings.find(object);
if (map_ci == symmetrical_object_mappings.end())
{
continue;
}
std::vector<const Object*>* symmetrical_objects = (*map_ci).second;
std::vector<const Object*>* split_mapping = new std::vector<const Object*>();
// Check if there is a discepancy between the symmetrical objects in the initial state and those in the goal state.
for (std::vector<const Object*>::reverse_iterator ri = symmetrical_objects->rbegin(); ri != symmetrical_objects->rend(); ++ri)
{
// If this object is in equivalent objects, we need to put it in a new group.
if (std::find(equivalent_objects->begin(), equivalent_objects->end(), object) != equivalent_objects->end())
{
split_mapping->push_back(object);
symmetrical_objects->erase((*ri.base()) - 1);
symmetrical_object_mappings.insert(std::make_pair(*ri, split_mapping));
}
}
}
}
}
*/
/*
//void State::setHelpfulActions(const std::vector<std::pair<const Action*, std::vector<const Object*>**> >& helpful_actions)
void State::setHelpfulActions(const std::vector<const REACHABILITY::AchievingTransition*>& helpful_actions)
{
deleteHelpfulActions();
helpful_actions_ = helpful_actions;
// std::cerr << *this << std::endl;
}
*/
/*
void State::setHelpfulActions(const std::vector<std::pair<const REACHABILITY::AchievingTransition*, const std::vector<HEURISTICS::VariableDomain*>* > >& helpful_actions)
{
deleteHelpfulActions();
helpful_actions_ = helpful_actions;
for (std::vector<std::pair<const REACHABILITY::AchievingTransition*, const std::vector<HEURISTICS::VariableDomain*>* > >::const_iterator ci = helpful_actions.begin(); ci != helpful_actions.end(); ++ci)
{
if ((*ci).first->getAchiever() == NULL)
{
std::cerr << "WTF!" << std::endl;
assert (false);
}
}
}
*/
/*
bool State::addFact(const MyPOP::GroundedAtom& fact, bool remove_fact)
{
// Don't add a fact twice!
for (std::vector<const GroundedAtom*>::iterator i = facts_.begin(); i != facts_.end(); i++)
{
if (*i == &fact)
{
return false;
}
}
facts_.push_back(&fact);
std::sort(facts_.begin(), facts_.end());
return true;
}
void State::removeFact(const GroundedAtom& fact)