         cg-map        (graphs/edge-list->outgoing-map causal-graph)
         av-map        (into {} (for [v (keys (:vars sas-problem))]
                                  [v (graphs/ancestor-set causal-graph [v])]))
+        root-set      (set (for [[v av] av-map
+                                 :when (every? (fn [a] (every? av (cg-map a))) (disj av v))]
+                             v))
         cav-map       (into {} (for [v (keys (:vars sas-problem))]
                                  [v (apply clojure.set/union (map av-map (cg-map v)))]))
         ap-map        (HashMap.)
 ;        pre-var-map   (util/map-vals #(set (map first %)) (group-by second causal-graph))
         tsi           (graphs/topological-sort-indices (remove #(apply = %) causal-graph))
+        goal-map (:precond-map (util/safe-singleton (filter #(= (:name %) sas/goal-action-name) (:actions sas-problem))))
         goal    (env/goal-fn sas-problem)
         init    (env/initial-state sas-problem)
         strat-actions (case type
                         :simple     (fn [last-a state] (inf-stratified-applicable-actions last-a state tsi actions))
-                        :structural (fn [last-a state] (improved-inf-stratified-applicable-actions last-a state tsi cav-map actions))                       :dynamic    (fn [last-a state] (dynamic-inf-stratified-applicable-actions last-a state tsi av-map ap-map actions)))]
-    (when (= type :dynamic) #_ (println av-map)
+                        :structural (fn [last-a state] (improved-inf-stratified-applicable-actions last-a state tsi cav-map actions))                       :dynamic    (fn [last-a state] (dynamic-inf-stratified-applicable-actions last-a state tsi av-map ap-map actions))
+                        :fluid    (fn [last-a state] (dynamic-fluid-inf-stratified-applicable-actions last-a state tsi av-map ap-map root-set goal-map cg-map actions))
+                        )]
+    (println root-set)
+    (when (#{:dynamic :fluid} type) #_ (println av-map)
       (doseq [a (:actions sas-problem), pp (:precond-map a)]
         (.put ap-map pp (cons a (.get ap-map pp)))))    
     (queues/pq-add! q [nil init] 0)

    (actions state)))

(defn stratified-search [sas-problem type]
  (assert (#{:simple :dynamic} type))
  (let [q       (queues/make-tree-search-pq)
        closed  (HashSet.)
        actions (env/actions-fn sas-problem)

            (.addAll open (av-map x))))))
    (set (seq ret))))

(defn dynamic-inf-stratified-applicable-actions [last-a state tsi av-map ap-map actions]
  (if last-a
    (let [a-var   (effect-var last-a)
          a-level (util/safe-get tsi a-var)
          p-set   (dynamic-var-set state av-map ap-map a-var)]
      (filter
       (fn [b]
         (let [b-var (effect-var b)
               b-level (util/safe-get tsi b-var)]
           (or (and (<= b-level a-level) (contains? p-set b-var))
               (contains? (:precond-map b) a-var))))       
       (actions state)))
    (actions state)))

(defn fluid-predecessors [state ap-map v]
  (apply clojure.set/intersection
         (for [a (util/safe-get ap-map [v (state/get-var state v)])
               :when (contains? (:effect-map a) v)]
           (set (for [[pvar pval] (:precond-map a)
                      :when (and (not (= pvar v)) (not (= pval (state/get-var state pvar))))]
                  pvar)))))


(defn fluid-vars [state ap-map goal]
  (let [open   (ArrayList.)
        ret    (HashSet.)
        closed (HashSet.)]
    (doseq [[var val] goal]
      (when-not (= val (state/get-var state var))
        (.add open var)))
    (while (not (.isEmpty open))
      (let [v (.remove open (dec (count open)))]
        (when-not (.contains closed v)
          (.add closed v)
          (.addAll open (fluid-predecessors state ap-map v)))))
    (set (seq closed))))

(defn dynamic-fluid-inf-stratified-applicable-actions [last-a state tsi av-map ap-map root-set goal cg-map actions]
  ;; Root set ignored for now -- doing it dynamically.
  (if last-a
    (let [a-var   (effect-var last-a)
          a-level (util/safe-get tsi a-var)
          p-set   (dynamic-var-set state av-map ap-map a-var)
          fluid-set (clojure.set/intersection p-set (fluid-vars state ap-map goal))
          fluid-root-set (set (for [v fluid-set
                                    :let [av (av-map v)]
                                    :when (every? (fn [a] (every? #(or (av %) (not (p-set %))) (cg-map a))) (disj av v))]
                                v)) 
          p-set   (if (seq fluid-root-set)
                    (av-map (apply min-key tsi fluid-root-set))
                    p-set)]
      (filter
       (fn [b]
         (let [b-var (effect-var b)
               b-level (util/safe-get tsi b-var)]
           (or (and (<= b-level a-level) (contains? p-set b-var))
               (contains? (:precond-map b) a-var))))       
       (actions state)))
    (actions state)))
