 
 
 
-;; (use 'angelic.env 'angelic.hierarchy 'angelic.search.textbook 'angelic.domains.taxi-infinite 'angelic.search.action-set.asplan  'angelic.domains.sas-problems 'angelic.sas 'angelic.sas.analysis 'angelic.util 'angelic.sas.hm-heuristic)
-
-;; (let [e (make-random-infinite-taxi-sas2 4 4 4 2)] (println (time (run-counted #(uniform-cost-search e)))) (println (time (run-counted #(asplan-solution-pair-name (uniform-cost-search (make-asplan-env e)))))))
-
-;; (let [i 25] (let [e (make-random-infinite-taxi-sas2 3 3 3 i) ae (make-asplan-env e)]  (println (time (run-counted #(uniform-cost-search e)))) (println (time (run-counted (fn [] (a*-search e (h-max (:actions e))))))) (println (time (run-counted #(asplan-solution-pair-name (uniform-cost-search ae))))) (println (time (run-counted #(asplan-solution-pair-name (a*-search ae (h-max (:actions ae))))))) (println (time (run-counted #(asplan-solution-pair-name (a*-search ae (let [h (h-max (:actions ae))] (fn [s] (- (h s) (unrealized-reward s)))))))))))
-
-; (let [e (force (nth ipc2-logistics 3)) ] #_ (println (time (run-counted #(uniform-cost-search e)))) (println (time (run-counted #(asplan-solution-pair-name (uniform-cost-search (make-asplan-env e)))))))
-
-
- ; (doseq [[alg sp]  [[make-asplan-env asplan-solution-pair-name] [angelic.search.action-set.asplan-broken/make-asplan-env angelic.search.action-set.asplan-broken/asplan-solution-pair-name]] ] (println (time (run-counted #(sp (uniform-cost-search (alg (make-random-infinite-taxi-sas2 3 3 3 3))))))))
-
-; (-> (nth (vals (sas-sample-files 1)) 5) make-sas-problem-from-pddl unarize make-asplan-env interactive-search)
-
+;; (do (use 'angelic.env 'angelic.hierarchy 'angelic.search.textbook 'angelic.domains.taxi-infinite  'angelic.domains.sas-problems 'angelic.sas 'angelic.sas.analysis 'angelic.util) (require '[angelic.search.action-set.asplan2 :as ap2]))
+
+;; (let [e (force (nth ipc2-logistics 5)) ]  (println (time (run-counted #(ap2/asplan-solution-pair-name (uniform-cost-search (ap2/make-asplan-env e )))))))
+
+;; (let [e (make-random-independent-taxi-env 3 3 3 false 1) ]  (println (time (run-counted #(ap/asplan-solution-pair-name (uniform-cost-search (ap/make-asplan-env e ))))))  (println (time (run-counted #(ap2/asplan-solution-pair-name (uniform-cost-search (ap2/make-asplan-env e )))))))
+
+;; (let [e (make-random-pairwise-taxi-env 2 2 3 true 1) ]  (println (time (run-counted #(ap/asplan-solution-pair-name (uniform-cost-search (ap/make-asplan-env e ))))))  (println (time (run-counted #(ap2/asplan-solution-pair-name (uniform-cost-search (ap2/make-asplan-env e )))))))
  env/FactoredEnv
    (goal-map      [e] g-map))

;; TODO: add assertions on precond var = effect var
;; Recall: greedy helps the most when, e.g., lots of passengers and not much room.
(defn make-asplan-env
  "Make an actino-set planning environment.  Switches are:
    check-deadlock?: check for cycles in precedence graph above and beyond lack of sources

       Greedy means we forget about reserving preconditions when free and we can fire right now (less branching).
       Sloppy means we ignore if preconditions are free, but require no action on them.
       Extra-sloppy means anything goes -- if you can execute ignoring commitments, go for it."
  ([sas-problem]
     (let [causal-graph  (remove #(apply = %) (sas-analysis/standard-causal-graph sas-problem))
           vars          (graphs/ancestor-set causal-graph [sas/goal-var-name])
           causal-graph  (filter (fn [[v1 v2]] (and (vars v1) (vars v2))) causal-graph)
           tsi           (graphs/df-topological-sort-indices causal-graph) ;; TODO: doesn't seem to matter...
           child-var-map (assoc (util/map-vals #(set (map second %)) (group-by first causal-graph))
                           sas/goal-var-name #{})
           dtgs          (sas-analysis/domain-transition-graphs sas-problem)
           possible-actions-fn (make-possible-actions-fn dtgs)]
;       (assert (graphs/dag? causal-graph))    
       (assert (sas-analysis/unary? sas-problem))
;       (println child-var-map)
       
       (ASPlanEnv.
        sas-problem
        (expand-initial-state (env/initial-state sas-problem) child-var-map (goal-action dtgs))

        (fn asplan-actions [s]
;          (println "\n"(:act-seq (meta s)))
          (identity #_ util/prln (or (some #(greedy-actions s % child-var-map possible-actions-fn) vars)
               (let [choices (for [v vars, p (var-precondition-choices s v)]
                               (refinement-actions s p v possible-actions-fn))]
;                 (when (empty? choices) (clojure.inspector/inspect-tree (state/as-map s)) #_ (assert nil))
;                 (println (map count choices))
                 (assert (not-any? empty? choices))
                 (if (seq choices)
                   (apply min-key count choices)
                   #_ (println "is a dead end"))))))
        (env/goal-map sas-problem)))))
;; TODO: assert no top-down.

(defn asplan-solution-name [sol]
  (map second (filter #(= (first %) ::Fire) sol)))
