模拟退火,如何规范化概率分布

时间:2016-02-28 03:12:18

标签: c++ machine-learning

我正在为任意非线性高阶方程求解器求解器。我想找到最低限度。我在这里遇到的问题是如何规范化概率分布函数。我不想整合所有变量(如果我这样做,我也可以通过暴力找到最小值。)这是我的代码。如何规范化指数项。

template<typename Real, int N, typename Funct>
    Vector<Real, N> simulated_anneling_solver(Funct f, const Vector<Real, N>& lower_bound, const Vector<Real, N>& upper_bound, int t_start)
    {
        static int called_count = 0;
        std::default_random_engine eng;
        eng.seed(time(0) * ++called_count);

        Vector<Real, N> x;
        for(int i = 0; i < N; i++)
            x[i] = (upper_bound[i] + lower_bound[i]) * (Real)0.5;
        Real v = f(x);
        for(int t = t_start; t > 0; t--)
        {
            Vector<Real, N> next_x;
            Real cooled_percent = (Real)t / t_start;
            for(int i = 0; i < N; i++)
            {
                Real dimw = upper_bound[i] - lower_bound[i];
                do
                {
                    next_x[i] = std::normal_distribution<Real>(x[i], dimw * cooled_percent)(eng);
                }
                while(next_x[i] < lower_bound[i] || next_x[i] > upper_bound[i]);
            }

            Real next_v = f(next_x);

            if(next_v <= v || expx((next_v - v) / t) > std::uniform_real_distribution<float>(0.0f, 1.0f)(eng))
            {
                v = next_v;
                x = next_x;
            }
        }

        return x;
    }

}

PS。我尝试将expx((next_v - v) / t) > std::uniform_real_distribution<float>(0.0f, 1.0f)(eng)更改为 (next_v - v) / v < cooled_percent这似乎带来了一些好的结果。现在我知道我只是将函数的百分比误差与它的冷却程度进行比较。因此,一切都归一化,并且在0和1之间。系统冷却时,您需要较低的百分比误差。我想现在虽然它不是模拟退火。

1 个答案:

答案 0 :(得分:0)

在模拟退火中,不需要进行规范化,因为程序中的if子句应该是:

if(next_v < v || 
     expx((v-next_v) / t) > std::uniform_real_distribution<float>(0.0f, 1.0f)(eng))
{
    v = next_v;
    x = next_x;
}

其中expx将始终在(0,1)中产生值。