Skip to content

Instantly share code, notes, and snippets.

@jlouis
Created April 25, 2012 18:46
Show Gist options
  • Save jlouis/2492127 to your computer and use it in GitHub Desktop.
Save jlouis/2492127 to your computer and use it in GitHub Desktop.
Anneal y = (X + 4)(X + 1)(X -2) / 4 :)
-module(annealing).
-export([anneal/1]).
-define(KMAX, 10000).
-define(EMAX, 0.00001).
-define(INITIAL_TEMP, 100000).
anneal(S0) ->
random:seed(os:timestamp()),
S = S0,
E = e(S),
SB = S,
EB = E,
anneal(S, E, SB, EB, 0).
anneal(S, E, SB, EB, K) when K < ?KMAX, E > ?EMAX ->
T = temperature(K / ?KMAX),
SN = neighbour(S),
EN = e(SN),
{NextS, NextE} = case p(E, EN, T) > random:uniform() of
true ->
io:format("(P)"),
{SN, EN};
false ->
io:format("(D)"),
{S, E}
end,
{NextSB, NextEB} = case E < EB of
true -> {SN, EN};
false -> {SB, EB}
end,
anneal(NextS, NextE, NextSB, NextEB, K+1);
anneal(_, _, SB, _, _) -> SB.
temperature(X) ->
(1 - X). %% This is very naive at the moment.
f(X) ->
(X + 4) * (X + 1) * (X - 2) / 4.0.
walk(S) ->
case random:uniform() of
K when K > 0.5 ->
S+(random:uniform() * 2);
_ ->
S-(random:uniform() * 2)
end.
clamp(Lo, _Hi, X) when X < Lo -> Lo;
clamp(_Lo, Hi, X) when X > Hi -> Hi;
clamp(_, _, X) -> X.
neighbour(S) ->
N = walk(S),
io:format("V: ~p~n", [N]),
clamp(-6, 6, N).
%% qlglicko:new_candidate(S).
p(E, NewE, _T) when NewE < E -> 1.0;
p(E, NewE, T ) -> math:exp((E - NewE) / T).
e(S) ->
100 - f(S).
%% PR = qlglicko:predict(S),
%% 1.0 - PR.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment