Hi Jim,

I tried a bit.
The performance of this is not perfect but seems not horrible.
It can do a level 5 exploration in 11 sec on my computer, with a
branching factor of 30.
(Of course, there is very little computation for the next move.)
The generation is now lazy, but the depth is used in the exploration.
THis will allow a more clever exploration strategy.
(Ideally you should go deeper in branch that are promising but uncertain.)

Apologies for the long code.
---------------------------------------------

(ns explore.core
  (:require [clojure.core.reducers :as r])
  )
(set! *warn-on-reflection* true)

(defrecord BoardAndChildren [board children])
(defrecord MoveAndTree [move tree])
(defrecord MoveAndBoard [move board])

(defn generate [board next-boards]
  ;; next-boards return a seq of MoveAndBoard

  (BoardAndChildren. board
                     (r/map (fn [m]
                            (MoveAndTree. (:move m) (generate (:board
board) next-boards)))
                          (next-boards board))))

(defn my-max ([x y] (max x y))
  ([] Double/NEGATIVE_INFINITY))

(defn tree-value ^double [tree evaluate ^long depth]
  (let [children (:children tree)]
  (if (or (zero? depth))
    (evaluate (:board tree))
    (let [^double res
          (r/reduce my-max
                    (r/map
                     #(- (tree-value (:tree %) evaluate (dec depth)))
children))]
      (if (= res Double/NEGATIVE_INFINITY) (evaluate (:board tree))
          res)))))

(defn best-move [board next-boards evaluate ^long depth]
  (apply max-key :value
        (into []   (r/map (fn [m]
                            {:move (:move m)
                             :value (tree-value (:tree m) evaluate depth)})
                          (:children (generate board next-boards))))))

(defn test-speed [^long depth]
  (best-move nil #(repeat 30  (MoveAndBoard. :a %)) (fn [x]  0.0) (dec depth)))

-----------------------------------------

Hope that will help,

Nicolas.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to