def test_matrix_split(): n = 100 nA = 4 # nC = 60 M = rd.rand(n, n) M = np.dot(M, M.transpose()) M_obj = util.Quadratic(M) A = M.copy() A[nA:n, :] = 0 A[:, nA:n] = 0 A = util.Quadratic(A) C = M.copy() C[0:nA, :] = 0 C[:, 0:nA] = 0 C = util.Quadratic(C) B = M.copy() B[0:nA, 0:nA] = 0 B[nA:n, nA:n] = 0 B = util.Quadratic(M) # global minimizer x_opt = np.zeros(n) # params e = 1 r = 2 niter = 10 x0 = np.ones(n) x_opt = dc.decomposable_decoupling([A, C, B], niter, e, r, n, x0=x0) # should actually be zero assert np.max(x_opt) <= 1 # should be zero assert M_obj(x_opt) <= 1
def main(): """TODO: solve min_x <x,Mx> by splitting M into A, and C M = (A 0) (0 C) """ n = 100 # construct SPD matrix A = ut.random_psd(n) C = ut.random_psd(n) M = A + C # construct projections P = ut.projection_onto_linkage_space(n, 2) # construct objective function in product space M_large = ut.Quadratic(block_diag(A.matrix, C.matrix)) e_0 = ut.compute_minimal_elicitation(M, M_large, P) pdb.set_trace() # global minimizer x_opt = np.zeros(n) # params e = np.round(e_0) r = e + 100 r = np.sqrt((e / 2)**2 + 1) + e / 2 # e = 0.01 # r = 0.02 niter = 10 x0 = np.ones(n) def dist_to_solution(x): return la.norm(x - x_opt) callback = ut.CallBack(M, dist_to_solution) x_opt = dc.decomposable_decoupling([A, C], niter, e, r, n, x0=x0, callback=callback) # make plots plt.plot(np.arange(niter), callback.stored_obj_fun_values) plt.xlabel('iterations') plt.ylabel('function value') plt.title(f'Decoupling, Matrix, convex, r={r}, e={e}') plt.show() plt.plot(np.arange(niter), callback.stored_dist_to_solution) plt.xlabel('iterations') plt.ylabel('distance to solution') plt.title(f'Decoupling, Matrix, convex, r={r}, e={e}') plt.show()
def test_prox2(): a11 = 2. a22 = 3. A = util.Quadratic(np.array([[a11, 0.], [0., a22]])) assert all(A.prox(np.zeros(2), 1.) == np.zeros(2)) w = np.array([2., 3.]) r = 1. prox = A.prox(w, r) should_be = w.copy() should_be[0] = r / (a11 + r) * should_be[0] should_be[1] = r / (a22 + r) * should_be[1] npt.assert_almost_equal(prox, should_be)
def test_class_matrix(): A = util.Quadratic(np.array([[3, 2], [2, 1]])) x, fun_vals = dc.decomposable_decoupling([A], 10, 0, 1, 2, x0=np.ones(2))
def main(): """TODO: solve min_x <x,Mx> by splitting M into A, C, and B M = (A B) (B^t C) """ n = 100 nA = 4 # construct PSD matrix M = ut.random_psd(n) # construct split A = M.matrix.copy() A[nA:n, :] = 0 A[:, nA:n] = 0 A = ut.Quadratic(A) C = M.matrix.copy() C[0:nA, :] = 0 C[:, 0:nA] = 0 C = ut.Quadratic(C) B = M.matrix.copy() B[0:nA, 0:nA] = 0 B[nA:n, nA:n] = 0 B = ut.Quadratic(B) # construct projections P = ut.projection_onto_linkage_space(n, 3) # construct objective function in product space M_large = np.zeros((3 * n, 3 * n)) M_large[:n, :n] = A.matrix M_large[n:2 * n, n:2 * n] = B.matrix M_large[2 * n:, 2 * n:] = C.matrix M_large = block_diag(A.matrix, B.matrix, C.matrix) M_large = ut.Quadratic(M_large) e_0 = ut.compute_minimal_elicitation(M, M_large, P) pdb.set_trace() # global minimizer x_opt = np.zeros(n) # params e = np.round(e_0) r = e + 100 # r = np.sqrt((e/2)**2+1) + e/2 niter = 100 x0 = np.ones(n) def dist_to_solution(x): return la.norm(x - x_opt) callback = ut.CallBack(M, dist_to_solution) x_opt = dc.decomposable_decoupling([A, C, B], niter, e, r, n, x0=x0, callback=callback) # make plots plt.plot(np.arange(niter), callback.stored_obj_fun_values) plt.xlabel('iterations') plt.ylabel('function value') plt.title(f'Decoupling, Matrix, indefinite, r={r}, e={e}') plt.show() plt.plot(np.arange(niter), callback.stored_dist_to_solution) plt.xlabel('iterations') plt.ylabel('distance to solution') plt.title(f'Decoupling, Matrix, indefinite, r={r}, e={e}') plt.show()