Advertisement
Guest User

woo2

a guest
May 18th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  (ns part44)
  2.  
  3. (require '[clojure.set :refer :all])
  4.  
  5.  
  6. ;definitions of objects and features
  7.  
  8.  
  9. (def agents '#{Penny Colin})
  10. (def manipulables '#{ball chair})
  11. (def platforms '#{table chair})
  12. (def climbables '#{table chair})
  13.  
  14. (defn agent? [x] (agents x))
  15. (defn manipulable? [x] (manipulables x))
  16. (defn climbable? [x] (climbables x))
  17. (defn platform? [x] (platforms x))
  18.  
  19.  
  20.  
  21. ;compound goals to connect states
  22.  (def world
  23.    '#{()})
  24.  
  25.  
  26.  
  27.  
  28. (def opsnew
  29.   '{pick-off {:pre ((agent ?agent)
  30.                      (manipulable ?obj)
  31.                      (at ?agent ?place)
  32.                      (on ?obj   ?place)
  33.                      (holds ?agent nil)
  34.                      (on ?obj   ?platform)
  35.                      )
  36.               :add ((holds ?agent ?obj))
  37.               :del ((on ?obj   ?place)
  38.                      (holds ?agent nil)
  39.                      (on ?obj   ?platform))
  40.               }
  41.     drop-on {:pre ((at ?agent ?place)
  42.                     (holds ?agent ?obj)
  43.                     (at ?agent ?platform)
  44.                     )
  45.              :add ((holds ?agent nil)
  46.                     (on ?obj   ?place)
  47.                     (on ?obj   ?platform))
  48.              :del ((holds ?agent ?obj))
  49.              }
  50.     climb-on {:pre ((agent ?agent)
  51.                      (climbable ?platform)
  52.                      (at ?agent ?platform)
  53.  
  54.                      )
  55.               :add ((on ?agent ?platform))
  56.               :del ((on ?obj   ?place)
  57.                      (holds ?agent nil))
  58.               }
  59.     climb-off {:pre ((agent ?agent)
  60.                       (climbable ?platform)
  61.                       (on ?agent ?platform)
  62.  
  63.                       )
  64.                :add ((at ?agent ?platform))
  65.                :del ((on ?agent   ?platform)
  66.                       )
  67.                }
  68.  
  69.     })
  70.  
  71. ( def ops
  72.  '{pickup {:pre ((agent ?agent)
  73.                   (manipulable ?obj)
  74.                   (at ?agent ?place)
  75.                   (on ?obj   ?place)
  76.                   (holds ?agent nil)
  77.                   (manipulable ?platform)
  78.                   )
  79.            :add ((holds ?agent ?obj)
  80.                   (holds ?agent ?platform))
  81.            :del ((on ?obj   ?place)
  82.                   (holds ?agent nil))
  83.            }
  84.    drop    {:pre ((at ?agent ?place)
  85.                    (holds ?agent ?obj)
  86.                    (holds ?agent ?platform)
  87.                    )
  88.             :add ((holds ?agent nil)
  89.                    (on ?obj   ?place))
  90.             :del ((holds ?agent ?obj)
  91.                    (holds ?agent ?platform))
  92.             }
  93.    move    {:pre ((agent ?agent)
  94.                    (at ?agent ?p1)
  95.                    (path ?p1 ?p2)
  96.                    )
  97.             :add ((at ?agent ?p2))
  98.             :del ((at ?agent ?p1))
  99.             }} )
  100.  
  101.  
  102.  
  103.  
  104.  (def starts
  105.    ')
  106.  
  107. (def goals
  108.   ')
  109.  
  110. (defn member? [coll x]
  111.   ;"check if a value is in a collection"
  112.   (some #(= x %) coll))
  113.  
  114.  
  115. (defn find-op [ops goal]
  116.   ;"find an op with the goal in its addition list"
  117.   (some (fn [op] (when (member? (:add op) goal) op)) (vals ops)))
  118.  
  119.  
  120.  
  121.  
  122. (defn prep-op [op]
  123.   ;"prepare an op and its preconditions for the goal stack"
  124.   (concat (map (fn [x] {:type :goal :obj x}) (:pre op))
  125.           (list {:type :op :obj op})))
  126.  
  127.  
  128. (defn apply-op [op state]
  129.   ;"apply an operator, returning a new state"
  130.   (let [{:keys [pre add del]} op]
  131.     ;; NB: all pre's should be satisfied
  132.     (when-not (subset? (set pre) state) (throw (Exception. "unsatisfied preconditions")))
  133.     (union (set add)
  134.            (difference state (set del)))
  135.     ))
  136.  
  137.  
  138.  
  139.  
  140. (defn plan [state goal ops debug]
  141.   ;"a simple MEA planner, sensitive to order of ops and their preconditions, no matching"
  142.   (loop [ state  state
  143.          path   []
  144.          gstack (list {:type :goal :obj goal})
  145.          ]
  146.     (when debug (pprint (list 'state= state 'path= path 'stack= gstack)))
  147.     (if (empty? gstack) path
  148.                         (let [[next & gstack] gstack
  149.                               {:keys [type obj]} next
  150.                               ]
  151.                           (cond
  152.                             (and (= type :goal) (member? state obj))
  153.                             (recur state path gstack)
  154.  
  155.                             (= type :goal)  ;; but not (contains? state obj)
  156.                             (recur state path
  157.                                    (concat (prep-op (find-op ops obj)) gstack))
  158.  
  159.                             (= type :op)
  160.                             (recur (apply-op obj state)
  161.                                    (conj path (:txt obj))
  162.                                    gstack)
  163.  
  164.                             :else
  165.                             (throw (Exception. "unknown goal type"))
  166.                             )))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement