pddl---FF Planner

FF Planer online: http://editor.planning.domains/# 注意不要使用https否则会报malformed url

pddl---Planning Domain Definition Language语法:

基本内容:

• Domain Name • Requirements • Types • Constants • Domain Variables • Predicates • Actions

以下图为例:

演示demo

  • spare tire

domain_spare_tire_d.pddl

;给出domain名称,语法使用就是(domain 名称)
(define (domain spare_tire )
   ;给出依赖项,比如这里的三个都是依赖项,语法使用是“:依赖项”,
   ;strip---The most basic subset of PDDL, consisting of STRIPS only
   ;equality---This requirement means that the domain uses the predicate =, interpreted as equality.
   ;typing---This requirement means that the domain uses types (see Typing below).
   ;adl---Means that the domain uses some or all of ADL (相当于同时满足这几个:strips:typing
:disjunctive-preconditions:equality:quantified-preconditions:condition-effects)
   (:requirements : strips : equality : typing )
   (:types physob location )
   ;定义该domain当中需要使用的类型,比如physic object、location都是类型,直接将你需要定义的类型加入到:type的后面
   (:predicates  ( Tire ?x − physob);define predicates here
                  ( at ?x − physob ?y − location ))
   ;定义该domain需要使用的谓词,单元谓词表示属性,如这里的Tire x表示x这个physic object是否有轮胎属性。
   ;谓词名称最先给出,然后依次给出该谓词的项,格式为“?参数 – 参数类型”,
   ;比如这里的“?x – physob ?y – physob”表示x这个参数的类型应该是physob的,不能传递其他类型的参数。
   ;整个“Tire ?x – physob”对应Tire(x)这一原子(公式)。
   ;二元谓词表示关系,比如这里的at表示x物体是否在y这个位置,分析与上面的单元谓词类似

;车轴上卸下胎x,x放地上
    (:action Remove
    :parameters (?x − physob ?y − location )
    :precondition (At ?x ?y)
    :effect (and(not(At ?x ?y) ) (At ?x Ground) )
    )
    ;定义该domain中的操作,这里的remove表示移除操作,这个操作需要给定参数,参数定义规则与前面的谓词一样;
    ;action执行需要一定的前提条件,前提条件是一个逻辑表达式,
    ;根据其真值判断操作是否可以执行;
    ;最后执行这个操作会导致一定的结果,这在effect中指定,指定的effect会改写当前状态(相当于改写知识库中基础原子的真值)
    ;语法上来说,precondition后面可以给出原子公式的与或非组合,
    ;其中原子公式语法前面谓词说过了,非:(not 原子公式);与:(and 原子公式1 原子公式2 等等);或与and用法一致,把and换为or即可

    (:action PutOn
    :parameters (?x − physob)
    :precondition (and( Tire ?x) (At ?x Ground)
                   (not(At Flat Axle) ) )
    :effect (and(not(At ?x Ground) ) (At ?x Axle) ) 
    ) ;把地上的X装上车轴

;放着不动过夜
    (:action LeaveOvernight
    :effect (and(not(At Spare Ground) ) (not(At Spare Axle) )
             (not(At Spare Trunk) ) (not(At Flat Ground) )
             (not(At Flat Axle) ) (not(At Flat Trunk) )
             )
    )

)

具体解释这个例子的含义 spare_tire_p.pddl

 (define (problem prob)
  (:domain spare_tire )
  (:objects Flat Spare −physob Axle Trunk Ground − location )
  (:init ( Tire Flat ) ( Tire Spare) (At Flat Axle) (At Spare Trunk) )
  (:goal (At Spare Axle) )
 )

然后执行

$ ff -o domain_spare_tire.pddl -f spare_tire.pddl

ff: parsing domain file
domain 'SPARE_TIRE' defined
 ... done.
ff: parsing problem file
problem 'PROB' defined
 ... done.



Cueing down from goal distance:    3 into depth [1]
                                   2            [1]
                                   1            [1]
                                   0

ff: found legal plan as follows

step    0: REMOVE FLAT AXLE把原来的轮胎flat卸下车轴
        1: REMOVE SPARE TRUNK把spare胎卸下货车
        2: PUTON SPARE装上车轴,到达目标


time spent:    0.00 seconds instantiating 9 easy, 0 hard action templates
               0.00 seconds reachability analysis, yielding 11 facts and 8 actions
               0.00 seconds creating final representation with 10 relevant facts
               0.00 seconds building connectivity graph
               0.00 seconds searching, evaluating 4 states, to a max depth of 1
               0.00 seconds total time
  • breiefcase world

forall和when的用法,

首先讲一下这个domain的含义,然后将下面的forall和when用法

briefcase_d.pddl

(define (domain briefcase)
(:requirements :strips :typing :conditional-effects :universal-preconditions)
(:types portable location - object)
(:predicates (at ?y - portable ?x - location)
             (in ?x - portable)
             (is-at ?x - location))


(:action move
  :parameters (?m ?l - location)
  :precondition  (is-at ?m)
  :effect (and (is-at ?l) (not (is-at ?m))
            (forall (?x - portable) (when (in ?x)
              (and (at ?x ?l) (not (at ?x ?m)))))))

  (:action take-out
      :parameters (?x - portable)
      :precondition (in ?x)
      :effect (not (in ?x)))

  (:action put-in
      :parameters (?x - portable ?l - location)
      :precondition (and (not (in ?x)) (at ?x ?l) (is-at ?l))
      :effect (in ?x)))

然后就是数据文件,给出这个问题的定义:

