@@ -1308,27 +1308,23 @@ def hierarchical_search(problem, hierarchy):
1308
1308
The problem is a real-world problem defined by the problem class, and the hierarchy is
1309
1309
a dictionary of HLA - refinements (see refinements generator for details)
1310
1310
"""
1311
- act = Node (problem .actions [0 ])
1311
+ act = Node (problem .init , None , [ problem . actions [0 ] ])
1312
1312
frontier = deque ()
1313
1313
frontier .append (act )
1314
1314
while True :
1315
1315
if not frontier :
1316
1316
return None
1317
1317
plan = frontier .popleft ()
1318
- print (plan .state .name )
1319
- hla = plan .state # first_or_null(plan)
1320
- prefix = None
1321
- if plan .parent :
1322
- prefix = plan .parent .state .action # prefix, suffix = subseq(plan.state, hla)
1323
- outcome = Problem .result (problem , prefix )
1324
- if hla is None :
1318
+ (hla , index ) = Problem .find_hla (plan , hierarchy ) # finds the first non primitive hla in plan actions
1319
+ prefix = plan .action [:index ]
1320
+ outcome = Problem (Problem .result (problem .init , prefix ), problem .goals , problem .actions )
1321
+ suffix = plan .action [index + 1 :]
1322
+ if not hla : # hla is None and plan is primitive
1325
1323
if outcome .goal_test ():
1326
- return plan .path ()
1324
+ return plan .action
1327
1325
else :
1328
- print ("else" )
1329
- for sequence in Problem .refinements (hla , outcome , hierarchy ):
1330
- print ("..." )
1331
- frontier .append (Node (plan .state , plan .parent , sequence ))
1326
+ for sequence in Problem .refinements (hla , outcome , hierarchy ): # find refinements
1327
+ frontier .append (Node (outcome .init , plan , prefix + sequence + suffix ))
1332
1328
1333
1329
def result (state , actions ):
1334
1330
"""The outcome of applying an action to the current problem"""
@@ -1365,12 +1361,12 @@ def angelic_search(problem, hierarchy, initialPlan):
1365
1361
if Problem .is_primitive ( plan , hierarchy ):
1366
1362
return ([x for x in plan .action ])
1367
1363
guaranteed = problem .intersects_goal (pes_reachable_set )
1368
- if guaranteed and Problem .making_progress (plan , plan ):
1364
+ if guaranteed and Problem .making_progress (plan , initialPlan ):
1369
1365
final_state = guaranteed [0 ] # any element of guaranteed
1370
1366
#print('decompose')
1371
1367
return Problem .decompose (hierarchy , problem , plan , final_state , pes_reachable_set )
1372
1368
(hla , index ) = Problem .find_hla (plan , hierarchy ) # there should be at least one HLA/Angelic_HLA, otherwise plan would be primitive.
1373
- prefix = plan .action [:index - 1 ]
1369
+ prefix = plan .action [:index ]
1374
1370
suffix = plan .action [index + 1 :]
1375
1371
outcome = Problem (Problem .result (problem .init , prefix ), problem .goals , problem .actions )
1376
1372
for sequence in Problem .refinements (hla , outcome , hierarchy ): # find refinements
@@ -1450,30 +1446,33 @@ def find_hla(plan, hierarchy):
1450
1446
1451
1447
def making_progress (plan , initialPlan ):
1452
1448
"""
1453
- Not correct
1449
+ Prevents from infinite regression of refinements
1454
1450
1455
- Normally should from infinite regression of refinements
1456
-
1457
- Only case covered: when plan contains one action (then there is no regression to be done)
1451
+ (infinite regression of refinements happens when the algorithm finds a plan that
1452
+ its pessimistic reachable set intersects the goal inside a call to decompose on the same plan, in the same circumstances)
1458
1453
"""
1459
- if (len (plan .action )== 1 ):
1460
- return False
1454
+ for i in range (len (initialPlan )):
1455
+ if (plan == initialPlan [i ]):
1456
+ return False
1461
1457
return True
1462
1458
1463
1459
def decompose (hierarchy , s_0 , plan , s_f , reachable_set ):
1464
1460
solution = []
1461
+ i = max (reachable_set .keys ())
1465
1462
while plan .action_pes :
1466
1463
action = plan .action_pes .pop ()
1467
- i = max (reachable_set .keys ())
1468
1464
if (i == 0 ):
1469
1465
return solution
1470
1466
s_i = Problem .find_previous_state (s_f , reachable_set ,i , action )
1471
1467
problem = Problem (s_i , s_f , plan .action )
1472
- j = 0
1473
- for x in Problem .angelic_search (problem , hierarchy , [Angelic_Node (s_i , Node (None ), [action ],[action ])]):
1474
- solution .insert (j ,x )
1475
- j += 1
1468
+ angelic_call = Problem .angelic_search (problem , hierarchy , [Angelic_Node (s_i , Node (None ), [action ],[action ])])
1469
+ if angelic_call :
1470
+ for x in angelic_call :
1471
+ solution .insert (0 ,x )
1472
+ else :
1473
+ return None
1476
1474
s_f = s_i
1475
+ i -= 1
1477
1476
return solution
1478
1477
1479
1478
0 commit comments