# -*- coding: iso-8859-15 -*-
###===================================================================
### UTILITAIRES POUR LE PLANNING DE BLOCS
###===================================================================
## Définit un opérateur pickup
def pickupOperator(b):
return ['(pickup '+b+')',
['(clear '+b+')', '(on-table '+b+')', 'handempty'],
['(holding '+b+')'],
['(clear '+b+')', '(on-table '+b+')', 'handempty'],
1]
## Définit un opérateur putdown
def putdownOperator(b):
return ['(putdown '+b+')',
['(holding '+b+')'],
['(on-table '+b+')', '(clear '+b+')', 'handempty'],
['(holding '+b+')'],
1]
## Définit un opérateur unstack
def unstackOperator(b1,b2):
return ['(unstack '+b1+' '+b2+')',
['(on '+b1+' '+b2+')', '(clear '+b1+')', 'handempty'],
['(holding '+b1+')', '(clear ' +b2+')'],
['(on '+b1+' '+b2+')', '(clear '+b1+')', 'handempty'],
1]
## Définit un opérateur stack
def stackOperator(b1,b2):
return ['(stack '+b1+' '+b2+')',
['(holding '+b1+')', '(clear '+b2+')'],
['(on '+b1+' '+b2+')', '(clear '+b1+')', 'handempty'],
['(holding '+b1+')', '(clear '+b2+')'],
1]
## Crée le plannificateur
## @param blocks Les blocs (a,b,c, ...)
## @param initialSituation La situation initiale
## @param finalSituation La situation à atteindre
def buildBlocksPlanner(blocks, initialSituation, finalSituation):
ops = []
for b1 in blocks:
for b2 in blocks:
if b1 == b2:
ops = [putdownOperator(b1)] + [pickupOperator(b1)] + ops
else:
ops = [stackOperator(b1, b2)] + [unstackOperator(b1, b2)] + ops
buildPlanner(initialSituation, finalSituation, ops,
consistentBlocks,
lambda s : s.meansEnds() + s.opCost())
## Définit la consistence d'un état
## @param s L'état
def consistentBlocks(s):
if s.operators and len(s.operators) > 1:
a1 = (s.operators[0]).action
a2 = (s.operators[1]).action
if a1[1:] == a2[1:]:
expr1 = a1[0]
expr2 = a2[0]
if expr1 == 'unstack':
return not expr2 == 'stack'
elif expr1 == 'stack':
return not expr2 == 'unstack'
elif expr1 == 'pickup':
return not expr2 == 'putdown'
elif expr1 == 'putdown':
return not expr2 == 'pickup'
return True