示例#1
0
 def test_epsilon(self, eps, locally_biased):
     result = direct(self.styblinski_tang,
                     self.bounds_stylinski_tang,
                     eps=eps,
                     vol_tol=1e-6,
                     locally_biased=locally_biased)
     assert result.status == 4
     assert result.success
示例#2
0
    def test_f_circle_with_args(self, locally_biased):
        bounds = 2 * [(-2.0, 2.0)]

        res = direct(self.circle_with_args,
                     bounds,
                     args=(1, 1),
                     maxfun=1250,
                     locally_biased=locally_biased)
        assert_allclose(res.x, np.array([1., 1.]), rtol=1e-5)
示例#3
0
 def test_segmentation_fault(self, locally_biased):
     # test that an excessive number of function evaluations
     # does not result in segmentation fault
     bounds = [(-5., 20.)] * 100
     result = direct(self.sphere,
                     bounds,
                     maxfun=10000000,
                     maxiter=1000000,
                     locally_biased=locally_biased)
     assert result is not None
示例#4
0
 def test_len_tol(self, len_tol, locally_biased):
     bounds = 4 * [(-10., 10.)]
     res = direct(self.sphere,
                  bounds=bounds,
                  len_tol=len_tol,
                  vol_tol=1e-30,
                  locally_biased=locally_biased)
     assert res.status == 5
     assert res.success
     assert_allclose(res.x, np.zeros((4, )))
示例#5
0
    def test_failure_maxiter(self, locally_biased):
        # test that if optimization runs for the maximal number of
        # iterations, success = False is returned

        maxiter = 10
        result = direct(self.styblinski_tang,
                        self.bounds_stylinski_tang,
                        maxiter=maxiter,
                        locally_biased=locally_biased)
        assert result.success is False
        assert result.status == 2
        assert result.nit >= maxiter
示例#6
0
    def test_failure_maxfun(self, locally_biased):
        # test that if optimization runs for the maximal number of
        # evaluations, success = False is returned

        maxfun = 100
        result = direct(self.styblinski_tang,
                        self.bounds_stylinski_tang,
                        maxfun=maxfun,
                        locally_biased=locally_biased)
        assert result.success is False
        assert result.status == 1
        assert result.nfev >= maxfun
示例#7
0
    def test_bounds_variants(self, locally_biased):
        # test that new and old bounds yield same result

        lb = [-6., 1., -5.]
        ub = [-1., 3., 5.]
        x_opt = np.array([-1., 1., 0.])
        bounds_old = list(zip(lb, ub))
        bounds_new = Bounds(lb, ub)

        res_old_bounds = direct(self.sphere,
                                bounds_old,
                                locally_biased=locally_biased)
        res_new_bounds = direct(self.sphere,
                                bounds_new,
                                locally_biased=locally_biased)

        assert res_new_bounds.nfev == res_old_bounds.nfev
        assert res_new_bounds.message == res_old_bounds.message
        assert res_new_bounds.success == res_old_bounds.success
        assert res_new_bounds.nit == res_old_bounds.nit
        assert_allclose(res_new_bounds.x, res_old_bounds.x)
        assert_allclose(res_new_bounds.x, x_opt, rtol=1e-2)
示例#8
0
 def test_f_min(self, f_min_rtol, locally_biased):
     # test that desired function value is reached within
     # relative tolerance of f_min_rtol
     f_min = 1.
     bounds = 4 * [(-2., 10.)]
     res = direct(self.sphere,
                  bounds=bounds,
                  f_min=f_min,
                  f_min_rtol=f_min_rtol,
                  locally_biased=locally_biased)
     assert res.status == 3
     assert res.success
     assert res.fun < f_min * (1. + f_min_rtol)
示例#9
0
    def test_direct_callback(self, locally_biased):
        # test that callback does not change the result
        res = direct(self.sphere,
                     self.bounds_sphere,
                     locally_biased=locally_biased)

        def callback(x):
            x = 2 * x
            dummy = np.square(x)
            print("DIRECT minimization algorithm callback test")
            return dummy

        res_callback = direct(self.sphere,
                              self.bounds_sphere,
                              locally_biased=locally_biased,
                              callback=callback)

        assert_allclose(res.x, res_callback.x)

        assert res.nit == res_callback.nit
        assert res.nfev == res_callback.nfev
        assert res.status == res_callback.status
        assert res.success == res_callback.success
        assert res.fun == res_callback.fun
        assert_allclose(res.x, res_callback.x)
        assert res.message == res_callback.message

        # test accuracy
        assert_allclose(res_callback.x,
                        self.optimum_sphere_pos,
                        rtol=1e-3,
                        atol=1e-3)
        assert_allclose(res_callback.fun,
                        self.optimum_sphere,
                        atol=1e-5,
                        rtol=1e-5)
示例#10
0
    def test_direct(self, locally_biased):
        res = direct(self.sphere,
                     self.bounds_sphere,
                     locally_biased=locally_biased)

        # test accuracy
        assert_allclose(res.x, self.optimum_sphere_pos, rtol=1e-3, atol=1e-3)
        assert_allclose(res.fun, self.optimum_sphere, atol=1e-5, rtol=1e-5)

        # test that result lies within bounds
        _bounds = np.asarray(self.bounds_sphere)
        assert_array_less(_bounds[:, 0], res.x)
        assert_array_less(res.x, _bounds[:, 1])

        # test number of function evaluations. Original DIRECT overshoots by
        # up to 500 evaluations in last iteration
        assert res.nfev <= 1000 * (len(self.bounds_sphere) + 1)
        # test that number of function evaluations is correct
        assert res.nfev == self.fun_calls

        # test that number of iterations is below supplied maximum
        assert res.nit <= self.maxiter
示例#11
0
 def test_inf_fun(self, locally_biased):
     # test that an objective value of infinity does not crash DIRECT
     bounds = [(-5., 5.)] * 2
     result = direct(self.inf_fun, bounds, locally_biased=locally_biased)
     assert result is not None
示例#12
0
 def test_nan(self, locally_biased):
     bounds = 4 * [(-10, 10)]
     direct(self.nan_fun, bounds=bounds, locally_biased=locally_biased)
示例#13
0
 def test_exception(self, locally_biased):
     bounds = 4 * [(-10, 10)]
     with pytest.raises(ZeroDivisionError):
         direct(self.inv, bounds=bounds, locally_biased=locally_biased)