aiplan4eu / unified-planning

The AIPlan4EU Unified Planning Library
Apache License 2.0
181 stars 39 forks source link

Matching a string plan to a compiled task #618

Open MFaisalZaki opened 2 months ago

MFaisalZaki commented 2 months ago

I'm facing a situation where I'm reading plans from files written for a non-grounded task and I have idea how to map them to a grounded task.

Here is an example: Assume that I have this plan for the rover domain:

plan1 = (navigate rover1 waypoint3 waypoint0)\n(calibrate rover1 camera1 objective0 waypoint0)\n(take_image rover1 waypoint0 objective0 camera1 colour)\n(sample_rock rover1 rover1store waypoint0)\n(drop rover1 rover1store)\n(navigate rover1 waypoint0 waypoint3)\n(communicate_rock_data rover1 general waypoint0 waypoint3 waypoint0)\n(navigate rover1 waypoint3 waypoint2)\n(communicate_image_data rover1 general objective0 colour waypoint2 waypoint0)\n(sample_soil rover1 rover1store waypoint2)\n(communicate_soil_data rover1 general waypoint2 waypoint2 waypoint0)\n; 11 cost (unit)

I use the PDDLReader().parse_plan_string(task, plan) to read it. Also, I have grounded this task; my question here is how to match the plan1 actions to the grounded version.

Here is my code:

plan1 = '(navigate rover1 waypoint3 waypoint0)\n(calibrate rover1 camera1 objective0 waypoint0)\n(take_image rover1 waypoint0 objective0 camera1 colour)\n(sample_rock rover1 rover1store waypoint0)\n(drop rover1 rover1store)\n(navigate rover1 waypoint0 waypoint3)\n(communicate_rock_data rover1 general waypoint0 waypoint3 waypoint0)\n(navigate rover1 waypoint3 waypoint2)\n(communicate_image_data rover1 general objective0 colour waypoint2 waypoint0)\n(sample_soil rover1 rover1store waypoint2)\n(communicate_soil_data rover1 general waypoint2 waypoint2 waypoint0)\n; 11 cost (unit)'

domainfile  = 'domain.pddl'
problemfile = 'p03.pddl'

task = PDDLReader().parse_problem(domainfile, problemfile)

compilationlist = [
    ('up_quantifiers_remover', CompilationKind.QUANTIFIERS_REMOVING), 
    ('up_disjunctive_conditions_remover', CompilationKind.DISJUNCTIVE_CONDITIONS_REMOVING), 
    ('up_grounder', CompilationKind.GROUNDING)
]

names = [name for name, _ in compilationlist]
compilationkinds = [kind for _, kind in compilationlist]
with Compiler(names=names, compilation_kinds=compilationkinds) as compiler:
    compiled_task = compiler.compile(task)

original_plan = PDDLReader().parse_plan_string(task, plan1)
# How to get the grounded version.

The way I do it now using this line of code:

converted_plan = list(map(lambda p: PDDLReader().parse_plan_string(compiled_task.problem,  PDDLWriter(compiled_task.problem).get_plan(PDDLReader().parse_plan_string(task, p)).replace(' ', '_')), set([plan1])))

task.zip