コード例 #1
0
ファイル: test_projections.py プロジェクト: wanglun1996/scipy
    def test_compare_dense_and_sparse(self):
        D = np.diag(range(1, 101))
        A = np.hstack([D, D, D, D])
        A_sparse = csc_matrix(A)
        np.random.seed(0)

        Z, LS, Y = projections(A)
        Z_sparse, LS_sparse, Y_sparse = projections(A_sparse)
        for k in range(20):
            z = np.random.normal(size=(400, ))
            assert_array_almost_equal(Z.dot(z), Z_sparse.dot(z))
            assert_array_almost_equal(LS.dot(z), LS_sparse.dot(z))
            x = np.random.normal(size=(100, ))
            assert_array_almost_equal(Y.dot(x), Y_sparse.dot(x))
コード例 #2
0
ファイル: test_projections.py プロジェクト: BranYang/scipy
    def test_compare_dense_and_sparse(self):
        D = np.diag(range(1, 101))
        A = np.hstack([D, D, D, D])
        A_sparse = csc_matrix(A)
        np.random.seed(0)

        Z, LS, Y = projections(A)
        Z_sparse, LS_sparse, Y_sparse = projections(A_sparse)
        for k in range(20):
            z = np.random.normal(size=(400,))
            assert_array_almost_equal(Z.dot(z), Z_sparse.dot(z))
            assert_array_almost_equal(LS.dot(z), LS_sparse.dot(z))
            x = np.random.normal(size=(100,))
            assert_array_almost_equal(Y.dot(x), Y_sparse.dot(x))
コード例 #3
0
ファイル: test_projections.py プロジェクト: wanglun1996/scipy
    def test_compare_dense_and_sparse2(self):
        D1 = np.diag([-1.7, 1, 0.5])
        D2 = np.diag([1, -0.6, -0.3])
        D3 = np.diag([-0.3, -1.5, 2])
        A = np.hstack([D1, D2, D3])
        A_sparse = csc_matrix(A)
        np.random.seed(0)

        Z, LS, Y = projections(A)
        Z_sparse, LS_sparse, Y_sparse = projections(A_sparse)
        for k in range(1):
            z = np.random.normal(size=(9, ))
            assert_array_almost_equal(Z.dot(z), Z_sparse.dot(z))
            assert_array_almost_equal(LS.dot(z), LS_sparse.dot(z))
            x = np.random.normal(size=(3, ))
            assert_array_almost_equal(Y.dot(x), Y_sparse.dot(x))
コード例 #4
0
ファイル: test_projections.py プロジェクト: BranYang/scipy
    def test_compare_dense_and_sparse2(self):
        D1 = np.diag([-1.7, 1, 0.5])
        D2 = np.diag([1, -0.6, -0.3])
        D3 = np.diag([-0.3, -1.5, 2])
        A = np.hstack([D1, D2, D3])
        A_sparse = csc_matrix(A)
        np.random.seed(0)

        Z, LS, Y = projections(A)
        Z_sparse, LS_sparse, Y_sparse = projections(A_sparse)
        for k in range(1):
            z = np.random.normal(size=(9,))
            assert_array_almost_equal(Z.dot(z), Z_sparse.dot(z))
            assert_array_almost_equal(LS.dot(z), LS_sparse.dot(z))
            x = np.random.normal(size=(3,))
            assert_array_almost_equal(Y.dot(x), Y_sparse.dot(x))
コード例 #5
0
 def test_negative_curvature_unconstrained(self):
     H = csc_matrix([[1, 2, 1, 3], [2, 0, 2, 4], [1, 2, 0, 2], [3, 4, 2,
                                                                0]])
     A = csc_matrix([[1, 0, 1, 0], [0, 1, 0, 1]])
     c = np.array([-2, -3, -3, 1])
     b = -np.array([3, 0])
     Z, _, Y = projections(A)
     with pytest.raises(ValueError):
         projected_cg(H, c, Z, Y, b, tol=0)
コード例 #6
0
 def test_trust_region_infeasible(self):
     H = csc_matrix([[6, 2, 1, 3], [2, 5, 2, 4], [1, 2, 4, 5], [3, 4, 5,
                                                                7]])
     A = csc_matrix([[1, 0, 1, 0], [0, 1, 1, 1]])
     c = np.array([-2, -3, -3, 1])
     b = -np.array([3, 0])
     trust_radius = 1
     Z, _, Y = projections(A)
     with pytest.raises(ValueError):
         projected_cg(H, c, Z, Y, b, trust_radius=trust_radius)
コード例 #7
0
 def test_nocedal_example(self):
     H = csc_matrix([[6, 2, 1], [2, 5, 2], [1, 2, 4]])
     A = csc_matrix([[1, 0, 1], [0, 1, 1]])
     c = np.array([-8, -3, -3])
     b = -np.array([3, 0])
     Z, _, Y = projections(A)
     x, info = projected_cg(H, c, Z, Y, b)
     assert_equal(info["stop_cond"], 4)
     assert_equal(info["hits_boundary"], False)
     assert_array_almost_equal(x, [2, -1, 1])
コード例 #8
0
ファイル: test_qp_subproblem.py プロジェクト: BranYang/scipy
    def test_3d_example(self):
        A = np.array([[1, 8, 1],
                      [4, 2, 2]])
        b = np.array([-16, 2])
        Z, LS, Y = projections(A)

        newton_point = np.array([-1.37090909, 2.23272727, -0.49090909])
        cauchy_point = np.array([0.11165723, 1.73068711, 0.16748585])
        origin = np.zeros_like(newton_point)

        # newton_point inside boundaries
        x = modified_dogleg(A, Y, b, 3, [-np.inf, -np.inf, -np.inf],
                            [np.inf, np.inf, np.inf])
        assert_array_almost_equal(x, newton_point)

        # line between cauchy_point and newton_point contains best point
        # (spherical constrain is active).
        x = modified_dogleg(A, Y, b, 2, [-np.inf, -np.inf, -np.inf],
                            [np.inf, np.inf, np.inf])
        z = cauchy_point
        d = newton_point-cauchy_point
        t = ((x-z)/(d))
        assert_array_almost_equal(t, 0.40807330*np.ones(3))
        assert_array_almost_equal(np.linalg.norm(x), 2)

        # line between cauchy_point and newton_point contains best point
        # (box constrain is active).
        x = modified_dogleg(A, Y, b, 5, [-1, -np.inf, -np.inf],
                            [np.inf, np.inf, np.inf])
        z = cauchy_point
        d = newton_point-cauchy_point
        t = ((x-z)/(d))
        assert_array_almost_equal(t, 0.7498195*np.ones(3))
        assert_array_almost_equal(x[0], -1)

        # line between origin and cauchy_point contains best point
        # (spherical constrain is active).
        x = modified_dogleg(A, Y, b, 1, [-np.inf, -np.inf, -np.inf],
                            [np.inf, np.inf, np.inf])
        z = origin
        d = cauchy_point
        t = ((x-z)/(d))
        assert_array_almost_equal(t, 0.573936265*np.ones(3))
        assert_array_almost_equal(np.linalg.norm(x), 1)

        # line between origin and newton_point contains best point
        # (box constrain is active).
        x = modified_dogleg(A, Y, b, 2, [-np.inf, -np.inf, -np.inf],
                            [np.inf, 1, np.inf])
        z = origin
        d = newton_point
        t = ((x-z)/(d))
        assert_array_almost_equal(t, 0.4478827364*np.ones(3))
        assert_array_almost_equal(x[1], 1)
コード例 #9
0
    def test_3d_example(self):
        A = np.array([[1, 8, 1],
                      [4, 2, 2]])
        b = np.array([-16, 2])
        Z, LS, Y = projections(A)

        newton_point = np.array([-1.37090909, 2.23272727, -0.49090909])
        cauchy_point = np.array([0.11165723, 1.73068711, 0.16748585])
        origin = np.zeros_like(newton_point)

        # newton_point inside boundaries
        x = modified_dogleg(A, Y, b, 3, [-np.inf, -np.inf, -np.inf],
                            [np.inf, np.inf, np.inf])
        assert_array_almost_equal(x, newton_point)

        # line between cauchy_point and newton_point contains best point
        # (spherical constrain is active).
        x = modified_dogleg(A, Y, b, 2, [-np.inf, -np.inf, -np.inf],
                            [np.inf, np.inf, np.inf])
        z = cauchy_point
        d = newton_point-cauchy_point
        t = ((x-z)/(d))
        assert_array_almost_equal(t, np.full(3, 0.40807330))
        assert_array_almost_equal(np.linalg.norm(x), 2)

        # line between cauchy_point and newton_point contains best point
        # (box constrain is active).
        x = modified_dogleg(A, Y, b, 5, [-1, -np.inf, -np.inf],
                            [np.inf, np.inf, np.inf])
        z = cauchy_point
        d = newton_point-cauchy_point
        t = ((x-z)/(d))
        assert_array_almost_equal(t, np.full(3, 0.7498195))
        assert_array_almost_equal(x[0], -1)

        # line between origin and cauchy_point contains best point
        # (spherical constrain is active).
        x = modified_dogleg(A, Y, b, 1, [-np.inf, -np.inf, -np.inf],
                            [np.inf, np.inf, np.inf])
        z = origin
        d = cauchy_point
        t = ((x-z)/(d))
        assert_array_almost_equal(t, np.full(3, 0.573936265))
        assert_array_almost_equal(np.linalg.norm(x), 1)

        # line between origin and newton_point contains best point
        # (box constrain is active).
        x = modified_dogleg(A, Y, b, 2, [-np.inf, -np.inf, -np.inf],
                            [np.inf, 1, np.inf])
        z = origin
        d = newton_point
        t = ((x-z)/(d))
        assert_array_almost_equal(t, np.full(3, 0.4478827364))
        assert_array_almost_equal(x[1], 1)
コード例 #10
0
 def test_compare_with_direct_fact(self):
     H = csc_matrix([[6, 2, 1, 3], [2, 5, 2, 4], [1, 2, 4, 5], [3, 4, 5,
                                                                7]])
     A = csc_matrix([[1, 0, 1, 0], [0, 1, 1, 1]])
     c = np.array([-2, -3, -3, 1])
     b = -np.array([3, 0])
     Z, _, Y = projections(A)
     x, info = projected_cg(H, c, Z, Y, b, tol=0)
     x_kkt, _ = eqp_kktfact(H, c, A, b)
     assert_equal(info["stop_cond"], 1)
     assert_equal(info["hits_boundary"], False)
     assert_array_almost_equal(x, x_kkt)
コード例 #11
0
ファイル: test_qp_subproblem.py プロジェクト: BranYang/scipy
 def test_negative_curvature_unconstrained(self):
     H = csc_matrix([[1, 2, 1, 3],
                     [2, 0, 2, 4],
                     [1, 2, 0, 2],
                     [3, 4, 2, 0]])
     A = csc_matrix([[1, 0, 1, 0],
                     [0, 1, 0, 1]])
     c = np.array([-2, -3, -3, 1])
     b = -np.array([3, 0])
     Z, _, Y = projections(A)
     with pytest.raises(ValueError):
         projected_cg(H, c, Z, Y, b, tol=0)
コード例 #12
0
 def test_hits_boundary(self):
     H = csc_matrix([[6, 2, 1, 3], [2, 5, 2, 4], [1, 2, 4, 5], [3, 4, 5,
                                                                7]])
     A = csc_matrix([[1, 0, 1, 0], [0, 1, 1, 1]])
     c = np.array([-2, -3, -3, 1])
     b = -np.array([3, 0])
     trust_radius = 3
     Z, _, Y = projections(A)
     x, info = projected_cg(H, c, Z, Y, b, tol=0, trust_radius=trust_radius)
     assert_equal(info["stop_cond"], 2)
     assert_equal(info["hits_boundary"], True)
     assert_array_almost_equal(np.linalg.norm(x), trust_radius)
コード例 #13
0
 def test_negative_curvature(self):
     H = csc_matrix([[1, 2, 1, 3], [2, 0, 2, 4], [1, 2, 0, 2], [3, 4, 2,
                                                                0]])
     A = csc_matrix([[1, 0, 1, 0], [0, 1, 0, 1]])
     c = np.array([-2, -3, -3, 1])
     b = -np.array([3, 0])
     Z, _, Y = projections(A)
     trust_radius = 1000
     x, info = projected_cg(H, c, Z, Y, b, tol=0, trust_radius=trust_radius)
     assert_equal(info["stop_cond"], 3)
     assert_equal(info["hits_boundary"], True)
     assert_array_almost_equal(np.linalg.norm(x), trust_radius)
コード例 #14
0
ファイル: test_qp_subproblem.py プロジェクト: BranYang/scipy
 def test_nocedal_example(self):
     H = csc_matrix([[6, 2, 1],
                     [2, 5, 2],
                     [1, 2, 4]])
     A = csc_matrix([[1, 0, 1],
                     [0, 1, 1]])
     c = np.array([-8, -3, -3])
     b = -np.array([3, 0])
     Z, _, Y = projections(A)
     x, info = projected_cg(H, c, Z, Y, b)
     assert_equal(info["stop_cond"], 4)
     assert_equal(info["hits_boundary"], False)
     assert_array_almost_equal(x, [2, -1, 1])
コード例 #15
0
 def test_trust_region_barely_feasible(self):
     H = csc_matrix([[6, 2, 1, 3], [2, 5, 2, 4], [1, 2, 4, 5], [3, 4, 5,
                                                                7]])
     A = csc_matrix([[1, 0, 1, 0], [0, 1, 1, 1]])
     c = np.array([-2, -3, -3, 1])
     b = -np.array([3, 0])
     trust_radius = 2.32379000772445021283
     Z, _, Y = projections(A)
     x, info = projected_cg(H, c, Z, Y, b, tol=0, trust_radius=trust_radius)
     assert_equal(info["stop_cond"], 2)
     assert_equal(info["hits_boundary"], True)
     assert_array_almost_equal(np.linalg.norm(x), trust_radius)
     assert_array_almost_equal(x, -Y.dot(b))
コード例 #16
0
ファイル: test_qp_subproblem.py プロジェクト: BranYang/scipy
 def test_trust_region_infeasible(self):
     H = csc_matrix([[6, 2, 1, 3],
                     [2, 5, 2, 4],
                     [1, 2, 4, 5],
                     [3, 4, 5, 7]])
     A = csc_matrix([[1, 0, 1, 0],
                     [0, 1, 1, 1]])
     c = np.array([-2, -3, -3, 1])
     b = -np.array([3, 0])
     trust_radius = 1
     Z, _, Y = projections(A)
     with pytest.raises(ValueError):
         projected_cg(H, c, Z, Y, b, trust_radius=trust_radius)
コード例 #17
0
ファイル: test_qp_subproblem.py プロジェクト: BranYang/scipy
 def test_compare_with_direct_fact(self):
     H = csc_matrix([[6, 2, 1, 3],
                     [2, 5, 2, 4],
                     [1, 2, 4, 5],
                     [3, 4, 5, 7]])
     A = csc_matrix([[1, 0, 1, 0],
                     [0, 1, 1, 1]])
     c = np.array([-2, -3, -3, 1])
     b = -np.array([3, 0])
     Z, _, Y = projections(A)
     x, info = projected_cg(H, c, Z, Y, b, tol=0)
     x_kkt, _ = eqp_kktfact(H, c, A, b)
     assert_equal(info["stop_cond"], 1)
     assert_equal(info["hits_boundary"], False)
     assert_array_almost_equal(x, x_kkt)
コード例 #18
0
ファイル: test_projections.py プロジェクト: wanglun1996/scipy
    def test_iterative_refinements_dense(self):
        A = np.array([[1, 2, 3, 4, 0, 5, 0, 7], [0, 8, 7, 0, 1, 5, 9, 0],
                      [1, 0, 0, 0, 0, 1, 2, 3]])
        test_points = ([1, 2, 3, 4, 5, 6, 7,
                        8], [1, 10, 3, 0, 1, 6, 7,
                             8], [1, 0, 0, 0, 0, 1, 2, 3 + 1e-10])

        for method in available_dense_methods:
            Z, LS, _ = projections(A, method, orth_tol=1e-18, max_refin=10)
            for z in test_points:
                # Test if x is in the null_space
                x = Z.matvec(z)
                assert_array_almost_equal(A.dot(x), 0, decimal=14)
                # Test orthogonality
                assert_array_almost_equal(orthogonality(A, x), 0, decimal=16)
コード例 #19
0
ファイル: test_projections.py プロジェクト: wanglun1996/scipy
    def test_rowspace_dense(self):
        A = np.array([[1, 2, 3, 4, 0, 5, 0, 7], [0, 8, 7, 0, 1, 5, 9, 0],
                      [1, 0, 0, 0, 0, 1, 2, 3]])
        test_points = ([1, 2, 3], [1, 10, 3], [1.12, 10, 0])

        for method in available_dense_methods:
            _, _, Y = projections(A, method)
            for z in test_points:
                # Test if x is solution of A x = z
                x = Y.matvec(z)
                assert_array_almost_equal(A.dot(x), z)
                # Test if x is in the return row space of A
                A_ext = np.vstack((A, x))
                assert_equal(np.linalg.matrix_rank(A),
                             np.linalg.matrix_rank(A_ext))
コード例 #20
0
ファイル: test_projections.py プロジェクト: BranYang/scipy
    def test_iterative_refinements_dense(self):
        A = np.array([[1, 2, 3, 4, 0, 5, 0, 7],
                            [0, 8, 7, 0, 1, 5, 9, 0],
                            [1, 0, 0, 0, 0, 1, 2, 3]])
        test_points = ([1, 2, 3, 4, 5, 6, 7, 8],
                       [1, 10, 3, 0, 1, 6, 7, 8],
                       [1, 0, 0, 0, 0, 1, 2, 3+1e-10])

        for method in available_dense_methods:
            Z, LS, _ = projections(A, method, orth_tol=1e-18, max_refin=10)
            for z in test_points:
                # Test if x is in the null_space
                x = Z.matvec(z)
                assert_array_almost_equal(A.dot(x), 0, decimal=14)
                # Test orthogonality
                assert_array_almost_equal(orthogonality(A, x), 0, decimal=16)
コード例 #21
0
    def test_cauchypoint_equalsto_newtonpoint(self):
        A = np.array([[1, 8]])
        b = np.array([-16])
        _, _, Y = projections(A)
        newton_point = np.array([0.24615385, 1.96923077])

        # Newton point inside boundaries
        x = modified_dogleg(A, Y, b, 2, [-np.inf, -np.inf], [np.inf, np.inf])
        assert_array_almost_equal(x, newton_point)

        # Spherical constraint active
        x = modified_dogleg(A, Y, b, 1, [-np.inf, -np.inf], [np.inf, np.inf])
        assert_array_almost_equal(x, newton_point/np.linalg.norm(newton_point))

        # Box Constraints active
        x = modified_dogleg(A, Y, b, 2, [-np.inf, -np.inf], [0.1, np.inf])
        assert_array_almost_equal(x, (newton_point/newton_point[0]) * 0.1)
コード例 #22
0
ファイル: test_qp_subproblem.py プロジェクト: BranYang/scipy
 def test_negative_curvature(self):
     H = csc_matrix([[1, 2, 1, 3],
                     [2, 0, 2, 4],
                     [1, 2, 0, 2],
                     [3, 4, 2, 0]])
     A = csc_matrix([[1, 0, 1, 0],
                     [0, 1, 0, 1]])
     c = np.array([-2, -3, -3, 1])
     b = -np.array([3, 0])
     Z, _, Y = projections(A)
     trust_radius = 1000
     x, info = projected_cg(H, c, Z, Y, b,
                            tol=0,
                            trust_radius=trust_radius)
     assert_equal(info["stop_cond"], 3)
     assert_equal(info["hits_boundary"], True)
     assert_array_almost_equal(np.linalg.norm(x), trust_radius)
コード例 #23
0
ファイル: test_qp_subproblem.py プロジェクト: BranYang/scipy
 def test_hits_boundary(self):
     H = csc_matrix([[6, 2, 1, 3],
                     [2, 5, 2, 4],
                     [1, 2, 4, 5],
                     [3, 4, 5, 7]])
     A = csc_matrix([[1, 0, 1, 0],
                     [0, 1, 1, 1]])
     c = np.array([-2, -3, -3, 1])
     b = -np.array([3, 0])
     trust_radius = 3
     Z, _, Y = projections(A)
     x, info = projected_cg(H, c, Z, Y, b,
                            tol=0,
                            trust_radius=trust_radius)
     assert_equal(info["stop_cond"], 2)
     assert_equal(info["hits_boundary"], True)
     assert_array_almost_equal(np.linalg.norm(x), trust_radius)
コード例 #24
0
ファイル: test_qp_subproblem.py プロジェクト: BranYang/scipy
    def test_cauchypoint_equalsto_newtonpoint(self):
        A = np.array([[1, 8]])
        b = np.array([-16])
        _, _, Y = projections(A)
        newton_point = np.array([0.24615385, 1.96923077])

        # Newton point inside boundaries
        x = modified_dogleg(A, Y, b, 2, [-np.inf, -np.inf], [np.inf, np.inf])
        assert_array_almost_equal(x, newton_point)

        # Spherical constraint active
        x = modified_dogleg(A, Y, b, 1, [-np.inf, -np.inf], [np.inf, np.inf])
        assert_array_almost_equal(x, newton_point/np.linalg.norm(newton_point))

        # Box Constraints active
        x = modified_dogleg(A, Y, b, 2, [-np.inf, -np.inf], [0.1, np.inf])
        assert_array_almost_equal(x, (newton_point/newton_point[0]) * 0.1)
コード例 #25
0
ファイル: test_projections.py プロジェクト: BranYang/scipy
    def test_rowspace_dense(self):
        A = np.array([[1, 2, 3, 4, 0, 5, 0, 7],
                      [0, 8, 7, 0, 1, 5, 9, 0],
                      [1, 0, 0, 0, 0, 1, 2, 3]])
        test_points = ([1, 2, 3],
                       [1, 10, 3],
                       [1.12, 10, 0])

        for method in available_dense_methods:
            _, _, Y = projections(A, method)
            for z in test_points:
                # Test if x is solution of A x = z
                x = Y.matvec(z)
                assert_array_almost_equal(A.dot(x), z)
                # Test if x is in the return row space of A
                A_ext = np.vstack((A, x))
                assert_equal(np.linalg.matrix_rank(A),
                             np.linalg.matrix_rank(A_ext))
コード例 #26
0
ファイル: test_qp_subproblem.py プロジェクト: BranYang/scipy
 def test_trust_region_barely_feasible(self):
     H = csc_matrix([[6, 2, 1, 3],
                     [2, 5, 2, 4],
                     [1, 2, 4, 5],
                     [3, 4, 5, 7]])
     A = csc_matrix([[1, 0, 1, 0],
                     [0, 1, 1, 1]])
     c = np.array([-2, -3, -3, 1])
     b = -np.array([3, 0])
     trust_radius = 2.32379000772445021283
     Z, _, Y = projections(A)
     x, info = projected_cg(H, c, Z, Y, b,
                            tol=0,
                            trust_radius=trust_radius)
     assert_equal(info["stop_cond"], 2)
     assert_equal(info["hits_boundary"], True)
     assert_array_almost_equal(np.linalg.norm(x), trust_radius)
     assert_array_almost_equal(x, -Y.dot(b))
コード例 #27
0
ファイル: test_projections.py プロジェクト: wanglun1996/scipy
    def test_iterative_refinements_sparse(self):
        A_dense = np.array([[1, 2, 3, 4, 0, 5, 0, 7], [0, 8, 7, 0, 1, 5, 9, 0],
                            [1, 0, 0, 0, 0, 1, 2, 3]])
        A = csc_matrix(A_dense)
        test_points = ([1, 2, 3, 4, 5, 6, 7,
                        8], [1, 10, 3, 0, 1, 6, 7,
                             8], [1.12, 10, 0, 0, 100000, 6, 0.7,
                                  8], [1, 0, 0, 0, 0, 1, 2, 3 + 1e-10])

        for method in available_sparse_methods:
            Z, LS, _ = projections(A, method, orth_tol=1e-18, max_refin=100)
            for z in test_points:
                # Test if x is in the null_space
                x = Z.matvec(z)
                atol = 1e-13 * abs(x).max()
                assert_allclose(A.dot(x), 0, atol=atol)
                # Test orthogonality
                assert_allclose(orthogonality(A, x), 0, atol=1e-13)
コード例 #28
0
 def test_active_box_constraints_hits_boundaries_infeasible_iter(self):
     H = csc_matrix([[6, 2, 1, 3],
                     [2, 5, 2, 4],
                     [1, 2, 4, 5],
                     [3, 4, 5, 7]])
     A = csc_matrix([[1, 0, 1, 0],
                     [0, 1, 1, 1]])
     c = np.array([-2, -3, -3, 1])
     b = -np.array([3, 0])
     trust_radius = 4
     Z, _, Y = projections(A)
     x, info = projected_cg(H, c, Z, Y, b,
                            tol=0,
                            ub=[np.inf, 0.1, np.inf, np.inf],
                            trust_radius=trust_radius,
                            return_all=True)
     assert_equal(info["stop_cond"], 2)
     assert_equal(info["hits_boundary"], True)
     assert_array_almost_equal(x[1], 0.1)
コード例 #29
0
 def test_active_box_constraints_maximum_iterations_reached(self):
     H = csc_matrix([[6, 2, 1, 3],
                     [2, 5, 2, 4],
                     [1, 2, 4, 5],
                     [3, 4, 5, 7]])
     A = csc_matrix([[1, 0, 1, 0],
                     [0, 1, 1, 1]])
     c = np.array([-2, -3, -3, 1])
     b = -np.array([3, 0])
     Z, _, Y = projections(A)
     x, info = projected_cg(H, c, Z, Y, b,
                            tol=0,
                            lb=[0.8, -np.inf,
                                -np.inf, -np.inf],
                            return_all=True)
     assert_equal(info["stop_cond"], 1)
     assert_equal(info["hits_boundary"], True)
     assert_array_almost_equal(A.dot(x), -b)
     assert_array_almost_equal(x[0], 0.8)
コード例 #30
0
 def test_inactive_box_constraints(self):
     H = csc_matrix([[6, 2, 1, 3],
                     [2, 5, 2, 4],
                     [1, 2, 4, 5],
                     [3, 4, 5, 7]])
     A = csc_matrix([[1, 0, 1, 0],
                     [0, 1, 1, 1]])
     c = np.array([-2, -3, -3, 1])
     b = -np.array([3, 0])
     Z, _, Y = projections(A)
     x, info = projected_cg(H, c, Z, Y, b,
                            tol=0,
                            lb=[0.5, -np.inf,
                                -np.inf, -np.inf],
                            return_all=True)
     x_kkt, _ = eqp_kktfact(H, c, A, b)
     assert_equal(info["stop_cond"], 1)
     assert_equal(info["hits_boundary"], False)
     assert_array_almost_equal(x, x_kkt)
コード例 #31
0
ファイル: test_qp_subproblem.py プロジェクト: BranYang/scipy
 def test_inactive_box_constraints(self):
     H = csc_matrix([[6, 2, 1, 3],
                     [2, 5, 2, 4],
                     [1, 2, 4, 5],
                     [3, 4, 5, 7]])
     A = csc_matrix([[1, 0, 1, 0],
                     [0, 1, 1, 1]])
     c = np.array([-2, -3, -3, 1])
     b = -np.array([3, 0])
     Z, _, Y = projections(A)
     x, info = projected_cg(H, c, Z, Y, b,
                            tol=0,
                            lb=[0.5, -np.inf,
                                -np.inf, -np.inf],
                            return_all=True)
     x_kkt, _ = eqp_kktfact(H, c, A, b)
     assert_equal(info["stop_cond"], 1)
     assert_equal(info["hits_boundary"], False)
     assert_array_almost_equal(x, x_kkt)
コード例 #32
0
ファイル: test_qp_subproblem.py プロジェクト: BranYang/scipy
 def test_active_box_constraints_maximum_iterations_reached(self):
     H = csc_matrix([[6, 2, 1, 3],
                     [2, 5, 2, 4],
                     [1, 2, 4, 5],
                     [3, 4, 5, 7]])
     A = csc_matrix([[1, 0, 1, 0],
                     [0, 1, 1, 1]])
     c = np.array([-2, -3, -3, 1])
     b = -np.array([3, 0])
     Z, _, Y = projections(A)
     x, info = projected_cg(H, c, Z, Y, b,
                            tol=0,
                            lb=[0.8, -np.inf,
                                -np.inf, -np.inf],
                            return_all=True)
     assert_equal(info["stop_cond"], 1)
     assert_equal(info["hits_boundary"], True)
     assert_array_almost_equal(A.dot(x), -b)
     assert_array_almost_equal(x[0], 0.8)
