Skip to content

Commit

Permalink
script update
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul M Bauer committed Nov 12, 2011
1 parent db72402 commit a1695f2
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
66 changes: 66 additions & 0 deletions scripts/anneal.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
; Copyright (c) Rich Hickey. All rights reserved.
; The use and distribution terms for this software are covered by the
; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
; which can be found in the file epl-v10.html at the root of this distribution.
; By using this software in any fashion, you are agreeing to be bound by
; the terms of this license.
; You must not remove this notice, or any other, from this software.

(ns twitterbuzz.anneal)

(defn exp [x]
(js/Math.exp x))

(defn abs [x]
(js/Math.abs x))

(defn random []
(js/Math.random))

(defn standard-prob [e e1 temp]
(if (< e1 e)
1
(exp (/ (- e e1) temp))))

(defn linear-cooling [steps]
(let [min (- 1 (/ steps (dec steps)))]
(fn [t]
(max min (- 1 (/ t steps))))))

(defn anneal
"Given an energy scoring function, a temperature function
(calculates temp given iteration t), a permutation function (creates
a candidate next state given a current state and iteration t), and
an acceptance probability function (of last next energy and temp),
yields a lazy seq of (accepted?) states of the form
{:state s :score :best best :best-score :t t}"

[energy ;;(energy state) -> score
temp ;;(temp t) -> 0-1.0
permute ;;(permute state t) -> new-state
prob ;;(prob e e1 temp) -> 0-1.0
state]

(let [init state
init-score (energy state)
step (fn step [{:keys [state score best best-score t]:as ret}]
(loop [next (permute state) t (inc t)]
(let [next-score (energy next)]
(if (> (prob score next-score (temp t)) (random))
(let [ret {:state next :score next-score :t t
:best (if (< next-score best-score) next best)
:best-score (min next-score best-score)}]
(lazy-seq (cons ret (step ret))))
(recur (permute state) (inc t))))))]
(step {:state init :score init-score :best init :best-score init-score :t 0})))


(comment

(take 10 (take-nth 100
(anneal #(abs (- % 42))
(linear-cooling 1000)
(fn [s _] (+ s (- (random) 0.5)))
standard-prob
55)))
)
4 changes: 2 additions & 2 deletions scripts/invoke
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! /bin/sh
curl --data "optimizations=${2:-advanced}&cs-code=${1}&pretty-print=true" \
http://localhost:5000/compile
#http://pure-beach-9367.herokuapp.com/compile
http://pure-beach-9367.herokuapp.com/compile
#http://localhost:5000/compile
4 changes: 2 additions & 2 deletions scripts/upload
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
curl --form optimizations=${2:-advanced} \
--form cs-code="@${1}" \
--form pretty-print=true \
http://localhost:5000/compile
#http://pure-beach-9367.herokuapp.com/compile
http://pure-beach-9367.herokuapp.com/compile
#http://localhost:5000/compile

0 comments on commit a1695f2

Please sign in to comment.