AI-Planning / pddl

Unquestionable PDDL 3.1 parser
https://ai-planning.github.io/pddl/
MIT License
85 stars 27 forks source link

Suspect formatting bug with Type Hierarchy #115

Closed marcofavorito closed 5 months ago

marcofavorito commented 5 months ago

Subject of the issue

This issue has been created to investigate more on several issue the user @fenjenahwe is experiencing with the library. Some of the issues the user encountered have been reported in the comments of issue #98, and they are about the formatting of domains with a non-flat hierarchy of types.

The user reported in this comment that the following code:

from pddl.core import Domain, Requirements
from pddl.formatter import domain_to_string

requirements = [Requirements.STRIPS, Requirements.TYPING, Requirements.NEG_PRECONDITION]
domain = Domain("dining-general",
    requirements=requirements,
    types={
        "objects": "world",
        "location": "world",
        "hypothesis": "world",
        "agent": "objects",
        "furniture": "objects",
        "consumable": "objects",
    })
print(domain_to_string(domain))

Prints:

(define (domain dining-general)
    (:requirements :negative-preconditions :strips :typing)
    (:types agent consumable furniture hypothesis location objects)

Whereas the expected behaviour is something like:

(define (domain dining-general)
    (:requirements :negative-preconditions :strips :typing)
    (:types agent consumable furniture - objects hypothesis location objects - world)
)
marcofavorito commented 5 months ago

Hi @fenjenahwe, I tried to install the library in a fresh virtual environment (Python 3.10.12). I installed pddl at the latest version at the time of writing, which is 0.4.0.

The above code gave me the expected outcome, follows a screenshot: image

Please, @fenjenahwe, can you provide more details on your environment (i.e., operating system, Python version, pddl version...) ? It would be greatly appreciated to better understand the issue you are having.

marcofavorito commented 5 months ago

Also, the result can be parsed correctly:

from pathlib import Path
from pddl.core import Domain, Requirements
from pddl.formatter import domain_to_string
from pddl import parse_domain

requirements = [Requirements.STRIPS, Requirements.TYPING, Requirements.NEG_PRECONDITION]
types = {
        "A": "B",
        "B": "C",
        "C": None
    }
domain = Domain("example",
    requirements=requirements,
    types=types
)

# write domain to file
Path("example.pddl").write_text(domain_to_string(domain))

# parse the domaion
actual_domain = parse_domain(Path("example.pddl"))

print(domain_to_string(actual_domain))

print("Original domain types:")
print(types)
print("Parsed domain types:")
print(actual_domain.types)

The output of the above code snippet is:

(define (domain example)
    (:requirements :negative-preconditions :strips :typing)
    (:types A - B B - C C)
)
Original domain types:
{'A': 'B', 'B': 'C', 'C': None}
Parsed domain types:
{'A': 'B', 'B': 'C', 'C': None}
fenjenahwe commented 5 months ago

Thanks @marcofavorito

My environment details are: OS: Ubuntu 22.04.4 Python version: 3.10.12

While reinstalling the library in a new environment, I noticed I had been installing pddl version 0.2.0 This must be the reason, my apologies. I will try with the newest library version and report back if it doesn't solve it.

marcofavorito commented 5 months ago

Perfect, thank you so much! If you don't mind, please let us know when you tried so we can be sure it is not an issue.

fenjenahwe commented 5 months ago

I confirm! This solved it. Thanks a lot for helping so quickly and sorry for the oversight.

marcofavorito commented 5 months ago

Perfect! No worries :)