iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 23
1
自我挑戰組

Julia語言—從入門到專案系列 第 23

[Day 23] Simulated annealing -- 實作

本來預定是要再寫一部份理論的,不過前面一不小心就把理論都講的差不多了XD

那我們就進入實作吧!

其實實作的部份沒有很難,照著前面的框架走應該是沒問題的

function SA_algorithm()
    x = State[]  # 蒐集走過的狀態
    push!(x, initiate())  # 初始化狀態
    T_0 = 10000  # 設定初始溫度
    while T > T_MIN && cost_best_counter < halt
        for i in 1:max_iteration
            new_state = generate_state()
            alpha = acceptance(new_state, x[i], T)
            push!(x, update(new_state, x[i], alpha))
            cooling!(T, T_0)
        end
    end
    return x
end

用julia寫真的很簡潔XD
這邊的energy計算是隱含在acceptance裏面,不過這樣做似乎不太好,補上剩下的function。

function acceptance(cost_new, cost_current, temperature)
    if cost_new < cost_current
        return 1.0
    end
    if temperature == 0.0
        return 0.0
    end
    return exp( -(cost_new - cost_current) / temperature )
end
function update(new_state, current_state, alpha::Float64)
    u = rand()
    if u <= alpha
        return new_state
    else
        return current_state
    end
end

以上有些細節還沒修,所以應該不太能動XD
今天先到這邊


上一篇
[Day 22] Simulated annealing -- 細緻平衡
下一篇
[Day 24] Simulated annealing -- thermo-scheduler
系列文
Julia語言—從入門到專案31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言