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 main(): n = 2 # param a = 1 b = 100 niter = 300 x_opt = np.array([a, a**2]) x0 = np.zeros(n) obj_fun = Rosenbrock(a, b) first_summand = RosenbrockFirstSummand(a, b) second_summand = RosenbrockSecondSummand(a, b) # hessian at the solution hessian_at_solution = obj_fun.hessian(x_opt) alpha, _ = eigh(hessian_at_solution, eigvals=(0, 0)) P = np.ones((n, n))*1/n P_perp = np.eye(n) - P gamma = la.norm(la.multi_dot([P_perp, hessian_at_solution, P_perp]), 2) beta = 0.5*la.norm(la.multi_dot([P, hessian_at_solution + hessian_at_solution.T, P_perp]), 2) e_0 = beta**2/alpha + gamma e = np.round(e_0) r = e+100 def dist_to_solution(x): return np.linalg.norm(x - x_opt) callback = CallBack(obj_fun, dist_to_solution) x_opt = decomposable_decoupling([first_summand, second_summand], niter, e, r, n, x0=x0, callback=callback) # x_opt = decomposable_decoupling([obj_fun], 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'Rosenbrock Decoupling, 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'Rosenbrock Decoupling, r={r}, e={e}') plt.show()
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()