Thank you for your reply.

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

The game from my program is from Serbian TV quiz Slagalica (http://
en.wikipedia.org/wiki/TV_Slagalica). As you said Krypto is only
slightly different from one in my example, so it is very interesting
to see your approach.

My initial goal was to use GP. However, it seemed to me that Lisp's
code-as-data style makes a much smaller difference between standard
genetic algorithms and genetic programming then imperative languages,
e.g. Java, so I decided to try this way first.

>Using this to solve Krypto, which is quite simple, is a bit like using a 
>sledgehammer to kill a fly..

Maybe, but so is every other "Hello word" ever written :)

I will definitively try using Clojush too, especially since GP was my
initial intention, so thanks again.

>
> 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.
> ;; Seehttp://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)

It seems that you used brute force calculation to get the result in
this code. May I use the code you pasted in my project (modified to
fit the specific rules of my game) to test the performance of my code
against it? It would be nice to see the gain of using a genetic
algorithm over brute force (if there is any in this example, of
course).

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