2010年6月13日日曜日

ICFP 2009 orbit main

Here's the code that glues it all together and starts a simulation of the first Hohmann scenario:


;; Problem specification at http://www.ittc.ku.edu/icfp-contest/

(set! *warn-on-reflection* true)

(ns orbit-main
(:use orbit-vm orbit-binary-reader orbit-plotter orbit-controllers (clojure.contrib repl-utils))
(:import (java.io FileInputStream) (java.net URL)))

(def obf (obf-seq (byte-seq
;; (.. (URL. "http://www.ittc.ku.edu/icfp-contest/binaries/bin1.obf") openConnection getInputStream)
(FileInputStream. "bin1.obf") ; in case you downloaded the file, use this instead
)))

(comment do
(println)
(println "DATA OPCODE PARAM1 PARAM2")
(println "--------------------------------")
(let [fn-to-name { > ">" < "<" = "=" >= ">=" <= "<=" }]
(doseq [[data [opcode p1 p2]] (take 10 obf)]
(let [p1 (if (fn? p1) (fn-to-name p1) p1)]
(printf "%+08.2e %-6s %6s %6s%n" data opcode p1 p2)))))

(def vm (create-vm obf))

(def objects (atom {}))
(def stats (atom []))

(defn simulate
[vm controller]
(loop [vm vm countdown (long 3e6)] ; countdown can be used to set an upper bound for the number of iterations
;; (Thread/sleep 1) ; no sleep necessary on MY machine, running THIS code
(if (and (> countdown 0) (zero? (or (get-in vm [:outputs 0]) 0)))
(let [[new-inputs new-objects new-stats] (controller (:inputs vm) (:outputs vm))
vm (assoc-in vm [:inputs] new-inputs)]
(when (zero? (mod countdown 80)) ; throttle gui updates
(reset! stats new-stats)
(reset! objects new-objects))
(recur (one-simulation-step vm) (dec countdown)))
vm)))

(def orbit-panel (create-orbit-panel objects))
(def stats-panel (create-stats-panel stats))

(def frame (create-frame orbit-panel stats-panel))
(.setSize frame 500 500)

;; spawn another thread so I can continue using the repl while it spins
(.start (Thread. #(do
(def new-vm (simulate vm (create-hohmann-controller 1001)))
(:outputs new-vm))))

0 件のコメント:

コメントを投稿

フォロワー