# Simulated Annealing

Fundamental idea: A change of scale, called temperature, allows for faster moves on the surface of the function of $h$ to maximize, whose negative is called energy.

It is important to note that if the larger $T$ is, accepting one decreasing is more likely.

Example: To maximize $h(x)=[\cos(50x)+\sin(20x)]^2$.

We can use the following Julia code to solve this problem:

r = 0.5function T(t)    return 1/log(t)end# target functionfunction h(x)    return (cos(50x) + sin(20x))^2end​N = 2500x = ones(N)y = ones(N)for t = 1:(N-1)    # step 1    at = max(x[t]-r, 0)    bt = min(x[t]+r, 1)    u = rand() * (bt - at) + at     # step 2    rho = min(exp( (h(u) - h(x[t])) / T(t) ), 1)    if rand() < rho        x[t+1] = u        y[t+1] = h(u)    else        x[t+1] = x[t]        y[t+1] = y[t]    endend

The trajectory of 2500 pairs $(x^{(t)}, y^{(t)})$ is
{"ft_lang_label":"__label__en","ft_lang_prob":0.69308156,"math_prob":0.9999486,"size":708,"snap":"2021-31-2021-39","text_gpt3_token_len":241,"char_repetition_ratio":0.09943182,"word_repetition_ratio":0.0,"special_character_ratio":0.35875705,"punctuation_ratio":0.094339624,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99999094,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-07-26T15:39:16Z\",\"WARC-Record-ID\":\"<urn:uuid:58a10236-e8c3-449d-b157-fb9cbe5ea509>\",\"Content-Length\":\"250609\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:eadcc318-045d-41ee-b6bb-8e7e002f0f36>\",\"WARC-Concurrent-To\":\"<urn:uuid:177d021d-a053-4f4c-b6ba-e7f6488ee6b3>\",\"WARC-IP-Address\":\"104.18.0.145\",\"WARC-Target-URI\":\"https://mc.hohoweiya.xyz/mcoptim\",\"WARC-Payload-Digest\":\"sha1:HJFHXVAZLMILRYWU6P42CI4VUY3JIGHE\",\"WARC-Block-Digest\":\"sha1:DWMLPWVM5QVGKY5U5IPKUW37GZQ6WDMJ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-31/CC-MAIN-2021-31_segments_1627046152144.81_warc_CC-MAIN-20210726152107-20210726182107-00314.warc.gz\"}"}