Database Reference
In-Depth Information
cost (get-cost initial)]
(loop [state initial
cost cost
k 1
best-seq [{:state state, :cost cost}]]
(println '>>> 'sa k \. state \$ cost)
(if (and (< k max-iter)
(or (nil? max-cost)
(> cost max-cost)))
(let [t (temp-fn (/ k max-iter))
next-state (neighbor-fn state)
next-cost (get-cost next-state)
next-place {:state next-state,
:cost next-cost}]
(if (> (p-fn cost next-cost t) (rand))
(recur next-state next-cost (inc k)
(conj best-seq next-place))
(recur state cost (inc k) best-seq)))
best-seq))))
2.
For parameters, annealing takes an initial state, a limit to the number of iterations,
a target output value, and a series of functions. The irst function takes the current
state and returns a new neighboring state.
To write this function, we have to decide how best to handle the state for this
problem. Namely, since the function to be evaluated has multiple parameters, we will
use a vector and randomly slide one value in that around. However, for this problem,
we only have one input value: the partition size.
So, for this problem, we'll use an integer between 0 and 20 instead. The actual
partition size will be 2 raised to that power. To ind a neighbor, we just randomly
slide the state's value up or down at most by ive units, within the range of 0 to 20:
(defn get-neighbor [state]
(max 0 (min 20 (+ state (- (rand-int 11) 5)))))
3.
The next function parameter used for annealing is the cost function. This will
take the state and return the value that we're trying to minimize. In this case, we
benchmark mc-pi-part with the given partition size (2 raised to the power) and
return the average time:
(defn get-pi-cost [n state]
(let [chunk-size (long (Math/pow 2 state))]
(first (:mean (quick-benchmark
(mc-pi-part chunk-size n)
{})))))
 
Search WWH ::




Custom Search