forked from gerardcanal/rddl_parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessor.cc
1183 lines (1038 loc) · 45.4 KB
/
preprocessor.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
#include "preprocessor.h"
#include "evaluatables.h"
#include "rddl.h"
#include "utils/math_utils.h"
#include "utils/system_utils.h"
#include "utils/timer.h"
#include <algorithm>
#include <iostream>
#include <sstream>
#include "utils/timer.h"
using namespace std;
void Preprocessor::preprocess(bool const& output) {
Timer t;
// Create and initialize CPFs, rewardCPF, and SACs
if (output)
cout << " Preparing evaluatables..." << endl;
prepareEvaluatables();
if (output)
cout << " ...finished (" << t() << ")" << endl;
t.reset();
// Create action fluents and calculate legal action states
if (output)
cout << " Preparing actions..." << endl;
prepareActions();
if (output)
cout << " ...finished (" << t() << ")" << endl;
t.reset();
// Approximate reachable values (domains) of CPFs
if (output)
cout << " Calculating CPF domain..." << endl;
calculateCPFDomains();
if (output)
cout << " ...finished (" << t() << ")" << endl;
t.reset();
// Remove CPFs with only one reachable value (i.e. a domain size of 1) and
// simplify remaining CPFs, rewardCPF, and SACs
if (output)
cout << " Finalizing evaluatables..." << endl;
finalizeEvaluatables();
if (output)
cout << " ...finished (" << t() << ")" << endl;
t.reset();
// Determinize CPFs
if (output)
cout << " Computing determinization..." << endl;
determinize();
if (output)
cout << " ...finished (" << t() << ")" << endl;
t.reset();
// Determine some non-trivial properties
if (output)
cout << " Determining task properties..." << endl;
determineTaskProperties();
if (output)
cout << " ...finished (" << t() << ")" << endl;
t.reset();
// Initialize Hash Key Bases and Mappings
if (output)
cout << " Preparing hash keys..." << endl;
prepareStateHashKeys();
prepareKleeneStateHashKeys();
prepareStateFluentHashKeys();
if (output)
cout << " ...finished (" << t() << ")" << endl;
t.reset();
// Precompute results of evaluate for (some) evaluatables
if (output)
cout << " Precomputing evaluatables..." << endl;
precomputeEvaluatables();
if (output)
cout << " ...finished (" << t() << ")" << endl;
t.reset();
// Approximate or calculate the min and max reward
if (output)
cout << " Calculating min and max reward..." << endl;
calculateMinAndMaxReward();
if (output)
cout << " ...finished (" << t() << ")" << endl;
}
/*****************************************************************
Basic Preprocesses
*****************************************************************/
void Preprocessor::prepareEvaluatables() {
// Before we create the CPFs we remove those that simplify to their initial
// value and replace them by their initial value in all other evaluatables
map<ParametrizedVariable*, double> replacements;
bool simplifyAgain = true;
while (simplifyAgain) {
simplifyAgain = false;
for (ConditionalProbabilityFunction*& cpf : task->CPFs) {
cpf->simplify(replacements);
NumericConstant* nc = dynamic_cast<NumericConstant*>(cpf->formula);
if (nc &&
MathUtils::doubleIsEqual(cpf->head->initialValue, nc->value)) {
simplifyAgain = true;
assert(replacements.find(cpf->head) == replacements.end());
replacements[cpf->head] = nc->value;
swap(cpf, task->CPFs[task->CPFs.size() - 1]);
task->CPFs.pop_back();
break;
}
}
}
// Remove state fluents that simplify to their initial value from the reward
// and collect properties of reward
task->rewardCPF->simplify(replacements);
// Sort CPFs
sort(task->CPFs.begin(), task->CPFs.end(),
ConditionalProbabilityFunction::TransitionFunctionSort());
// We set the CPF indices as we have to evaluate formulas in the next step
// to determine legal action combinations. The indices are still temporal,
// though, as it is possible that we remove additional state fluents later.
for (unsigned int i = 0; i < task->CPFs.size(); ++i) {
task->CPFs[i]->setIndex(i);
}
vector<LogicalExpression*> preconds;
// Create and initialize SACs,then split all conjunctions in their elements
for (unsigned int index = 0; index < task->SACs.size(); ++index) {
// Remove the state fluents from the SAC formula that simplify to their
// initial value
task->SACs[index] = task->SACs[index]->simplify(replacements);
// Split conjunctions into their elements
Conjunction* conj = dynamic_cast<Conjunction*>(task->SACs[index]);
if (conj) {
preconds.insert(preconds.end(), conj->exprs.begin(),
conj->exprs.end());
} else {
preconds.push_back(task->SACs[index]);
}
}
// Divide preconds into dynamic SACs, static SACs, primitive static SACs and
// state invariants.
for (unsigned int index = 0; index < preconds.size(); ++index) {
stringstream name;
name << "SAC " << index;
ActionPrecondition* sac =
new ActionPrecondition(name.str(), preconds[index]);
// Collect the properties of the SAC that are necessary for distinction
// of
// action preconditions, (primitive) static SACs and state invariants
sac->initialize();
if (sac->containsStateFluent()) {
// An SAC that contain both state and action fluents is an action
// precondition, and an SAC that contains only state fluents is a
// state invariant that is ignored in PROST.
if (!sac->isActionIndependent()) {
sac->index = task->actionPreconds.size();
task->actionPreconds.push_back(sac);
}
} else {
Negation* neg = dynamic_cast<Negation*>(sac->formula);
if (neg) {
ActionFluent* act = dynamic_cast<ActionFluent*>(neg->expr);
if (act) {
// This is a "primitive" static action constraint of the
// form "not doSomething(params)", i.e., an sac that forbids
// the usage of an action fluent in general. We extract
// these early to make sure that the number of possibly
// legal action states in prepareActions() doesn't become
// too big.
task->primitiveStaticSACs.insert(act);
} else {
task->staticSACs.push_back(sac);
}
} else {
// An SAC that only contains action fluents is used to
// statically forbid action combinations.
task->staticSACs.push_back(sac);
}
}
}
}
void Preprocessor::removeInapplicableActionFluents(
bool const& updateActionStates) {
// Check if there are action fluents that aren't used in any CPF or SAC
vector<bool> fluentIsUsed(task->actionFluents.size(), false);
for (unsigned int i = 0; i < task->actionPreconds.size(); ++i) {
for (set<ActionFluent*>::const_iterator it =
task->actionPreconds[i]->dependentActionFluents.begin();
it != task->actionPreconds[i]->dependentActionFluents.end();
++it) {
fluentIsUsed[(*it)->index] = true;
}
}
for (unsigned int i = 0; i < task->staticSACs.size(); ++i) {
for (set<ActionFluent*>::const_iterator it =
task->staticSACs[i]->dependentActionFluents.begin();
it != task->staticSACs[i]->dependentActionFluents.end(); ++it) {
fluentIsUsed[(*it)->index] = true;
}
}
for (unsigned int i = 0; i < task->CPFs.size(); ++i) {
for (set<ActionFluent*>::const_iterator it =
task->CPFs[i]->dependentActionFluents.begin();
it != task->CPFs[i]->dependentActionFluents.end(); ++it) {
fluentIsUsed[(*it)->index] = true;
}
}
for (set<ActionFluent*>::const_iterator it =
task->rewardCPF->dependentActionFluents.begin();
it != task->rewardCPF->dependentActionFluents.end(); ++it) {
fluentIsUsed[(*it)->index] = true;
}
vector<ActionFluent*> afCopy = task->actionFluents;
task->actionFluents.clear();
int nextIndex = 0;
for (unsigned int i = 0; i < fluentIsUsed.size(); ++i) {
if (fluentIsUsed[i]) {
afCopy[i]->index = nextIndex;
++nextIndex;
task->actionFluents.push_back(afCopy[i]);
}
}
if (updateActionStates) {
set<ActionState> newActionStates;
for (unsigned int i = 0; i < task->actionStates.size(); ++i) {
ActionState& state = task->actionStates[i];
ActionState newState(nextIndex);
int newVarIndex = 0;
assert(fluentIsUsed.size() == state.state.size());
for (unsigned int j = 0; j < fluentIsUsed.size(); ++j) {
if (fluentIsUsed[j]) {
newState[newVarIndex] = state[j];
++newVarIndex;
}
}
newActionStates.insert(newState);
}
task->actionStates.clear();
task->actionStates.insert(task->actionStates.end(),
newActionStates.begin(),
newActionStates.end());
initializeActionStates();
} else {
assert(task->actionStates.empty());
}
}
void Preprocessor::prepareActions() {
// Remove action fluents that are never reasonable
removeInapplicableActionFluents(false);
// Check if there are action fluents that can never be set to a nondefault
// value due to a primitive static SAC (i.e., an action precondition of the
// form ~a).
vector<ActionFluent*> finalActionFluents;
map<ParametrizedVariable*, double> replacements;
if (task->primitiveStaticSACs.empty()) {
finalActionFluents = task->actionFluents;
} else {
for (unsigned int index = 0; index < task->actionFluents.size();
++index) {
if (task->primitiveStaticSACs.find(task->actionFluents[index]) ==
task->primitiveStaticSACs.end()) {
finalActionFluents.push_back(task->actionFluents[index]);
} else {
replacements[task->actionFluents[index]] = 0.0;
}
}
task->actionFluents.clear();
task->actionFluents = finalActionFluents;
// Simplify all evaluatables by removing the unused action fluents from
// their formula
for (unsigned int index = 0; index < task->CPFs.size(); ++index) {
task->CPFs[index]->simplify(replacements);
}
task->rewardCPF->simplify(replacements);
for (unsigned int index = 0; index < task->actionPreconds.size();
++index) {
task->actionPreconds[index]->simplify(replacements);
// TODO: What if this became static?
}
for (unsigned int index = 0; index < task->staticSACs.size(); ++index) {
task->staticSACs[index]->simplify(replacements);
// TODO: Check if this became a state invariant
}
replacements.clear();
}
// Sort action fluents for deterministic behaviour and assign (possibly
// temporary) indices
sort(task->actionFluents.begin(), task->actionFluents.end(),
ActionFluent::ActionFluentSort());
for (unsigned int index = 0; index < task->actionFluents.size(); ++index) {
task->actionFluents[index]->index = index;
}
State current(task->CPFs);
vector<ActionState> legalActionStates;
if (useIPC2018Rules) {
// For IPC 2018, the rules say that an action cannot be legal
// unless there is at least one legal action where the same
// action fluents are "active" except for one action fluent.
// Actions with exactly one "active" action fluent are an
// exception, these can be legal even if noop isn't. To use this
// part of the code, invoke the parser with
// rddl-parser DOMAIN_FILE INSTANCE_FILE OUTPUT -ipc2018 1 or
// rddl-parser INSTEACE_NAME DESTINATION -ipc2018 1
// Check if noop is legal
ActionState noop((int)task->actionFluents.size());
bool isLegal = true;
for (unsigned int i = 0; i < task->staticSACs.size(); ++i) {
double res = 0.0;
task->staticSACs[i]->formula->evaluate(res, current, noop);
if (MathUtils::doubleIsEqual(res, 0.0)) {
isLegal = false;
break;
}
}
if (isLegal) {
// cout << "Noop is legal!" << endl;
legalActionStates.push_back(noop);
}
vector<ActionState> base;
task->numberOfConcurrentActions = 1;
while (true) {
// cout << "Generating action states with " << task->numberOfConcurrentActions << " many action fluents." << endl;
// cout << "Total number of legal action states: " << legalActionStates.size() << endl;
// cout << "Number of base actions: " << base.size() << endl;
set<ActionState> candidates;
calcAllActionStatesForIPC2018(base, candidates);
// cout << "number of action state candidates with up to "
// << task->numberOfConcurrentActions
// << " many action fluents: " << candidates.size() << endl;
vector<ActionState> addedActionStates;
bool foundLegal = false;
for (const ActionState& actionState : candidates) {
bool isLegal = true;
for (unsigned int i = 0; i < task->staticSACs.size(); ++i) {
double res = 0.0;
task->staticSACs[i]->formula->evaluate(res, current, actionState);
if (MathUtils::doubleIsEqual(res, 0.0)) {
isLegal = false;
break;
}
}
foundLegal |= isLegal;
if (isLegal) {
legalActionStates.push_back(actionState);
addedActionStates.push_back(actionState);
}
}
if (!foundLegal || (task->numberOfConcurrentActions == task->actionFluents.size())) {
break;
}
++task->numberOfConcurrentActions;
base = addedActionStates;
}
} else {
if (task->numberOfConcurrentActions > task->actionFluents.size()) {
task->numberOfConcurrentActions = task->actionFluents.size();
}
// Calculate all action states with up to
// numberOfConcurrentActions concurrent actions TODO: Make sure
// this stops also if a static SAC is violated that constrains
// the number of concurrently applicable actions. Currently, if
// max-nondef-actions is not used, this will always produce the
// power set over all action fluents.
vector<ActionState> actionStateCandidates;
calcAllActionStates(actionStateCandidates, 0, 0);
// Remove all illegal action combinations by checking the SACs
// that are state independent
for (ActionState const& actionState : actionStateCandidates) {
bool isLegal = true;
for (unsigned int i = 0; i < task->staticSACs.size(); ++i) {
double res = 0.0;
task->staticSACs[i]->formula->evaluate(res, current, actionState);
if (MathUtils::doubleIsEqual(res, 0.0)) {
isLegal = false;
break;
}
}
if (isLegal) {
legalActionStates.push_back(actionState);
}
}
}
// Now that all legal actions have been created, we check if there are
// action fluents that are false (TODO: that are equal to their default
// value) in every legal action. We can safely remove those action fluents
// entirely from each action.
ActionState usedFluents((int)task->actionFluents.size());
for (unsigned int i = 0; i < legalActionStates.size(); ++i) {
for (unsigned int j = 0; j < legalActionStates[i].state.size(); ++j) {
usedFluents[j] |= legalActionStates[i].state[j];
}
}
// Assign new indices
int newIndex = 0;
for (unsigned int i = 0; i < usedFluents.state.size(); ++i) {
if (usedFluents[i]) {
task->actionFluents[i]->index = newIndex;
++newIndex;
} else {
task->actionFluents[i]->index = -1;
}
}
if (newIndex < task->actionFluents.size()) {
// Remove values of unused action fluents from action states
for (unsigned int i = 0; i < legalActionStates.size(); ++i) {
ActionState state(newIndex); // newIndex is equal to the number of
// used action fluents
for (unsigned int j = 0; j < legalActionStates[i].state.size();
++j) {
if (task->actionFluents[j]->index != -1) {
assert(task->actionFluents[j]->index < state.state.size());
state[task->actionFluents[j]->index] =
legalActionStates[i][j];
} else {
replacements[task->actionFluents[j]] = 0.0;
}
}
task->actionStates.push_back(state);
}
// Remove unused action fluents
finalActionFluents.clear();
for (unsigned int i = 0; i < task->actionFluents.size(); ++i) {
if (task->actionFluents[i]->index != -1) {
assert(task->actionFluents[i]->index ==
finalActionFluents.size());
finalActionFluents.push_back(task->actionFluents[i]);
}
}
task->actionFluents.clear();
task->actionFluents = finalActionFluents;
// Simplify all evaluatables by removing unused action fluents from the
// formulas
for (unsigned int index = 0; index < task->CPFs.size(); ++index) {
task->CPFs[index]->simplify(replacements);
}
task->rewardCPF->simplify(replacements);
for (unsigned int index = 0; index < task->actionPreconds.size();
++index) {
task->actionPreconds[index]->index = index;
}
} else {
task->actionStates = legalActionStates;
}
initializeActionStates();
}
void Preprocessor::initializeActionStates() {
// Sort action states again for deterministic behaviour
sort(task->actionStates.begin(), task->actionStates.end(),
ActionState::ActionStateSort());
// Set inidices and calculate properties of action states
for (unsigned int i = 0; i < task->actionStates.size(); ++i) {
ActionState& actionState = task->actionStates[i];
actionState.index = i;
for (unsigned int i = 0; i < actionState.state.size(); ++i) {
if (actionState.state[i]) {
actionState.scheduledActionFluents.push_back(
task->actionFluents[i]);
}
}
// Determine which dynamic SACs are relevant for this action
for (unsigned int i = 0; i < task->actionPreconds.size(); ++i) {
if (task->actionPreconds[i]->containsArithmeticFunction()) {
// If the SAC contains an arithmetic function we treat it as if
// it influenced this action. TODO: Implement a function that
// checks if it does influence this!
actionState.relevantSACs.push_back(task->actionPreconds[i]);
} else if (sacContainsNegativeActionFluent(task->actionPreconds[i],
actionState)) {
// If the SAC contains one of this ActionStates' action fluents
// negatively it might forbid this action.
actionState.relevantSACs.push_back(task->actionPreconds[i]);
} else if (sacContainsAdditionalPositiveActionFluent(
task->actionPreconds[i], actionState)) {
// If the SAC contains action fluents positively that are not in
// this ActionStates' action fluents it might enforce that
// action fluent (and thereby forbid this action)
actionState.relevantSACs.push_back(task->actionPreconds[i]);
}
}
}
}
void Preprocessor::calcAllActionStatesForIPC2018(vector<ActionState>& base,
set<ActionState>& result) const {
if (base.empty()) {
// Generate all action states with exactly one active action fluent
for (unsigned int j = 0; j < task->actionFluents.size(); ++j) {
ActionState state((int)task->actionFluents.size());
state[j] = 1;
result.insert(state);
}
return;
}
for (const ActionState& baseState : base) {
for (unsigned int index = 0; index < task->actionFluents.size(); ++index) {
if (!baseState[index]) {
ActionState copy(baseState);
copy[index] = 1;
result.insert(copy);
}
}
}
}
void Preprocessor::calcAllActionStates(vector<ActionState>& result,
int minElement,
int scheduledActions) const {
if (result.empty()) {
result.push_back(ActionState((int)task->actionFluents.size()));
} else {
int lastIndex = result.size();
for (unsigned int i = minElement; i < lastIndex; ++i) {
for (unsigned int j = 0; j < task->actionFluents.size(); ++j) {
if (!result[i][j]) {
bool isExtension = true;
for (unsigned int k = 0; k < j; ++k) {
if (result[i][k]) {
isExtension = false;
break;
}
}
if (isExtension) {
ActionState copy(result[i]);
copy[j] = 1;
result.push_back(copy);
}
}
}
}
minElement = lastIndex;
}
++scheduledActions;
if (scheduledActions <= task->numberOfConcurrentActions) {
calcAllActionStates(result, minElement, scheduledActions);
}
}
bool Preprocessor::sacContainsNegativeActionFluent(
ActionPrecondition* const& sac, ActionState const& actionState) const {
set<ActionFluent*> const& actionFluents = sac->negativeActionDependencies;
for (unsigned int index = 0;
index < actionState.scheduledActionFluents.size(); ++index) {
if (actionFluents.find(actionState.scheduledActionFluents[index]) !=
actionFluents.end()) {
return true;
}
}
return false;
}
bool Preprocessor::sacContainsAdditionalPositiveActionFluent(
ActionPrecondition* const& sac, ActionState const& actionState) const {
set<ActionFluent*> const& actionFluents = sac->positiveActionDependencies;
for (set<ActionFluent*>::iterator it = actionFluents.begin();
it != actionFluents.end(); ++it) {
bool isScheduledActionFluent = false;
for (unsigned int index = 0;
index < actionState.scheduledActionFluents.size(); ++index) {
if ((*it) == actionState.scheduledActionFluents[index]) {
isScheduledActionFluent = true;
break;
}
}
if (!isScheduledActionFluent) {
return true;
}
}
return false;
}
/*****************************************************************
Domain Calculation / Reachability Analysis
*****************************************************************/
void Preprocessor::calculateCPFDomains() {
// Insert initial values to set of reachable values
vector<set<double>> domains(task->CPFs.size());
for (unsigned int index = 0; index < task->CPFs.size(); ++index) {
domains[index].insert(task->CPFs[index]->getInitialValue());
}
// Simulate a run in the planning task but evaluate to all possible
// outcomes. Terminate if the fixpoint iteration doesn't change anything
// anymore
int currentHorizon = 0;
while (currentHorizon < task->horizon) {
// The values that are reachable in this step
vector<set<double>> reachable(task->CPFs.size());
// For each cpf, apply all actions and collect all reachable values
for (unsigned int varIndex = 0; varIndex < task->CPFs.size();
++varIndex) {
for (unsigned int actionIndex = 0;
actionIndex < task->actionStates.size(); ++actionIndex) {
set<double> actionDependentValues;
task->CPFs[varIndex]->formula->calculateDomain(
domains, task->actionStates[actionIndex],
actionDependentValues);
assert(!actionDependentValues.empty());
reachable[varIndex].insert(actionDependentValues.begin(),
actionDependentValues.end());
}
}
// All possible value of this step have been computed. Check if there
// are additional values, if so insert them and continue, otherwise the
// fixpoint iteration has reached its end
bool someDomainChanged = false;
for (unsigned int varIndex = 0; varIndex < domains.size(); ++varIndex) {
int sizeBefore = domains[varIndex].size();
domains[varIndex].insert(reachable[varIndex].begin(),
reachable[varIndex].end());
if (domains[varIndex].size() != sizeBefore) {
someDomainChanged = true;
}
}
if (!someDomainChanged) {
break;
}
++currentHorizon;
}
// QUICK FIX START
for (unsigned int index = 0; index < task->CPFs.size(); ++index) {
double max_val = *domains[index].rbegin();
if (domains[index].size() > 1 && (max_val != domains[index].size() -1)) {
cout << "State-fluent " << task->CPFs[index]->head->fullName << " has a domain size of " << domains[index].size() << " and a max val of " << max_val << endl;
cout << "Inserting values into domain of state-fluent " << task->CPFs[index]->head->fullName << endl;
for (unsigned int val = 0; val < max_val; ++val) {
domains[index].insert(val);
}
}
}
// QUICK FIX END
// Set domains
for (unsigned int index = 0; index < task->CPFs.size(); ++index) {
task->CPFs[index]->setDomain(domains[index]);
}
}
void Preprocessor::finalizeEvaluatables() {
// Remove all CPFs with a domain that only includes their initial value
map<ParametrizedVariable*, double> replacements;
for (vector<ConditionalProbabilityFunction*>::iterator it =
task->CPFs.begin(); it != task->CPFs.end(); ++it) {
assert(!(*it)->getDomainSize() == 0);
if ((*it)->getDomainSize() == 1) {
// As the initial value must be included, the only value must be the
// initial value
assert(MathUtils::doubleIsEqual(*((*it)->domain.begin()),
(*it)->getInitialValue()));
replacements[(*it)->head] = (*it)->getInitialValue();
task->CPFs.erase(it);
--it;
}
}
// Simplify CPFs by replacing all previously removed CPFs with their
// constant initial value, then sort again and reset indices.
for (unsigned int index = 0; index < task->CPFs.size(); ++index) {
task->CPFs[index]->simplify(replacements);
}
sort(task->CPFs.begin(), task->CPFs.end(),
ConditionalProbabilityFunction::TransitionFunctionSort());
for (unsigned int index = 0; index < task->CPFs.size(); ++index) {
task->CPFs[index]->setIndex(index);
}
// Simplify rewardCPF
task->rewardCPF->simplify(replacements);
// Simplify actionPreconds and check if they have become a state invariant
for (unsigned int i = 0; i < task->actionPreconds.size(); ++i) {
task->actionPreconds[i]->simplify(replacements);
NumericConstant* nc =
dynamic_cast<NumericConstant*>(task->actionPreconds[i]->formula);
if (nc && !MathUtils::doubleIsEqual(nc->value, 0.0)) {
// This SAC is not dynamic anymore after simplification as it
// simplifies to a state invariant that is always true
swap(task->actionPreconds[i],
task->actionPreconds[task->actionPreconds.size() - 1]);
task->actionPreconds.pop_back();
--i;
} // TODO: Otherwise, we can remove the actions that violate the
// now static action precondition
}
for (unsigned int index = 0; index < task->actionPreconds.size(); ++index) {
task->actionPreconds[index]->index = index;
}
// Finalize action fluents
removeInapplicableActionFluents(true);
}
/*****************************************************************
Determinization
*****************************************************************/
void Preprocessor::determinize() {
// Calculate determinzation of CPFs.
map<ParametrizedVariable*, double> replacementsDummy;
for (unsigned int index = 0; index < task->CPFs.size(); ++index) {
if (task->CPFs[index]->isProbabilistic()) {
task->CPFs[index]->determinization =
task->CPFs[index]->formula->determinizeMostLikely(task->actionStates);
task->CPFs[index]->determinization =
task->CPFs[index]->determinization->simplify(replacementsDummy);
}
}
}
/*****************************************************************
Calculation of non-trivial properties
*****************************************************************/
void Preprocessor::determineTaskProperties() {
// Determine if there is a single action that could be goal maintaining,
// i.e., that could always yield the maximal reward. TODO: We could use an
// action that contains as many positive and no negative occuring fluents as
// possible, but we should first figure out if reward lock detection still
// pays off after switching to FDDs from BDDs.
if (task->rewardCPF->positiveActionDependencies.empty() &&
task->actionStates[0].scheduledActionFluents.empty()) {
task->rewardFormulaAllowsRewardLockDetection = true;
} else {
task->rewardFormulaAllowsRewardLockDetection = false;
}
if (task->rewardCPF->positiveActionDependencies.empty() &&
task->actionStates[0].scheduledActionFluents.empty() &&
task->actionStates[0].relevantSACs.empty()) {
// The first action is noop, noop is always applicable and action
// fluents occur in the reward only as costs -> noop is always optimal
// as final action
task->finalRewardCalculationMethod = "NOOP";
} else if (task->rewardCPF->isActionIndependent()) {
// The reward formula does not contain any action fluents -> all actions
// yield the same reward, so any action that is applicable is optimal
task->finalRewardCalculationMethod = "FIRST_APPLICABLE";
} else {
task->finalRewardCalculationMethod = "BEST_OF_CANDIDATE_SET";
// Determine the actions that suffice to be applied in the final step.
for (unsigned int i = 0; i < task->actionStates.size(); ++i) {
if (!actionStateIsDominated(i)) {
addDominantState(i);
}
}
}
}
void Preprocessor::addDominantState(int stateIndex) const {
// cout << "Adding action state " <<
// task->actionStates[stateIndex].getName() << endl;
for (vector<int>::iterator it =
task->candidatesForOptimalFinalAction.begin();
it != task->candidatesForOptimalFinalAction.end(); ++it) {
if (actionStateDominates(task->actionStates[stateIndex],
task->actionStates[*it])) {
// cout << "It dominates " << task->actionStates[*it].getName() <<
// endl;
task->candidatesForOptimalFinalAction.erase(it);
--it;
}
}
task->candidatesForOptimalFinalAction.push_back(stateIndex);
}
bool Preprocessor::actionStateIsDominated(int stateIndex) const {
for (unsigned int i = 0; i < task->candidatesForOptimalFinalAction.size();
++i) {
if (actionStateDominates(
task->actionStates[task->candidatesForOptimalFinalAction[i]],
task->actionStates[stateIndex])) {
return true;
}
}
return false;
}
bool Preprocessor::actionStateDominates(ActionState const& lhs,
ActionState const& rhs) const {
// An action state with preconditions cannot dominate another action state
if (!lhs.relevantSACs.empty()) {
return false;
}
// Determine all fluents of both action states that influence the reward
// positively or negatively
set<ActionFluent*> lhsPos;
for (unsigned int i = 0; i < lhs.scheduledActionFluents.size(); ++i) {
if (task->rewardCPF->positiveActionDependencies.find(
lhs.scheduledActionFluents[i]) !=
task->rewardCPF->positiveActionDependencies.end()) {
lhsPos.insert(lhs.scheduledActionFluents[i]);
}
}
set<ActionFluent*> rhsPos;
for (unsigned int i = 0; i < rhs.scheduledActionFluents.size(); ++i) {
if (task->rewardCPF->positiveActionDependencies.find(
rhs.scheduledActionFluents[i]) !=
task->rewardCPF->positiveActionDependencies.end()) {
rhsPos.insert(rhs.scheduledActionFluents[i]);
}
}
set<ActionFluent*> lhsNeg;
for (unsigned int i = 0; i < lhs.scheduledActionFluents.size(); ++i) {
if (task->rewardCPF->negativeActionDependencies.find(
lhs.scheduledActionFluents[i]) !=
task->rewardCPF->negativeActionDependencies.end()) {
lhsNeg.insert(lhs.scheduledActionFluents[i]);
}
}
set<ActionFluent*> rhsNeg;
for (unsigned int i = 0; i < rhs.scheduledActionFluents.size(); ++i) {
if (task->rewardCPF->negativeActionDependencies.find(
rhs.scheduledActionFluents[i]) !=
task->rewardCPF->negativeActionDependencies.end()) {
rhsNeg.insert(rhs.scheduledActionFluents[i]);
}
}
// Action state lhs dominates rhs if lhs contains all action fluents that
// influence the reward positively of rhs, and if rhs contains all action
// fluents that influence the reward negatively of lhs
for (set<ActionFluent*>::iterator it = rhsPos.begin(); it != rhsPos.end();
++it) {
if (lhsPos.find(*it) == lhsPos.end()) {
return false;
}
}
for (set<ActionFluent*>::iterator it = lhsNeg.begin(); it != lhsNeg.end();
++it) {
if (rhsNeg.find(*it) == rhsNeg.end()) {
return false;
}
}
return true;
}
/*****************************************************************
HashKeys
*****************************************************************/
// Check if hashing of States is possible, and assign hash key bases if so
void Preprocessor::prepareStateHashKeys() {
bool stateHashingPossible = true;
long nextHashKeyBase = 1;
for (unsigned int index = 0; index < task->CPFs.size(); ++index) {
ConditionalProbabilityFunction* cpf = task->CPFs[index];
task->stateHashKeys.push_back(vector<long>(cpf->getDomainSize()));
for (unsigned int valueIndex = 0; valueIndex < cpf->getDomainSize();
++valueIndex) {
task->stateHashKeys[index][valueIndex] =
(valueIndex * nextHashKeyBase);
}
if (!cpf->hasFiniteDomain() ||
!MathUtils::multiplyWithOverflowCheck(nextHashKeyBase,
cpf->getDomainSize())) {
stateHashingPossible = false;
break;
}
}
if (!stateHashingPossible) {
task->stateHashKeys.clear();
}
}
// Check if hashing of KleeneStates is possible, and assign hash key bases if so
void Preprocessor::prepareKleeneStateHashKeys() {
bool kleeneStateHashingPossible = true;
// We start by calculating the kleene
for (unsigned int index = 0; index < task->CPFs.size(); ++index) {
// The number of possible values of this variable in Kleene states is
// 2^0 + 2^1 + ... + 2^{n-1} = 2^n -1 with n = CPFs[i]->domain.size()
task->CPFs[index]->kleeneDomainSize = 2;
if (!MathUtils::toThePowerOfWithOverflowCheck(
task->CPFs[index]->kleeneDomainSize,
task->CPFs[index]->getDomainSize())) {
// KleeneState hashing is impossible because the kleeneDomain of
// this CPF is too large. We nevertheless compute the other
// kleenDomainSizes as they can still be used for
// kleeneStateFluentHashKeys
kleeneStateHashingPossible = false;
task->CPFs[index]->kleeneDomainSize = 0;
} else {
--task->CPFs[index]->kleeneDomainSize;
}
}
if (!kleeneStateHashingPossible) {
return;
}
long nextHashKeyBase = 1;
for (unsigned int index = 0; index < task->CPFs.size(); ++index) {
// Set the kleeneStateHashKeyBase
task->kleeneStateHashKeyBases.push_back(nextHashKeyBase);
if (!MathUtils::multiplyWithOverflowCheck(
nextHashKeyBase, task->CPFs[index]->kleeneDomainSize)) {
kleeneStateHashingPossible = false;
break;
}
}
if (!kleeneStateHashingPossible) {
task->kleeneStateHashKeyBases.clear();
}
}
void Preprocessor::prepareStateFluentHashKeys() {
task->indexToStateFluentHashKeyMap.resize(task->CPFs.size());
task->indexToKleeneStateFluentHashKeyMap.resize(task->CPFs.size());
int hashIndex = 0;
for (; hashIndex < task->CPFs.size(); ++hashIndex) {
task->CPFs[hashIndex]->hashIndex = hashIndex;
task->CPFs[hashIndex]->initializeHashKeys(task);
}