-
Notifications
You must be signed in to change notification settings - Fork 2
/
compile.cpp
1198 lines (1068 loc) · 44.3 KB
/
compile.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
/****** COMPILER INCLUDES ***********/
#include "include.h"
#include "domain_creator.h"
#include "types.h"
#include "predicates.h"
#include "actions.h"
#include "instance_creator.h"
#include "derived_actions.h"
#include "sequential_actions.h"
#include "goto_actions.h"
#include "terminal_actions.h"
#include "call_actions.h"
#include "choice_actions.h"
#include "hl_cond_actions.h"
#include "unsupervised_actions.h"
#include "cfg_actions.h"
#include "neg_actions.h"
/****** COMPILER GLOBAL VARIABLES ***********/
parser::pddl::Domain * d; // Original domain
parser::pddl::Domain * cd; // Compiled domain
parser::pddl::Instance * cins; // Compiled instance
InstVec ins; // Vector of instances
unsigned max_bound; // Max number of lines for the planning program
unsigned bound; // Max number of lines for this procedure
unsigned total_instances; // Number of total instances
unsigned procedures; // Number of procedures
unsigned novariables; // Number of variables
unsigned noslots; // Number of slots
unsigned noclasses; // Number of classes
bool to_program; // Boolean to know if program instructions can be called
// PREDICATE & CONSTANT VARIABLES
StringVec rows; // Constant objects ROWs for the stack
StringVec tests; // Tests predicates
StringVec empty_lines; // Empty lines predicates
StringVec select_programs; // Select programs for CFG procedures
//StringVec allowed_lines; // Lines allowed to program/repeat for CFG
StringDVec allowed_lines; // Lines allowed to program/repeat for CFG
StringDVec endinstr; // End instructions predicates
StringTVec calls; // Calls predicates
StringTVec instr; // Instructions predicates
StringTVec conds; // Condition predicates
StringQVec hl_conds; // High-Level state condition predicates
StringTVec gotos; // Gotos predicates
StringQVec hl_neg_conds; // High-Level state negated condition predicates
StringTVec neg_gotos; // Negated gotos predicates
StringTVec choiceInstr; // Choice instructions
StringVec copiedPredicates; // Copied predicates to detect infinite loops
StringVec correctPredicates; // Detect correct copies in the current state
StringVec constant_slots; // slots as constants
StringVec constant_vars; // variables as constants
StringVec constant_programs; // programs as constants
StringVec constant_states; // states as constants
StringVec empty_slots; // empty slot predicates
StringDVec fsc_true_calls_preds; // true programming calls predicates
StringDVec fsc_false_calls_preds; // false programming calls predicates
StringVec fsc_accumulator_preds; // accumulator predicates
StringVec fsc_evaluating_preds; // evaluating predicates
StringVec fsc_action_preds; // action predicates
StringVec fsc_noconds_preds; // states without conditions
StringVec fsc_tgotos; // gotos when true
StringVec fsc_fgotos; // gotos when false
StringDVec fsc_conds_preds; // condition predicates
StringDVec fsc_tacts; // actions when true
StringDVec fsc_facts; // actions when false
StringVec true_no_act_preds; // true no-action predicates
StringVec false_no_act_preds; // false no-action predicates
/****** COMPILER CLASSES ***********/
Types *types;
Predicates *predicates;
Actions *actions;
SequentialActions *sequential_actions;
GotoActions *goto_actions;
TerminalActions *terminal_actions;
CallActions *call_actions;
ChoiceActions *choice_actions;
DerivedActions *derived_actions;
HLCondActions *hl_cond_actions;
UActions *u_actions;
CFGActions *cfg_actions;
NegActions *neg_actions;
//NEGATIVE INSTANCES COMPILATION
unsigned negative_instances;
unsigned positive_instances;
/****** COMPILER FUNCTIONS ***********/
void init(int argc, char *argv[]){
// check input correctness
if ( argc < 14 ) {
std::cout << "Usage: ./bin/compile <compiler_type> <domain.pddl> <num of tasks> <task\\s.pddl> <max_bound> <current_bound> <procedures> <num of predicates to the stack> <predicate\\s> <num of objects to the call> <object\\s> <stack size> <to program?> <variable\\s> <slot\\s>\n";
std::cout << "Writes domain file to standard output and instance file to standard error\n";
exit( 1 );
}
int offset = 1;
// read the compiler type
compiler_type = String( argv[ offset++ ] );
// read input domain
d = new parser::pddl::Domain( argv[ offset++ ] );
// extract the total number of instances
ISStream is( argv[ offset++ ] );
if( ( compiler_type == "NEG" ) || ( compiler_type == "NEGLITE" ) ){
is >> positive_instances;
is.clear();
is.str( argv[ offset++ ] );
is >> negative_instances;
total_instances = positive_instances + negative_instances;
}
else{
is >> total_instances;
}
// read input instances
for(unsigned i = 0 ; i < total_instances; i++){
ins.push_back( new parser::pddl::Instance( *d, argv[ offset++ ] ) );
}
// extract max number of lines per procedure and main (bound)
is.clear();
is.str( argv[ offset++ ] );
is >> max_bound;
is.clear();
is.str( argv[ offset++ ] );
is >> bound;
// extract number of procedures
is.clear();
is.str( argv[ offset++ ] );
is >> procedures;
// extract number of predicates in stack
is.clear();
is.str( argv[ offset++ ] );
is >> total_stack_pred;
INSTR_WITH_PARAMS = ( total_stack_pred > 0 );
// read input predicates
for( unsigned i = 0; i < total_stack_pred; i++ ){
stack_predicates.insert( String( argv[ offset++ ] ) );
}
// extract variables for recursive calls
is.clear();
is.str( argv[ offset++ ] );
is >> total_stack_vars;
// read input variables for recursive calls
for( unsigned i = 0; i < total_stack_vars; i++ ){
stack_variables.insert( String( argv[ offset++ ] ) );
}
// assigning pile size by default
is.clear();
is.str( argv[ offset++ ] );
is >> stack_size;
// know if we have to program or not
is.clear();
is.str( argv[ offset++ ] );
is >> to_program;
// Number of variables for searching high-level state features
is.clear();
is.str( argv[ offset++ ] );
is >> novariables;
// Number of slots for searching high-level state features
is.clear();
is.str( argv[ offset++ ] );
is >> noslots;
// Number of classes
is.clear();
is.str( argv[ offset++ ] );
is >> noclasses;
if( compiler_type == "TWO-STEPS" ){
// Bound on the number of gotos
is.clear();
is.str( argv[ offset++ ] );
is >> max_gotos;
}
IS_HIGH_LEVEL = ( novariables > 0 );
IS_UNSUPERVISED = ( noclasses > 0 );
// PREPROCESSING GOTOS
if( IS_HIGH_LEVEL ){
// Any predicate is a potential condition!!!
for ( unsigned i = 0; i < d->preds.size(); ++i ) {
if ( d->preds[i]->name.substr( 0, 6 ) == "ASSIGN" ||
d->preds[i]->name.substr( 0, 8 ) == "INCLUDES" )
effs.insert( i );
}
}
else{
// Detect non-static fluents
for ( unsigned i = 0; i < d->actions.size(); ++i ) {
IntSet s = preds( d, d->actions[i]->eff );
effs.insert( s.begin(), s.end() );
}
// Avoid delete derived predicates in the effects
for ( unsigned i = 0; i < d->derived.size(); ++i ){
unclean_effs.insert( d->preds.index( d->derived[i]->name ) );
effs.insert( d->preds.index( d->derived[i]->name ) );
}
}
LIFTED_INSTR = ( compiler_type == "LIFTED" );
}
void create_compiled_domain( String &name, bool condeffects = true, bool typed = true, bool cons = true, bool costs = true){
cd = DomainCreator::create( name, condeffects, typed, cons, costs );
}
void create_instance( ){
if( compiler_type == "PLPR" ){
cins = InstanceCreator::create( d, cd, ins[ 0 ], rows, empty_lines, procedures, to_program, true, constant_slots, noclasses );
}
else if( compiler_type == "HFSC" ){
cins = InstanceCreator::createFSCInstance( d, cd, ins[ 0 ], procedures, constant_states, rows, bound, to_program );
}
else if( compiler_type == "CFG" ){
cins = InstanceCreator::createCFGInstance( d, cd, ins[ 0 ], rows, empty_lines, to_program, true, select_programs, bound );
}
else if( compiler_type == "NEG" or compiler_type == "NEGLITE" ){
cins = InstanceCreator::create( d, cd, ins[ 0 ], rows, empty_lines, procedures, to_program, true, constant_slots, noclasses );
}
else if( compiler_type == "LIFTED" ){
cins = InstanceCreator::create( d, cd, ins[ 0 ], rows, empty_lines, procedures, to_program, true, constant_slots, noclasses );
}
else if( compiler_type == "TWO-STEPS" ){
cins = InstanceCreator::create( d, cd, ins[ 0 ], rows, empty_lines, procedures, to_program, true, constant_slots, noclasses, ins[ int(ins.size()) - 1 ] );
}
else if( compiler_type == "PL-CTRL" ){
cins = InstanceCreator::create( d, cd, ins[ 0 ], rows, empty_lines, procedures, to_program, true, constant_slots, noclasses );
}
else if( compiler_type == "TWO-STEPS-2" ){
cins = InstanceCreator::create( d, cd, ins[ 0 ], rows, empty_lines, procedures, to_program, true, constant_slots, noclasses );
}
else if( compiler_type == "OPT" ){
cins = InstanceCreator::create( d, cd, ins[ 0 ], rows, empty_lines, procedures, to_program, true, constant_slots, noclasses );
}
else if( compiler_type == "NFA" or compiler_type == "NFA2"){ // Non-deterministic Turing Machine
cins = InstanceCreator::createFSCInstance( d, cd, ins[ 0 ], procedures, constant_states, rows, bound, to_program );
}
}
void create_classes( ){
types = new Types();
predicates = new Predicates();
actions = new Actions( d, cd );
sequential_actions = new SequentialActions( d, cd );
goto_actions = new GotoActions( d, cd );
terminal_actions = new TerminalActions( d, cd );
call_actions = new CallActions( d, cd );
choice_actions = new ChoiceActions( d, cd );
derived_actions = new DerivedActions( d, cd );
hl_cond_actions = new HLCondActions( d, cd );
u_actions = new UActions( d, cd );
cfg_actions = new CFGActions( d, cd );
neg_actions = new NegActions( d, cd );
}
void add_types(){
types->copyTypes( d, cd );
types->createType( cd, "STACKROW" );
types->moveObjectsToConstants( cd );
types->clearObjects( cd );
types->addValuedConstants( cd, "STACKROW", "ROW", rows, stack_size );
if( compiler_type == "PLPR" ){
if( IS_HIGH_LEVEL ){
types->createType( cd, "CONDITION-SLOT" );
types->createType( cd, "CONDITION-VARIABLE" );
types->addValuedConstants( cd, "CONDITION-SLOT", "SLOT", constant_slots, noslots + 1 );
//types->addValuedConstants( cd, "CONDITION-VARIABLE", "CVAR", constant_vars, novariables + 1, 1 );
types->addValuedConstants( cd, "CONDITION-VARIABLE", "CVAR", constant_vars, novariables + 1 );
}
if( IS_UNSUPERVISED ){
types->createType( cd, "PROGRAM" );
types->addValuedConstants( cd, "PROGRAM", "PROG", constant_programs, noclasses);
}
}
else if( compiler_type == "HFSC" or compiler_type == "NFA" or compiler_type == "NFA2"){
types->createType( cd, "STACKSTATE" );
types->addValuedConstants( cd, "STACKSTATE", "STATE", constant_states, max_bound + 1 );
}
else if( compiler_type == "CFG" ){
//types->createType( cd, "PROGRAM" );
//types->addValuedConstants( cd, "PROGRAM", "PROG", constant_programs, bound+1 , 0 );
}
else if( compiler_type == "NEG" or compiler_type == "NEGLITE" ){
/*
IFStream ifs( "constants.txt" );
String type_aux, const_aux;
while( ifs >> const_aux ){
ifs >> type_aux;
//std::cout << const_aux << " " << type_aux << std::endl;
types->addConstant( cd, type_aux, const_aux );
}
ifs.close();
*/
}
else if( compiler_type == "LIFTED" ){
if( IS_HIGH_LEVEL ){
types->createType( cd, "CONDITION-SLOT" );
types->createType( cd, "CONDITION-VARIABLE" );
types->addValuedConstants( cd, "CONDITION-SLOT", "SLOT", constant_slots, noslots + 1 );
types->addValuedConstants( cd, "CONDITION-VARIABLE", "CVAR", constant_vars, novariables + 1 );
}
}
else if( compiler_type == "TWO-STEPS" ){
if( max_gotos > 0 ){
types->createType( cd, "NUM-GOTOS" );
StringVec constant_num_gotos;
types->addValuedConstants( cd, "NUM-GOTOS", "CG", constant_num_gotos , max_gotos + 2 );
}
}
else if( compiler_type == "PL-CTRL" ){
}
else if( compiler_type == "TWO-STEPS-2" ){
}
}
void add_predicates(){
predicates->copyPredicates( d, cd );
if( compiler_type == "PLPR" || compiler_type == "LIFTED" ){
predicates->createPredicates( d, cd, procedures, max_bound, total_instances, tests, empty_lines, endinstr, calls, instr, choiceInstr );
predicates->createTValuedPredicates( cd, "GOTO", gotos, procedures + 1, max_bound, max_bound + 1 );
if( LIFTED_INSTR ){
predicates->createTValuedPredicates( cd, "NEG-GOTO", neg_gotos, procedures + 1, max_bound, max_bound + 1 );
}
//[PERFORMANCE] Not-gotos
/*StringTVec notgotos; // notGotos predicates
predicates->createTValuedPredicates( cd, "NOT-GOTO", notgotos, procedures + 1, max_bound, max_bound + 1 );*/
//[END-OF-PERFORMANCE]
if( IS_HIGH_LEVEL ){
hl_conds.resize( d->preds.size() );
hl_neg_conds.resize( d->preds.size() );
for( int eff : effs ){
StringVec parameters = d->typeList( d->preds[ eff ] );
for ( unsigned j = 1; j < parameters.size(); ++j )
parameters[ j ] = "CONDITION-VARIABLE";
String name = "COND-" + d->preds[ eff ]->name;
predicates->createTValuedPredicates( cd, name, hl_conds[ eff ], procedures + 1, bound, noslots + 1, 0, 0, 1, parameters);
if( LIFTED_INSTR ){
name = "NEG-COND-" + d->preds[ eff ]->name;
predicates->createTValuedPredicates( cd, name, hl_neg_conds[ eff ], procedures + 1, bound, noslots + 1, 0, 0, 1, parameters);
}
}
// Add predicates related to slots
predicates->createPredicate( cd, "CURRENT-SLOT", StringVec( 1, "CONDITION-SLOT" ) );
predicates->createValuedPredicates( cd, "EMPTYSLOT", empty_slots, bound, 0, StringVec( 1, "CONDITION-SLOT" ) );
StringVec parameters( 1, "CONDITION-SLOT" );
parameters.insert( parameters.end(), novariables, "VALUE" );
predicates->createPredicate( cd, "POSSIBLE-VALUE", parameters );
}
else{
predicates->createTValuedPredicates( d, cd, conds, procedures + 1, max_bound, 0, 0 );
}
if( IS_UNSUPERVISED ){
predicates->createPredicate( cd, "INS-SELECT-PROGRAM" );
predicates->createPredicate( cd, "CHOSEN-PROGRAM", StringVec( 2, "PROGRAM" ) );
}
}
else if( compiler_type == "HFSC" or compiler_type == "NFA" or compiler_type == "NFA2"){
// Add empty predicates
if( compiler_type == "HFSC" or compiler_type == "NFA2" ){
predicates->createPredicate( cd, stack_empty_cond_pred, StringVec( 1, "STACKSTATE" ) );
}
predicates->createPredicate( cd, stack_empty_tgoto_pred, StringVec( 1, "STACKSTATE" ) );
predicates->createPredicate( cd, stack_empty_fgoto_pred, StringVec( 1, "STACKSTATE" ) );
predicates->createPredicate( cd, stack_empty_tact_pred, StringVec( 1, "STACKSTATE" ) );
predicates->createPredicate( cd, stack_empty_fact_pred, StringVec( 1, "STACKSTATE" ) );
// Add true and false predicates for no action instructions
//predicates->createPredicate( cd, true_no_act_pred, StringVec( 1, "STACKSTATE" ) );
//predicates->createPredicate( cd, false_no_act_pred, StringVec( 1, "STACKSTATE" ) );
predicates->createValuedPredicates( cd, true_no_act_pred, true_no_act_preds, procedures + 1, 0, StringVec( 1, "STACKSTATE" ) );
predicates->createValuedPredicates( cd, false_no_act_pred, false_no_act_preds, procedures + 1, 0, StringVec( 1, "STACKSTATE" ) );
// Add predicates for each test
predicates->createValuedPredicates( cd, "TEST", tests, total_instances );
if( compiler_type == "HFSC" ){
// Add stack state predicates
predicates->createPredicate( cd, stack_state_pred, StringVec() = { "STACKSTATE", "STACKROW" } );
// Link stack rows
predicates->createPredicate( cd, stack_row_pred, StringVec( 2, "STACKROW" ) );
// Add top stack predicate
predicates->createPredicate( cd, stack_top_pred, StringVec( 1, "STACKROW" ) );
// Add predicates for each pair procedure-row in the stack
predicates->createValuedPredicates( cd, "STACK-PROCEDURE", stack_procedures, procedures + 1, 0, StringVec( 1, "STACKROW" ) );
// Add calls predicates for procedures
StringVec call_params( stack_variables.begin(), stack_variables.end());
call_params.push_back( "STACKSTATE" );
fsc_true_calls_preds.resize( procedures + 1 );
fsc_false_calls_preds.resize( procedures + 1 );
for(unsigned p_from = 0; p_from < procedures + 1; p_from++){
fsc_true_calls_preds[ p_from ].resize( procedures + 1 );
fsc_false_calls_preds[ p_from ].resize( procedures + 1 );
for( unsigned p_to = 0; p_to < procedures + 1; p_to++ ){
//SStream ss;
//ss << "TRUE-CALL-" << p_from << "-" << p_to ;
//fsc_true_calls_preds[ p_from ][ p_to ] = ss.str();
//ss.str( "" );
//ss << "FALSE-CALL-" << p_from << "-" << p_to ;
//fsc_false_calls_preds[p_from ][ p_to ] = ss.str();
fsc_true_calls_preds[ p_from ][ p_to ] = "TRUE-CALL-" + tostr( p_from ) + "-" + tostr( p_to );
fsc_false_calls_preds[p_from ][ p_to ] = "FALSE-CALL-" + tostr( p_from ) + "-" + tostr( p_to );
if( p_to >= p_from ){
predicates->createPredicate( cd, fsc_true_calls_preds[ p_from ][ p_to ], call_params );
predicates->createPredicate( cd, fsc_false_calls_preds[ p_from ][ p_to ], call_params );
}
}
}
// Add evaluation predicates
predicates->createValuedPredicates( cd, "ACCUMULATOR", fsc_accumulator_preds, procedures + 1, 0, StringVec( 1, "STACKROW" ) );
predicates->createValuedPredicates( cd, "DONE-EVALUATING", fsc_evaluating_preds, procedures + 1, 0, StringVec( 1, "STACKROW" ) );
predicates->createValuedPredicates( cd, "DONE-APPLYING", fsc_action_preds, procedures + 1, 0, StringVec( 1, "STACKROW" ) );
}
if( compiler_type == "NFA2" ){
// Add evaluation predicates
//predicates->createValuedPredicates( cd, "ACCUMULATOR", fsc_accumulator_preds, procedures + 1 );
//predicates->createValuedPredicates( cd, "DONE-EVALUATING", fsc_evaluating_preds, procedures + 1 );
//predicates->createValuedPredicates( cd, "DONE-APPLYING", fsc_action_preds, procedures + 1 );
predicates->createPredicate( cd, "DONE-STRUCTURE" );
predicates->createPredicate( cd, "MODE-STRUCTURE" );
}
if( compiler_type == "NFA" or compiler_type == "NFA2" ){
// Add stack state predicates
predicates->createPredicate( cd, stack_state_pred, StringVec() = { "STACKSTATE" } );
predicates->createPredicate( cd, "TRUE-APPLIED" );
predicates->createPredicate( cd, "FALSE-APPLIED" );
predicates->createPredicate( cd, "EVAL-TRUE" );
predicates->createPredicate( cd, "EVAL-FALSE" );
}
if( compiler_type == "NFA2" ){
}
// Add goal predicate
predicates->createPredicate( cd, goal_pred );
// Add available state to program
predicates->createPredicate( cd, available_pred, StringVec( 1, "STACKSTATE" ) );
// Add next state relations
predicates->createPredicate( cd, next_state_pred, StringVec( 2, "STACKSTATE" ) );
// Add predicates for conditions
if( compiler_type == "HFSC" or compiler_type == "NFA2" ){
predicates->createValuedPredicates( cd, "NCOND", fsc_noconds_preds, procedures + 1, 0, StringVec( 1, "STACKSTATE" ) );
fsc_conds_preds.resize( d->preds.size() );
for ( IntSet::iterator i = effs.begin(); i != effs.end(); ++i ) {
fsc_conds_preds[ *i ].resize( procedures + 1 );
for( unsigned p = 0; p < procedures + 1; p++ ){
SStream ss;
ss << "COND-" << d->preds[ *i ]->name << "-" << p;
fsc_conds_preds[ *i ][ p ] = ss.str();
StringVec params_aux = d->typeList( d->preds[ *i ] );
params_aux.push_back( "STACKSTATE" );
predicates->createPredicate( cd, fsc_conds_preds[ *i ][ p ], params_aux );
}
}
}
// Add predicates for jumps
predicates->createValuedPredicates( cd, "TRUE-GOTO", fsc_tgotos, procedures + 1, 0, StringVec( 2, "STACKSTATE" ) );
predicates->createValuedPredicates( cd, "FALSE-GOTO", fsc_fgotos, procedures + 1, 0, StringVec( 2, "STACKSTATE" ) );
fsc_tacts.resize( d->actions.size() );
fsc_facts.resize( d->actions.size() );
for ( unsigned i = 0; i < d->actions.size(); ++i ) {
fsc_tacts[ i ].resize( procedures + 1 );
fsc_facts[ i ].resize( procedures + 1 );
for( unsigned p = 0; p < procedures + 1; p++ ) {
SStream ss;
ss << "TRUE-" << d->actions[ i ]->name << "-" << p;
fsc_tacts[ i ][ p ] = ss.str();
StringVec params_aux = d->typeList( d->actions[ i ] );
params_aux.push_back( "STACKSTATE" );
predicates->createPredicate( cd, fsc_tacts[ i ][ p ], params_aux );
ss.str( "" );
ss << "FALSE-" << d->actions[ i ]->name << "-" << p;
fsc_facts[ i ][ p ] = ss.str();
predicates->createPredicate( cd, fsc_facts[ i ][ p ], params_aux );
}
}
predicates->createDValuedPredicates( cd, "END-COND", endinstr, procedures + 1, max_bound + 1 );
}
else if( compiler_type == "CFG" ){
predicates->createPredicates( d, cd, procedures, max_bound, total_instances, tests, empty_lines, endinstr, calls, instr, choiceInstr );
//predicates->createPredicate( cd, "INS-SELECT-PROGRAM" );
predicates->createValuedPredicates( cd, "INS-SELECT-PROGRAM", select_programs, procedures + 1 );
//predicates->createValuedPredicates( cd, "ALLOWED-LINE", allowed_lines, bound + 1 );
predicates->createDValuedPredicates( cd, "ALLOWED-LINE", allowed_lines, procedures + 1, max_bound + 1 );
}
else if( compiler_type == "NEG" or compiler_type == "NEGLITE" ){
predicates->createPredicates( d, cd, procedures, max_bound, total_instances, tests, empty_lines, endinstr, calls, instr, choiceInstr );
predicates->createTValuedPredicates( cd, "GOTO", gotos, procedures + 1, max_bound, max_bound + 1 );
predicates->createTValuedPredicates( d, cd, conds, procedures + 1, max_bound, 0, 0 );
predicates->createPredicate( cd, "HOLDS" );
predicates->createPredicate( cd, "CHECKED" );
predicates->createPredicate( cd, "NEGEX" );
predicates->createPredicate( cd, "ACTED" );
// Predicates to detect inifinite executions
if( compiler_type == "NEG" ){
predicates->createPredicate( cd, "STORED" );
//predicates->createPredicate( cd, "STORE-AVAILABLE" );
predicates->createPredicate( cd, "LOOP" );
for ( unsigned i = 0; i < d->preds.size(); ++i ){
String predicate_name = d->preds[ i ]->name;
StringVec parameter_types = d->typeList( d->preds[ i ] );
// Check if the predicate must be stackable
if( stack_predicates.find( predicate_name ) != stack_predicates.end() ){
parameter_types.push_back( "STACKROW" );
}
predicates->createPredicate( cd, "COPY-"+predicate_name, parameter_types );
copiedPredicates.emplace_back( "COPY-"+predicate_name );
predicates->createPredicate( cd, "CORRECT-"+predicate_name, parameter_types );
correctPredicates.emplace_back( "CORRECT-"+predicate_name );
}
for( unsigned i = 0; i < stack_lines.size(); i++ ){
predicates->createPredicate( cd, "COPY-"+stack_lines[ i ], StringVec( 1, "STACKROW" ) );
predicates->createPredicate( cd, "CORRECT-"+stack_lines[ i ], StringVec( 1, "STACKROW" ) );
}
}
}
if( compiler_type == "TWO-STEPS" ){
predicates->createPredicates( d, cd, procedures, max_bound, total_instances, tests, empty_lines, endinstr, calls, instr, choiceInstr );
predicates->createTValuedPredicates( cd, "GOTO", gotos, procedures + 1, max_bound, max_bound + 1 );
//predicates->createTValuedPredicates( d, cd, conds, procedures + 1, max_bound, 0, 0 );
if( max_gotos > 0 ){
predicates->createPredicate( cd, "CURRENT-GOTOS", StringVec() = { "NUM-GOTOS" } );
predicates->createPredicate( cd, "NEXT-NUM-GOTOS", StringVec( 2, "NUM-GOTOS" ) );
}
}
else if( compiler_type == "PL-CTRL" ){
predicates->createPredicates( d, cd, procedures, max_bound, total_instances, tests, empty_lines, endinstr, calls, instr, choiceInstr );
predicates->createTValuedPredicates( cd, "GOTO", gotos, procedures + 1, max_bound, max_bound + 1 );
}
else if( compiler_type == "TWO-STEPS-2" ){
predicates->createPredicates( d, cd, procedures, max_bound, total_instances, tests, empty_lines, endinstr, calls, instr, choiceInstr );
predicates->createTValuedPredicates( cd, "GOTO", gotos, procedures + 1, max_bound, max_bound + 1 );
predicates->createTValuedPredicates( d, cd, conds, procedures + 1, max_bound, 0, 0 );
StringVec empty_conds;
predicates->createValuedPredicates( cd, "EMPTY-COND", empty_conds, max_bound );
predicates->createPredicate( cd, "MODE-STRUCTURE" );
predicates->createPredicate( cd, "MODE-COND" );
}
else if( compiler_type == "OPT" ){
predicates->createPredicates( d, cd, procedures, max_bound, total_instances, tests, empty_lines, endinstr, calls, instr, choiceInstr );
predicates->createTValuedPredicates( cd, "GOTO", gotos, procedures + 1, max_bound, max_bound + 1 );
predicates->createTValuedPredicates( d, cd, conds, procedures + 1, max_bound, 0, 0 );
StringVec steps;
unsigned max_steps = 0;
for( unsigned t = 0; t < total_instances; t++ ){
max_steps = std::max( max_steps, (unsigned)ins[ t ]->goal.size() );
}
predicates->createValuedPredicates( cd, "STEP", steps, max_steps );
}
}
void add_cost_function(bool total_cost = true){
// Add total-cost
if( total_cost ){
cd->createFunction( "TOTAL-COST", -1 );
}
}
void add_derived_evaluation(){
derived_actions->createEvalActions( "DERIVED-EVALUATION" );
}
void add_program_actions(){
if( compiler_type == "PLPR" or compiler_type == "TWO-STEPS" or compiler_type == "PL-CTRL" or compiler_type == "TWO-STEPS-2" or compiler_type == "OPT" ){
sequential_actions->createProgrammingActions( procedures, bound, empty_lines, instr );
}
else if( compiler_type == "HFSC" or compiler_type == "NFA" or compiler_type == "NFA2" ){
sequential_actions->createFSCProgrammingActions( procedures, fsc_accumulator_preds, fsc_evaluating_preds, fsc_tacts, fsc_facts, true_no_act_preds, false_no_act_preds );
}
else if( compiler_type == "CFG" ){
sequential_actions->createProgrammingActions( procedures, bound, empty_lines, instr );
}
else if( compiler_type == "NEG" or compiler_type == "NEGLITE" ){
sequential_actions->createProgrammingActions( procedures, bound, empty_lines, instr );
}
else if( compiler_type == "LIFTED" ){
sequential_actions->createProgrammingActions( procedures, bound, empty_lines, instr );
}
}
void add_repeat_actions(){
if( compiler_type == "PLPR" or compiler_type == "TWO-STEPS" or compiler_type == "PL-CTRL" or compiler_type == "TWO-STEPS-2" or compiler_type == "OPT" ){
sequential_actions->createRepeatActions( procedures, max_bound, instr );
}
else if( compiler_type == "HFSC" or compiler_type == "NFA" or compiler_type == "NFA2" ){
sequential_actions->createRepeatFSCActions( procedures, fsc_accumulator_preds, fsc_evaluating_preds, fsc_action_preds, fsc_tacts, fsc_facts, true_no_act_preds, false_no_act_preds );
}
else if( compiler_type == "CFG" ){
sequential_actions->createRepeatActions( procedures, max_bound, instr );
}
else if( compiler_type == "NEG" or compiler_type == "NEGLITE" ){
sequential_actions->createRepeatNegActions( procedures, max_bound, instr );
}
else if( compiler_type == "LIFTED" ){
sequential_actions->createRepeatActions( procedures, max_bound, instr );
}
}
void add_program_goto_actions(){ // goto actions
if( compiler_type == "PLPR" or compiler_type == "OPT" ){
goto_actions->createProgrammingActions( procedures, bound, empty_lines, conds, gotos, empty_slots, noclasses );
}
else if( compiler_type == "HFSC" or compiler_type == "NFA" or compiler_type == "NFA2" ){
goto_actions->createFSCProgrammingActions( procedures, fsc_evaluating_preds, fsc_accumulator_preds, fsc_action_preds, fsc_noconds_preds, fsc_conds_preds, fsc_tgotos, fsc_fgotos );
}
else if( compiler_type == "CFG" ){}
else if( compiler_type == "NEG" or compiler_type == "NEGLITE" ){
goto_actions->createProgrammingActions( procedures, bound, empty_lines, conds, gotos, empty_slots, noclasses );
}
else if( compiler_type == "LIFTED" ){
goto_actions->createProgrammingActions( procedures, bound, empty_lines, conds, gotos, empty_slots, noclasses, false );
goto_actions->createProgrammingActions( procedures, bound, empty_lines, conds, neg_gotos, empty_slots, noclasses, true );
}
else if( compiler_type == "TWO-STEPS" ){
goto_actions->createProgrammingActionsNoConds( procedures, bound, empty_lines, gotos );
}
else if( compiler_type == "PL-CTRL" ){
goto_actions->createProgrammingActionsNoConds( procedures, bound, empty_lines, gotos );
}
else if( compiler_type == "TWO-STEPS-2" ){
goto_actions->createProgrammingActionsNoConds( procedures, bound, empty_lines, gotos );
goto_actions->createProgrammingActionConds( procedures, bound, conds, gotos );
}
}
void add_do_goto_actions(){
if( compiler_type == "PLPR" or compiler_type == "OPT" ){
goto_actions->createRepeatActions( procedures, max_bound, gotos, conds, noclasses );
}
else if( compiler_type == "HFSC" or compiler_type == "NFA" or compiler_type == "NFA2" ){
goto_actions->createFSCTrueGotoActions( procedures, fsc_evaluating_preds, fsc_accumulator_preds, fsc_action_preds, fsc_tgotos);
goto_actions->createFSCFalseGotoActions( procedures, fsc_evaluating_preds, fsc_accumulator_preds, fsc_action_preds, fsc_fgotos);
goto_actions->createFSCEvalConditions( procedures, fsc_evaluating_preds, fsc_accumulator_preds, fsc_action_preds, fsc_noconds_preds, fsc_conds_preds );
}
else if( compiler_type == "NEG" or compiler_type == "NEGLITE" ){
goto_actions->createRepeatActions( procedures, max_bound, gotos, conds, noclasses );
}
else if( compiler_type == "LIFTED" ){
goto_actions->createRepeatActions( procedures, max_bound, gotos, conds, noclasses, false );
goto_actions->createRepeatActions( procedures, max_bound, neg_gotos, conds, noclasses, true );
}
else if( compiler_type == "TWO-STEPS" ){
//goto_actions->evalGotoNoCond( procedures, max_bound );
goto_actions->createExecuteGotoActionsNoCond( procedures, max_bound, gotos );
}
else if( compiler_type == "PL-CTRL" ){
goto_actions->createExecuteGotoActionsNoCond( procedures, max_bound, gotos );
}
else if( compiler_type == "TWO-STEPS-2" ){
goto_actions->createRepeatActions( procedures, max_bound, gotos, conds );
if( to_program )
goto_actions->createExecuteGotoActionsNoCond( procedures, max_bound, gotos );
}
}
void add_program_end(){
if( compiler_type == "HFSC" or compiler_type == "NFA" or compiler_type == "NFA2" ){
terminal_actions->createFSCProgrammingActions( procedures, bound, endinstr, constant_states );
}
else{
terminal_actions->createProgrammingActions( procedures, bound, empty_lines, endinstr, allowed_lines );
}
}
void add_repeat_end_actions(){
if( compiler_type == "PLPR" or compiler_type == "TWO-STEPS" or compiler_type == "PL-CTRL" ){
terminal_actions->createRepeatActions( ins, procedures, total_instances, max_bound, endinstr, tests );
}
else if( compiler_type == "HFSC" or compiler_type == "NFA" or compiler_type == "NFA2" ){
if( compiler_type == "NFA2" )
terminal_actions->createFSCRepeatActions( ins, procedures, total_instances, bound, max_bound, constant_states, tests, endinstr, true );
terminal_actions->createFSCRepeatActions( ins, procedures, total_instances, bound, max_bound, constant_states, tests, endinstr );
}
else if( compiler_type == "CFG" ){
terminal_actions->createRepeatActions( ins, procedures, total_instances, max_bound, endinstr, tests );
}
else if( compiler_type == "NEG" or compiler_type == "NEGLITE" ){
terminal_actions->createRepeatActions( ins, procedures, total_instances, max_bound, endinstr, tests, negative_instances );
}
else if( compiler_type == "LIFTED" ){
terminal_actions->createRepeatActions( ins, procedures, total_instances, max_bound, endinstr, tests );
}
else if( compiler_type == "TWO-STEPS-2" ){
terminal_actions->createRepeatActions( ins, procedures, total_instances, max_bound, endinstr, tests, 0, false );
terminal_actions->createRepeatActions( ins, procedures, total_instances, max_bound, endinstr, tests, 0, true );
}
else if( compiler_type == "OPT" ){
terminal_actions->createNoGoalRepeatActions( ins, procedures, total_instances, max_bound, endinstr, tests );
}
}
void add_program_call_actions(){
if( compiler_type == "PLPR" ){
call_actions->createProgrammingActions( procedures, bound, empty_lines, calls );
}
else if( compiler_type == "HFSC" ){
call_actions->createFSCProgrammingActions( procedures, fsc_evaluating_preds, fsc_accumulator_preds, fsc_true_calls_preds, fsc_false_calls_preds );
}
else if( compiler_type == "CFG" ){
call_actions->createProgrammingActions( procedures, bound, empty_lines, calls );
}
else if( compiler_type == "NEG" ){
call_actions->createProgrammingActions( procedures, bound, empty_lines, calls );
}
else if( compiler_type == "LIFTED" ){
call_actions->createProgrammingActions( procedures, bound, empty_lines, calls );
}
else if( compiler_type == "TWO-STEPS" ){
}
else if( compiler_type == "PL-CTRL" ){
}
else if( compiler_type == "TWO-STEPS-2" ){
}
else if( compiler_type == "OPT" ){
}
else if( compiler_type == "NFA" or compiler_type == "NFA2" ){
}
}
void add_repeat_call_actions(){
if( compiler_type == "PLPR" ){
call_actions->createRepeatActions( procedures, max_bound, calls );
}
else if( compiler_type == "HFSC" ){
call_actions->createFSCRepeatActions( procedures, constant_states, fsc_accumulator_preds, fsc_evaluating_preds, fsc_action_preds, fsc_true_calls_preds, fsc_false_calls_preds );
}
else if( compiler_type == "CFG" ){
call_actions->createRepeatActions( procedures, max_bound, calls );
}
else if( compiler_type == "NEG" or compiler_type == "NEGLITE" ){
call_actions->createRepeatActions( procedures, max_bound, calls );
}
else if( compiler_type == "LIFTED" ){
call_actions->createRepeatActions( procedures, max_bound, calls );
}
else if( compiler_type == "TWO-STEPS" ){
}
else if( compiler_type == "PL-CTRL" ){
}
else if( compiler_type == "TWO-STEPS-2" ){
}
else if( compiler_type == "OPT" ){
}
else if( compiler_type == "NFA" or compiler_type == "NFA2" ){
}
}
void add_program_choice_actions(){
choice_actions->createProgrammingActions( choiceInstr, procedures, bound, empty_lines );
}
void add_repeat_choice_actions(){
choice_actions->createRepeatActions( choiceInstr, procedures, max_bound );
}
void add_program_cond_actions(){ // Cond actions
hl_cond_actions->createProgrammingActions( procedures, bound, noslots, empty_slots, hl_conds, constant_slots );
}
void add_step_conditions(){
hl_cond_actions->createDoStepsConditions( procedures, bound, noslots, constant_slots, constant_vars, novariables, hl_conds );
}
void add_eval_high_level_conditions(){
hl_cond_actions->evalHighLevelActions( procedures, bound, constant_slots, noslots, novariables );
}
void print_domain(){
//cd->PDDLPrint( std::cout );
cd->print( std::cout );
}
void print_instance(){
//cins->PDDLPrint( std::cerr );
cins->print( std::cerr );
}
void print_act2ins(){
// Print actions to instructions file
OFStream ofs( "act2ins.txt" );
for( MapStrSVec::iterator it = act2ins.begin(); it != act2ins.end(); it++){
ofs << it->first; // Action
for( String instruction : (it->second) ){
ofs << " " << instruction;
}
ofs << std::endl;
}
ofs.close();
}
void print_constants(){
OFStream ofs( "constants.txt" );
for( unsigned i = 0; i < cd->types.size(); i++ ){
for ( unsigned j = 0; j < cd->types[ i ]->constants.size(); ++j){
ofs << cd->types[ i ]->constants[ j ] << " " << cd->types[ i ]->getName() << std::endl;
}
}
ofs.close();
}
void clean_variables(){
delete cins;
delete cd;
for(unsigned i = 0; i < total_instances; i++)
delete ins[i];
delete d;
if( types )
delete types;
if( predicates )
delete predicates;
if( sequential_actions )
delete sequential_actions;
if( terminal_actions )
delete terminal_actions;
if( call_actions )
delete call_actions;
if( choice_actions )
delete choice_actions;
if( derived_actions )
delete derived_actions;
if( hl_cond_actions )
delete hl_cond_actions;
if( u_actions )
delete u_actions;
if( cfg_actions )
delete cfg_actions;
if( neg_actions )
delete neg_actions;
if( actions )
delete actions;
}
void planning_programs_compilation( ){
if( EVAL_DERIVED && !IS_HIGH_LEVEL )
add_derived_evaluation();
// PROGRAMMING ACTIONS
if( to_program ){
add_program_actions();
add_program_goto_actions();
if( !IS_UNSUPERVISED ){
add_program_end();
add_program_call_actions();
add_program_choice_actions();
}
if( IS_HIGH_LEVEL ){
add_program_cond_actions();
}
}
// REPEAT ACTIONS
add_repeat_actions();
add_do_goto_actions();
add_repeat_end_actions();
if( !IS_UNSUPERVISED ){
add_repeat_call_actions();
add_repeat_choice_actions();
}
if( IS_HIGH_LEVEL ){
add_step_conditions();
add_eval_high_level_conditions();
}
if( IS_UNSUPERVISED ){
u_actions->createRepeatActions( bound, noclasses, constant_programs );
}
}
void hierachical_fsc_compilation(){
if( to_program ){
add_program_actions();
add_program_end();
add_program_goto_actions();
add_program_call_actions();
}
add_repeat_actions();
add_do_goto_actions();
add_repeat_end_actions();
add_repeat_call_actions();
}
void nfa_compilation(){
if( to_program ){
add_program_actions();
add_program_goto_actions();
add_program_end();
}
add_repeat_actions();
add_do_goto_actions();
add_repeat_end_actions();
}
void cfg_compilation(){
if( to_program ){
add_program_actions();
add_program_end();
add_program_call_actions();
}
add_repeat_actions();
add_repeat_end_actions();
add_repeat_call_actions();
cfg_actions->createRepeatActions( procedures + 1, max_bound, allowed_lines, select_programs );
}
void neg_compilation(){
if( to_program ){
add_program_actions();
add_program_goto_actions();
add_program_end();
//add_program_call_actions();
}
add_repeat_actions();
add_do_goto_actions();
add_repeat_end_actions();
//add_repeat_call_actions();
neg_actions->createRepeatActions( ins, procedures, total_instances, max_bound, endinstr, tests, negative_instances );
if( compiler_type == "NEG" ){
neg_actions->createInfiniteDetectionActions( copiedPredicates, correctPredicates, ins, procedures, total_instances, max_bound, endinstr, tests, negative_instances );
}
}
void twosteps_compilation(){
if( to_program ){
add_program_actions();
add_program_goto_actions();
add_program_end();
}
add_repeat_actions();
add_do_goto_actions();
add_repeat_end_actions();
}
void plctrl_compilation(){
if( to_program ){
add_program_actions();
//add_program_goto_actions();
add_program_end();
}
add_repeat_actions();
add_do_goto_actions();
add_repeat_end_actions();
}
void programs_optimization( ){