PDDL in Python – Python DSL for writing a PDDL

A minimal implementation of a DSL which allows people to write PDDL in python. Based on parsing python’s AST.

Author: Masataro Asai

License: MIT.

Example in examples/blocksworld.py:

#+begin_src python
class Blocksworld(Domain):
    def move_b_to_b(bm, bf, bt):
        if clear[bm] and clear[bt] and on[bm, bf]:
            clear[bt]  = False
            on[bm, bf] = False
            on[bm, bt] = True
            clear[bf]  = True

    def move_b_to_t(bm, bf):
        if clear[bm] and on[bm, bf]:
            on[bm, bf]   = False
            on_table[bm] = True
            clear[bf]    = True

    def move_t_to_b(bm, bt):
        if clear[bm] and clear[bt] and on_table[bm]:
            clear[bt]    = False
            on_table[bm] = False
            on[bm, bt]   = True

print(Blocksworld())
#+end_src

will print

#+begin_src lisp
(domain blocksworld
  (:requirement :strips)
  (:types)
  (:predicates
    (clear ?x0)
    (on ?x0 ?x1)
    (on-table ?x0))
  (:action move-b-to-b :parameters (?bm ?bf ?bt)
   :preconditions
   (and
     (clear ?bm)
     (clear ?bt)
     (on ?bm ?bf))
   :effects
   (and
     (not (clear ?bt))
     (not (on ?bm ?bf))
     (on ?bm ?bt)
     (clear ?bf)))
  (:action move-b-to-t :parameters (?bm ?bf)
   :preconditions
   (and
     (clear ?bm)
     (on ?bm ?bf))
   :effects
   (and
     (not (on ?bm ?bf))
     (on-table ?bm)
     (clear ?bf)))
  (:action move-t-to-b :parameters (?bm ?bt)
   :preconditions
   (and
     (clear ?bm)
     (clear ?bt)
     (on-table ?bm))
   :effects
   (and
     (not (clear ?bt))
     (not (on-table ?bm))
     (on ?bm ?bt))))
#+end_src

Example in examples/briefcaseworld.py:

#+begin_src python
class location:
    pass
class portable:
    pass
class Briefcaseworld(Domain):
    def move(m : location, l : location):
        if is_at[m]:
            is_at[l] = True
            is_at[m] = False
            for x in all(portable): # current python syntax does not allow annotating loop variable
                if _in[x]:
                    at[x,l] = True
                    at[x,m] = False

    def take_out(x : portable):
        if _in[x]:
            _in[x] = False

    def put_in(x : portable, l : location):
        if not _in[x] and at[x,l] and is_at[l]:
            _in[x] = True

print(Briefcaseworld())
#+end_src

will print

#+begin_src lisp
(domain briefcaseworld
  (:requirement :strips)
  (:types
    portable - object)
  (:predicates
    (is-at ?x0)
    (in ?x0)
    (at ?x0 ?x1))
  (:action move :parameters (?m - location ?l - location)
   :preconditions
   (is-at ?m)
   :effects
   (and
     (is-at ?l)
     (not (is-at ?m))
     (forall (?x - portable)
       (when (in ?x)
         (and
           (at ?x ?l)
           (not (at ?x ?m)))))))
  (:action put-in :parameters (?x - portable ?l - location)
   :preconditions
   (and
     (not (in ?x))
     (at ?x ?l)
     (is-at ?l))
   :effects
   (in ?x))
  (:action take-out :parameters (?x - portable)
   :preconditions
   (in ?x)
   :effects
   (not (in ?x))))
#+end_src

GitHub

https://github.com/IBM/pddl-in-python