-
Notifications
You must be signed in to change notification settings - Fork 2
/
hl_cond_actions.h
181 lines (147 loc) · 8.07 KB
/
hl_cond_actions.h
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
#ifndef HL_COND_H
#define HL_COND_H
class HLCondActions : public Actions{
public:
HLCondActions( parser::pddl::Domain *_od, parser::pddl::Domain *_cd ): Actions( _od, _cd ){}
void recursive_step_conditions( unsigned k, UIntVec & apars, const StringVec& constant_slots, const StringVec &constant_vars, unsigned novariables, const StringQVec& hl_conds ){
if ( k == 4 + od->preds[ apars[ 3 ] ]->params.size() ) {
// Step condition
SStream ss;
ss << "STEP-COND-" << od->preds[ apars[ 3 ] ]->name << "-" << apars[ 1 ] << "-" << apars[ 2 ] << "-" << apars[ 4 ];
for ( unsigned j = 5; j < 4 + od->preds[ apars[ 3 ] ]->params.size(); ++j )
ss << "-" << apars[ j ];
String name = ss.str();
StringVec pars = od->typeList( od->preds[ apars[ 3 ] ] );
pars.resize( 1 );
pars.insert( pars.begin(), "STACKROW" );
parser::pddl::Action * stepcond = createAction( name, pars );
addPrecondition( name, stack_top_pred, false, IntVec( 1, 0 ) );
addPrecondition( name, stack_procedures[ apars[ 1 ] ], false, IntVec( 1, 0 ) );
addPrecondition( name, stack_lines[ apars[ 2 ] ], false, IntVec( 1, 0 ) );
IntVec mypars( 1, 1 );
for ( unsigned j = 5; j < 4 + od->preds[ apars[ 3 ] ]->params.size(); ++j ){
mypars.push_back( cd->types.get( "CONDITION-VARIABLE" )->parseConstant( constant_vars[ apars[ j ] ] ).second );
}
addPrecondition( name, hl_conds[ apars[ 3 ] ][ apars[ 1 ] ][ apars[ 2 ] ][ apars[ 4 ] ], false, mypars );
addPrecondition( name, "CURRENT-SLOT", false, IntVec( 1, cd->constantIndex( constant_slots[ apars[ 4 ] - 1], "CONDITION-SLOT" ) ) );
if ( apars[ 4 ] == 1 )
addPrecondition( name, eval_pred, true );
addEffect( name, "CURRENT-SLOT", true, IntVec( 1, cd->constantIndex( constant_slots[ apars[ 4 ] - 1 ], "CONDITION-SLOT" ) ) );
addEffect( name, "CURRENT-SLOT", false, IntVec( 1, cd->constantIndex( constant_slots[ apars[ 4 ] ], "CONDITION-SLOT" ) ) );
parser::pddl::Forall * f = new parser::pddl::Forall;
f->params.insert( f->params.end(), novariables, cd->types.index( "VALUE" ) );
parser::pddl::When * w = new parser::pddl::When;
w->pars = new parser::pddl::And;
if ( 1 < apars[ 4 ] ) {
IntVec ppars = incvec( 0, novariables + 1 );
ppars[ 0 ] = cd->constantIndex( constant_slots[ apars[ 4 ] - 1 ], "CONDITION-SLOT" );
( ( parser::pddl::And * )w->pars )->add( cd->ground( "POSSIBLE-VALUE", ppars ) );
}
IntVec cpars( 1, 0 );
for ( unsigned j = 5; j < 4 + od->preds[ apars[ 3 ] ]->params.size(); ++j )
cpars.push_back( apars[ j ] );
( ( parser::pddl::And * )w->pars )->add( cd->ground( od->preds[ apars[ 3 ] ]->name, cpars ) );
w->cond = new parser::pddl::And;
IntVec ppars = incvec( 0, novariables + 1 );
ppars[ 0 ] = cd->constantIndex( constant_slots[ apars[ 4 ] ], "CONDITION-SLOT" );
( ( parser::pddl::And * )w->cond )->add( cd->ground( "POSSIBLE-VALUE", ppars ) );
// Use offset param in parser::pddl::Forall because of STACKROW param
w->addParams( 0 , 1 );
f->cond = w;
( ( parser::pddl::And * )stepcond->eff )->add( f );
addCost( name, 1 );
}
else for ( apars[ k ] = 1; apars[ k ] < novariables + 1; ++apars[ k ] )
recursive_step_conditions( k + 1, apars, constant_slots, constant_vars, novariables, hl_conds );
}
void createDoStepsConditions( unsigned procedures, unsigned lines, unsigned no_slots, StringVec& constant_slots, StringVec& constant_vars, unsigned novariables, StringQVec& hl_conds ){
UIntVec apars( 20, 0 ); // ( current_pos, p, k, *i, s, v1, v2, ... )
for( int eff : effs ){
apars[ 3 ] = eff;
for ( apars[ 1 ] = 0; apars[ 1 ] < procedures + 1; ++apars[ 1 ] ){
for ( apars[ 2 ] = 0; apars[ 2 ] < lines; ++apars[ 2 ] ){
for ( apars[ 4 ] = 1; apars[ 4 ] < no_slots + 1; ++apars[ 4 ] ){
recursive_step_conditions( 5, apars, constant_slots, constant_vars, novariables, hl_conds );
}
}
}
}
}
void createProgrammingActions( unsigned procedures, unsigned lines, unsigned no_slots, const StringVec &empty_slots, const StringQVec &hl_conds, const StringVec &constant_slots ){
for ( int eff : effs ){
for ( unsigned j = 0; j < lines; ++j ){
for ( unsigned s = 1; s < no_slots + 1; ++s ) {
// Program cond
SStream ss;
ss << "PROGRAM-COND-" << od->preds[ eff ]->name << "-" << j << "-" << s;
String name = ss.str();
StringVec parameters = od->typeList( od->preds[ eff ] );
for ( unsigned k = 1; k < parameters.size(); ++k )
parameters[ k ] = "CONDITION-VARIABLE";
parameters.push_back( "STACKROW" );
createAction( name, parameters );
addPrecondition( name, empty_slots[ j ], false, IntVec( 1, cd->constantIndex( constant_slots[ s ], "CONDITION-SLOT" ) ) );
addPrecondition( name, "CURRENT-SLOT", false, IntVec( 1, cd->constantIndex( constant_slots[ s - 1 ], "CONDITION-SLOT" ) ) );
addPrecondition( name, stack_top_pred, false, IntVec( 1, int( parameters.size() ) - 1 ) );
addPrecondition( name, stack_procedures[ procedures ], false, IntVec( 1, int( parameters.size() ) - 1 ) );
addPrecondition( name, stack_lines[ j ], false, IntVec( 1, int( parameters.size() ) - 1 ) );
//[BUG] Add arity (possible bug)
if( name.find( "ARY-" ) != String::npos ){
if( !LIFTED_INSTR ){
SStream ordinal;
ordinal << "ORDINAL-" << parameters.size() - 2 << "ARY";
addPrecondition( name, ordinal.str(), false, IntVec( 1, 0 ) );
}
else{
addPrecondition( name, "ARITY-" + tostr( parameters.size()-2 ), false, IntVec( 1, 0 ) );
}
}
addEffect( name, empty_slots[ j ], true, IntVec( 1, cd->constantIndex( constant_slots[ s ], "CONDITION-SLOT" ) ) );
addEffect( name, hl_conds[ eff ][ procedures ][ j ][ s ], false, incvec( 0, int( parameters.size() ) - 1 ) );
addCost( name, 1 );
addActionToInstruction( name, hl_conds[ eff ][ procedures ][ j ][ s ] );
}
}
}
}
void evalHighLevelActions( unsigned procedures, unsigned lines, StringVec& constant_slots, unsigned no_slots, unsigned no_variables ){
for ( unsigned p = 0; p < procedures + 1; p++ ){
for ( unsigned k = 0; k < lines; ++k ) {
// Evaluate true condition
SStream ss;
ss << "EVAL-COND-" << p << "-" << k;
String name = ss.str();
parser::pddl::Action * evalcond = createAction( name , StringVec( 1, "STACKROW" ) );
addPrecondition( name, stack_top_pred, false, IntVec( 1, 0) );
addPrecondition( name, stack_procedures[ p ], false, IntVec( 1, 0 ) );
addPrecondition( name, stack_lines[ k ], false, IntVec( 1, 0 ) );
addPrecondition( name, "CURRENT-SLOT", false, IntVec( 1, cd->constantIndex( constant_slots[ no_slots ], "CONDITION-SLOT" ) ) );
addEffect( name, eval_pred );
parser::pddl::Forall * f = new parser::pddl::Forall;
f->params.insert( f->params.end(), no_variables, cd->types.index( "VALUE" ) );
parser::pddl::When * w = new parser::pddl::When;
IntVec ppars = incvec( 1, no_variables + 1);
ppars.insert( ppars.begin(), cd->constantIndex( constant_slots[ no_slots ], "CONDITION-SLOT" ) );
w->pars = cd->ground( "POSSIBLE-VALUE", ppars );
w->cond = new parser::pddl::And;
( ( parser::pddl::And * )w->cond )->add( cd->ground( accu_pred ) );
( ( parser::pddl::And * )w->cond )->add( new parser::pddl::Not( cd->ground( "POSSIBLE-VALUE", ppars ) ) );
f->cond = w;
( ( parser::pddl::And * )evalcond->eff )->add( f );
// delete everything about slots !!!
addEffect( name, "CURRENT-SLOT", true, IntVec( 1, cd->constantIndex( constant_slots[ no_slots ], "CONDITION-SLOT" ) ) );
addEffect( name, "CURRENT-SLOT", false, IntVec( 1, cd->constantIndex( constant_slots[ 0 ], "CONDITION-SLOT" ) ) );
f = new parser::pddl::Forall;
f->params.insert( f->params.end(), no_variables, cd->types.index( "VALUE" ) );
f->cond = new parser::pddl::And;
for ( unsigned s = 1; s < no_slots; ++s ) {
ppars[ 0 ] = cd->constantIndex( constant_slots[ s ], "CONDITION-SLOT" );
( ( parser::pddl::And * )f->cond )->add( new parser::pddl::Not( cd->ground( "POSSIBLE-VALUE", ppars ) ) );
}
( ( parser::pddl::And * )evalcond->eff )->add( f );
addCost( name, 1 );
}
}
}
};
#endif