AI-Planning/pddl

Write the problem dynamically

RodrigoFBernardo opened this issue · 8 comments

Hi,

Is there any example of generating a problem using the information in a python dictionary? Or how to write the problem dynamically?

Thank you very much in advance

haz commented

Not sure I follow...what are you looking for beyond what's in the main README?

For example, based on this yaml file:

types:
  robot: 
    - idmind
  waypoints: 
    - wp1
    - wp2
    - wp3
init:
  robot_at:
    - wp1
  docked:
    - idmind
  dock_at:
    - wp1

goals:  
  visited:
    - wp1
    - wp2
    - wp3
  docked:
    - idmind  

I want to generate my problem.pddl file

By now, I have managed to write the whole problem except for the goal. Because it needs a "Formula" data structure.

I leave an excerpt of the code I've done so far.:

with open(file_,"rb") as file:
    Dict = yaml.full_load(file)
  
    for item, doc in Dict.items():
        if (item == "types"):
            types = doc
        elif ( item == "init"):
            init = doc 
        elif (item == "goals"):
            goals = doc

    
    if "types" in locals():
        elements_keys = []; objects_ = []
        element = list(types.keys())        
        for x in range(len(element)):
            elements_keys.append(list(types[str(element[x])]))
            for i in range(len(elements_keys)):
                objects_.append(Constant(str(elements_keys[x][i]), type_tags=[str(element[x])]))
    else:
        print("Error types is nod defined in file of config{file_}")
problem = Problem(
    "problem-1",
    domain=domain,
    requirements=requirements,
    objects= objects_,
    init = init_,
    goal= goals_ ,
)

print(problem_to_string(problem))

I'm now seeing how I can write automatically as I did in the example above: p1(a, b) & p2(c,d). I haven't found how to put the symbol &.

haz commented

From the README, it's just a list:

image

Yes, but the goal is not a list. For example, taking example, if we want to add another goal, then goal = p2(b,c) & p2(b,c),.

haz commented

Right, the goal is a Formula: https://github.com/AI-Planning/pddl/blob/main/pddl/core.py#L132

You're curious how to build it up from a list of fluents?

Yes, that's the format.

however I still can't understand how I can write from a list ("list of fluents").

For exemple: How can I convert such a list to the formula format?

[Predicate(visited, wp1), Predicate(docked, idmind)]

The result should be: visited(wp1) & docked(idmind)

The result in pddl: (:goal (and visited(wp1) docked(idmind) ))

haz commented

Hrmz...maybe try this:

from pddl.logic.base import And
new_goal = And(Predicate(visited, wp1), Predicate(docked, idmind))

Or if it's already in a list...

from pddl.logic.base import And
goal_list = [Predicate(visited, wp1), Predicate(docked, idmind)]
new_goal = And(*goal_list)

Yes, that's right. Thank you very much.