Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stochastic Tunneling #45

Open
cpfiffer opened this issue Apr 16, 2019 · 0 comments
Open

Stochastic Tunneling #45

cpfiffer opened this issue Apr 16, 2019 · 0 comments

Comments

@cpfiffer
Copy link
Member

@mohamed82008 wrote a nifty thing to do stochastic tunneling and it might be nice to do a little write up on it.

using Distributions, LinearAlgebra

mutable struct ObjDist{F, Tobj, Tsol, Tstep, Tbound} <: Distribution{Multivariate, Continuous}
    f::F
    best_obj::Tobj
    best_sol::Tsol
    step::Tstep
    lb::Tbound
    ub::Tbound
end

function ObjDist(f, N=1; step = 1.0, lb=-Inf, ub=Inf)
    x0 = rand.(TruncatedNormal.(zeros(N), step, lb, ub))
    obj = f(x0)
    return ObjDist(f, obj, x0, step, lb, ub)
end

function Base.rand(dist::ObjDist)
    N = length(dist.best_sol)
    r = rand.(TruncatedNormal.(dist.best_sol, dist.step, dist.lb, dist.ub))
    return r
end

function Distributions.logpdf(dist::ObjDist, x::AbstractVector)
    obj = dist.f(x)
    if obj > dist.best_obj || isnan(dist.best_obj)
        dist.best_obj = obj
        dist.best_sol .= x
    end
    return obj
end

using Turing

function STUN(f, N, alg = MH(10000))
    dist = ObjDist(f, N)
    @model obj_model() = begin
        obj ~ dist
    end
    sample(obj_model(), alg)
    return dist.best_sol, dist.best_obj
end

STUN(x->-norm(x .- 20), 3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant