def test_sum_squared(self): x0 = np.array([0.5, -np.pi, np.pi]) sol = np.zeros(3) fun = 0. results = nelder_mead(sum_squared, x0, tol_f=1e-50, tol_x=1e-50) assert_allclose(results.x, sol, atol=1e-5) assert_allclose(results.fun, fun, atol=1e-5)
def test_powell(self): sol = np.zeros(4) fun = 0. x0 = np.array([3., -1., 0., 1.]) results = nelder_mead(powell, x0, tol_x=1e-20, tol_f=1e-20) assert_allclose(results.x, sol, atol=1e-4) assert_allclose(results.fun, fun, atol=1e-4)
def test_discontinuous(self): sol = np.array([1.]) fun = -0.5 x0 = np.array([-10.]) results = nelder_mead(g, x0) assert_allclose(results.x, sol) assert_allclose(results.fun, fun)
def test_easom(self): sol = np.array([np.pi, np.pi]) fun = 1. x0 = np.array([5, -1]) results = nelder_mead(easom, x0, tol_x=1e-20, tol_f=1e-20) assert_allclose(results.x, sol, atol=1e-4) assert_allclose(results.fun, fun, atol=1e-4)
def test_rosenbrock(self): sol = np.array([1., 1.]) fun = 0. x0 = np.array([-2, 1]) results = nelder_mead(rosenbrock, x0, tol_x=1e-20, tol_f=1e-20) assert_allclose(results.x, sol, atol=1e-4) assert_allclose(results.fun, fun, atol=1e-4)
def test_goldstein_price(self): x0 = np.array([-1.5, 0.5]) results = nelder_mead(goldstein_price, x0) sol = np.array([0., -1.]) fun = -3. assert_allclose(results.x, sol, atol=1e-5) assert_allclose(results.fun, fun)
def test_booth(self): x0 = np.array([0., 0.]) sol = np.array([1., 3.]) fun = 0. results = nelder_mead(booth, x0, tol_x=1e-20, tol_f=1e-20) assert_allclose(results.x, sol, atol=1e-7) assert_allclose(results.fun, fun, atol=1e-7)
def test_corner_sol(self): sol = np.array([0.]) fun = 0. x0 = np.array([10.]) bounds = np.array([[0., np.inf]]) results = nelder_mead(f, x0, bounds=bounds, tol_f=1e-20) assert_allclose(results.x, sol) assert_allclose(results.fun, fun)
def test_bohachevsky(self): sol = np.array([0., 0.]) fun = 0. # Starting point makes significant difference x0 = np.array([np.pi, -np.pi]) results = nelder_mead(bohachevsky, x0) assert_allclose(results.x, sol, atol=1e-4) assert_allclose(results.fun, fun, atol=1e-4)
def test_mccormick(self): sol = np.array([-0.54719, -1.54719]) fun = 1.9133 x0 = np.array([-1., -1.5]) bounds = np.array([[-1.5, 4.], [-3., 4.]]) results = nelder_mead(mccormick, x0, bounds=bounds) assert_allclose(results.x, sol, rtol=1e-3) assert_allclose(results.fun, fun, rtol=1e-3)
def test_zakharov(self): x0 = np.array([-3., 8., 1., 3., -0.5]) bounds = np.array([[-5., 10.]] * 5) sol = np.zeros(5) fun = 0. results = nelder_mead(zakharov, x0, bounds=bounds, tol_f=1e-30, tol_x=1e-30) assert_allclose(results.x, sol, atol=1e-7) assert_allclose(results.fun, fun, atol=1e-7)
def test_colville(self): x0 = np.array([-3.5, 9., 0.25, -1.]) bounds = np.array([[-10., 10.]] * 4) sol = np.ones(4) fun = 0. results = nelder_mead(colville, x0, bounds=bounds, tol_f=1e-35, tol_x=1e-35) assert_allclose(results.x, sol) assert_allclose(results.fun, fun, atol=1e-7)
def test_perm_function(self): d = 4. x0 = np.ones(int(d)) bounds = np.array([[-d, d]] * int(d)) sol = np.array([1 / d for d in range(1, int(d)+1)]) fun = 0. results = nelder_mead(perm_function, x0, bounds=bounds, args=(1., ), tol_x=1e-30, tol_f=1e-30) assert_allclose(results.x, sol, atol=1e-7) assert_allclose(results.fun, fun, atol=1e-7)
def test_rotated_hyper_ellipsoid(self): d = 5 x0 = np.random.normal(size=d) bounds = np.array([[-65.536, 65.536]] * d) sol = np.zeros(d) fun = 0. results = nelder_mead(rotated_hyper_ellipsoid, x0, bounds=bounds, tol_x=1e-30, tol_f=1e-30) assert_allclose(results.x, sol, atol=1e-7) assert_allclose(results.fun, fun, atol=1e-7)
def test_styblinski_tang(self): d = 8 x0 = -np.ones(d) bounds = np.array([[-5., 5.]] * d) sol = np.array([-2.903534] * d) fun = 39.16599 * d results = nelder_mead(styblinski_tang, x0, bounds=bounds, tol_f=1e-35, tol_x=1e-35) assert_allclose(results.x, sol, rtol=1e-4) assert_allclose(results.fun, fun, rtol=1e-5)
def test_perm_function(self): d = 4. x0 = np.ones(int(d)) bounds = np.array([[-d, d]] * int(d)) sol = np.array([1 / d for d in range(1, int(d) + 1)]) fun = 0. results = nelder_mead(perm_function, x0, bounds=bounds, args=(1., ), tol_x=1e-30, tol_f=1e-30) assert_allclose(results.x, sol, atol=1e-7) assert_allclose(results.fun, fun, atol=1e-7)
def test_invalid_bounds_2(): vertices = np.array([[-2., 1.], [1.05 * -2., 1.], [-2., 1.05 * 1.]]) x0 = np.array([-2., 1.]) bounds = np.array([[10., -10., 10., -10.]]) nelder_mead(rosenbrock, x0, bounds=bounds)
def test_invalid_bounds_2(): x0 = np.array([-2., 1.]) bounds = np.array([[10., -10., 10., -10.]]) nelder_mead(rosenbrock, x0, bounds=bounds)