   [(asplan-solution-name sol) rew])
 
 
-
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Fancier search ;;;;;;;;;;;;;;;;;;;;;;;;;;;
+
+;; Just hte cost of committed actions
+(defn committed-actions-heuristic [s]
+  (reduce +
+          (for [[var val] (state/as-map s)
+                :when (and (= (first var) :actions)
+                           (not (= :wait val))
+                           (not (empty? val)))]
+            (apply min (map :reward val)))))
+
+;; Committed actions + shortest paths to target values.
+(defn domain-paths-heuristic [s]
+  0)
+
+;; Committed actions + shortest paths + matching for preconds.
+(defn all-committments-heuristic [s]
+  0)
+
+(defn canonicalize-state [s]
+  (into {} (for [[var val] (state/as-map s)
+                 :when (not (#{:actions :child} (first var)))]
+             [var val])))
+
+;; Keeps a separate closed list for canonical state pruning.
+(defn asplan-search
+  ([env] (asplan-search env (constantly 0)))
+  ([env heuristic]
+     (let [q       (queues/make-graph-search-pq)
+           actions (env/actions-fn env) 
+           goal    (env/goal-fn env)
+           init    (env/initial-state env)
+           closed  (HashMap.)]
+        (queues/pq-add! q init (heuristic init))
+        (loop []
+          (when-not (queues/pq-empty? q)
+            (let [[s c] (queues/pq-remove-min-with-cost! q)
+                  s-r     (or (:reward (meta s)) 0)
+                  canon-s (canonicalize-state s)
+                  canon-r (get closed canon-s Double/NEGATIVE_INFINITY)]
+              (util/print-debug 3 "dequeueing " (:act-seq (meta s)) c s "\n")
+              (or (and (goal s) [(asplan-solution-name (reverse (:act-seq (meta s))))
+                                 (:reward (meta s)) #_ (println s)])
+                  (if (< s-r canon-r)
+                    (recur)
+                    (do
+                      (when (> s-r canon-r) (.put closed canon-s s-r))
+                      (let [acts (actions s)]
+                        (util/print-debug 4 "Actions are:" (map env/action-name acts) "\n")
+                        (util/print-debug 4 (for [a acts :when (not (env/applicable? a s))] (str (:name a) (:precond-map a) "not applicable" )))
+                        (doseq [a acts :when (env/applicable? a s)]
+                          (let [[ss sc] (env/successor a s)
+                                f-val (+ (:reward (meta ss)) (heuristic ss))]
+                            (when (> f-val Double/NEGATIVE_INFINITY)
+                              #_(util/print-debug 1 "warning: pruning " ss)
+                              (queues/pq-add! q ss (- 0 f-val ))))))
+                      (recur))))))))))
 
 
 ;; (do (use 'angelic.env 'angelic.hierarchy 'angelic.search.textbook 'angelic.domains.taxi-infinite 'angelic.domains.sas-problems 'angelic.sas 'angelic.sas.analysis 'angelic.util 'angelic.sas.hm-heuristic 'angelic.search.interactive) (require '[angelic.search.action-set.gasplan2 :as gap2] '[angelic.search.action-set.asplan-r :as rap]))

           possible-actions-fn (make-possible-actions-fn dtgs effect-cluster-map part?)

           auxiliary-map (if aux? (get-auxiliary-map sas-problem) {})
           canonicalize  (fn [bu-map]
                           (reduce (fn [bum k]
                                     (if-let [nk (auxiliary-map k)]
                                       (assoc (dissoc bum k) nk (util/union (get bum k) (get bum nk #{})))
                                       bum))
                                   (util/map-vals set bu-map) (keys bu-map)))] 
;       (assert (graphs/dag? causal-graph))    

  (some #(guaranteed-scheduled-side-effector? s v %)
        (keys (dissoc (:effect-map a) v))))

;; Is there a precondition reserved for a scheduled action
;; that needs v, which is also needed for a?
(defn conflicted-precond? [s v a]
  (some #(when-let [c (state/get-var s (child-var %))]
           (let [acts (state/get-var s (possible-actions-var c))]
             (and (not (= c v)) (not (= acts :wait)) (seq acts)
                  (every? (fn [ca] (contains? (:precond-map ca) v)) acts))))        
        (keys (dissoc (:precond-map a) v))))

(defn conflicted-action? [s v a]
  (or (conflicted-effects? s v a)
      (conflicted-precond? s v a)))



(defn structural-partitions [actions]
  (map set (vals (group-by (comp util/keyset :precond-map) actions))))
