-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpartial_htn_method.cpp
More file actions
1662 lines (1513 loc) · 57.9 KB
/
partial_htn_method.cpp
File metadata and controls
1662 lines (1513 loc) · 57.9 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 <vector>
#include <string>
#include <sstream>
#include <set>
#include <tr1/memory>
#include <iostream>
#include "exception.hpp"
#include "funcs.hpp"
#include "term.hpp"
#include "term_string.hpp"
#include "term_variable.hpp"
#include "term_constant.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 "state.hpp"
#include "htn_task_head.hpp"
#include "htn_task_descr.hpp"
#include "htn_method.hpp"
#include "htn_domain.hpp"
#include "partial_htn_method.hpp"
/** \file partial_htn_method.hpp
* Declaration of the PartialHtnMethod class.
*/
/** \file partial_htn_method.cpp
* Definition of the PartialHtnMethod class.
*/
/** \class PartialHtnMethod
* An HTN method in the midst of being constructed by adding subtasks one at
* a time from last to first.
* The use of substitutions here is quite complicated, so here is a bit of
* documentation:
*
* m_TaskSubs contains a pair linking each variable in the task definition
* with a created variable.
* m_vOpers->m_pSub contains a pair linking each variable in the operator
* definition with a created variable.
* m_vMasterSubs contains a pair linking each created variable to a
* constant.
*
* Two created variables may refer to the same constant without any special
* meaning. However, when a variable is used in the definition of an
* operator and satisfies a precondition of another operator or fulfills an
* effect of the task, then it should be linked to the same created variable
* as in the operator precondition or task effects.
*
* m_Preconditions are stored in terms of created variables.
* m_RemainingAddList are stored in terms of created variables.
*
* When creating a new method, it will contain only the created variables.
*/
/** \var PartialHtnMethod::m_pDomain
* A smart pointer to the domain from which subtasks of this method may come.
*/
/** \var PartialHtnMethod::m_pTaskDescr
* A smart pointer to the task description associated with this method.
*/
/** \var PartialHtnMethod::m_vRemainingAddList
* A list of smart pointers to formulas from the add list of the task
* description that have not yet been made true by any subtask.
* \todo Is this really necessary, or could they just be part of
* m_vRemainingPrecs?
*/
/** \var PartialHtnMethod::m_vRemainingPrecs
* A list of smart pointers to outstanding preconditions of subtasks that
* have not been accomplished by earlier subtasks.
*/
/** \var PartialHtnMethod::m_TaskSubs
* A substitution from the variables in the task description to newly created
* variables.
*/
/** \var PartialHtnMethod::m_MasterSubs
* A substitution from the newly created variables for this method to
* constants.
*/
/** \var PartialHtnMethod::m_vOperators
* A list of operators that will be subtasks of the new method.
* These must be deallocated with the PartialHtnMethod.
*/
/** \var PartialHtnMethod::m_vOperSubs
* A substitution from operator variables to created variables for each
* operator in m_vOperators.
* These must be deallocated with the PartialHtnMethod.
*/
/** \var PartialHtnMethod::m_vOperBeforeStates
* The state in which each operator in m_vOperators should be executed. Used
* to determine subtask ordering.
*/
/** \var PartialHtnMethod::m_vMethods
* A list of methods whose heads will be subtasks of this method.
* These must be deallocated with the PartialHtnMethod.
*/
/** \var PartialHtnMethod::m_vMethodSubs
* A list of substitutions from method variables to created variables for each
* member of m_vMethods.
* These must be deallocated with the PartialHtnMethod.
*/
/** \var PartialHtnMethod::m_vMethodEffects
* A list of smart pointers to the effects associated with each of the
* methods in m_vMethods, in original method variables.
*/
/** \var PartialHtnMethod::m_vMethodBeforeStates
* A list of the states in which each member of m_vMethods starts.
*/
/** \var PartialHtnMethod::m_vMethodAfterStates
* A list of the states in which each member of m_vMethods ends.
*/
/** \var PartialHtnMethod::m_iInitStateIndex
* The index of the state in some plan where this method begins.
*/
/** \var PartialHtnMethod::m_iFinalStateIndex
* The index of the state in some plan where this method ends.
*/
/** \var PartialHtnMethod::m_iCurrentStateIndex
* The index of the state we have reached thusfar in the goal regression from
* m_iFinalStateIndex to m_iInitStateIndex.
*/
/** \var PartialHtnMethod::m_iTotalCost
* The sum of the costs of the new method's subtasks.
* This is used for reinforcement learning.
*/
/**
* The one and only TermTable.
*/
extern TermTable g_TermTable;
/**
* Create a PartialHtnMethod with no subtasks.
* \param p_pDomain IN A smart pointer to the domain from which subtasks will
* come.
* \param p_pTaskDescr IN A smart pointer to a task description for this
* method.
* \param p_pTaskSubs IN A substitution from the variables in the task
* description to constants from the problem.
* \param p_iFinalStateIndex IN The index of the state in the problem where
* the effects of the annotated task were satisfied.
*/
PartialHtnMethod::PartialHtnMethod( const std::tr1::shared_ptr< HtnDomain > & p_pDomain,
const std::tr1::shared_ptr< HtnTaskDescr > & p_pTaskDescr,
const Substitution * p_pTaskSubs,
unsigned int p_iFinalStateIndex )
: m_pDomain( p_pDomain ),
m_pTaskDescr( p_pTaskDescr )
{
for( SubMap::const_iterator l_iIter = p_pTaskSubs->Begin();
l_iIter != p_pTaskSubs->End();
l_iIter++ )
{
TermVariableP l_pVar;
if( l_iIter->first->HasTyping() )
l_pVar = std::tr1::dynamic_pointer_cast< TermVariable >( g_TermTable.Lookup( MakeVarId(), l_iIter->first->GetTyping() ) );
else
l_pVar = std::tr1::dynamic_pointer_cast< TermVariable >( g_TermTable.Lookup( MakeVarId() ) );
m_TaskSubs.AddPair( l_iIter->first, l_pVar );
m_MasterSubs.AddPair( l_pVar, l_iIter->second );
}
FormulaConjP l_pAfterTaskSubs( std::tr1::dynamic_pointer_cast< FormulaConj >( m_pTaskDescr->GetCEffects()->AfterSubstitution( m_TaskSubs, 0 ) ) );
for( FormulaPVecCI i = l_pAfterTaskSubs->GetBeginConj();
i != l_pAfterTaskSubs->GetEndConj();
i++ )
{
bool l_bFound = false;
for( unsigned int j = 0; j < m_vRemainingAddList.size(); j++ )
{
if( m_vRemainingAddList[j]->Equal( *( *i ) ) )
l_bFound = true;
}
if( !l_bFound )
m_vRemainingAddList.push_back( *i );
}
m_iFinalStateIndex = p_iFinalStateIndex;
m_iCurrentStateIndex = p_iFinalStateIndex;
m_iTotalCost = 0;
}
/**
* Construct a PartialHtnMethod as a copy of an existing one.
* \param p_Other IN The PartialHtnMethod to copy.
*/
PartialHtnMethod::PartialHtnMethod( const PartialHtnMethod & p_Other )
: m_pDomain( p_Other.m_pDomain ),
m_pTaskDescr( p_Other.m_pTaskDescr ),
m_vRemainingAddList( p_Other.m_vRemainingAddList ),
m_vRemainingPrecs( p_Other.m_vRemainingPrecs ),
m_TaskSubs( p_Other.m_TaskSubs ),
m_MasterSubs( p_Other.m_MasterSubs ),
m_vOperBeforeStates( p_Other.m_vOperBeforeStates ),
m_vMethodBeforeStates( p_Other.m_vMethodBeforeStates ),
m_vMethodAfterStates( p_Other.m_vMethodAfterStates ),
m_iTotalCost( p_Other.m_iTotalCost )
{
for( unsigned int i = 0; i < p_Other.m_vOperators.size(); i++ )
m_vOperators.push_back( new Operator( *p_Other.m_vOperators[i] ) );
for( unsigned int i = 0; i < p_Other.m_vOperSubs.size(); i++ )
m_vOperSubs.push_back( new Substitution( *p_Other.m_vOperSubs[i] ) );
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_vMethodSubs.size(); i++ )
m_vMethodSubs.push_back( new Substitution( *p_Other.m_vMethodSubs[i] ) );
m_iInitStateIndex = p_Other.m_iInitStateIndex;
m_iFinalStateIndex = p_Other.m_iFinalStateIndex;
m_iCurrentStateIndex = p_Other.m_iCurrentStateIndex;
}
/**
* Destruct a PartialHtnMethod.
*/
PartialHtnMethod::~PartialHtnMethod()
{
for( unsigned int i = 0; i < m_vOperators.size(); i++ )
delete m_vOperators[i];
for( unsigned int i = 0; i < m_vOperSubs.size(); i++ )
delete m_vOperSubs[i];
for( unsigned int i = 0; i < m_vMethods.size(); i++ )
delete m_vMethods[i];
for( unsigned int i = 0; i < m_vMethodSubs.size(); i++ )
delete m_vMethodSubs[i];
}
/**
* Determine whether or not a formula includes any of the oustanding
* preconditions in m_vRemainingPrecs.
* \todo Constantly recreating and deleting the grounded versions of this are
* ridiculous, and should probably be optimized away.
* \param p_pForm IN A smart pointer to the formula that might supply a
* precondition.
* \return Whether or not p_pForm supplies an open precondition.
*/
bool PartialHtnMethod::SuppliesPrec( const FormulaConjP & p_pForm ) const
{
for( unsigned int i = 0; i < m_vRemainingPrecs.size(); i++ )
{
FormulaP l_pPreElementNoVars( m_vRemainingPrecs[ i ]->AfterSubstitution( m_MasterSubs, 0 ) );
bool l_bFound = p_pForm->Implies( l_pPreElementNoVars );
if( l_bFound )
return true;
}
return false;
}
/**
* Determine whether or not a formula includes any of the remaining
* effects in m_vRemainingAddList.
* \todo Constantly recreating and deleting the grounded versions of this are
* ridiculous, and should probably be optimized away.
* \param p_pForm IN A smart pointer to the formula that might supply an
* effect.
* \return Whether or not p_pForm supplies an open effect.
*/
bool PartialHtnMethod::SuppliesEffect( const FormulaConjP & p_pForm ) const
{
for( unsigned int i = 0; i < m_vRemainingAddList.size(); i++ )
{
FormulaP l_pAddElementNoVars( m_vRemainingAddList[ i ]->AfterSubstitution( m_MasterSubs, 0 ) );
bool l_bFound = p_pForm->Implies( l_pAddElementNoVars );
if( l_bFound )
return true;
}
return false;
}
/**
* Determine whether or not a formula provides a precondition that is not also
* provided by some other formula.
* This is used to guarantee that a subtask is actually useful; that it does
* supplies something that it does not also require.
* \param p_pForm IN A smart pointer to the formula that should provide new
* preconditions.
* \param p_pOld IN A smart pointer to the formula that may have already
* provided those preconditions.
* Return Whether or not p_pForm provides preconditions not in p_pOld.
*/
bool PartialHtnMethod::SuppliesNewPrec( const FormulaConjP & p_pForm,
const FormulaConjP & p_pOld ) const
{
for( unsigned int i = 0; i < m_vRemainingPrecs.size(); i++ )
{
FormulaP l_pPreElementNoVars( m_vRemainingPrecs[ i ]->AfterSubstitution( m_MasterSubs, 0 ) );
bool l_bFound = p_pForm->Implies( l_pPreElementNoVars ) && !p_pOld->Implies( l_pPreElementNoVars );
if( l_bFound )
return true;
}
return false;
}
/**
* Determine whether or not a formula provides an effect that is not also
* provided by some other formula.
* This is used to guarantee that a subtask is actually useful; that it does
* supplies something that it does not also require.
* \param p_pForm IN A smart pointer to the formula that should provide new
* effects.
* \param p_pOld IN A smart pointer to the formula that may have already
* provided those effects.
* Return Whether or not p_pForm provides effects not in p_pOld.
*/
bool PartialHtnMethod::SuppliesNewEffect( const FormulaConjP & p_pForm,
const FormulaConjP & p_pOld ) const
{
for( unsigned int i = 0; i < m_vRemainingAddList.size(); i++ )
{
FormulaP l_pAddElementNoVars( m_vRemainingAddList[ i ]->AfterSubstitution( m_MasterSubs, 0 ) );
bool l_bFound = p_pForm->Implies( l_pAddElementNoVars ) && !p_pOld->Implies( l_pAddElementNoVars );
if( l_bFound )
return true;
}
return false;
}
/**
* Determine whether or not an operator starting at a specified state
* conflicts with another subtask.
* Operators and methods added have associated before and after states, none
* of which should overlap.
* \param p_iBeforeStateNum IN The state where the operator starts.
* \return Whether or not the operator conflicts with anything.
*/
bool PartialHtnMethod::DoesOperatorConflict( unsigned int p_iBeforeStateNum ) const
{
for( unsigned int i = 0; i < m_vOperBeforeStates.size(); i++ )
{
if( p_iBeforeStateNum == m_vOperBeforeStates[i] )
return true;
}
for( unsigned int i = 0; i < m_vMethods.size(); i++ )
{
if( p_iBeforeStateNum >= m_vMethodBeforeStates[i] &&
p_iBeforeStateNum < m_vMethodAfterStates[i] )
return true;
}
return false;
}
/**
* Determine whether or not a method starting at a specified state
* conflicts with another subtask.
* Operators and methods added have associated before and after states, none
* of which should overlap.
* \param p_iBeforeStateNum IN The state where the method starts.
* \param p_iAfterStateNum IN The state where the method ends.
* \return Whether or not the method conflicts with anything.
*/
bool PartialHtnMethod::DoesMethodConflict( unsigned int p_iBeforeStateNum,
unsigned int p_iAfterStateNum ) const
{
for( unsigned int i = 0; i < m_vOperBeforeStates.size(); i++ )
{
for( unsigned int j = p_iBeforeStateNum; j < p_iAfterStateNum; j++ )
{
if( j == m_vOperBeforeStates[i] )
{
return true;
}
}
}
for( unsigned int i = 0; i < m_vMethods.size(); i++ )
{
for( unsigned int j = p_iBeforeStateNum; j < p_iAfterStateNum; j++ )
{
if( j >= m_vMethodBeforeStates[i] &&
j < m_vMethodAfterStates[i] )
return true;
}
}
return false;
}
/**
* Given a substitution from schema vars to constants, create vars to replace
* them.
* This also updates the master substitution list to include new created to
* constant pairs.
* \param p_pRealSub IN A pointer to the substitution from schema vars to
* constants.
* \param p_NewSub INOUT An initially empty substitution that will be filled
* with created vars to schema vars.
* \param p_bPartialGeneralization IN Whether or not variables that refer to
* the same constant will be forced to have the same name.
*/
void PartialHtnMethod::CreateNewVars( const Substitution * p_pRealSub,
Substitution & p_NewSub,
bool p_bPartialGeneralization )
{
if( p_bPartialGeneralization )
{
for( SubMap::const_iterator l_iIter = p_pRealSub->Begin();
l_iIter != p_pRealSub->End();
l_iIter++ )
{
SubMap::const_iterator l_iOldIndex = m_MasterSubs.FindIndexByTerm( l_iIter->second );
if( l_iOldIndex == m_MasterSubs.End() )
{
TermVariableP l_pVar;
if( l_iIter->first->HasTyping() )
l_pVar = std::tr1::dynamic_pointer_cast< TermVariable >( g_TermTable.Lookup( MakeVarId(), l_iIter->first->GetTyping() ) );
else
l_pVar = std::tr1::dynamic_pointer_cast< TermVariable >( g_TermTable.Lookup( MakeVarId() ) );
p_NewSub.AddPair( l_iIter->first, l_pVar );
m_MasterSubs.AddPair( l_pVar, l_iIter->second );
}
else
{
p_NewSub.AddPair( l_iIter->first, l_iOldIndex->first );
}
}
}
else
{
// Substitute in created variables for those from the definition.
for( SubMap::const_iterator l_iIter = p_pRealSub->Begin();
l_iIter != p_pRealSub->End();
l_iIter++ )
{
TermVariableP l_pVar;
if( l_iIter->first->HasTyping() )
l_pVar = std::tr1::dynamic_pointer_cast< TermVariable >( g_TermTable.Lookup( MakeVarId(), l_iIter->first->GetTyping() ) );
else
l_pVar = std::tr1::dynamic_pointer_cast< TermVariable >( g_TermTable.Lookup( MakeVarId() ) );
p_NewSub.AddPair( l_iIter->first, l_pVar );
m_MasterSubs.AddPair( l_pVar, l_iIter->second );
}
}
}
/**
* Process a list of effects, removing any oustanding preconditions or
* remaining effects that can be unified to them.
* \param p_pEffects IN A smart pointer to the list of effects in schema vars.
* \param p_pRealSub IN A pointer to a substitution from schema vars to
* consts.
* \param p_bPartialGeneralization IN Whether or not two variables that refer
* to the same constant should be merged.
* \param p_Subst INOUT A substitution from schema vars to created vars. If
* unification is performed, this may be changed.
*/
void PartialHtnMethod::ProcessEffects( const FormulaConjP & p_pEffects,
const Substitution * p_pRealSub,
bool p_bPartialGeneralization,
Substitution & p_Subst )
{
// Process each member of the add list, which may provide one of the
// effects of the task or fulfill a precondition of a later operator or
// method.
for( FormulaPVecCI i = p_pEffects->GetBeginConj();
i != p_pEffects->GetEndConj();
i++ )
{
// Equality formulas may never be added, and we do not care about
// negative effects.
if( ( *i )->GetType() != FT_PRED )
continue;
if( p_bPartialGeneralization )
{
FormulaPredP l_pOpAdd( std::tr1::dynamic_pointer_cast< FormulaPred >( ( *i )->AfterSubstitution( p_Subst, 0 ) ) );
for( unsigned int j = 0; j < m_vRemainingAddList.size(); j++ )
{
if( *l_pOpAdd == *m_vRemainingAddList[ j ] )
{
std::vector< FormulaP >::iterator l_Iter = m_vRemainingAddList.begin();
for( unsigned int k = 0; k < j; k++ )
l_Iter++;
m_vRemainingAddList.erase( l_Iter );
j--;
}
}
for( unsigned int j = 0; j < m_vRemainingPrecs.size(); j++ )
{
if( *l_pOpAdd == *m_vRemainingPrecs[ j ] )
{
std::vector< FormulaP >::iterator l_Iter = m_vRemainingPrecs.begin();
for( unsigned int k = 0; k < j; k++ )
l_Iter++;
m_vRemainingPrecs.erase( l_Iter );
j--;
}
}
}
else
{
FormulaPredP l_pOpAdd( std::tr1::dynamic_pointer_cast< FormulaPred >( ( *i )->AfterSubstitution( *p_pRealSub, 0 ) ) );
// Check each of the task effects to see if this satisfies it.
for( unsigned int j = 0; j < m_vRemainingAddList.size(); j++ )
{
FormulaP l_pAddElementNoVars( m_vRemainingAddList[ j ]->AfterSubstitution( m_MasterSubs, 0 ) );
// If this does solve a task effect, make the variables the same.
if( *l_pOpAdd == *l_pAddElementNoVars )
{
DoAddListUnification( *i, j, &p_Subst );
j = -1;
}
}
// Check each of the preconditions of later operators and methods to see
// if this satisfies it.
for( unsigned int j = 0; j < m_vRemainingPrecs.size(); j++ )
{
FormulaP l_pPreElementNoVars( m_vRemainingPrecs[ j ]->AfterSubstitution( m_MasterSubs, 0 ) );
// If this does solve a precondition, make the variables the same.
if( *l_pOpAdd == *l_pPreElementNoVars )
{
DoPrecondUnification( std::tr1::dynamic_pointer_cast< FormulaPred >( *i ), j, &p_Subst );
j = -1;
}
}
}
}
}
/**
* Add the preconditions of a new subtask to the list of outstanding ones.
* \param p_pPrecs IN A smart pointer to the preconditions of the new
* subtask, in schema variables.
* \param p_Subst IN A substitution from schema vars to created vars.
*/
void PartialHtnMethod::ProcessPreconditions( const FormulaConjP & p_pPrecs,
const Substitution & p_Subst )
{
for( FormulaPVecCI i = p_pPrecs->GetBeginConj();
i != p_pPrecs->GetEndConj();
i++ )
{
FormulaP l_pAfter( ( *i )->AfterSubstitution( p_Subst, 0 ) );
switch( l_pAfter->GetType() )
{
case FT_NEG:
if( std::tr1::dynamic_pointer_cast< FormulaNeg >( l_pAfter )->GetCNegForm()->GetType() != FT_EQU )
throw Exception( E_NOT_IMPLEMENTED,
"Negative preconditions (other than equalities) are not allowed.",
__FILE__,
__LINE__ );
// break intentionally left out.
case FT_PRED:
case FT_EQU:
{
bool l_bFound = false;
for( unsigned int j = 0; j < m_vRemainingPrecs.size() && !l_bFound; j++ )
{
if( *m_vRemainingPrecs[ j ] == *l_pAfter )
{
l_bFound = true;
}
}
if( !l_bFound )
{
m_vRemainingPrecs.push_back( l_pAfter );
}
}
break;
case FT_CONJ:
throw Exception ( E_NOT_IMPLEMENTED,
"A conjunction was not in simplest form.",
__FILE__,
__LINE__ );
break;
default:
throw Exception( E_FORMULA_TYPE_UNKNOWN,
"Unknown formula type.",
__FILE__,
__LINE__ );
}
}
}
/**
* Add an operator as a subtask of this method.
* \param p_pOp IN A pointer to the operator to add.
* \param p_pRealSub IN A substitution from variables in the operator to
* constants in the problem.
* \param p_iBeforeStateNum IN The index of the state in the plan from which
* this operator was taken.
* \param p_bPartialGeneralization IN Whether or not variables that refer to
* different constants should be allowed to remain distinct variables.
*/
void PartialHtnMethod::AddOperator( const Operator * p_pOp,
const Substitution * p_pRealSub,
unsigned int p_iBeforeStateNum,
bool p_bPartialGeneralization )
{
if( p_iBeforeStateNum != m_iCurrentStateIndex - 1 )
throw Exception( E_OPER_OVERLAP,
"Operator added to partial method in wrong order.",
__FILE__,
__LINE__ );
if( DoesOperatorConflict( p_iBeforeStateNum ) )
throw Exception( E_OPER_OVERLAP,
"Operator added to partial method conflicts with an existing operator or method.",
__FILE__,
__LINE__ );
Substitution l_Subst;
CreateNewVars( p_pRealSub,
l_Subst,
p_bPartialGeneralization );
ProcessEffects( p_pOp->GetCEffects(),
p_pRealSub,
p_bPartialGeneralization,
l_Subst );
ProcessPreconditions( p_pOp->GetCPreconditions(),
l_Subst );
m_vOperators.push_back( new Operator( *p_pOp ) );
m_vOperSubs.push_back( new Substitution( l_Subst ) );
m_vOperBeforeStates.push_back( p_iBeforeStateNum );
m_iCurrentStateIndex = p_iBeforeStateNum;
m_iTotalCost += p_pOp->GetCost();
}
/**
* Add the head of a method as a subtask of this method.
* \param p_pMethod IN A pointer to the method to add.
* \param p_pRealSub IN A substitution from variables in the method to
* constants in the problem.
* \param p_iBeforeStateNum IN The index of the state in the plan from which
* this method began.
* \param p_iAfterStateNum IN The index of the state in the plan at which this
* method ended.
* \param p_pEffects IN A smart pointer to the effects associated with the
* method.
* \param p_bPartialGeneralization IN Whether or not variables that refer to
* different constants should be allowed to remain distinct variables.
* \param p_iCost IN The cost associated with decomposing this method, used
* for calculating a Q-value for the new method.
*/
void PartialHtnMethod::AddMethod( const HtnMethod * p_pMethod,
const Substitution * p_pRealSub,
unsigned int p_iBeforeStateNum,
unsigned int p_iAfterStateNum,
const FormulaConjP & p_pEffects,
bool p_bPartialGeneralization,
int p_iCost )
{
if( p_iAfterStateNum != m_iCurrentStateIndex )
throw Exception( E_OPER_OVERLAP,
"Method is added to partial out of order.",
__FILE__,
__LINE__ );
if( DoesMethodConflict( p_iBeforeStateNum,
p_iAfterStateNum ) )
throw Exception( E_OPER_OVERLAP,
"Method added to partial method conflicts with an existing operator or method.",
__FILE__,
__LINE__ );
Substitution l_Subst;
CreateNewVars( p_pRealSub,
l_Subst,
p_bPartialGeneralization );
ProcessEffects( p_pEffects,
p_pRealSub,
p_bPartialGeneralization,
l_Subst );
ProcessPreconditions( p_pMethod->GetCPreconditions(),
l_Subst );
m_vMethods.push_back( new HtnMethod( *p_pMethod ) );
m_vMethodSubs.push_back( new Substitution( l_Subst ) );
m_vMethodBeforeStates.push_back( p_iBeforeStateNum );
m_vMethodAfterStates.push_back( p_iAfterStateNum );
m_vMethodEffects.push_back( p_pEffects );
m_iCurrentStateIndex = p_iBeforeStateNum;
m_iTotalCost += p_iCost;
}
/**
* Replace certain variables in the remaining preconditions and effects.
* This is used when the effects of a subtask can be unified with a
* remaining effect or precondition.
* \param p_ReplaceSub IN The substitution to apply.
*/
void PartialHtnMethod::UpdateAddsAndPrecs( const Substitution & p_ReplaceSub )
{
std::vector< FormulaP > l_vTempAddList( m_vRemainingAddList );
m_vRemainingAddList.clear();
for( unsigned int i = 0; i < l_vTempAddList.size(); i++ )
{
FormulaP l_pAfter( l_vTempAddList[i]->AfterSubstitution( p_ReplaceSub, 0 ) );
bool l_bFound = false;
for( unsigned int j = 0; j < m_vRemainingAddList.size() && !l_bFound; j++ )
{
if( l_pAfter->Equal( *m_vRemainingAddList[j] ) )
l_bFound = true;
}
if( !l_bFound )
m_vRemainingAddList.push_back( l_pAfter );
}
std::vector< FormulaP > l_vTempPrecs( m_vRemainingPrecs );
m_vRemainingPrecs.clear();
for( unsigned int i = 0; i < l_vTempPrecs.size(); i++ )
{
FormulaP l_pAfter( l_vTempPrecs[i]->AfterSubstitution( p_ReplaceSub, 0 ) );
bool l_bFound = false;
for( unsigned int j = 0; j < m_vRemainingPrecs.size() && !l_bFound; j++ )
{
if( l_pAfter->Equal( *m_vRemainingPrecs[j] ) )
l_bFound = true;
}
if( !l_bFound )
m_vRemainingPrecs.push_back( l_pAfter );
}
}
/**
* Replace one created variable with a new one in various substitutions.
* This is used when an effect of a subtask can unify to an outstanding
* effect or precondition.
* \param p_pOldVar IN A pointer to the created variable to be replaced.
* \param p_pNewVar In A pointer to the new created variable.
* \param p_pSub INOUT A pointer to a substitution that will eventually be
* from the variables in an operator or method schema to created variables.
* \param p_ReplaceSub INOUT A substitution that will eventually contain all
* pairs of old-new variables and can be applied to outstanding preconditions
* and effects.
*/
void PartialHtnMethod::DoAddListTermReplacement( const TermVariableP & p_pOldVar,
const TermVariableP & p_pNewVar,
Substitution * p_pSub,
Substitution & p_ReplaceSub )
{
switch( p_pOldVar->GetType() )
{
case TT_CONSTANT:
break;
case TT_VARIABLE:
{
TermVariableP l_pOldTerm = std::tr1::dynamic_pointer_cast< TermVariable >( p_pSub->FindIndexByVar( p_pOldVar )->second );
TermVariableP l_pNewTerm = p_pNewVar;
if( !( *l_pOldTerm == *l_pNewTerm ) )
{
p_pSub->ReplaceTerm( l_pOldTerm, l_pNewTerm );
m_MasterSubs.ReplaceTerm( l_pOldTerm, l_pNewTerm );
m_TaskSubs.ReplaceTerm( l_pOldTerm, l_pNewTerm );
p_ReplaceSub.AddPair( l_pOldTerm, l_pNewTerm );
for( unsigned int j = 0; j < m_vOperSubs.size(); j++ )
m_vOperSubs[j]->ReplaceTerm( l_pOldTerm, l_pNewTerm );
for( unsigned int j = 0; j < m_vMethodSubs.size(); j++ )
m_vMethodSubs[j]->ReplaceTerm( l_pOldTerm, l_pNewTerm );
}
}
break;
default:
throw Exception( E_TERM_TYPE_UNKNOWN,
"Unknown term type.",
__FILE__,
__LINE__ );
}
}
/**
* Unify the effect of a subtask with a remaining effect, removing it.
* \param p_pNewEffects IN A smart pointer to the effect of the subtask.
* \param p_iTaskAddListIndex IN An index of the matching entry in the
* m_vRemainingAddList.
* \param p_pSub INOUT A substitution for the subtask, which will be altered.
*/
void PartialHtnMethod::DoAddListUnification( const FormulaP & p_pNewEffects,
unsigned int p_iTaskAddListIndex,
Substitution * p_pSub )
{
Substitution l_ReplaceSub;
if( p_pNewEffects->GetType() != m_vRemainingAddList[ p_iTaskAddListIndex ]->GetType() )
throw Exception( E_NOT_IMPLEMENTED,
"Formulas are not of the same type.",
__FILE__,
__LINE__ );
switch( p_pNewEffects->GetType() )
{
case FT_PRED:
{
for( unsigned int i = 0; i < std::tr1::dynamic_pointer_cast< FormulaPred >( p_pNewEffects )->GetValence(); i++ )
{
TermVariableP l_pOldVar = std::tr1::dynamic_pointer_cast< TermVariable >( std::tr1::dynamic_pointer_cast< FormulaPred >( p_pNewEffects )->GetCParam( i ) );
TermVariableP l_pNewVar = std::tr1::dynamic_pointer_cast< TermVariable >( std::tr1::dynamic_pointer_cast< FormulaPred >( m_vRemainingAddList[ p_iTaskAddListIndex ] )->GetCParam( i ) );
DoAddListTermReplacement( l_pOldVar,
l_pNewVar,
p_pSub,
l_ReplaceSub );
}
}
break;
case FT_CONJ:
throw Exception( E_NOT_IMPLEMENTED,
"A conjunct was not in simplest form.",
__FILE__,
__LINE__ );
case FT_NEG:
{
if( std::tr1::dynamic_pointer_cast< FormulaNeg >( p_pNewEffects )->GetCNegForm()->GetType() != FT_EQU ||
std::tr1::dynamic_pointer_cast< FormulaNeg >( m_vRemainingAddList[ p_iTaskAddListIndex ] )->GetCNegForm()->GetType() != FT_EQU )
throw Exception( E_NOT_IMPLEMENTED,
"Only equality formulas may be negated in preconditions.",
__FILE__,
__LINE__ );
TermVariableP l_pOldVar = std::tr1::dynamic_pointer_cast< TermVariable >( std::tr1::dynamic_pointer_cast< FormulaEqu >( std::tr1::dynamic_pointer_cast< FormulaNeg >( p_pNewEffects )->GetCNegForm() )->GetCFirst() );
TermVariableP l_pNewVar = std::tr1::dynamic_pointer_cast< TermVariable >( std::tr1::dynamic_pointer_cast< FormulaEqu >( std::tr1::dynamic_pointer_cast< FormulaNeg >( m_vRemainingAddList[ p_iTaskAddListIndex ] )->GetCNegForm() )->GetCFirst() );
DoAddListTermReplacement( l_pOldVar,
l_pNewVar,
p_pSub,
l_ReplaceSub );
l_pOldVar = std::tr1::dynamic_pointer_cast< TermVariable >( std::tr1::dynamic_pointer_cast< FormulaEqu >( std::tr1::dynamic_pointer_cast< FormulaNeg >( p_pNewEffects )->GetCNegForm() )->GetCSecond() );
l_pNewVar = std::tr1::dynamic_pointer_cast< TermVariable >( std::tr1::dynamic_pointer_cast< FormulaEqu >( std::tr1::dynamic_pointer_cast< FormulaNeg >( m_vRemainingAddList[ p_iTaskAddListIndex ] )->GetCNegForm() )->GetCSecond() );
DoAddListTermReplacement( l_pOldVar,
l_pNewVar,
p_pSub,
l_ReplaceSub );
}
break;
case FT_EQU:
{
TermVariableP l_pOldVar = std::tr1::dynamic_pointer_cast< TermVariable >( std::tr1::dynamic_pointer_cast< FormulaEqu >( p_pNewEffects )->GetCFirst() );
TermVariableP l_pNewVar = std::tr1::dynamic_pointer_cast< TermVariable >( std::tr1::dynamic_pointer_cast< FormulaEqu >( m_vRemainingAddList[ p_iTaskAddListIndex ] )->GetCFirst() );
DoAddListTermReplacement( l_pOldVar,
l_pNewVar,
p_pSub,
l_ReplaceSub );
l_pOldVar = std::tr1::dynamic_pointer_cast< TermVariable >( std::tr1::dynamic_pointer_cast< FormulaEqu >( p_pNewEffects )->GetCSecond() );
l_pNewVar = std::tr1::dynamic_pointer_cast< TermVariable >( std::tr1::dynamic_pointer_cast< FormulaEqu >( m_vRemainingAddList[ p_iTaskAddListIndex ] )->GetCSecond() );
DoAddListTermReplacement( l_pOldVar,
l_pNewVar,
p_pSub,
l_ReplaceSub );
}
break;
default:
throw Exception( E_NOT_IMPLEMENTED,
"Formula is of unknown type.",
__FILE__,
__LINE__ );
}
std::vector< FormulaP >::iterator l_Iter = m_vRemainingAddList.begin();
for( unsigned int i = 0; i < p_iTaskAddListIndex; i++ )
{
l_Iter++;
}
m_vRemainingAddList.erase( l_Iter );
UpdateAddsAndPrecs( l_ReplaceSub );
}
/**
* Unify an effect of a subtask with an oustanding precondition, which is
* removed.
* \param p_pNewEffects IN A smart pointer to the subtask effect.
* \param p_iPrecondIndex IN The index of the matching entry in
* m_vRemainingPrecs.
* \param p_pSub INOUT A substitution for the subtask, which will be altered.
*/
void PartialHtnMethod::DoPrecondUnification( const FormulaPredP & p_pNewEffects,
unsigned int p_iPrecondIndex,
Substitution * p_pSub )
{
Substitution l_ReplaceSub;
if( p_pNewEffects->GetType() != m_vRemainingPrecs[ p_iPrecondIndex ]->GetType() )
throw Exception( E_NOT_IMPLEMENTED,
"Formulas are not of the same type.",
__FILE__,
__LINE__ );
switch( p_pNewEffects->GetType() )
{
case FT_PRED:
{
for( unsigned int i = 0; i < std::tr1::dynamic_pointer_cast< FormulaPred >( p_pNewEffects )->GetValence(); i++ )
{
TermVariableP l_pOldVar = std::tr1::dynamic_pointer_cast< TermVariable >( std::tr1::dynamic_pointer_cast< FormulaPred >( p_pNewEffects )->GetCParam( i ) );
TermVariableP l_pNewVar = std::tr1::dynamic_pointer_cast< TermVariable >( std::tr1::dynamic_pointer_cast< FormulaPred >( m_vRemainingPrecs[ p_iPrecondIndex ] )->GetCParam( i ) );
DoAddListTermReplacement( l_pOldVar,
l_pNewVar,
p_pSub,
l_ReplaceSub );
}
}
break;
case FT_CONJ:
throw Exception( E_NOT_IMPLEMENTED,
"A conjunct was not in simplest form.",
__FILE__,
__LINE__ );
case FT_NEG:
{
if( std::tr1::dynamic_pointer_cast< FormulaNeg >( p_pNewEffects )->GetCNegForm()->GetType() != FT_EQU ||
std::tr1::dynamic_pointer_cast< FormulaNeg >( m_vRemainingPrecs[ p_iPrecondIndex ] )->GetCNegForm()->GetType() != FT_EQU )
throw Exception( E_NOT_IMPLEMENTED,
"Only equality formulas may be negated in preconditions.",
__FILE__,
__LINE__ );
TermVariableP l_pOldVar = std::tr1::dynamic_pointer_cast< TermVariable >( std::tr1::dynamic_pointer_cast< FormulaEqu >( std::tr1::dynamic_pointer_cast< FormulaNeg >( p_pNewEffects )->GetCNegForm() )->GetCFirst() );
TermVariableP l_pNewVar = std::tr1::dynamic_pointer_cast< TermVariable >( std::tr1::dynamic_pointer_cast< FormulaEqu >( std::tr1::dynamic_pointer_cast< FormulaNeg >( m_vRemainingPrecs[ p_iPrecondIndex ] )->GetCNegForm() )->GetCFirst() );
DoAddListTermReplacement( l_pOldVar,
l_pNewVar,
p_pSub,
l_ReplaceSub );
l_pOldVar = std::tr1::dynamic_pointer_cast< TermVariable >( std::tr1::dynamic_pointer_cast< FormulaEqu >( std::tr1::dynamic_pointer_cast< FormulaNeg >( p_pNewEffects )->GetCNegForm() )->GetCSecond() );
l_pNewVar = std::tr1::dynamic_pointer_cast< TermVariable >( std::tr1::dynamic_pointer_cast< FormulaEqu >( std::tr1::dynamic_pointer_cast< FormulaNeg >( m_vRemainingPrecs[ p_iPrecondIndex ] )->GetCNegForm() )->GetCSecond() );
DoAddListTermReplacement( l_pOldVar,
l_pNewVar,
p_pSub,
l_ReplaceSub );
}
break;
case FT_EQU:
{
TermVariableP l_pOldVar = std::tr1::dynamic_pointer_cast< TermVariable >( std::tr1::dynamic_pointer_cast< FormulaEqu >( p_pNewEffects )->GetCFirst() );
TermVariableP l_pNewVar = std::tr1::dynamic_pointer_cast< TermVariable >( std::tr1::dynamic_pointer_cast< FormulaEqu >( m_vRemainingPrecs[ p_iPrecondIndex ] )->GetCFirst() );
DoAddListTermReplacement( l_pOldVar,
l_pNewVar,
p_pSub,
l_ReplaceSub );
l_pOldVar = std::tr1::dynamic_pointer_cast< TermVariable >( std::tr1::dynamic_pointer_cast< FormulaEqu >( p_pNewEffects )->GetCSecond() );
l_pNewVar = std::tr1::dynamic_pointer_cast< TermVariable >( std::tr1::dynamic_pointer_cast< FormulaEqu >( m_vRemainingPrecs[ p_iPrecondIndex ] )->GetCSecond() );
DoAddListTermReplacement( l_pOldVar,
l_pNewVar,
p_pSub,
l_ReplaceSub );
}
break;
default:
throw Exception( E_NOT_IMPLEMENTED,
"Formula is of unknown type.",
__FILE__,
__LINE__ );
}