-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtn_method.cpp
More file actions
1540 lines (1387 loc) · 47.7 KB
/
htn_method.cpp
File metadata and controls
1540 lines (1387 loc) · 47.7 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
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 <string>
#include <vector>
#include <sstream>
#include <iostream>
#include <set>
#include <tr1/memory>
#include <cstdlib>
#include <algorithm>
#include "exception.hpp"
#include "funcs.hpp"
#include "term.hpp"
#include "term_string.hpp"
#include "term_constant.hpp"
#include "term_variable.hpp"
#include "term_table.hpp"
#include "type_table.hpp"
#include "substitution.hpp"
#include "formula.hpp"
#include "formula_pred.hpp"
#include "formula_equ.hpp"
#include "formula_neg.hpp"
#include "formula_conj.hpp"
#include "operator.hpp"
#include "htn_task_head.hpp"
#include "htn_method.hpp"
/** \file htn_method.hpp
* Declaration of HtnMethod class.
*/
/** \file htn_method.cpp
* Definition of HtnMethod class.
*/
/** \class HtnMethod
* A method in the Ordered Task Decomposition version of Hierarchical Task
* Networks, for decomposing its head into a list of subtasks.
* A method consists of its head, which is a task, its precondition, which is
* a formula in first-order logic, and an ordered list of subtasks, which
* are tasks.
*/
/** \var HtnMethod::m_sId
* The ID of this method.
* This exists because for some applications you want to be able to print the
* full decomposition tree and know which method was applied at each branch.
*/
/** \var HtnMethod::m_pHead
* A smart pointer to the head of this method.
*/
/** \var HtnMethod::m_pPreconditions
* A smart pointer to the precondition of this method.
*/
/** \var HtnMethod::m_vSubtasks
* A list of smart pointers to the subtasks of this method.
*/
/** \var HtnMethod::m_TypeTable
* A mapping from terms used in the method to their types.
*/
/** \var HtnMethod::m_fQValue
* The current Q-value of this method.
* This is used by reinforcement learning.
*/
/** \var HtnMethod::m_iQCount
* The number of times the Q-value for this method has been updated.
* This is used by reinforcement learning.
*/
/**
* The one and only TermTable.
*/
extern TermTable g_TermTable;
/**
* Retrieve a pointer to a new HtnMethod from a textual description in the
* PDDL language and other stuff.
* \param p_sInput INOUT A stream containing a textual description in PDDL of
* the method. This will be advanced beyond it.
* \param p_sTypes IN A set of types that may be used. If non-empty, all
* terms must be of one of these types. Otherwise, any types are allowed.
* \param p_vAllowablePredicates IN A list of predicate symbols with the
* number and types of their parameters that may be used in the precondition.
* If non-empty, only these predicates may be used. Otherwise, any may be.
* \return A pointer to a new HtnMethod described in the arguments. The
* caller is responsible for deallocating this pointer.
*/
HtnMethod * HtnMethod::FromPddl( std::stringstream & p_sInput,
const std::set< std::string, StrLessNoCase > & p_sTypes,
const std::vector< FormulaPred > & p_vAllowablePredicates,
long p_iRequirements )
{
HtnMethod * l_pRet = new HtnMethod();
EatWhitespace( p_sInput );
EatString( p_sInput, "(" );
EatWhitespace( p_sInput );
EatString( p_sInput, ":method" );
EatWhitespace( p_sInput );
std::string l_sTaskName = "( " + ReadString( p_sInput ) + " ";
EatWhitespace( p_sInput );
bool l_bHasParameters = false,
l_bHasPreconditions = false,
l_bHasSubtasks = false,
l_bHasId = false,
l_bHasVars = false;
while( p_sInput.peek() != ')' )
{
std::string l_sFeatureName = ReadString( p_sInput );
EatWhitespace( p_sInput );
if( CompareNoCase( l_sFeatureName, ":parameters" ) == 0 )
{
if( l_bHasParameters )
throw Exception( E_NOT_IMPLEMENTED,
"A method may not have multiple parameter lists.",
__FILE__,
__LINE__ );
l_bHasParameters = true;
EatString( p_sInput, "(" );
EatWhitespace( p_sInput );
while( p_sInput.peek() != ')' )
{
std::string l_sName = ReadString( p_sInput );
l_sTaskName += l_sName + " ";
if( p_iRequirements & PDDL_REQ_TYPING )
{
EatWhitespace( p_sInput );
EatString( p_sInput, "-" );
EatWhitespace( p_sInput );
std::string l_sTyping = ReadString( p_sInput );
if( p_sTypes.find( l_sTyping ) == p_sTypes.end() )
throw Exception( E_NOT_IMPLEMENTED,
"Attempt to use an unregistered type.",
__FILE__,
__LINE__ );
if( l_pRet->m_TypeTable.find( l_sName ) != l_pRet->m_TypeTable.end() )
{
if( CompareNoCase( (*l_pRet->m_TypeTable.find( l_sName )).second, l_sTyping ) == 0 )
throw Exception( E_NOT_IMPLEMENTED,
"Attempt to use a term with multiple types.",
__FILE__,
__LINE__ );
}
else
l_pRet->m_TypeTable[l_sName] = l_sTyping;
}
EatWhitespace( p_sInput );
}
EatString( p_sInput, ")" );
l_sTaskName += ")";
}
else if( CompareNoCase( l_sFeatureName, ":vars" ) == 0 )
{
if( l_bHasVars )
throw Exception( E_NOT_IMPLEMENTED,
"A method may not have multiple vars blocks.",
__FILE__,
__LINE__ );
if( l_bHasPreconditions )
throw Exception( E_NOT_IMPLEMENTED,
"The vars block of a method may not be after the precondition block.",
__FILE__,
__LINE__ );
if( l_bHasSubtasks )
throw Exception( E_NOT_IMPLEMENTED,
"The vars block of a method may not be after the subtasks block.",
__FILE__,
__LINE__ );
l_bHasVars = true;
EatString( p_sInput, "(" );
EatWhitespace( p_sInput );
while( p_sInput.peek() != ')' )
{
std::string l_sName = ReadString( p_sInput );
if( p_iRequirements & PDDL_REQ_TYPING )
{
EatWhitespace( p_sInput );
EatString( p_sInput, "-" );
EatWhitespace( p_sInput );
std::string l_sTyping = ReadString( p_sInput );
if( p_sTypes.find( l_sTyping ) == p_sTypes.end() )
throw Exception( E_NOT_IMPLEMENTED,
"Attempt to use an unregistered type.",
__FILE__,
__LINE__ );
if( l_pRet->m_TypeTable.find( l_sName ) != l_pRet->m_TypeTable.end() )
{
if( CompareNoCase( (*l_pRet->m_TypeTable.find( l_sName )).second, l_sTyping ) == 0 )
throw Exception( E_NOT_IMPLEMENTED,
"Attempt to use a term with multiple types.",
__FILE__,
__LINE__ );
}
else
l_pRet->m_TypeTable[l_sName] = l_sTyping;
}
EatWhitespace( p_sInput );
}
EatString( p_sInput, ")" );
}
else if( CompareNoCase( l_sFeatureName, ":precondition" ) == 0 )
{
if( l_bHasPreconditions )
throw Exception( E_NOT_IMPLEMENTED,
"A method may not have multiple precondition blocks.",
__FILE__,
__LINE__ );
l_bHasPreconditions = true;
l_pRet->m_pPreconditions = FormulaConjP( new FormulaConj( p_sInput, l_pRet->m_TypeTable, p_vAllowablePredicates ) );
}
else if( CompareNoCase( l_sFeatureName, ":subtasks" ) == 0 )
{
if( l_bHasSubtasks )
throw Exception( E_NOT_IMPLEMENTED,
"A method may not have multiple subtask blocks.",
__FILE__,
__LINE__ );
l_bHasSubtasks = true;
EatString( p_sInput, "(" );
EatWhitespace( p_sInput );
while( p_sInput.peek() == '(' )
{
l_pRet->m_vSubtasks.push_back( HtnTaskHeadP( new HtnTaskHead( p_sInput, l_pRet->m_TypeTable ) ) );
EatWhitespace( p_sInput );
}
EatString( p_sInput, ")" );
}
else if( CompareNoCase( l_sFeatureName, ":id" ) == 0 )
{
if( l_bHasId )
throw Exception( E_NOT_IMPLEMENTED,
"A method may not have multiple id blocks.",
__FILE__,
__LINE__ );
if( ( p_iRequirements & PDDL_REQ_METHOD_IDS ) == 0 )
throw Exception( E_PARSE_BAD_STRING,
"Attempt to use a method ID without declaring the :method-ids PDDL requirement.",
__FILE__,
__LINE__ );
l_bHasId = true;
std::stringstream l_sIdStream( ReadParenthetical( p_sInput ) );
EatString( l_sIdStream, "(" );
EatWhitespace( l_sIdStream );
l_pRet->m_sId = ReadString( l_sIdStream );
}
else if( CompareNoCase( l_sFeatureName, ":q-value" ) == 0 )
{
if( ( p_iRequirements & PDDL_REQ_QVALUES ) == 0 )
throw Exception( E_PARSE_BAD_STRING,
"Attempt to use a method Q-value without declaring the :q-values PDDL requirement.",
__FILE__,
__LINE__ );
EatString( p_sInput, "(" );
EatWhitespace( p_sInput );
l_pRet->m_fQValue = atof( ReadString( p_sInput ).c_str() );
EatWhitespace( p_sInput );
EatString( p_sInput, ")" );
EatWhitespace( p_sInput );
}
else if( CompareNoCase( l_sFeatureName, ":q-count" ) == 0 )
{
if( ( p_iRequirements & PDDL_REQ_QVALUES ) == 0 )
throw Exception( E_PARSE_BAD_STRING,
"Attempt to use a method Q-value counter without declaring the :q-values PDDL requirement.",
__FILE__,
__LINE__ );
EatString( p_sInput, "(" );
EatWhitespace( p_sInput );
l_pRet->m_iQCount = atoi( ReadString( p_sInput ).c_str() );
EatWhitespace( p_sInput );
EatString( p_sInput, ")" );
EatWhitespace( p_sInput );
}
else
{
throw Exception( E_NOT_IMPLEMENTED,
"Unknown method feature.",
__FILE__,
__LINE__ );
}
EatWhitespace( p_sInput );
}
std::stringstream l_sTaskStream( l_sTaskName );
l_pRet->m_pHead = HtnTaskHeadP( new HtnTaskHead( l_sTaskStream, l_pRet->m_TypeTable ) );
return l_pRet;
}
/**
* Retrieve a pointer to a new HtnMethod from its textual description in the
* SHOP formalism.
* \param p_sInput INOUT A stream containing a textual description of the
* method in the SHOP formalism. The stream is advanced beyond it.
* \return A pointer to a new HtnMethod based on the provided text. The
* caller is responsible for deallocating it.
*/
HtnMethod * HtnMethod::FromShop( std::stringstream & p_sInput )
{
HtnMethod * l_pRet = new HtnMethod();
EatWhitespace( p_sInput );
EatString( p_sInput, "(" );
EatWhitespace( p_sInput );
EatString( p_sInput, ":method" );
EatWhitespace( p_sInput );
l_pRet->m_pHead = HtnTaskHeadP( new HtnTaskHead( p_sInput, l_pRet->m_TypeTable ) );
EatWhitespace( p_sInput );
l_pRet->m_pPreconditions = FormulaConjP( new FormulaConj( p_sInput, l_pRet->m_TypeTable, std::vector< FormulaPred >() ) );
EatWhitespace( p_sInput );
EatString( p_sInput, "(" );
EatWhitespace( p_sInput );
while( p_sInput.peek() == '(' )
{
l_pRet->m_vSubtasks.push_back( HtnTaskHeadP( new HtnTaskHead( p_sInput, l_pRet->m_TypeTable ) ) );
EatWhitespace( p_sInput );
}
EatString( p_sInput, ")" );
EatWhitespace( p_sInput );
EatString( p_sInput, ")" );
return l_pRet;
}
/**
* Construct a default HtnMethod. This should only be called by the
* FromShop() and FromPddl() methods.
*/
HtnMethod::HtnMethod()
{
m_sId = "";
m_fQValue = 999999;
m_iQCount = 0;
}
/**
* Construct an HtnMethod as a copy of an existing one.
* \param p_Other IN The HtnMethod to copy.
*/
HtnMethod::HtnMethod( const HtnMethod & p_Other )
: m_pHead( p_Other.m_pHead ),
m_pPreconditions( p_Other.m_pPreconditions ),
m_fQValue( p_Other.m_fQValue ),
m_iQCount( p_Other.m_iQCount )
{
for( unsigned int i = 0; i < p_Other.m_vSubtasks.size(); i++ )
m_vSubtasks.push_back( p_Other.m_vSubtasks[i] );
m_sId = p_Other.m_sId;
}
/**
* Destruct an HtnMethod.
*/
HtnMethod::~HtnMethod()
{
}
/**
* Retrieve a smart pointer to the head of this HtnMethod.
* \return A smart pointer to the head of this HtnMethod.
*/
HtnTaskHeadP HtnMethod::GetCHead() const
{
return m_pHead;
}
/**
* Retrieve a smart pointer to the preconditions of this HtnMethod.
* \return A smart pointer to the precondition of this HtnMethod.
*/
FormulaConjP HtnMethod::GetCPreconditions() const
{
return m_pPreconditions;
}
/**
* Retrieve the number of subtasks in this HtnMethod.
* \return The number of subtasks in this HtnMethod.
*/
unsigned int HtnMethod::GetNumSubtasks() const
{
return m_vSubtasks.size();
}
/**
* Retrieve a smart pointer to one of this HtnMethod's subtasks.
* \param p_iIndex IN The 0-based index of the desired subtask.
* \return A smart pointer to the requested subtask.
*/
HtnTaskHeadP HtnMethod::GetCSubtask( unsigned int p_iIndex ) const
{
if( p_iIndex >= m_vSubtasks.size() )
throw Exception( E_INDEX_OUT_OF_BOUNDS,
"Bounds error.",
__FILE__,
__LINE__ );
return m_vSubtasks[ p_iIndex ];
}
/**
* Retrieve a list of pointers to the variables in this HtnMethod.
* \return A list of pointers to the variables in this HtnMethod.
*/
std::vector< TermVariableP > HtnMethod::GetVariables() const
{
std::vector< TermVariableP > l_vRet = m_pPreconditions->GetVariables();
std::vector< TermVariableP > l_vTemp = m_pHead->GetVariables();
for( unsigned int j = 0; j < l_vTemp.size(); j++ )
{
bool l_bFound = false;
for( unsigned int k = 0; k < l_vRet.size() && !l_bFound; k++ )
{
if( *l_vTemp[j] == *l_vRet[k] )
l_bFound = true;
}
if( !l_bFound )
l_vRet.push_back( l_vTemp[j] );
}
for( unsigned int i = 0; i < m_vSubtasks.size(); i++ )
{
l_vTemp = m_vSubtasks[i]->GetVariables();
for( unsigned int j = 0; j < l_vTemp.size(); j++ )
{
bool l_bFound = false;
for( unsigned int k = 0; k < l_vRet.size() && !l_bFound; k++ )
{
if( *l_vTemp[j] == *l_vRet[k] )
l_bFound = true;
}
if( !l_bFound )
l_vRet.push_back( l_vTemp[j] );
}
}
return l_vRet;
}
std::set< TermVariableP > HtnMethod::GetRelVars() const
{
std::set< TermVariableP > l_vRet;
std::vector< TermVariableP > l_vTemp = m_pHead->GetVariables();
for( unsigned int j = 0; j < l_vTemp.size(); j++ )
{
l_vRet.insert( l_vTemp[j] );
}
for( unsigned int i = 0; i < m_vSubtasks.size(); i++ )
{
l_vTemp = m_vSubtasks[i]->GetVariables();
for( unsigned int j = 0; j < l_vTemp.size(); j++ )
{
l_vRet.insert( l_vTemp[j] );
}
}
return l_vRet;
}
/**
* Retrieve a list of pointers to the relevant variables in this HtnMethod.
* That is, these are the variables that appear in either the head of the
* task or one of its subtasks. Variables that only appear in the
* precondition are not considered relevant.
* \return A list of pointers to the variables in this HtnMethod.
*/
std::tr1::shared_ptr< const std::vector< TermVariableP > > HtnMethod::GetRelevantVariables() const
{
if( m_pVariables )
return m_pVariables;
std::vector< TermVariableP > l_vRet = m_pHead->GetVariables();
for( unsigned int i = 0; i < m_vSubtasks.size(); i++ )
{
std::vector< TermVariableP > l_vTemp = m_vSubtasks[i]->GetVariables();
for( unsigned int j = 0; j < l_vTemp.size(); j++ )
{
bool l_bFound = false;
for( unsigned int k = 0; k < l_vRet.size() && !l_bFound; k++ )
{
if( *l_vTemp[j] == *l_vRet[k] )
l_bFound = true;
}
if( !l_bFound )
l_vRet.push_back( l_vTemp[j] );
}
}
m_pVariables = std::tr1::shared_ptr< std::vector< TermVariableP > >( new std::vector< TermVariableP >( l_vRet ) );
return m_pVariables;
}
/**
* A class that exists simply to maintain some bookkeeping information about
* a Formula.
* This is used so that a list of formulas can be sorted based on the number
* of variables constraining them without continually regenerating those
* numbers as the members in the list are updated.
*/
class SortableFormulaNode
{
public:
/**
* Construct a Formula node with all old variables.
* \param l_pForm IN The Formula to encapsulate.
*/
SortableFormulaNode( const FormulaP & l_pForm )
: m_pForm( l_pForm ),
m_vOldVars( l_pForm->GetVariables() )
{
if( m_pForm->GetType() == FT_PRED )
m_iNumVars = std::tr1::dynamic_pointer_cast< FormulaPred >( m_pForm )->GetValence();
else
m_iNumVars = 2;
}
/**
* Get a Node with the substitution applied and counts updated.
* \param IN l_Subst The susbtitution to apply.
* \return A node based on this after the substitution.
*/
std::tr1::shared_ptr< SortableFormulaNode > AfterSubstitution( const Substitution & l_Subst ) const
{
std::tr1::shared_ptr< SortableFormulaNode > l_pNewNode( new SortableFormulaNode );
l_pNewNode->m_iNumVars = m_iNumVars;
for( std::vector< TermVariableP >::const_iterator i = m_vOldVars.begin();
i != m_vOldVars.end(); i++ )
{
if( l_Subst.FindIndexByVar( *i ) == l_Subst.End() )
l_pNewNode->m_vOldVars.push_back( *i );
}
if( m_vOldVars.size() > l_pNewNode->m_vOldVars.size() )
l_pNewNode->m_pForm = m_pForm->AfterSubstitution( l_Subst, 0 );
else
l_pNewNode->m_pForm = m_pForm;
return l_pNewNode;
}
/**
* Retrieve the number of variables that have not been substituted.
* \return The number of variables that have not been substituted.
*/
unsigned int GetNumOldVars() const
{
return m_vOldVars.size();
}
/**
* Retrieve the valence of the formula.
* \return The valence of the formula.
*/
unsigned int GetNumVars() const
{
return m_iNumVars;
}
/**
* Retrieve the encapsulated formula.
* \return The encapsulated formula.
*/
const FormulaP GetFormula() const
{
return m_pForm;
}
private:
/**
* Construct an empty node.
*/
SortableFormulaNode()
{
}
/**
* The encapsulated formula.
*/
FormulaP m_pForm;
/**
* A list of variables that have not yet been substituted.
*/
std::vector< TermVariableP > m_vOldVars;
/**
* The valence of the formula (2 for equalities).
*/
unsigned int m_iNumVars;
};
/**
* A functor for determining which of two formulas is closest to being
* specified completely in new variables.
*/
struct SortableNodeClosest
{
/**
* Determine whether or not one formula is closer than another to being
* specified completely in new variables.
* \param p_pForm1 IN The first encapsulated formula.
* \param p_pForm2 IN The second encapsulated formula.
* \return Whether or not p_pForm1 is closer than p_pForm2.
*/
bool operator()( const std::tr1::shared_ptr< SortableFormulaNode > & p_pForm1,
const std::tr1::shared_ptr< SortableFormulaNode > & p_pForm2 ) const
{
FormulaType l_Type1 = p_pForm1->GetFormula()->GetType();
FormulaType l_Type2 = p_pForm2->GetFormula()->GetType();
if( l_Type1 == FT_EQU && l_Type2 == FT_PRED )
return true;
if( l_Type1 == FT_EQU && l_Type2 == FT_NEG )
return true;
if( l_Type1 == FT_PRED && l_Type2 == FT_EQU )
return false;
if( l_Type1 == FT_PRED && l_Type2 == FT_NEG )
return true;
if( l_Type1 == FT_NEG && l_Type2 == FT_EQU )
return false;
if( l_Type1 == FT_NEG && l_Type2 == FT_PRED )
return false;
if( p_pForm1->GetNumOldVars() < p_pForm2->GetNumOldVars() )
return true;
else if( p_pForm1->GetNumOldVars() == p_pForm2->GetNumOldVars() )
return p_pForm1->GetNumVars() > p_pForm2->GetNumVars();
else
return false;
}
} SortableNodeClosestSorter;
bool CanSubsume( const FormulaPVec & p_vMyPrecs,
const FormulaPVec & p_vHisPrecs,
const Substitution & p_Sub );
bool NewCanSubsume( const std::vector< std::tr1::shared_ptr< SortableFormulaNode > > & p_vMyPrecs,
const FormulaPVec & p_vHisPrecs,
const Substitution & p_Sub );
/**
* Determine whether or not "my" formulas can subsume "his".
* This is a helper function for NewCanSubsume().
* This tries all ways that the first member of p_vMyPrecs, which must be a
* predicate, can be unified with a member of p_vHisPrecs, then recurses.
* \param p_vMyPrecs IN A list of "my" formulas.
* \param p_vHisPrecs IN A list of "his" formulas.
* \return Whether or not "my" formulas can subsume "his".
*/
bool NewCanSubsumePredicate( const std::vector< std::tr1::shared_ptr< SortableFormulaNode > > & p_vMyPrecs,
const FormulaPVec & p_vHisPrecs )
{
FormulaPredP l_pCurPrec = std::tr1::dynamic_pointer_cast< FormulaPred >( p_vMyPrecs[0]->GetFormula() );
for( unsigned int i = 0; i < p_vHisPrecs.size(); i++ )
{
if( p_vHisPrecs[i]->GetType() != FT_PRED )
continue;
FormulaPredP l_pCurHis = std::tr1::dynamic_pointer_cast< FormulaPred >( p_vHisPrecs[i] );
if( l_pCurPrec->GetRelationIndex() != l_pCurHis->GetRelationIndex() )
continue;
if( l_pCurPrec->GetValence() != l_pCurHis->GetValence() )
continue;
Substitution l_Sub;
bool l_bBad = false;
for( unsigned int j = 0; j < l_pCurPrec->GetValence() && !l_bBad; j++ )
{
if( CompareNoCase( l_pCurPrec->GetCParam( j )->ToStr().substr( 0, 10 ), "?temp_new_" ) == 0 )
{
if( !( *l_pCurPrec->GetCParam( j ) == *l_pCurHis->GetCParam( j ) ) )
l_bBad = true;;
}
else
{
SubMap::const_iterator l_iConjSubsIndex = l_Sub.FindIndexByVar( std::tr1::dynamic_pointer_cast< TermVariable >( l_pCurPrec->GetCParam( j ) ) );
if( l_iConjSubsIndex != l_Sub.End() )
{
if( !( *l_pCurHis->GetCParam( j ) == *l_iConjSubsIndex->second ) )
l_bBad = true;
}
else
{
l_Sub.AddPair( std::tr1::dynamic_pointer_cast< TermVariable >( l_pCurPrec->GetCParam( j ) ), l_pCurHis->GetCParam( j ) );
}
}
}
if( !l_bBad )
{
if( NewCanSubsume( p_vMyPrecs, p_vHisPrecs, l_Sub ) )
return true;
}
}
return false;
}
/**
* Determine whether or not "my" formulas can subsume "his".
* This is a helper function for NewCanSubsume().
* This tries all ways that the first member of p_vMyPrecs, which must be an
* equality, can be unified with a member of p_vHisPrecs, then recurses.
* \param p_vMyPrecs IN A list of "my" formulas.
* \param p_vHisPrecs IN A list of "his" formulas.
* \return Whether or not "my" formulas can subsume "his".
*/
bool NewCanSubsumeEquality( const std::vector< std::tr1::shared_ptr< SortableFormulaNode > > & p_vMyPrecs,
const FormulaPVec & p_vHisPrecs )
{
FormulaEquP l_pCurPrec = std::tr1::dynamic_pointer_cast< FormulaEqu >( p_vMyPrecs[0]->GetFormula() );
for( unsigned int i = 0; i < p_vHisPrecs.size(); i++ )
{
if( p_vHisPrecs[i]->GetType() != FT_EQU )
continue;
FormulaEquP l_pCurHis = std::tr1::dynamic_pointer_cast< FormulaEqu >( p_vHisPrecs[i] );
Substitution l_Sub;
if( CompareNoCase( l_pCurPrec->GetCFirst()->ToStr().substr( 0, 10 ), "?temp_new_" ) == 0 )
{
if( !( *l_pCurPrec->GetCFirst() == *l_pCurHis->GetCFirst() ) )
continue;
}
else
{
l_Sub.AddPair( std::tr1::dynamic_pointer_cast< TermVariable >( l_pCurPrec->GetCFirst() ), l_pCurHis->GetCFirst() );
}
if( CompareNoCase( l_pCurPrec->GetCSecond()->ToStr().substr( 0, 10 ), "?temp_new_" ) == 0 )
{
if( !( *l_pCurPrec->GetCSecond() == *l_pCurHis->GetCSecond() ) )
continue;
}
else
{
if( *l_pCurPrec->GetCFirst() == *l_pCurPrec->GetCSecond() )
{
if( !( *l_pCurHis->GetCFirst() == *l_pCurHis->GetCSecond() ) )
continue;
}
else
{
l_Sub.AddPair( std::tr1::dynamic_pointer_cast< TermVariable >( l_pCurPrec->GetCSecond() ), l_pCurHis->GetCSecond() );
}
}
if( NewCanSubsume( p_vMyPrecs, p_vHisPrecs, l_Sub ) )
return true;
}
return false;
}
/**
* Determine whether or not "my" formulas can subsume "his".
* This is a helper function for NewCanSubsume().
* This tries all ways that the first member of p_vMyPrecs, which must be a
* negation, can be unified with a member of p_vHisPrecs, then recurses.
* \param p_vMyPrecs IN A list of "my" formulas.
* \param p_vHisPrecs IN A list of "his" formulas.
* \return Whether or not "my" formulas can subsume "his".
*/
bool NewCanSubsumeNegation( const std::vector< std::tr1::shared_ptr< SortableFormulaNode > > & p_vMyPrecs,
const FormulaPVec & p_vHisPrecs )
{
FormulaNegP l_pCurPrec = std::tr1::dynamic_pointer_cast< FormulaNeg >( p_vMyPrecs[0]->GetFormula() );
if( l_pCurPrec->GetCNegForm()->GetType() != FT_EQU )
throw Exception( E_NOT_IMPLEMENTED,
"Only equalities may be negated here.",
__FILE__,
__LINE__ );
FormulaEquP l_pMyEqu = std::tr1::dynamic_pointer_cast< FormulaEqu >( l_pCurPrec->GetCNegForm() );
for( unsigned int i = 0; i < p_vHisPrecs.size(); i++ )
{
if( p_vHisPrecs[i]->GetType() != FT_NEG )
continue;
FormulaNegP l_pCurHis = std::tr1::dynamic_pointer_cast< FormulaNeg >( p_vHisPrecs[i] );
if( l_pCurHis->GetCNegForm()->GetType() != FT_EQU )
throw Exception( E_NOT_IMPLEMENTED,
"Only equalities may be negated here.",
__FILE__,
__LINE__ );
FormulaEquP l_pHisEqu = std::tr1::dynamic_pointer_cast< FormulaEqu >( l_pCurHis->GetCNegForm() );
Substitution l_Sub;
if( CompareNoCase( l_pMyEqu->GetCFirst()->ToStr().substr( 0, 10 ), "?temp_new_" ) == 0 )
{
if( !( *l_pMyEqu->GetCFirst() == *l_pHisEqu->GetCFirst() ) )
continue;
}
else
{
l_Sub.AddPair( std::tr1::dynamic_pointer_cast< TermVariable >( l_pMyEqu->GetCFirst() ), l_pHisEqu->GetCFirst() );
}
if( CompareNoCase( l_pMyEqu->GetCSecond()->ToStr().substr( 0, 10 ), "?temp_new_" ) == 0 )
{
if( !( *l_pMyEqu->GetCSecond() == *l_pHisEqu->GetCSecond() ) )
continue;
}
else
{
if( *l_pMyEqu->GetCFirst() == *l_pMyEqu->GetCSecond() )
{
if( !( *l_pHisEqu->GetCFirst() == *l_pHisEqu->GetCSecond() ) )
continue;
}
else
{
l_Sub.AddPair( std::tr1::dynamic_pointer_cast< TermVariable >( l_pMyEqu->GetCSecond() ), l_pHisEqu->GetCSecond() );
}
}
if( NewCanSubsume( p_vMyPrecs, p_vHisPrecs, l_Sub ) )
return true;
}
return false;
}
/**
* Determine whether or not one list of preconditions can subsume another.
* Specifically, is there some substitution that is an extension of p_Sub and
* that can be applied to the members of p_vMyPrecs such that each of those
* members will be implied by the members of p_vHisPrecs.
* The members of p_vHisPrecs should use only variables of the form ?temp_new
* while the members of p_vMyPrecs initially use only ?temp_old variables but
* through recursive calls replace them with ?temp_new variables.
* This works by applying the substitution to the members of p_vMyPrecs,
* verifying that any in only ?temp_new variables match a member of
* p_vHisPrecs, and sorts the remaining ones in order from closest to a
* member of p_vHisPrecs to furthest. Then it recursively calls itself
* through a helper that generates all possible unifications of the first
* member of the new p_vMyPrecs to a member of p_vHisPrecs.
* See State::GetInstantiations() for a very similar algorithm.
* \param p_vMyPrecs IN A vector of "my" formulas.
* \param p_vHisPrecs IN A vector of "his" formulas.
* \param p_Sub IN A substitution that can be applied to p_vMyPrecs to move it
* closer to p_vHisPrecs.
* \return Whether or not my formulas can subsume his.
*/
bool NewCanSubsume( const std::vector< std::tr1::shared_ptr< SortableFormulaNode > > & p_vMyPrecs,
const FormulaPVec & p_vHisPrecs,
const Substitution & p_Sub )
{
std::vector< std::tr1::shared_ptr< SortableFormulaNode > > l_vNewMyPrecs;
for( unsigned int i = 0; i < p_vMyPrecs.size(); i++ )
{
std::tr1::shared_ptr< SortableFormulaNode > l_pNewNode = p_vMyPrecs[i]->AfterSubstitution( p_Sub );
if( l_pNewNode->GetNumOldVars() == 0 )
{
bool l_bFound = false;
for( unsigned int j = 0; j < p_vHisPrecs.size() && !l_bFound; j++ )
{
if( l_pNewNode->GetFormula()->Equal( *p_vHisPrecs[j] ) )
l_bFound = true;
}
if( !l_bFound )
return false;
}
else
{
l_vNewMyPrecs.push_back( l_pNewNode );
}
}
if( l_vNewMyPrecs.size() == 0 )
return true;
std::sort< std::vector< std::tr1::shared_ptr< SortableFormulaNode > >::iterator, SortableNodeClosest >( l_vNewMyPrecs.begin(), l_vNewMyPrecs.end(), SortableNodeClosestSorter );
switch( l_vNewMyPrecs[0]->GetFormula()->GetType() )
{
case FT_PRED:
return NewCanSubsumePredicate( l_vNewMyPrecs, p_vHisPrecs );
case FT_EQU:
return NewCanSubsumeEquality( l_vNewMyPrecs, p_vHisPrecs );
case FT_NEG:
return NewCanSubsumeNegation( l_vNewMyPrecs, p_vHisPrecs );
case FT_CONJ:
throw Exception( E_NOT_IMPLEMENTED,
"A conjunction contained another conjunction.",
__FILE__,
__LINE__ );
default:
throw Exception( E_NOT_IMPLEMENTED,
"A conjunction contained an unknown formuxla type.",
__FILE__,
__LINE__ );
}
}
/**
* Determine whether or not one list of preconditions can subsume another.
* This just encapsulates "my" formulas and calls NewCanSubsume().
* \param p_vMyPrecs IN A vector of "my" formulas.
* \param p_vHisPrecs IN A vector of "his" formulas.
* \param p_Sub IN A substitution that can be applied to p_vMyPrecs to move it
* closer to p_vHisPrecs.
* \return Whether or not my formulas can subsume his.
*/
bool CanSubsume( const FormulaPVec & p_vMyPrecs,
const FormulaPVec & p_vHisPrecs,
const Substitution & p_Sub )
{
std::vector< std::tr1::shared_ptr< SortableFormulaNode > > l_vMyPrecs;
for( unsigned int i = 0; i < p_vMyPrecs.size(); i++ )
l_vMyPrecs.push_back( std::tr1::shared_ptr< SortableFormulaNode >( new SortableFormulaNode( p_vMyPrecs[i] ) ) );
return NewCanSubsume( l_vMyPrecs, p_vHisPrecs, p_Sub );
}
/**
* Determine whether or not this HtnMethod subsumes another.
* Subsumption means that there exists a Substitution that may be applied to
* this such that both have the same head and subtasks, and the
* precondition of the other implies the precondition of this.
* In other words, the preconditions of this are more general than the other.
* Finding a Substitution that makes this true, or proving that none such
* exists, is non-trivial.
* \param p_pOther IN A pointer to the other HtnMethod that this might
* subsume.
* \return Whether or not this subsumes the other.
* \todo Break this function up, possibly simplify or optimize it.
*/
bool HtnMethod::Subsumes( const HtnMethod * p_pOther ) const
{
// Check the simple things
// *start*
if( CompareNoCase( GetCHead()->GetName(), p_pOther->GetCHead()->GetName() ) != 0 )
return false;
if( GetCHead()->GetNumParams() != p_pOther->GetCHead()->GetNumParams() )
return false;
if( GetNumSubtasks() != p_pOther->GetNumSubtasks() )
return false;
for( unsigned int i = 0; i < GetNumSubtasks(); i++ )
{
if( CompareNoCase( GetCSubtask( i )->GetName(),
p_pOther->GetCSubtask( i )->GetName() ) != 0 )
return false;
}
// *end*
// Create a substitution from all variables in this to new temporary
// variables (l_MySubs), the same for all variables in the other method
// (l_OtherSubs), and one from this's new variables to the other's new
// variables (l_pMasterSubs).
// This level of indirection is necessary in case this and the other method
// might use some of the same variable names.
// *start*
Substitution l_MySubs;
Substitution l_OtherSubs;
Substitution l_MasterSubs;
std::vector< TermVariableP > l_vVars = GetVariables();
for( unsigned int i = 0; i < l_vVars.size(); i++ )
{
if( l_vVars[i]->HasTyping() )
{
l_MySubs.AddPair( l_vVars[i],
g_TermTable.Lookup( MakeTempOldId(), l_vVars[i]->GetTyping() ) );
}
else
{
l_MySubs.AddPair( l_vVars[i],
g_TermTable.Lookup( MakeTempOldId() ) );
}
}