Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns part44)
- (require '[clojure.set :refer :all])
- ;definitions of objects and features
- (def agents '#{Penny Colin})
- (def manipulables '#{ball chair})
- (def platforms '#{table chair})
- (def climbables '#{table chair})
- (defn agent? [x] (agents x))
- (defn manipulable? [x] (manipulables x))
- (defn climbable? [x] (climbables x))
- (defn platform? [x] (platforms x))
- ;compound goals to connect states
- (def world
- '#{()})
- (def opsnew
- '{pick-off {:pre ((agent ?agent)
- (manipulable ?obj)
- (at ?agent ?place)
- (on ?obj ?place)
- (holds ?agent nil)
- (on ?obj ?platform)
- )
- :add ((holds ?agent ?obj))
- :del ((on ?obj ?place)
- (holds ?agent nil)
- (on ?obj ?platform))
- }
- drop-on {:pre ((at ?agent ?place)
- (holds ?agent ?obj)
- (at ?agent ?platform)
- )
- :add ((holds ?agent nil)
- (on ?obj ?place)
- (on ?obj ?platform))
- :del ((holds ?agent ?obj))
- }
- climb-on {:pre ((agent ?agent)
- (climbable ?platform)
- (at ?agent ?platform)
- )
- :add ((on ?agent ?platform))
- :del ((on ?obj ?place)
- (holds ?agent nil))
- }
- climb-off {:pre ((agent ?agent)
- (climbable ?platform)
- (on ?agent ?platform)
- )
- :add ((at ?agent ?platform))
- :del ((on ?agent ?platform)
- )
- }
- })
- ( def ops
- '{pickup {:pre ((agent ?agent)
- (manipulable ?obj)
- (at ?agent ?place)
- (on ?obj ?place)
- (holds ?agent nil)
- (manipulable ?platform)
- )
- :add ((holds ?agent ?obj)
- (holds ?agent ?platform))
- :del ((on ?obj ?place)
- (holds ?agent nil))
- }
- drop {:pre ((at ?agent ?place)
- (holds ?agent ?obj)
- (holds ?agent ?platform)
- )
- :add ((holds ?agent nil)
- (on ?obj ?place))
- :del ((holds ?agent ?obj)
- (holds ?agent ?platform))
- }
- move {:pre ((agent ?agent)
- (at ?agent ?p1)
- (path ?p1 ?p2)
- )
- :add ((at ?agent ?p2))
- :del ((at ?agent ?p1))
- }} )
- (def starts
- ')
- (def goals
- ')
- (defn member? [coll x]
- ;"check if a value is in a collection"
- (some #(= x %) coll))
- (defn find-op [ops goal]
- ;"find an op with the goal in its addition list"
- (some (fn [op] (when (member? (:add op) goal) op)) (vals ops)))
- (defn prep-op [op]
- ;"prepare an op and its preconditions for the goal stack"
- (concat (map (fn [x] {:type :goal :obj x}) (:pre op))
- (list {:type :op :obj op})))
- (defn apply-op [op state]
- ;"apply an operator, returning a new state"
- (let [{:keys [pre add del]} op]
- ;; NB: all pre's should be satisfied
- (when-not (subset? (set pre) state) (throw (Exception. "unsatisfied preconditions")))
- (union (set add)
- (difference state (set del)))
- ))
- (defn plan [state goal ops debug]
- ;"a simple MEA planner, sensitive to order of ops and their preconditions, no matching"
- (loop [ state state
- path []
- gstack (list {:type :goal :obj goal})
- ]
- (when debug (pprint (list 'state= state 'path= path 'stack= gstack)))
- (if (empty? gstack) path
- (let [[next & gstack] gstack
- {:keys [type obj]} next
- ]
- (cond
- (and (= type :goal) (member? state obj))
- (recur state path gstack)
- (= type :goal) ;; but not (contains? state obj)
- (recur state path
- (concat (prep-op (find-op ops obj)) gstack))
- (= type :op)
- (recur (apply-op obj state)
- (conj path (:txt obj))
- gstack)
- :else
- (throw (Exception. "unknown goal type"))
- )))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement