def test_in_bounds(self):
     for jac in ['2-point', '3-point', jac_trivial]:
         res = least_squares(fun_trivial, 2.0, jac=jac,
                             bounds=(-1.0, 3.0), method=self.method)
         assert_allclose(res.x, 0.0, atol=1e-4)
         assert_equal(res.active_mask, [0])
         assert_(-1 <= res.x <= 3)
         res = least_squares(fun_trivial, 2.0, jac=jac,
                             bounds=(0.5, 3.0), method=self.method)
         assert_allclose(res.x, 0.5, atol=1e-4)
         assert_equal(res.active_mask, [-1])
         assert_(0.5 <= res.x <= 3)
 def test_bounds_shape(self):
     for jac in ['2-point', '3-point', jac_2d_trivial]:
         x0 = [1.0, 1.0]
         res = least_squares(fun_2d_trivial, x0, jac=jac)
         assert_allclose(res.x, [0.0, 0.0])
         res = least_squares(fun_2d_trivial, x0, jac=jac,
                             bounds=(0.5, [2.0, 2.0]), method=self.method)
         assert_allclose(res.x, [0.5, 0.5])
         res = least_squares(fun_2d_trivial, x0, jac=jac,
                             bounds=([0.3, 0.2], 3.0), method=self.method)
         assert_allclose(res.x, [0.3, 0.2])
         res = least_squares(
             fun_2d_trivial, x0, jac=jac, bounds=([-1, 0.5], [1.0, 3.0]),
             method=self.method)
         assert_allclose(res.x, [0.0, 0.5], atol=1e-5)
 def test_diff_step(self):
     # res1 and res2 should be equivalent.
     # res2 and res3 should be different.
     res1 = least_squares(fun_trivial, 2.0, diff_step=1e-2,
                          method=self.method)
     res2 = least_squares(fun_trivial, 2.0, diff_step=-1e-2,
                          method=self.method)
     res3 = least_squares(fun_trivial, 2.0,
                          diff_step=None, method=self.method)
     assert_allclose(res1.x, 0, atol=1e-4)
     assert_allclose(res2.x, 0, atol=1e-4)
     assert_allclose(res3.x, 0, atol=1e-4)
     assert_equal(res1.x, res2.x)
     assert_equal(res1.nfev, res2.nfev)
     assert_(res2.nfev != res3.nfev)
Example #4
0
 def test_nfev_options(self):
     for max_nfev in [None, 20]:
         res = least_squares(fun_trivial,
                             2.0,
                             max_nfev=max_nfev,
                             method=self.method)
         assert_allclose(res.x, 0, atol=1e-4)
 def test_rosenbrock(self):
     x0 = [-2, 1]
     x_opt = [1, 1]
     for scaling in [1.0, np.array([1.0, 5.0]), 'jac']:
         for jac in ['2-point', '3-point', jac_rosenbrock]:
             res = least_squares(fun_rosenbrock, x0, jac, scaling=scaling,
                                 method=self.method)
             assert_allclose(res.x, x_opt)
 def test_scaling_options(self):
     for scaling in [1.0, np.array([2.0]), 'jac']:
         res = least_squares(fun_trivial, 2.0, scaling=scaling)
         assert_allclose(res.x, 0)
     assert_raises(ValueError, least_squares, fun_trivial,
                   2.0, scaling='auto', method=self.method)
     assert_raises(ValueError, least_squares, fun_trivial,
                   2.0, scaling=-1.0, method=self.method)
Example #7
0
    def test_args_kwargs(self):
        # Test that args and kwargs are passed correctly to the functions.
        # And that kwargs are not supported by 'lm'.
        a = 3.0
        for jac in ['2-point', '3-point', jac_trivial]:
            res = least_squares(fun_trivial,
                                2.0,
                                jac,
                                args=(a, ),
                                method=self.method)
            assert_allclose(res.x, a, rtol=1e-4)
            assert_allclose(res.fun, fun_trivial(res.x, a))

            assert_raises(TypeError,
                          least_squares,
                          fun_trivial,
                          2.0,
                          args=(
                              3,
                              4,
                          ),
                          method=self.method)

            # Test that kwargs works for everything except 'lm.
            if self.method == 'lm':
                assert_raises(ValueError,
                              least_squares,
                              fun_trivial,
                              2.0,
                              kwargs={'a': a},
                              method=self.method)
            else:
                res = least_squares(fun_trivial,
                                    2.0,
                                    jac,
                                    kwargs={'a': a},
                                    method=self.method)
                assert_allclose(res.x, a, rtol=1e-4)
                assert_allclose(res.fun, fun_trivial(res.x, a))
                assert_raises(TypeError,
                              least_squares,
                              fun_trivial,
                              2.0,
                              kwargs={'kaboom': 3},
                              method=self.method)