コード例 #33
0
ファイル: test_qp_subproblem.py プロジェクト: BranYang/scipy
 def test_active_box_constraints_hits_boundaries_infeasible_iter(self):
     H = csc_matrix([[6, 2, 1, 3],
                     [2, 5, 2, 4],
                     [1, 2, 4, 5],
                     [3, 4, 5, 7]])
     A = csc_matrix([[1, 0, 1, 0],
                     [0, 1, 1, 1]])
     c = np.array([-2, -3, -3, 1])
     b = -np.array([3, 0])
     trust_radius = 4
     Z, _, Y = projections(A)
     x, info = projected_cg(H, c, Z, Y, b,
                            tol=0,
                            ub=[np.inf, 0.1, np.inf, np.inf],
                            trust_radius=trust_radius,
                            return_all=True)
     assert_equal(info["stop_cond"], 2)
     assert_equal(info["hits_boundary"], True)
     assert_array_almost_equal(x[1], 0.1)
コード例 #34
0
ファイル: test_projections.py プロジェクト: BranYang/scipy
    def test_iterative_refinements_sparse(self):
        A_dense = np.array([[1, 2, 3, 4, 0, 5, 0, 7],
                            [0, 8, 7, 0, 1, 5, 9, 0],
                            [1, 0, 0, 0, 0, 1, 2, 3]])
        A = csc_matrix(A_dense)
        test_points = ([1, 2, 3, 4, 5, 6, 7, 8],
                       [1, 10, 3, 0, 1, 6, 7, 8],
                       [1.12, 10, 0, 0, 100000, 6, 0.7, 8],
                       [1, 0, 0, 0, 0, 1, 2, 3+1e-10])

        for method in available_sparse_methods:
            Z, LS, _ = projections(A, method, orth_tol=1e-18, max_refin=100)
            for z in test_points:
                # Test if x is in the null_space
                x = Z.matvec(z)
                atol = 1e-13 * abs(x).max()
                err = abs(A.dot(x)).max()
                assert_allclose(A.dot(x), 0, atol=atol)
                # Test orthogonality
                assert_allclose(orthogonality(A, x), 0, atol=1e-13)
コード例 #35
0
ファイル: test_projections.py プロジェクト: wanglun1996/scipy
    def test_nullspace_and_least_squares_dense(self):
        A = np.array([[1, 2, 3, 4, 0, 5, 0, 7], [0, 8, 7, 0, 1, 5, 9, 0],
                      [1, 0, 0, 0, 0, 1, 2, 3]])
        At = A.T
        test_points = ([1, 2, 3, 4, 5, 6, 7,
                        8], [1, 10, 3, 0, 1, 6, 7,
                             8], [1.12, 10, 0, 0, 100000, 6, 0.7, 8])

        for method in available_dense_methods:
            Z, LS, _ = projections(A, method)
            for z in test_points:
                # Test if x is in the null_space
                x = Z.matvec(z)
                assert_array_almost_equal(A.dot(x), 0)
                # Test orthogonality
                assert_array_almost_equal(orthogonality(A, x), 0)
                # Test if x is the least square solution
                x = LS.matvec(z)
                x2 = scipy.linalg.lstsq(At, z)[0]
                assert_array_almost_equal(x, x2)
コード例 #36
0
ファイル: test_projections.py プロジェクト: BranYang/scipy
    def test_nullspace_and_least_squares_dense(self):
        A = np.array([[1, 2, 3, 4, 0, 5, 0, 7],
                      [0, 8, 7, 0, 1, 5, 9, 0],
                      [1, 0, 0, 0, 0, 1, 2, 3]])
        At = A.T
        test_points = ([1, 2, 3, 4, 5, 6, 7, 8],
                       [1, 10, 3, 0, 1, 6, 7, 8],
                       [1.12, 10, 0, 0, 100000, 6, 0.7, 8])

        for method in available_dense_methods:
            Z, LS, _ = projections(A, method)
            for z in test_points:
                # Test if x is in the null_space
                x = Z.matvec(z)
                assert_array_almost_equal(A.dot(x), 0)
                # Test orthogonality
                assert_array_almost_equal(orthogonality(A, x), 0)
                # Test if x is the least square solution
                x = LS.matvec(z)
                x2 = scipy.linalg.lstsq(At, z)[0]
                assert_array_almost_equal(x, x2)