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)
评论列表
文章目录