These days, the web have been noisy about the brand new Google sponsored AI Challenge organized by the University of Waterloo Computer Science Club. I find that it's a great initiative and is a good occasion for amateur AI researchers like me to do something else than web development or database related coding. Being fond of Clojure, I've taken on the task of making a starter package for this language and making it available on a GitHub repository. It's not an official package yet, but it shouldn't be a problem as the organizers seems really open.
The code has been translated from the Java starter package. I've tried to keep the map code close to the original, so it may not be the most idiomatic Clojure code. There's four global refs containing the state of the game: width, height, walls and players. The two first are simple integers, walls is an hash map keyed with points (which are vectors) and the last one a vector of points. There's some convenient functions to access that data, wall? to tell if a wall is found at the given coordinates and two others to get each players position, you and them. To move your bike you must call make-move with one of :north, :east, :south or :west. The map code end up being nearly half the size of the Java version.
With this code we can rewrite the sample random bot in Clojure, I'll spare you any more explanation and only show the code.
(load "map") (defn valid-moves [x y] [(when-not (wall? x (- y 1)) :north) (when-not (wall? (+ x 1) y) :east) (when-not (wall? x (+ y 1)) :south) (when-not (wall? (- x 1) y) :west)]) (defn compact [coll] (filter identity coll)) (defn choose-at-random [coll] (let [size (count coll)] (when (< 0 size) (nth coll (rand-int size))))) (defn next-move [] (choose-at-random (compact (apply valid-moves (you))))) (defn game-loop [] (loop [] (initialize) (make-move (next-move)) (recur))) (game-loop)
Hack and be mery!
Update: I've updated the GitHub repository with some improvements and instructions on how to compile your entry. While discussing with one of the contest organizer over their forum, I realized that even though Clojure is more than fast enough for this kind of task, the Clojure jar loading time take a good chunk of the first turn time, so be careful. By the way, it's now an official package.
saw your clojure package at the AI Challenge. wondering how you got into clojure, considering you do 'web develoment or database related coding'.
ReplyDeleteor in other words: what's the sweet spot of clojure?
I got into it just out of curiosity, I studied the language itself at
ReplyDeletefirst, played with, read the source code, etc. Overall I'd say that
Clojure is a very versatile language, but like any other it has its
quirks. I'll certainly write a blog post on what I like and don't like
about this language in the coming months, but I still need more (good)
time with it before. It's sweet spot would be mostly the same as other
functional languages: symbolic programming, like writing other languages
for example, you know stuff that compute like math's functions! Yet, it
has more to offer than just this, as touted everywhere it has great
concurrency support, which I didn't really used. Also, another important
point is that it's the perfect glue language (for the JVM env.), with
it's Java interop, macros and interactive development it offer you a
kind of super console that you can use to do all sorts of things.
Fair enough, i was just wondering since the last few days i have found various blogs stating it would be beneficial to learn a functional programming language if you want to do some AI. just can't seem to figure out why ;)
ReplyDeleteI couldn't either, AI is a very broad field, where, for example, logic programming is quite important, more than functional programming I'd say. But, as is the case of Clojure, Lisps are very well suited to experimental coding which AI is all about. It's more about the interactive style of programming than the language itself though. The whole paradigm concept is overrated anyway, most languages support lots of them in one way or another, and you could always hack something yourself when something is missing, it's more a question of personal taste.
ReplyDelete