Skip to content

Commit

Permalink
comments in experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Schmelzer committed Oct 21, 2023
1 parent e06ba32 commit 12ba3d8
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions experiments/talk/minVariance2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -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")

0 comments on commit 12ba3d8

Please sign in to comment.