Example #8
0
 def test_in_bounds(self):
     for jac in ['2-point', '3-point', jac_trivial]:
         res = least_squares(fun_trivial,
                             2.0,
                             jac=jac,
                             bounds=(-1.0, 3.0),
                             method=self.method)
         assert_allclose(res.x, 0.0, atol=1e-4)
         assert_equal(res.active_mask, [0])
         assert_(-1 <= res.x <= 3)
         res = least_squares(fun_trivial,
                             2.0,
                             jac=jac,
                             bounds=(0.5, 3.0),
                             method=self.method)
         assert_allclose(res.x, 0.5, atol=1e-4)
         assert_equal(res.active_mask, [-1])
         assert_(0.5 <= res.x <= 3)
Example #9
0
 def test_jac_options(self):
     for jac in ['2-point', '3-point', jac_trivial]:
         res = least_squares(fun_trivial, 2.0, jac, method=self.method)
         assert_allclose(res.x, 0, atol=1e-4)
     assert_raises(ValueError,
                   least_squares,
                   fun_trivial,
                   2.0,
                   jac='oops',
                   method=self.method)
Example #10
0
 def test_rosenbrock(self):
     x0 = [-2, 1]
     x_opt = [1, 1]
     for scaling in [1.0, np.array([1.0, 5.0]), 'jac']:
         for jac in ['2-point', '3-point', jac_rosenbrock]:
             res = least_squares(fun_rosenbrock,
                                 x0,
                                 jac,
                                 scaling=scaling,
                                 method=self.method)
             assert_allclose(res.x, x_opt)
Example #11
0
 def test_diff_step(self):
     # res1 and res2 should be equivalent.
     # res2 and res3 should be different.
     res1 = least_squares(fun_trivial,
                          2.0,
                          diff_step=1e-2,
                          method=self.method)
     res2 = least_squares(fun_trivial,
                          2.0,
                          diff_step=-1e-2,
                          method=self.method)
     res3 = least_squares(fun_trivial,
                          2.0,
                          diff_step=None,
                          method=self.method)
     assert_allclose(res1.x, 0, atol=1e-4)
     assert_allclose(res2.x, 0, atol=1e-4)
     assert_allclose(res3.x, 0, atol=1e-4)
     assert_equal(res1.x, res2.x)
     assert_equal(res1.nfev, res2.nfev)
     assert_(res2.nfev != res3.nfev)
Example #12
0
 def test_bounds_shape(self):
     for jac in ['2-point', '3-point', jac_2d_trivial]:
         x0 = [1.0, 1.0]
         res = least_squares(fun_2d_trivial, x0, jac=jac)
         assert_allclose(res.x, [0.0, 0.0])
         res = least_squares(fun_2d_trivial,
                             x0,
                             jac=jac,
                             bounds=(0.5, [2.0, 2.0]),
                             method=self.method)
         assert_allclose(res.x, [0.5, 0.5])
         res = least_squares(fun_2d_trivial,
                             x0,
                             jac=jac,
                             bounds=([0.3, 0.2], 3.0),
                             method=self.method)
         assert_allclose(res.x, [0.3, 0.2])
         res = least_squares(fun_2d_trivial,
                             x0,
                             jac=jac,
                             bounds=([-1, 0.5], [1.0, 3.0]),
                             method=self.method)
         assert_allclose(res.x, [0.0, 0.5], atol=1e-5)
Example #13
0
 def test_tolerance_thresholds(self):
     assert_warns(UserWarning,
                  least_squares,
                  fun_trivial,
                  2.0,
                  ftol=0.0,
                  method=self.method)
     res = least_squares(fun_trivial,
                         2.0,
                         ftol=1e-20,
                         xtol=-1.0,
                         gtol=0.0,
                         method=self.method)
     assert_allclose(res.x, 0, atol=1e-4)
    def test_args_kwargs(self):
        # Test that args and kwargs are passed correctly to the functions.
        # And that kwargs are not supported by 'lm'.
        a = 3.0
        for jac in ['2-point', '3-point', jac_trivial]:
            res = least_squares(fun_trivial, 2.0, jac, args=(a,),
                                method=self.method)
            assert_allclose(res.x, a, rtol=1e-4)
            assert_allclose(res.fun, fun_trivial(res.x, a))

            assert_raises(TypeError, least_squares, fun_trivial, 2.0,
                          args=(3, 4,), method=self.method)
        
            # Test that kwargs works for everything except 'lm.
            if self.method == 'lm':
                assert_raises(ValueError, least_squares, fun_trivial, 2.0,
                              kwargs={'a': a}, method=self.method)
            else:
                res = least_squares(fun_trivial, 2.0, jac, kwargs={'a': a},
                                    method=self.method)
                assert_allclose(res.x, a, rtol=1e-4)
                assert_allclose(res.fun, fun_trivial(res.x, a))
                assert_raises(TypeError, least_squares, fun_trivial, 2.0,
                              kwargs={'kaboom': 3}, method=self.method)
Example #15
0
 def test_scaling_options(self):
     for scaling in [1.0, np.array([2.0]), 'jac']:
         res = least_squares(fun_trivial, 2.0, scaling=scaling)
         assert_allclose(res.x, 0)
     assert_raises(ValueError,
                   least_squares,
                   fun_trivial,
                   2.0,
                   scaling='auto',
                   method=self.method)
     assert_raises(ValueError,
                   least_squares,
                   fun_trivial,
                   2.0,
                   scaling=-1.0,
                   method=self.method)
def run_least_squares(problem, ftol, xtol, gtol, jac, **kwargs):
    l, u = problem.bounds
    if l is None:
        l = -np.inf
    if u is None:
        u = np.inf
    bounds = l, u
    if jac == 'exact':
        jac = problem.jac

    result = least_squares(problem.fun, problem.x0, jac=jac, bounds=bounds,
                           ftol=ftol, gtol=gtol, xtol=xtol, **kwargs)
    x = result.x
    g = 0.5 * problem.grad(x)
    optimality = CL_optimality(result.x, g, l, u)

    return (result.nfev, optimality, result.obj_value,
            np.sum(result.active_mask != 0), result.status)
 def test_rosenbrock_bounds(self):
     x0_1 = np.array([-2.0, 1.0])
     x0_2 = np.array([2.0, 2.0])
     x0_3 = np.array([-2.0, 2.0])
     x0_4 = np.array([0.0, 2.0])
     x0_5 = np.array([-1.2, 1.0])
     problems = [
         (x0_1, ([-np.inf, -1.5], np.inf)),
         (x0_2, ([-np.inf, 1.5], np.inf)),
         (x0_3, ([-np.inf, 1.5], np.inf)),
         (x0_4, ([-np.inf, 1.5], [1.0, np.inf])),
         (x0_2, ([1.0, 1.5], [3.0, 3.0])),
         (x0_5, ([-50.0, 0.0], [0.5, 100]))
     ]
     for x0, bounds in problems:
         for scaling in [1.0, [1.0, 2.0], 'jac']:
             for jac in ['2-point', '3-point', jac_rosenbrock]:
                 res = least_squares(fun_rosenbrock, x0, jac, bounds,
                                     method=self.method, scaling=scaling)
                 assert_allclose(res.optimality, 0.0, atol=1e-5)
Example #18
0
 def test_full_result(self):
     res = least_squares(fun_trivial, 2.0, method=self.method)
     # Use assert_almost_equal to check shapes of arrays too.
     assert_almost_equal(res.x, np.array([0]), decimal=1)
     assert_almost_equal(res.obj_value, 25)
     assert_almost_equal(res.fun, np.array([5]))
     assert_almost_equal(res.jac, np.array([[0.0]]), decimal=2)
     # 'lm' works weired on this problem
     assert_almost_equal(res.optimality, 0, decimal=3)
     assert_equal(res.active_mask, np.array([0]))
     if self.method == 'lm':
         assert_(res.nfev < 25)
     else:
         assert_(res.nfev < 10)
     if self.method == 'lm':
         assert_(res.njev is None)
     else:
         assert_(res.njev < 10)
     assert_(res.status > 0)
     assert_(res.success)
 def test_full_result(self):
     res = least_squares(fun_trivial, 2.0, method=self.method)
     # Use assert_almost_equal to check shapes of arrays too.
     assert_almost_equal(res.x, np.array([0]), decimal=1)
     assert_almost_equal(res.obj_value, 25)
     assert_almost_equal(res.fun, np.array([5]))
     assert_almost_equal(res.jac, np.array([[0.0]]), decimal=2)
     # 'lm' works weired on this problem
     assert_almost_equal(res.optimality, 0, decimal=3)
     assert_equal(res.active_mask, np.array([0]))
     if self.method == 'lm':
         assert_(res.nfev < 25)
     else:
         assert_(res.nfev < 10)
     if self.method == 'lm':
         assert_(res.njev is None)
     else:
         assert_(res.njev < 10)
     assert_(res.status > 0)
     assert_(res.success)
