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))
def test_feasible_problem_with_box_constr(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 lb = np.array([-1, -0.7, -1, -1.5]) ub = np.array([1.9, np.inf, np.inf, np.inf]) Z, _, Y = projections(A) x_n, x_t, info = qp_subproblem(H, c, A, Z, Y, b, trust_radius, lb, ub, tr_factor=1, box_factor=1) x_pcg, info_pcg = projected_cg(H, c, Z, Y, b, trust_radius, lb, ub) x = x_n + x_t r = A.dot(x_n) + b assert_array_almost_equal(r, np.zeros(2)) assert_array_almost_equal(x, x_pcg) assert_equal(info["hits_boundary"], info_pcg["hits_boundary"]) assert_equal(info, info_pcg) assert_array_almost_equal(A.dot(x)+b, r) assert_(np.linalg.norm(x) <= trust_radius) assert_(inside_box_boundaries(x, lb, ub))
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, )) print(np.max(Z.dot(z) - Z_sparse.dot(z))) print(np.max(LS.dot(z) - LS_sparse.dot(z))) x = np.random.normal(size=(3, )) print(np.max(Y.dot(x) - Y_sparse.dot(x)))
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)
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) assert_raises(ValueError, projected_cg, H, c, Z, Y, b, tol=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) assert_raises(ValueError, projected_cg, H, c, Z, Y, b, trust_radius=trust_radius)
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])
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)
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 ("QRFactorization", ): 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)
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 ('QRFactorization', ): _, _, 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))
def test_rowspace_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]]) At_dense = A_dense.T A = csc_matrix(A_dense) test_points = ([1, 2, 3], [1, 10, 3], [1.12, 10, 0]) for method in ('NormalEquation', 'AugmentedSystem'): _, _, 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_dense, x)) assert_equal(np.linalg.matrix_rank(A_dense), np.linalg.matrix_rank(A_ext))
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)
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)
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)
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]]) At_dense = A_dense.T 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 ("NormalEquation", "AugmentedSystem"): 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) assert_array_almost_equal(A.dot(x), 0, decimal=14) # Test orthogonality assert_array_almost_equal(orthogonality(A, x), 0, decimal=16)
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))
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)
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)
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)
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 ("QRFactorization", ): 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 = np.linalg.lstsq(At, z)[0] assert_array_almost_equal(x, x2)
def test_nullspace_and_least_squares_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]]) At_dense = A_dense.T 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]) for method in ("NormalEquation", "AugmentedSystem"): 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 = np.linalg.lstsq(At_dense, z)[0] assert_array_almost_equal(x, x2)
def test_infeasible_problem_with_box_constr(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 lb = np.array([-1, -0.7, -1, -1.5]) ub = np.array([0.7, np.inf, np.inf, np.inf]) Z, _, Y = projections(A) x_n, x_t, info = qp_subproblem(H, c, A, Z, Y, b, trust_radius, lb, ub) x = x_n + x_t r = A.dot(x_n) + b assert_array_less(0, np.linalg.norm(r)) assert_array_almost_equal(A.dot(x)+b, r) assert_(np.linalg.norm(x) <= trust_radius) assert_(inside_box_boundaries(x, lb, ub))
def test_infeasible_problem(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 Z, _, Y = projections(A) x_n, x_t, info = qp_subproblem(H, c, A, Z, Y, b, trust_radius, tr_factor=1, box_factor=1) x = x_n + x_t r = A.dot(x_n) + b assert_raises(ValueError, projected_cg, H, c, Z, Y, b, trust_radius) assert_array_less(0, np.linalg.norm(r)) assert_array_almost_equal(A.dot(x)+b, r) assert_(np.linalg.norm(x) <= trust_radius)
def test_feasible_problem(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_n, x_t, info = qp_subproblem(H, c, A, Z, Y, b, trust_radius, tr_factor=1) x_pcg, info_pcg = projected_cg(H, c, Z, Y, b, trust_radius) x = x_n + x_t r = A.dot(x_n) + b assert_array_almost_equal(r, np.zeros(2)) assert_array_almost_equal(x, x_pcg) assert_equal(info["hits_boundary"], info_pcg["hits_boundary"]) assert_equal(info, info_pcg) assert_array_almost_equal(np.linalg.norm(x) - trust_radius, 0) assert_array_almost_equal(A.dot(x)+b, r)