TuringLang / TuringLang/docs

Stochastic Tunneling

Open
#45 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Markdown
Stars
238
Forks
107
Avg merge
1d 1h
Merged PRs (30d)
2

Description

@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)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from the embedded Julia implementation of ObjDist and STUN, and read the surrounding documentation structure in the repository to find the appropriate tutorial or example location. Done means adding a clear write-up of stochastic tunneling that explains the shown usage and integrates it into the documentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.