 
 ; Only changes from sahucs.clj are in expand-sa-node.
 
+(def *infinite-depth* 1000000000000)
+
 (declare get-sa-node)
-(defn get-sanode-entry [cache state reward-to-state actions]
+(defn get-sanode-entry [cache state reward-to-state actions min-cycle-depth]
   (make-sanode-entry state 
    (when (seq actions) (get-sa-node cache state (first actions)))
-   reward-to-state actions))
+   reward-to-state actions min-cycle-depth))
 
 (defn get-sa-node [#^HashMap cache s a]
   "Create a new sa-node, or returned the cached copy if it exists."

    (do (assert (pred (get m k) v)) m)
    (assoc m k v)))


(defn extract-effect [state context opt]
  (vary-meta (env/extract-effects state context) assoc :opt opt))

;; (or nil, if none), and in this case cutoff may be artificially low. 
;; Also returns a separate bad-result-map for results that cannot be cached.

(deftype PartialResult [result-map cutoff min-cycle-depth])

(deftype SANode [context action result-map-atom queue])



;; Represents an action sequence from a state, with sanode representing the first action
; in remaining-actions. (or nil, if remaining-actions is empty.)
(deftype SANodeEntry [state sanode reward-to-state remaining-actions min-cycle-depth hash-code] :as this
  Object
  (equals [y] (or (identical? this y) 
                  (and (= state (:state y)) (= remaining-actions (:remaining-actions y)))))
  (hashCode [] hash-code))

(defn make-sanode-entry [state sanode reward-to-state remaining-actions min-cycle-depth]
  (SANodeEntry state sanode reward-to-state remaining-actions min-cycle-depth
               (unchecked-add (int (hash state)) 
                              (unchecked-multiply (int 13) (int (hash remaining-actions))))))

;(defn change-depth [entry new-cycle-depth]
;  (SANodeEntry (:state entry) (:sanode entry) (:reward-to-state entry) (:remaining-actions entry)
;               new-cycle-depth (:hash-code entry)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Core Algorithm  ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
