On Sep 19, 2010, at 9:41 AM, Goran Jovic wrote:
> Anyway, being a Clojure newbie myself, I had a huge benefit from
> freely available code examples, so I decided to share my own project
> with other folks who are learning Clojure.
> 
> It's hosted on:
> http://code.google.com/p/genetic-my-number/
> 
> So, enjoy... and, of course, I always welcome ideas, suggestions,
> (constructive) criticism or any other comments.

Perhaps you'd be interested in a related but slightly different approach to 
solving a related but slightly different problem.

The problem is the one posed by the numeric card game Krypto ( 
http://en.wikipedia.org/wiki/Krypto_(game) ), and the solution uses not a 
standard genetic algorithm but rather a genetic programming system (Clojush, a 
Clojure implementation of Push/PushGP). Using this to solve Krypto, which is 
quite simple, is a bit like using a sledgehammer to kill a fly, but it made a 
cute little exercise so I included it as one of the examples in the latest 
version of the Clojush system:

http://github.com/lspector/Clojush

BTW I mentioned the Clojush project here previously but I've recently made a 
number of significant improvements based on comments I received. It's much more 
idiomatic Clojure code now, although I'm sure it still bears traces of its 
history in Common Lisp and Scheme.

Also, just for kicks I recently wrote Clojure code to solve Krypto instances 
directly, since it's pretty easy. I'll paste in the code below. People will 
yell that I shouldn't use eval or non-recur recursive calls, but I think 
they're fine in this (toy) case.

 -Lee

;; kryptosolve.clj
;; Clojure code for solving instances of the game of Krypto.
;; See http://en.wikipedia.org/wiki/Krypto_(game)
;; Lee Spector, lspec...@hampshire.edu, 2010

(ns kryptosolve
  (:use clojure.contrib.combinatorics))

(defn safe-eval
  "Returns the result of evaluating e, or :error if it throws an exception."
  [e]
  (try (eval e) (catch java.lang.Exception _ :error)))

(defn all-splits
  "Returns a sequence of all ways of splitting values into 2 subsequences."
  [values]
  (map #(split-at (inc %) values) (range (dec (count values)))))

(defn expressions 
  "Returns a sequence of all of the arithmetic expressions that can be formed
with the provided values in the given order."
  [values]
  (if (< (count values) 2)
    values
    (mapcat
      (fn [[left-values right-values]]
        (mapcat
          (fn [[left-expression right-expression]]
            (map #(list % left-expression right-expression) '(+ - * /)))
          (cartesian-product (expressions left-values) (expressions 
right-values))))
      (all-splits values))))
  
(defn solve-krypto
  "Returns a solution expression if one can be found for the given
values and target."
  [values target]
  (first (filter 
           #(= (safe-eval %) target) 
           (mapcat expressions (lex-permutations values)))))

(solve-krypto [1 2 3 4 5] 25)

-- 
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