-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtn_domain.cpp
More file actions
826 lines (753 loc) · 24.5 KB
/
htn_domain.cpp
File metadata and controls
826 lines (753 loc) · 24.5 KB
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
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <cassert>
#include <set>
#include <tr1/memory>
#include <algorithm>
#include "exception.hpp"
#include "funcs.hpp"
#include "term.hpp"
#include "term_string.hpp"
#include "term_constant.hpp"
#include "term_variable.hpp"
#include "type_table.hpp"
#include "formula.hpp"
#include "formula_pred.hpp"
#include "formula_neg.hpp"
#include "formula_conj.hpp"
#include "operator.hpp"
#include "htn_task_head.hpp"
#include "htn_method.hpp"
#include "htn_domain.hpp"
/** \file htn_domain.hpp
* Declaration of the HtnDomain class.
*/
/** \file htn_domain.cpp
* Definition of the HtnDomain class.
*/
/** \class HtnDomain
* An Ordered Task Decomposition description of a planning domain.
*/
/** \var HtnDomain::m_vOperators
* A list of pointers to the planning operators in this domain.
* These need to be deallocated with the HtnDomain.
*/
/** \var HtnDomain::m_vMethods
* A list of pointers to the decomposition methods in this domain.
* These need to be deallocated with the HtnDomain.
*/
/** \var HtnDomain::m_sDomainName
* The name of this domain.
*/
/** \var HtnDomain::m_iRequirements
* The PDDL requirements of this domain.
* See the constants defined in funcs.hpp.
*/
/** \var HtnDomain::m_sAllowableTypes
* A list of types that may be used in this domain.
* If non-empty, all terms in the domain must be of one of these types.
* Otherwise, any types (or no typing) are allowed.
*/
/** \var HtnDomain::m_ConstantTypes
* A list of constants in the domain with their types.
* If non-empty, only these constants may be used in problems for this domain.
* Otherwise, any may be used and they will be untyped.
*/
/** \var HtnDomain::m_vAllowablePredicates
* A list of allowable predicate symbols with the number and types of their
* arguments.
* If non-empty, only these predicate forms may be used in the domain.
* Otherwise, any may be used.
*/
/**
* Retrieve a pointer to a new HtnDomain from its PDDL representation.
* \param p_sInput INOUT A stream containing a textual description of the
* domain in the PDDL language. The stream is advanced beyond this.
* \return A pointer to a new HtnDomain based on the text. The caller is
* responsible for deallocating it.
*/
HtnDomain * HtnDomain::FromPddl( std::stringstream & p_sInput )
{
HtnDomain * l_pRet = new HtnDomain();
l_pRet->m_iRequirements = PDDL_REQ_STRIPS | PDDL_REQ_HTN;
EatWhitespace( p_sInput );
EatString( p_sInput, "(" );
EatWhitespace( p_sInput );
EatString( p_sInput, "define" );
EatWhitespace( p_sInput );
EatString( p_sInput, "(" );
EatWhitespace( p_sInput );
EatString( p_sInput, "domain" );
EatWhitespace( p_sInput );
l_pRet->m_sDomainName = ReadString( p_sInput );
EatWhitespace( p_sInput );
EatString( p_sInput, ")" );
EatWhitespace( p_sInput );
bool l_bHasRequirements = false,
l_bHasTypes = false,
l_bHasConstants = false,
l_bHasPredicates = false,
l_bHasActions = false;
while( p_sInput.peek() == '(' )
{
std::string l_sFeature = ReadParenthetical( p_sInput );
std::stringstream l_sFeatureStream( l_sFeature );
EatString( l_sFeatureStream, "(" );
EatWhitespace( l_sFeatureStream );
std::string l_sFeatureName = ReadString( l_sFeatureStream );
if( CompareNoCase( l_sFeatureName, ":action" ) == 0 )
{
l_bHasActions = true;
l_sFeatureStream.seekg( 0, std::ios::beg );
l_pRet->m_vOperators.push_back( Operator::FromPddl( l_sFeatureStream, l_pRet->m_sAllowableTypes, l_pRet->m_vAllowablePredicates ) );
}
else if( CompareNoCase( l_sFeatureName, ":requirements" ) == 0 )
{
if( l_bHasRequirements )
throw Exception( E_NOT_IMPLEMENTED,
"A PDDL domain may not have multiple requirements blocks.",
__FILE__,
__LINE__ );
if( l_bHasTypes )
throw Exception( E_NOT_IMPLEMENTED,
"The PDDL requirements block must come before the types block.",
__FILE__,
__LINE__ );
if( l_bHasConstants )
throw Exception( E_NOT_IMPLEMENTED,
"The PDDL requirements block must come before the constants block.",
__FILE__,
__LINE__ );
if( l_bHasPredicates )
throw Exception( E_NOT_IMPLEMENTED,
"The PDDL requirements block must come before the predicates block.",
__FILE__,
__LINE__ );
if( l_bHasActions )
throw Exception( E_NOT_IMPLEMENTED,
"The PDDL requirements block must come before any actions.",
__FILE__,
__LINE__ );
l_bHasRequirements = true;
l_pRet->m_iRequirements = ParseRequirements( l_sFeature, true );
}
else if( CompareNoCase( l_sFeatureName, ":types" ) == 0 )
{
if( l_bHasTypes )
throw Exception( E_NOT_IMPLEMENTED,
"A PDDL domain may not have multiple types blocks.",
__FILE__,
__LINE__ );
if( l_bHasConstants )
throw Exception( E_NOT_IMPLEMENTED,
"The PDDL types block must come before the constants block.",
__FILE__,
__LINE__ );
if( l_bHasPredicates )
throw Exception( E_NOT_IMPLEMENTED,
"The PDDL types block must come before the predicates block.",
__FILE__,
__LINE__ );
if( l_bHasActions )
throw Exception( E_NOT_IMPLEMENTED,
"The PDDL types block must come before any actions.",
__FILE__,
__LINE__ );
if( !( l_pRet->m_iRequirements & PDDL_REQ_TYPING ) )
throw Exception( E_NOT_IMPLEMENTED,
"The PDDL types block requires the PDDL typing requirement.",
__FILE__,
__LINE__ );
l_bHasTypes = true;
EatWhitespace( l_sFeatureStream );
while( l_sFeatureStream.peek() != ')' )
{
std::string l_sNewType = ReadString( l_sFeatureStream );
if( l_pRet->m_sAllowableTypes.find( l_sNewType ) == l_pRet->m_sAllowableTypes.end() )
{
l_pRet->m_sAllowableTypes.insert( l_sNewType );
}
EatWhitespace( l_sFeatureStream );
}
}
else if( CompareNoCase( l_sFeatureName, ":constants" ) == 0 )
{
if( l_bHasConstants )
throw Exception( E_NOT_IMPLEMENTED,
"A PDDL domain may not have multiple constants blocks.",
__FILE__,
__LINE__ );
if( l_bHasPredicates )
throw Exception( E_NOT_IMPLEMENTED,
"The PDDL constants block must come before the predicates block.",
__FILE__,
__LINE__ );
if( l_bHasActions )
throw Exception( E_NOT_IMPLEMENTED,
"The PDDL constants block must come before any actions.",
__FILE__,
__LINE__ );
l_bHasConstants = true;
EatWhitespace( l_sFeatureStream );
while( l_sFeatureStream.peek() != ')' )
{
std::string l_sConstant = ReadString( l_sFeatureStream );
EatWhitespace( l_sFeatureStream );
std::string l_sTyping = "";
if( l_pRet->m_iRequirements & PDDL_REQ_TYPING )
{
EatString( l_sFeatureStream, "-" );
EatWhitespace( l_sFeatureStream );
l_sTyping = ReadString( l_sFeatureStream );
EatWhitespace( l_sFeatureStream );
}
if( l_pRet->m_ConstantTypes.find( l_sConstant ) == l_pRet->m_ConstantTypes.end() )
l_pRet->m_ConstantTypes[l_sConstant] = l_sTyping;
else
throw Exception( E_NOT_IMPLEMENTED,
"A constant may not be declared twice.",
__FILE__,
__LINE__ );
}
}
else if( CompareNoCase( l_sFeatureName, ":predicates" ) == 0 )
{
if( l_bHasPredicates )
throw Exception( E_NOT_IMPLEMENTED,
"A PDDL domain may not have multiple predicates blocks.",
__FILE__,
__LINE__ );
if( l_bHasActions )
throw Exception( E_NOT_IMPLEMENTED,
"The PDDL predicates block must come before any actions.",
__FILE__,
__LINE__ );
l_bHasPredicates = true;
EatWhitespace( l_sFeatureStream );
while( l_sFeatureStream.peek() != ')' )
{
TypeTable l_TempTypes;
std::string l_sNewPred = "";
EatString( l_sFeatureStream, "(" );
EatWhitespace( l_sFeatureStream );
std::string l_sRelation = ReadString( l_sFeatureStream );
l_sNewPred += "( " + l_sRelation;
EatWhitespace( l_sFeatureStream );
while( l_sFeatureStream.peek() != ')' )
{
std::string l_sVar = ReadString( l_sFeatureStream );
EatWhitespace( l_sFeatureStream );
l_sNewPred += " " + l_sVar;
if( l_pRet->m_iRequirements & PDDL_REQ_TYPING )
{
EatString( l_sFeatureStream, "-" );
EatWhitespace( l_sFeatureStream );
std::string l_sTyping = ReadString( l_sFeatureStream );
EatWhitespace( l_sFeatureStream );
l_TempTypes[l_sVar] = l_sTyping;
if( l_pRet->m_sAllowableTypes.size() != 0 )
{
if( l_pRet->m_sAllowableTypes.find( l_sTyping ) == l_pRet->m_sAllowableTypes.end() )
throw Exception( E_NOT_IMPLEMENTED,
"An undeclared type was used in a predicate declaration.",
__FILE__,
__LINE__ );
}
}
}
EatString( l_sFeatureStream, ")" );
EatWhitespace( l_sFeatureStream );
l_sNewPred += " )";
for( unsigned int i = 0; i < l_pRet->m_vAllowablePredicates.size(); i++ )
{
if( CompareNoCase( l_pRet->m_vAllowablePredicates[i].GetRelation(),
l_sRelation ) == 0 )
throw Exception( E_NOT_IMPLEMENTED,
"A predicate was declared twice.",
__FILE__,
__LINE__ );
}
l_pRet->m_vAllowablePredicates.push_back( FormulaPred( l_sNewPred, l_TempTypes, std::vector< FormulaPred >() ) );
}
}
else if( CompareNoCase( l_sFeatureName, ":functions" ) == 0 )
{
throw Exception( E_NOT_IMPLEMENTED,
"Functions are not supported.",
__FILE__,
__LINE__ );
}
else if( CompareNoCase( l_sFeatureName, ":constraints" ) == 0 )
{
throw Exception( E_NOT_IMPLEMENTED,
"Constraints are not supported.",
__FILE__,
__LINE__ );
}
else if( CompareNoCase( l_sFeatureName, ":method" ) == 0 )
{
l_bHasActions = true;
l_sFeatureStream.seekg( 0, std::ios::beg );
l_pRet->m_vMethods.push_back( HtnMethod::FromPddl( l_sFeatureStream, l_pRet->m_sAllowableTypes, l_pRet->m_vAllowablePredicates, l_pRet->m_iRequirements ) );
}
else
{
std::string l_sMessage;
l_sMessage += "Unrecognized PDDL feature: ";
l_sMessage += l_sFeature;
throw Exception( E_NOT_IMPLEMENTED,
l_sMessage,
__FILE__,
__LINE__ );
}
EatWhitespace( p_sInput );
}
EatString( p_sInput, ")" );
if( ( l_pRet->m_iRequirements & PDDL_REQ_TYPING ) && !l_bHasTypes )
throw Exception( E_NOT_IMPLEMENTED,
"The pddl typing requirement means that you must have a pddl types block.",
__FILE__,
__LINE__ );
return l_pRet;
}
/**
* Retrieve a pointer to a new HtnDomain created from its SHOP representation.
* \param p_sInput INOUT A stream containing a textual description of the
* domain in the SHOP formalism. The stream is advanced beyond this.
* \return A pointer to a new HtnDomain created from the text. The caller is
* responsible for deallocating it.
*/
HtnDomain * HtnDomain::FromShop( std::stringstream & p_sInput )
{
HtnDomain * l_pRet = new HtnDomain();
EatWhitespace( p_sInput );
EatString( p_sInput, "(" );
EatWhitespace( p_sInput );
EatString( p_sInput, "defdomain" );
EatWhitespace( p_sInput );
l_pRet->m_sDomainName = ReadString( p_sInput );
EatWhitespace( p_sInput );
EatString( p_sInput, "(" );
EatWhitespace( p_sInput );
while( p_sInput.peek() == '(' )
{
std::streampos l_CurPos = p_sInput.tellg();
EatString( p_sInput, "(" );
EatWhitespace( p_sInput );
EatString( p_sInput, ":" );
if( p_sInput.peek() == 'o' || p_sInput.peek() == 'O' )
{
p_sInput.seekg( l_CurPos );
l_pRet->m_vOperators.push_back( Operator::FromShop( p_sInput ) );
}
else if( p_sInput.peek() == 'm' || p_sInput.peek() == 'M' )
{
p_sInput.seekg( l_CurPos );
l_pRet->m_vMethods.push_back( HtnMethod::FromShop( p_sInput ) );
}
else
{
assert( false );
}
EatWhitespace( p_sInput );
}
assert( p_sInput.peek() == ')' );
l_pRet->m_iRequirements = PDDL_REQ_STRIPS | PDDL_REQ_HTN;
return l_pRet;
}
/**
* Construct a default HtnDomain.
* This exists only to be called from HtnDomain::FromPddl() and
* HtnDomain::FromShop().
*/
HtnDomain::HtnDomain()
{
}
/**
* Construct an HtnDomain as a copy of an existing one.
* \param p_Other IN The HtnDomain to copy.
*/
HtnDomain::HtnDomain( const HtnDomain & p_Other )
: m_sAllowableTypes( p_Other.m_sAllowableTypes ),
m_ConstantTypes( p_Other.m_ConstantTypes ),
m_vAllowablePredicates( p_Other.m_vAllowablePredicates )
{
for( unsigned int i = 0; i < p_Other.m_vMethods.size(); i++ )
m_vMethods.push_back( new HtnMethod( *p_Other.m_vMethods[i] ) );
for( unsigned int i = 0; i < p_Other.m_vOperators.size(); i++ )
m_vOperators.push_back( new Operator( *p_Other.m_vOperators[i] ) );
m_sDomainName = p_Other.m_sDomainName;
m_iRequirements = p_Other.m_iRequirements;
}
/**
* Destruct an HtnDomain.
*/
HtnDomain::~HtnDomain()
{
for( unsigned int i = 0; i < m_vMethods.size(); i++ )
delete m_vMethods[i];
for( unsigned int i = 0; i < m_vOperators.size(); i++ )
delete m_vOperators[i];
}
/**
* Add a new planning Operator to the domain.
* \param p_pNewOper IN A pointer to the Operator to add. The caller
* relinquishes control over this pointer.
* \todo This should probably check to make sure the operator is not already
* in the list.
*/
void HtnDomain::AddOperator( Operator * p_pNewOper )
{
m_vOperators.push_back( p_pNewOper );
}
/**
* Add a new decomposition HtnMethod to the domain.
* \param p_pNewMethod IN A pointer to the HtnMethod to add. The caller
* relinquishes control over this pointer.
* \todo This should probably check to make sure the method is not already
* in the list.
*/
void HtnDomain::AddMethod( HtnMethod * p_pNewMethod )
{
m_vMethods.push_back( p_pNewMethod );
}
/**
* Remove an operator from the domain.
* This will potentially invalidate any stored indexes.
* \param p_iIndex IN The 0-based index of the operator to remove.
*/
void HtnDomain::RemoveOperator( unsigned int p_iIndex )
{
if( p_iIndex < m_vOperators.size() )
{
std::vector<Operator *>::iterator l_Iter = m_vOperators.begin();
for( unsigned int i = 0; i < p_iIndex; i++ )
l_Iter++;
delete *l_Iter;
m_vOperators.erase( l_Iter );
}
else
throw Exception( E_INDEX_OUT_OF_BOUNDS,
"Bounds error.",
__FILE__,
__LINE__ );
}
/**
* Remove a method from the domain.
* This will potentially invalidate any stored indexes.
* \param p_iIndex IN The 0-based index of the method to remove.
*/
void HtnDomain::RemoveMethod( unsigned int p_iIndex )
{
if( p_iIndex < m_vMethods.size() )
{
std::vector<HtnMethod *>::iterator l_Iter = m_vMethods.begin();
for( unsigned int i = 0; i < p_iIndex; i++ )
l_Iter++;
delete *l_Iter;
m_vMethods.erase( l_Iter );
}
else
throw Exception( E_INDEX_OUT_OF_BOUNDS,
"Bounds error.",
__FILE__,
__LINE__ );
}
/**
* Replace a method in the domain with a new one.
* \param p_iIndex IN The index of the method that will be replaced.
* \param p_pNewMethod IN A pointer to the method to be inserted. The caller
* relinquishes control of this pointer.
*/
void HtnDomain::ReplaceMethod( unsigned int p_iIndex, HtnMethod * p_pNewMethod )
{
delete m_vMethods[p_iIndex];
m_vMethods[p_iIndex] = p_pNewMethod;
}
/**
* Retrieve the number of operators in the domain.
*/
unsigned int HtnDomain::GetNumOperators() const
{
return m_vOperators.size();
}
/**
* Retrieve the number of methods in the domain.
*/
unsigned int HtnDomain::GetNumMethods() const
{
return m_vMethods.size();
}
/**
* Retrieve a pointer to one of the operators in the domain.
* \param p_iIndex IN The 0-based index of the requested operator.
* \return A pointer to the requested operator. This pointer has the same
* lifetime as this HtnDomain.
*/
const Operator * HtnDomain::GetCOperator( unsigned int p_iIndex ) const
{
if( p_iIndex < m_vOperators.size() )
return m_vOperators[ p_iIndex ];
else
throw Exception( E_INDEX_OUT_OF_BOUNDS,
"Bounds error.",
__FILE__,
__LINE__ );
}
/**
* Retrieve a pointer to one of the methods in this domain.
* \param p_iIndex IN The 0-based index of the requested method.
* \return A pointer to the requested method. This pointer has the same
* lifetime as this HtnDomain.
*/
const HtnMethod * HtnDomain::GetCMethod( unsigned int p_iIndex ) const
{
if( p_iIndex < m_vMethods.size() )
return m_vMethods[ p_iIndex ];
else
throw Exception( E_INDEX_OUT_OF_BOUNDS,
"Bounds error.",
__FILE__,
__LINE__ );
}
/**
* Retrieve the name of the domain.
*/
std::string HtnDomain::GetDomainName() const
{
return m_sDomainName;
}
/**
* Retrieve a string containing the SHOP representation of this domain.
* \return A string containing the SHOP representation of this domain.
* \todo Change the name of this function to ToShop.
*/
std::string HtnDomain::ToStr() const
{
std::string l_sRet;
l_sRet += "( defdomain ";
l_sRet += m_sDomainName;
l_sRet += "\n (\n";
for( unsigned int i = 0; i < m_vOperators.size(); i++ )
{
l_sRet += m_vOperators[i]->ToStr( true, 4 );
}
for( unsigned int i = 0; i < m_vMethods.size(); i++ )
{
l_sRet += m_vMethods[i]->ToStr();
}
l_sRet += " )\n)\n";
return l_sRet;
}
/**
* Retrieve a string containing the PDDL representation of this domain.
* \return A string containing the PDDL representation of this domain.
*/
std::string HtnDomain::ToPddl() const
{
std::string l_sRet;
l_sRet += "( define ( domain " + m_sDomainName + " )\n";
l_sRet += " " + PrintRequirements( m_iRequirements ) + "\n";
if( m_iRequirements & PDDL_REQ_TYPING )
{
l_sRet += " ( :types ";
for( std::set< std::string, StrLessNoCase >::const_iterator l_SetIter = m_sAllowableTypes.begin(); l_SetIter != m_sAllowableTypes.end(); l_SetIter++ )
l_sRet += *l_SetIter + " ";
l_sRet += ")\n";
}
if( m_ConstantTypes.size() > 0 )
{
l_sRet += " ( :constants\n";
for( TypeTable::const_iterator l_Iter = m_ConstantTypes.begin();
l_Iter != m_ConstantTypes.end(); l_Iter++ )
l_sRet += " " + (*l_Iter).first + " - " + (*l_Iter).second + "\n";
l_sRet += " )\n";
}
if( m_vAllowablePredicates.size() > 0 )
{
l_sRet += " ( :predicates\n";
for( unsigned int i = 0; i < m_vAllowablePredicates.size(); i++ )
{
l_sRet += " " + m_vAllowablePredicates[i].ToStr() + "\n";
}
l_sRet += " )\n";
}
for( unsigned int i = 0; i < m_vOperators.size(); i++ )
{
l_sRet += m_vOperators[i]->ToStr( false, 2 ) + "\n";
}
for( unsigned int i = 0; i < m_vMethods.size(); i++ )
{
l_sRet += m_vMethods[i]->ToPddl( m_iRequirements ) + "\n";
}
l_sRet += ")\n";
return l_sRet;
}
/**
* Retrieve a string containing the SHOP representation of this domain.
* \return A string containing the SHOP representation of this domain.
* \todo This is redundant.
*/
std::string HtnDomain::ToShop() const
{
return ToStr();
}
/**
* Determine whether or not another domain is equivalent to this one.
* \param p_pOther A pointer to the other domain.
* \return Whether or not this and the other domain are equivalent.
* \todo This should also check the constants and allowable predicates and
* whatnot.
*/
bool HtnDomain::Equivalent( const HtnDomain * p_pOther ) const
{
if( m_iRequirements != p_pOther->m_iRequirements )
return false;
if( m_vOperators.size() != p_pOther->m_vOperators.size() )
return false;
if( m_vMethods.size() != p_pOther->m_vMethods.size() )
return false;
if( m_sDomainName != p_pOther->m_sDomainName )
return false;
for( unsigned int i = 0; i < m_vOperators.size(); i++ )
{
bool l_bFound = false;
for( unsigned int j = 0; j < p_pOther->m_vOperators.size() && !l_bFound; j++ )
{
if( m_vOperators[i]->Equivalent( p_pOther->m_vOperators[j] ) )
l_bFound = true;
}
if( !l_bFound )
return false;
}
for( unsigned int i = 0; i < m_vMethods.size(); i++ )
{
bool l_bFound = false;
for( unsigned int j = 0; j < p_pOther->m_vMethods.size() && !l_bFound; j++ )
{
if( m_vMethods[i]->Subsumes( p_pOther->m_vMethods[j] ) &&
p_pOther->m_vMethods[j]->Subsumes( m_vMethods[i] ) )
l_bFound = true;
}
if( !l_bFound )
return false;
}
return true;
}
/**
* Retrieve a list of types that may be used in this domain.
* If the list is empty, any may be used.
* \return A list of types that may be used in this domain.
*/
const std::set< std::string, StrLessNoCase > & HtnDomain::GetAllowableTypes() const
{
return m_sAllowableTypes;
}
/**
* Retrieve a list of constants that may be used in this domain.
* If the list is empty, any may be used.
* \return A list of constants that may be used in this domain.
*/
const TypeTable & HtnDomain::GetConstantTypes() const
{
return m_ConstantTypes;
}
/**
* Retrieve a list of predicate symbols with the number and types of their
* parameters that may be used in this domain.
* If the list is empty, any may be used.
* \return A list of predicate symbols with the number and types of their
* parameters that may be used in this domain.
*/
const std::vector< FormulaPred > & HtnDomain::GetAllowablePredicates() const
{
return m_vAllowablePredicates;
}
/**
* Retrieve the requirements of this domain.
*/
long HtnDomain::GetRequirements() const
{
return m_iRequirements;
}
/**
* Update the Q-value of method at index by averaging in new cost.
* \param p_iMethodIndex IN The index of the method to be adjusted.
* \param p_fNewCost IN The cost of the method in this instance.
*/
void HtnDomain::UpdateMethodQValue( unsigned int p_iMethodIndex,
double p_fNewCost )
{
m_vMethods[p_iMethodIndex]->UpdateQValue( p_fNewCost );
}
size_t HtnDomain::GetMemSizeMin() const
{
size_t l_iSize = sizeof( HtnDomain ) + m_vOperators.capacity() * sizeof( Operator * ) + m_vMethods.capacity() * sizeof( HtnMethod * ) + m_sDomainName.capacity() + m_sAllowableTypes.size() * sizeof( std::string ) + m_ConstantTypes.size() * sizeof( std::string ) * 2 + m_vAllowablePredicates.capacity() * sizeof( FormulaPred );
for( unsigned int i = 0; i < m_vOperators.size(); i++ )
l_iSize += m_vOperators[i]->GetMemSizeMin();
for( unsigned int i = 0; i < m_vMethods.size(); i++ )
l_iSize += m_vMethods[i]->GetMemSizeMin();
for( std::set< std::string, StrLessNoCase >::const_iterator i = m_sAllowableTypes.begin(); i != m_sAllowableTypes.end(); i++ )
l_iSize += (*i).capacity();
for( TypeTable::const_iterator i = m_ConstantTypes.begin(); i != m_ConstantTypes.end(); i++ )
l_iSize += (*i).first.capacity() + (*i).second.capacity();
for( unsigned int i = 0; i < m_vAllowablePredicates.size(); i++ )
l_iSize += m_vAllowablePredicates[i].GetMemSizeMin();
return l_iSize;
}
size_t HtnDomain::GetMemSizeMax() const
{
size_t l_iSize = sizeof( HtnDomain ) + m_vOperators.capacity() * sizeof( Operator * ) + m_vMethods.capacity() * sizeof( HtnMethod * ) + m_sDomainName.capacity() + m_sAllowableTypes.size() * sizeof( std::string ) + m_ConstantTypes.size() * sizeof( std::string ) * 2 + m_vAllowablePredicates.capacity() * sizeof( FormulaPred );
for( unsigned int i = 0; i < m_vOperators.size(); i++ )
l_iSize += m_vOperators[i]->GetMemSizeMax();
for( unsigned int i = 0; i < m_vMethods.size(); i++ )
l_iSize += m_vMethods[i]->GetMemSizeMax();
for( std::set< std::string, StrLessNoCase >::const_iterator i = m_sAllowableTypes.begin(); i != m_sAllowableTypes.end(); i++ )
l_iSize += (*i).capacity();
for( TypeTable::const_iterator i = m_ConstantTypes.begin(); i != m_ConstantTypes.end(); i++ )
l_iSize += (*i).first.capacity() + (*i).second.capacity();
for( unsigned int i = 0; i < m_vAllowablePredicates.size(); i++ )
l_iSize += m_vAllowablePredicates[i].GetMemSizeMax();
return l_iSize;
}
struct HtnMethodStarLess
{
bool operator() ( HtnMethod * p_pFirst, HtnMethod * p_pSecond )
{
return p_pFirst->GetQValue() < p_pSecond->GetQValue();
}
};
/**
* Re-order the list of methods from lowest Q-value to highest.
* This should be a stable sort.
*/
void HtnDomain::SortMethods()
{
static HtnMethodStarLess sorter;
std::stable_sort( m_vMethods.begin(), m_vMethods.end(), sorter );
}
void HtnDomain::RandomizeMethodOrder()
{
std::random_shuffle( m_vMethods.begin(), m_vMethods.end() );
}
void HtnDomain::SetMethodId( unsigned int p_iIndex,
std::string p_sNewId )
{
if( p_iIndex >= m_vMethods.size() )
throw Exception( E_INDEX_OUT_OF_BOUNDS,
"Method index out of bounds.\n",
__FILE__,
__LINE__ );
if( m_vMethods[p_iIndex]->GetId() != "" )
throw Exception( E_NOT_IMPLEMENTED,
"Attempt to overwrite a method ID.\n",
__FILE__,
__LINE__ );
m_vMethods[p_iIndex]->SetId( p_sNewId );
}
void HtnDomain::AddRequirement( long p_iAdditionalRequirement )
{
m_iRequirements |= p_iAdditionalRequirement;
}