test_noisyopt.py 文件源码

python
阅读 18 收藏 0 点赞 0 评论 0

项目:noisyopt 作者: andim 项目源码 文件源码
def test_bisect():
    xtol = 1e-6 

    ## simple tests
    # ascending
    root = noisyopt.bisect(lambda x: x, -2, 2, xtol=xtol,
                           errorcontrol=False)
    npt.assert_allclose(root, 0.0, atol=xtol)

    root = noisyopt.bisect(lambda x: x-1, -2, 2, xtol=xtol,
                           errorcontrol=False)
    npt.assert_allclose(root, 1.0, atol=xtol)

    # descending
    root = noisyopt.bisect(lambda x: -x, -2, 2, xtol=xtol,
                           errorcontrol=False)
    npt.assert_allclose(root, 0.0, atol=xtol)

    ## extrapolate if 0 outside of interval
    root = noisyopt.bisect(lambda x: x, 1, 2, xtol=xtol,
                           errorcontrol=False)
    npt.assert_allclose(root, 0.0, atol=xtol)
    npt.assert_raises(noisyopt.BisectException,
                      noisyopt.bisect, lambda x: x, 1, 2,
                      xtol=xtol, outside='raise', errorcontrol=False)

    ## extrapolate with nonlinear function
    root = noisyopt.bisect(lambda x: x+x**2, 1.0, 2, xtol=xtol,
                           errorcontrol=False)
    assert root < 1.0

    ## test with stochastic function
    xtol = 1e-1
    func = lambda x: x - 0.25 + np.random.normal(scale=0.01)
    root = noisyopt.bisect(noisyopt.AveragedFunction(func), -2, 2, xtol=xtol,
                           errorcontrol=True)
    npt.assert_allclose(root, 0.25, atol=xtol)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号