From 12ba3d89745b0b2a6a8f5dcb5e72565a54a653ea Mon Sep 17 00:00:00 2001 From: Thomas Schmelzer Date: Sat, 21 Oct 2023 06:29:39 +0400 Subject: [PATCH] comments in experiment --- experiments/talk/minVariance2.py | 33 ++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/experiments/talk/minVariance2.py b/experiments/talk/minVariance2.py index e7889f38..9db6d3f8 100644 --- a/experiments/talk/minVariance2.py +++ b/experiments/talk/minVariance2.py @@ -5,18 +5,17 @@ def f(): - L = np.linalg.cholesky(A) - + U = uu x = cp.Variable(n) - objective = cp.Minimize(cp.norm2(L.T @ x)) + objective = cp.Minimize(cp.norm2(U @ x)) constraints = [cp.sum(x) == 1, x >= 0] prob = cp.Problem(objective, constraints) prob.solve(solver) def g(): - L.value = np.linalg.cholesky(A) + U.value = uu prob.solve(solver) @@ -25,27 +24,45 @@ def g(): C = np.random.rand(n, n) A = C @ C.T + uu = np.transpose(np.linalg.cholesky(A)) + + # check all eigenvalue are positive result = np.linalg.eigh(A) assert np.all(result.eigenvalues > 0) for solver in [cp.MOSEK, cp.CLARABEL, cp.ECOS]: print(solver) + + # solve one problem execution_time = timeit.timeit(f, number=1) print(f"{execution_time:.6f} seconds") + # solve 10 problems, each time the problem is reconstructed + # Should take 10times longer than the previous one execution_time = timeit.timeit(f, number=10) print(f"{execution_time:.6f} seconds") + # construct the problem once with parameters x = cp.Variable(n) - L = cp.Parameter((n, n)) + # would be good if the parameter could be an upper triangular matrix + # rather than just a matrix + U = cp.Parameter((n, n)) - objective = cp.Minimize(cp.norm2(L.T @ x)) + # construct the problem + objective = cp.Minimize(cp.norm2(U @ x)) constraints = [cp.sum(x) == 1, x >= 0] - prob = cp.Problem(objective, constraints) + # could we know construct the problem + xxx = prob.get_problem_data(solver) + # what can we do with xxx? + + # solve the problem only once, should be faster + # than the previous one, but not for CLARABEL and ECOS execution_time = timeit.timeit(g, number=1) print(f"{execution_time:.6f} seconds") + # assert False - execution_time = timeit.timeit(g, number=100) + # solve the problem 10 times + execution_time = timeit.timeit(g, number=10) print(f"{execution_time:.6f} seconds")