;注意到写在effect中的这段代码: (forall (?z - physob) (when (and (in ?z) (not (= ?z B))) ;这里意思就是遍历所有的object z,当z在B中,就产生effect ;即将z的位置location设置为l(因为z在briefcase当中,briefcase被携带到了l地点,那么z自然也被移动到l) (and (at ?z ?l) (not (at ?z ?m))) ;这里必须使用forall,因为你无法设置是哪个对象在briefcase中,所以需要遍历判断

它涉及的domain、 问题的参数定义、 问题的初始状态、 问题的目标状态

briefcase_p.pddl

(define (problem pb1)
    (:domain briefcase)
    (:requirements :strips :typing :conditional-effects :universal-preconditions)
    (:objects home l1 - location
             o1 - portable)
    (:init (is-at home) (at o1 l1))              
    (:goal (and (is-at home)  (at o1 home)))
)

定义好问题后我们调用求解器,他会给我们求解出一系列的action使得问题从初始状态到达目标状态(如果有解) result

$ ff -o briefcase_d.pddl  -f br
iefcase_p.pddl

ff: parsing domain file
domain 'BRIEFCASE' defined
 ... done.
ff: parsing problem file
problem 'PB1' defined
 ... done.



Cueing down from goal distance:    3 into depth [1][2]
                                   1            [1]
                                   0

ff: found legal plan as follows

step    0: MOVE HOME L1
        1: PUT-IN O1 L1
        2: MOVE L1 HOME


time spent:    0.01 seconds instantiating 7 easy, 0 hard action templates
               0.00 seconds reachability analysis, yielding 6 facts and 7 actions
               0.00 seconds creating final representation with 6 relevant facts
               0.00 seconds building connectivity graph
               0.00 seconds searching, evaluating 4 states, to a max depth of 2
               0.01 seconds total time
  • boxman

boxman_d.pddl

(define (domain boxman)
  (:requirements :strips :typing:equality
                 :universal-preconditions
                 :conditional-effects)
  (:types loc)
  (:predicates
      (boxAt ?y - loc)
      (manAt ?y - loc)
      (adjacent ?x - loc ?y -loc) 
      (notClear ?x - loc)
      (line ?x ?y ?z -loc)
      )   

    (:action push
    :parameters(?mp ?bp ?np - loc ?)
    :precondition(and (manAt ?mp)(boxAt ?bp)(not (notClear ?np))(line ?mp ?bp ?np))
        :effect(and (not (notClear ?mp))(notClear ?np)
                    (not (manAt ?mp))(manAt ?bp)
                    (not (boxAt ?bp))(boxAt ?np)
        )
    )

    (:action move
        :parameters(?x ?y - loc)
        :precondition(and (manAt ?x)(not (notClear ?y))(adjacent ?x ?y) )
        :effect(and (not (manAt ?x))(manAt ?y)
                    (not (notClear ?x))(notClear ?y)
        )
    )


)

boxman_p.pddl

(define (problem prob)
    (:domain boxman)
    (:objects P1 P2 P3 P4 P5 P6 P7 P8 P9 P10 P11 P12 P13 P14 P15 P16 P17 P18 P19 P20 - loc)
    (:init (manAt P20) (boxAt P7) (boxAt P11) (boxAt P17) (notClear P20) (notClear P7) (notClear P11) (notClear P17)
            (adjacent P1 P2) (adjacent P1 P3) 
            (adjacent P2 P1) (adjacent P2 P4)
            (adjacent P3 P1) (adjacent P3 P4) (adjacent P3 P5)
            (adjacent P4 P2) (adjacent P4 P3) (adjacent P4 P6)
            (adjacent P5 P3) (adjacent P5 P6)
            (adjacent P6 P4) (adjacent P6 P5) (adjacent P6 P7) (adjacent P6 P11)
            (adjacent P7 P8) (adjacent P7 P6) (adjacent P7 P12)
            (adjacent P8 P9) (adjacent P8 P7) (adjacent P8 P13)
            (adjacent P9 P10) (adjacent P9 P8) (adjacent P9 P14)
            (adjacent P10 P9) (adjacent P10 P15)
            (adjacent P11 P12) (adjacent P11 P6) (adjacent P11 P16)
            (adjacent P12 P7) (adjacent P12 P11) (adjacent P12 P13)
            (adjacent P13 P8) (adjacent P13 P14) (adjacent P13 P12) (adjacent P13 P17)
            (adjacent P14 P15) (adjacent P14 P9) (adjacent P14 P13)
            (adjacent P15 P14) (adjacent P15 P10)
            (adjacent P16 P11) (adjacent P16 P18)
            (adjacent P17 P13) (adjacent P17 P20)
            (adjacent P18 P16) (adjacent P18 P19)
            (adjacent P19 P18) (adjacent P19 P20)
            (adjacent P20 P17) (adjacent P20 P19)
            (line P1 P3 P5)
            (line P2 P4 P6)
            (line P4 P6 P11)
            (line P6 P11 P16)
            (line P11 P16 P18)
            (line P8 P13 P17)
            (line P13 P17 P20)
            (line P5 P6 P7)
            (line P6 P7 P8)
            (line P7 P8 P9)
            (line P8 P9 P10)
            (line P11 P12 P13)
            (line P12 P13 P14)
            (line P13 P14 P15)
            (line P18 P19 P20)
            (line P5 P3 P1)
            (line P6 P4 P2)
            (line P11 P6 P4)
            (line P16 P11 P6)
            (line P18 P16 P11)
            (line P17 P13 P8)
            (line P20 P17 P13)
            (line P7 P6 P5)
            (line P8 P7 P6)
            (line P9 P8 P7)
            (line P10 P9 P8)
            (line P13 P12 P11)
            (line P14 P13 P12)
            (line P15 P14 P13)
            (line P20 P19 P18)
    )
    (:goal (and (boxAt P7) (boxAt P6) (boxAt P18)) )
)

result

$ ff -o boxman_d.pddl  -f boxman_p.pddl

ff: parsing domain file
domain 'BOXMAN' defined
 ... done.
ff: parsing problem file
problem 'PROB' defined
 ... done.



Cueing down from goal distance:   11 into depth [1]
                                   6            [1]
                                   5            [1]
                                   4            [1][2]

Enforced Hill-climbing failed !
switching to Best-first Search now.

advancing to distance :   11
                           6
                           5
                           4
                           3
                           2
                           1
                           0

ff: found legal plan as follows

step    0: MOVE P20 P19
        1: MOVE P19 P18
        2: MOVE P18 P16
        3: PUSH P16 P11 P6
        4: MOVE P11 P16
        5: MOVE P16 P18
        6: MOVE P18 P19
        7: MOVE P19 P20
        8: PUSH P20 P17 P13
        9: MOVE P17 P20
       10: MOVE P20 P19
       11: MOVE P19 P18
       12: MOVE P18 P16
       13: MOVE P16 P11
       14: PUSH P11 P6 P4
       15: MOVE P6 P5
       16: MOVE P5 P3
       17: MOVE P3 P1
       18: MOVE P1 P2
       19: PUSH P2 P4 P6
       20: PUSH P4 P6 P11
       21: PUSH P6 P11 P16
       22: MOVE P11 P12
       23: PUSH P12 P13 P14
       24: MOVE P13 P8
       25: MOVE P8 P9
       26: MOVE P9 P10
       27: MOVE P10 P15
       28: PUSH P15 P14 P13
       29: PUSH P14 P13 P12
       30: MOVE P13 P17
       31: MOVE P17 P20
       32: MOVE P20 P19
       33: MOVE P19 P18
       34: PUSH P18 P16 P11
       35: PUSH P16 P11 P6
       36: PUSH P11 P6 P4
       37: MOVE P6 P5
       38: MOVE P5 P3
       39: MOVE P3 P1
       40: MOVE P1 P2
       41: PUSH P2 P4 P6
       42: PUSH P4 P6 P11
       43: PUSH P6 P11 P16
       44: MOVE P11 P6
       45: PUSH P6 P7 P8
       46: PUSH P7 P8 P9
       47: MOVE P8 P13
       48: MOVE P13 P17
       49: MOVE P17 P20
       50: MOVE P20 P19
       51: MOVE P19 P18
       52: PUSH P18 P16 P11
       53: PUSH P16 P11 P6
       54: PUSH P11 P6 P4
       55: MOVE P6 P5
       56: MOVE P5 P3
       57: MOVE P3 P1
       58: MOVE P1 P2
       59: PUSH P2 P4 P6
       60: MOVE P4 P3
       61: MOVE P3 P5
       62: PUSH P5 P6 P7
       63: MOVE P6 P11
       64: MOVE P11 P16
       65: MOVE P16 P18
       66: MOVE P18 P19
       67: MOVE P19 P20
       68: MOVE P20 P17
       69: MOVE P17 P13
       70: PUSH P13 P12 P11
       71: MOVE P12 P13
       72: MOVE P13 P17
       73: MOVE P17 P20
       74: MOVE P20 P19
       75: MOVE P19 P18
       76: MOVE P18 P16
       77: PUSH P16 P11 P6
       78: PUSH P11 P6 P4
       79: MOVE P6 P5
       80: MOVE P5 P3
       81: MOVE P3 P1
       82: MOVE P1 P2
       83: PUSH P2 P4 P6
       84: PUSH P4 P6 P11
       85: PUSH P6 P11 P16
       86: PUSH P11 P16 P18
       87: MOVE P16 P11
       88: MOVE P11 P12
       89: MOVE P12 P13
       90: MOVE P13 P8
       91: PUSH P8 P7 P6
       92: MOVE P7 P8
       93: MOVE P8 P13
       94: MOVE P13 P14
       95: MOVE P14 P15
       96: MOVE P15 P10
       97: PUSH P10 P9 P8
       98: PUSH P9 P8 P7


time spent:    0.00 seconds instantiating 82 easy, 0 hard action templates
               0.00 seconds reachability analysis, yielding 77 facts and 78 actions
               0.00 seconds creating final representation with 77 relevant facts
               0.00 seconds building connectivity graph
               0.01 seconds searching, evaluating 4711 states, to a max depth of 2
               0.01 seconds total time

网站http://editor.planning.domains/# 还找到不一样的求解过程:

Found Plan (output)
(push p20 p17 p13)
(move p17 p20)
(move p20 p19)
(move p19 p18)
(move p18 p16)
(push p16 p11 p6)
(move p11 p12)
(push p12 p13 p14)
(move p13 p8)
(move p8 p9)
(move p9 p10)
(move p10 p15)
(push p15 p14 p13)
(push p14 p13 p12)
(move p13 p17)
(move p17 p20)
(move p20 p19)
(move p19 p18)
(move p18 p16)
(move p16 p11)
(push p11 p6 p4)
(push p6 p7 p8)
(push p7 p8 p9)
(move p8 p13)
(push p13 p12 p11)
(move p12 p7)
(move p7 p6)
(push p6 p11 p16)
(push p11 p16 p18)
(move p16 p11)
(move p11 p6)
(move p6 p7)
(move p7 p8)
(move p8 p13)
(move p13 p14)
(move p14 p15)
(move p15 p10)
(push p10 p9 p8)
(push p9 p8 p7)
(move p8 p13)
(move p13 p12)
(move p12 p11)
(move p11 p6)
(move p6 p5)
(move p5 p3)
(move p3 p1)
(move p1 p2)
(push p2 p4 p6)
  • blockworld

There are a collection of blocks: a block can be on the table, or on the top of another block.

There are three predicates

•clear(x): there is no block on top of block x;

•on(x,y): block x is on the top of block y;

•onTable(x): block x is on the table

There are two actions in this task:

•move(x,y): move block x onto block y, provided that both x and y are clear;

•moveToTable(x): move block x on to the table, provided that x is clear and x is not on thetable;

Give initial state and goal state, find the actions change the initial state to the goal state.

domain_blocks_d.pddl

; Header  and  description
( define (domain blocks )
    ;remove  requirements  that  are  not  needed
    (:requirements :strips :typing :conditional−effects :equality :universal−preconditions : negative−preconditions )
    (:types
        ; todo: enumerate types and their  hierarchy here , e.g. car truckbus  −  vehicle
        physob
    )

    ;un−comment following  line  if constants are  needed
    ;( :constants  )

    (:predicates; todo: define  predicates here
        (ontable ?x − physob)
        (clear ?x − physob)
        (on ?x ?y − physob)
    )

    ;(:functions; todo:define numeric functions here
    ;)

    ;define actions here
    (:action move
        :parameters (?x ?y − physob)
        :precondition (and( clear ?x) ( clear ?y) )
        :effect (and(on ?x ?y) (not( clear ?y) )
            (when( ontable ?x) (not( ontable ?x) ) )
            (forall (?z − physob) (when(on ?x ?z) (and(not(on ?x ?z) ) (clear ?z) ) ) )
        )
    )

    (:action moveToTable
        :parameters (?x − physob)
        :precondition (and( clear ?x) (not( ontable ?x) ) )
        :effect (and(not( clear ?x) ) ( ontable ?x)
        ( forall (?z − physob) (when(on ?x ?z) (and(not(on ?x ?z) ) (clear ?z) ) ) )
        )
    )
)

domain_blocks_p.pddl

(define (problem prob) (:domain blocks)
    (:objects
        A B C D E F − physob
    )
    (:init
        ; todo :put  the   initial   state ’ s   facts  and  numeric  values   here
        (clear A) (on A B) (on B C) (ontable C) (ontable D)
        (ontable F) (on E D) (clear E) (clear F)
    )
    (:goal (and
        ; todo :   put  the   goal   condition   here
        (clear F) (on F A) (on A C) (ontable C) (clear E)
        (on E B) (on B D) (ontable D)
        )
    )
    ;un−comment  the   following   line   if   metric   is   needed
    ;(:metric  minimize  ( ? ? ? ) )
)

FF规划器并不能生成最优的方案,例如在Puzzle问题中就出现了连续拨动数字6的情况,经过手动验证两个问题的Plan都是正确的。