Example #20
0
 def test_rosenbrock_bounds(self):
     x0_1 = np.array([-2.0, 1.0])
     x0_2 = np.array([2.0, 2.0])
     x0_3 = np.array([-2.0, 2.0])
     x0_4 = np.array([0.0, 2.0])
     x0_5 = np.array([-1.2, 1.0])
     problems = [(x0_1, ([-np.inf, -1.5], np.inf)),
                 (x0_2, ([-np.inf, 1.5], np.inf)),
                 (x0_3, ([-np.inf, 1.5], np.inf)),
                 (x0_4, ([-np.inf, 1.5], [1.0, np.inf])),
                 (x0_2, ([1.0, 1.5], [3.0, 3.0])),
                 (x0_5, ([-50.0, 0.0], [0.5, 100]))]
     for x0, bounds in problems:
         for scaling in [1.0, [1.0, 2.0], 'jac']:
             for jac in ['2-point', '3-point', jac_rosenbrock]:
                 res = least_squares(fun_rosenbrock,
                                     x0,
                                     jac,
                                     bounds,
                                     method=self.method,
                                     scaling=scaling)
                 assert_allclose(res.optimality, 0.0, atol=1e-5)
Example #21
0
def run_least_squares(problem, ftol, xtol, gtol, jac, **kwargs):
    l, u = problem.bounds
    if l is None:
        l = -np.inf
    if u is None:
        u = np.inf
    bounds = l, u
    if jac == 'exact':
        jac = problem.jac

    result = least_squares(problem.fun,
                           problem.x0,
                           jac=jac,
                           bounds=bounds,
                           ftol=ftol,
                           gtol=gtol,
                           xtol=xtol,
                           **kwargs)
    x = result.x
    g = 0.5 * problem.grad(x)
    optimality = CL_optimality(result.x, g, l, u)

    return (result.nfev, optimality, result.obj_value,
            np.sum(result.active_mask != 0), result.status)
def test_basic():
    # test that 'method' arg is really optional
    res = least_squares(fun_trivial, 2.0)
    assert_allclose(res.x, 0, atol=1e-10)
    assert_allclose(res.fun, fun_trivial(res.x))
 def test_basic(self):
     # Test that the basic calling sequence works.
     res = least_squares(fun_trivial, 2., method=self.method)
     assert_allclose(res.x, 0, atol=1e-4)
     assert_allclose(res.fun, fun_trivial(res.x))
Example #24
0
 def test_basic(self):
     # Test that the basic calling sequence works.
     res = least_squares(fun_trivial, 2., method=self.method)
     assert_allclose(res.x, 0, atol=1e-4)
     assert_allclose(res.fun, fun_trivial(res.x))
Example #25
0
def test_basic():
    # test that 'method' arg is really optional
    res = least_squares(fun_trivial, 2.0)
    assert_allclose(res.x, 0, atol=1e-10)
    assert_allclose(res.fun, fun_trivial(res.x))
 def test_jac_options(self):
     for jac in ['2-point', '3-point', jac_trivial]:
         res = least_squares(fun_trivial, 2.0, jac, method=self.method)
         assert_allclose(res.x, 0, atol=1e-4)
     assert_raises(ValueError, least_squares, fun_trivial, 2.0, jac='oops',
                   method=self.method)
 def test_nfev_options(self):
     for max_nfev in [None, 20]:
         res = least_squares(fun_trivial, 2.0, max_nfev=max_nfev,
                             method=self.method)
         assert_allclose(res.x, 0, atol=1e-4)
 def test_tolerance_thresholds(self):
     assert_warns(UserWarning, least_squares, fun_trivial, 2.0, ftol=0.0,
                  method=self.method)
     res = least_squares(fun_trivial, 2.0, ftol=1e-20, xtol=-1.0, gtol=0.0,
                         method=self.method)
     assert_allclose(res.x, 0, atol=1e-4)