-
Notifications
You must be signed in to change notification settings - Fork 6
/
Instance.h
294 lines (246 loc) · 7.54 KB
/
Instance.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
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
#ifndef _INSTANCE_H_
#define _INSTANCE_H_
#include "Domain.h"
class Instance {
public:
Domain &d;
std::string name;
GroundVec init, goal; // initial and goal states
TaskVec tgoal; //goal for shop instances
bool metric, shop;
Instance( Domain & dom )
: d( dom ), metric( false ), shop( false ) {}
Instance( Domain & dom, const std::string & s, bool htn = false )
: d( dom ), metric( false ), shop( htn ) {
if(shop) SHOPparse(s);
else parse(s);
}
~Instance() {
for ( unsigned i = 0; i < init.size(); ++i )
delete init[i];
for ( unsigned i = 0; i < goal.size(); ++i )
delete goal[i];
for ( unsigned i = 0; i < tgoal.size(); ++i )
delete tgoal[i];
}
void parse( const std::string &s) {
Filereader f( s );
name = f.parseName( "PROBLEM" );
if ( DOMAIN_DEBUG ) std::cout << name << "\n";
for ( ; f.getChar() != ')'; f.next() ) {
f.assert( "(" );
f.assert( ":" );
std::string t = f.getToken();
if ( DOMAIN_DEBUG ) std::cout << t << "\n";
if ( t == "DOMAIN" ) parseDomain( f );
else if ( t == "OBJECTS" ) parseObjects( f );
else if ( t == "INIT" ) parseInit( f );
else if ( t == "GOAL" ) parseGoal( f );
else if ( t == "METRIC" ) parseMetric( f );
else f.tokenExit( t );
}
}
void SHOPparse( const std::string &s) {
Filereader f( s );
name = f.parseHTNProblemName();
f.next();
if ( DOMAIN_DEBUG ) std::cout << name << "\n";
for ( ; f.getChar() != ')'; f.next() ) {
parseInit( f );
parseGoal( f );
}
}
void parseDomain( Filereader & f ) {
f.next();
f.assert( d.name );
f.assert( ")" );
}
void parseObjects( Filereader & f ) {
TokenStruct< std::string > ts = f.parseTypedList( true, d.types );
for ( unsigned i = 0; i < ts.size(); ++i ) {
Type * type = d.getType( ts.types[i] );
std::pair< bool, unsigned > pair = type->parseObject( ts[i] );
if ( pair.first == false )
type->objects.insert( ts[i] );
}
for ( unsigned i = 0; DOMAIN_DEBUG && i < d.types.size(); ++i ) {
std::cout << " ";
if ( d.typed ) std::cout << " " << d.types[i] << ":";
for ( unsigned j = 0; j < d.types[i]->objects.size(); ++j )
std::cout << " " << d.types[i]->objects[j];
std::cout << "\n";
}
}
void parseGround( Filereader & f, GroundVec & v ) {
if ( shop ) {
Ground * cs = 0;
cs = new Ground( d.preds.get( f.getToken( d.preds ) ) );
cs->SHOPparse( f, d.types[0]->constants, d );
v.push_back( cs );
}
else {
TypeGround * c = 0;
if ( f.getChar() == '=' && !shop) {
f.assert( "=" );
f.assert( "(" );
std::string s = f.getToken();
int i = d.funcs.index( s );
if ( i < 0 ) f.tokenExit( s );
if ( d.funcs[i]->returnType < 0 ) c = new GroundFunc< double >( d.funcs[i] );
else c = new GroundFunc< int >( d.funcs[i] );
}
else c = new TypeGround( d.preds.get( f.getToken( d.preds ) ) );
c->parse( f, d.types[0]->constants, d );
v.push_back( c );
}
}
void parseTasks( Filereader & f, TaskVec & v ) {
Task * t = 0;
t = new Task( d.tasks.get( f.getToken( d.tasks ) ) );
t->SHOPparse( f, d.types[0]->constants, d );
v.push_back( t );
}
void parseInit( Filereader & f ) {
for ( f.next(); f.getChar() != ')'; f.next() ) {
f.assert( "(" );
parseGround( f, init );
}
++f.c;
for ( unsigned i = 0; DOMAIN_DEBUG && i < init.size(); ++i )
std::cout << " " << init[i];
}
void parseGoal( Filereader & f ) {
f.next();
f.assert( "(" );
if ( shop ) {
for ( f.next(); f.getChar() != ')'; f.next() ) {
f.assert( "(" );
parseTasks( f, tgoal );
}
++f.c;
}
else
{
std::string s = f.getToken();
if ( s == "AND" ) {
for ( f.next(); f.getChar() != ')'; f.next() ) {
f.assert( "(" );
parseGround( f, goal );
}
++f.c;
f.next();
}
else {
f.c -= s.size();
parseGround( f, goal );
}
f.assert( ")" );
}
for ( unsigned i = 0; DOMAIN_DEBUG && i < goal.size(); ++i )
std::cout << " " << goal[i];
}
// for the moment only parse total-cost/total-time
void parseMetric( Filereader & f ) {
if ( !d.temp && !d.costs ) {
std::cerr << "METRIC only defined for temporal actions or actions with costs!\n";
std::exit( 1 );
}
metric = true;
f.next();
f.assert( "MINIMIZE" );
f.assert( "(" );
if ( d.temp ) f.assert( "TOTAL-TIME" );
else f.assert( "TOTAL-COST" );
f.assert( ")" );
f.assert( ")" );
}
// add an object of a certain type
void addObject( const std::string & name, const std::string & type ) {
d.getType( type )->objects.insert( name );
}
// add a predicate fluent to the initial state
void addInit( const std::string & name, const StringVec & v = StringVec() ) {
TypeGround * tg = new TypeGround( d.preds.get( name ) );
tg->insert( d, v );
init.push_back( tg );
}
// add a function value to the initial state
void addInit( const std::string & name, int value, const StringVec & v = StringVec() ) {
GroundFunc< int > * gf = new GroundFunc< int >( d.funcs.get( name ), value );
gf->insert( d, v );
init.push_back( gf );
}
// add a function value to the initial state
void addInit( const std::string & name, double value, const StringVec & v = StringVec() ) {
GroundFunc< double > * gf = new GroundFunc< double >( d.funcs.get( name ), value );
gf->insert( d, v );
init.push_back( gf );
}
// add a fluent (predicate or function) to the initial state
void addInit( TypeGround * g, const StringVec & v = StringVec() ) {
TypeGround * tg = 0;
GroundFunc< int > * f1 = dynamic_cast< GroundFunc< int > * >( g );
GroundFunc< double > * f2 = dynamic_cast< GroundFunc< double > * >( g );
if ( f1 ) tg = new GroundFunc< int >( d.funcs.get( g->name ), f1->value );
else if ( f2 ) tg = new GroundFunc< double >( d.funcs.get( g->name ), f2->value );
else tg = new TypeGround( d.preds.get( g->name ) );
tg->insert( d, v );
init.push_back( tg );
}
// add a fluent to the goal state
void addGoal( const std::string & name, const StringVec & v = StringVec() ) {
TypeGround * tg = new TypeGround( d.preds.get( name ) );
tg->insert( d, v );
goal.push_back( tg );
}
void PDDLPrint( std::ostream & stream ) {
stream << "( DEFINE ( PROBLEM " << name << " )\n";
stream << "( :DOMAIN " << d.name << " )\n";
stream << "( :OBJECTS\n";
for ( unsigned i = 0; i < d.types.size(); ++i )
if ( d.types[i]->objects.size() ) {
stream << "\t";
for ( unsigned j = 0; j < d.types[i]->objects.size(); ++j )
stream << d.types[i]->objects[j] << " ";
if ( d.typed ) stream << "- " << d.types[i]->name;
stream << "\n";
}
stream << ")\n";
stream << "( :INIT\n";
for ( unsigned i = 0; i < init.size(); ++i ) {
( (TypeGround*)init[i])->PDDLPrint( stream, 1, TokenStruct< std::string >(), d );
stream << "\n";
}
stream << ")\n";
stream << "( :GOAL\n";
stream << "\t( AND\n";
for ( unsigned i = 0; i < goal.size(); ++i ) {
( (TypeGround*)goal[i])->PDDLPrint( stream, 2, TokenStruct< std::string >(), d );
stream << "\n";
}
stream << "\t)\n";
stream << ")\n";
if ( metric ) {
stream << "( :METRIC MINIMIZE ( TOTAL-";
if ( d.temp ) stream << "TIME";
else stream << "COST";
stream << " ) )\n";
}
stream << ")\n";
}
//Print SHOP problem
void SHOPPrint( std::ostream & stream ) {
stream << "( DEFPROBLEM PROBLEM " << name << " (\n";
for ( unsigned i = 0; i < init.size(); ++i ) {
init[i]->PDDLPrint( stream, 1, TokenStruct< std::string >(), d );
stream << "\n";
}
stream << ")\n(\n";
for ( unsigned i = 0; i < tgoal.size(); ++i ) {
tgoal[i]->SHOPPrint( stream, 1, TokenStruct< std::string >(), d );
stream << "\n";
}
stream << ") ) )\n";
}
};
#endif