-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merge LPGD into diffcp #67
base: master
Are you sure you want to change the base?
Changes from 14 commits
20fd10c
26727e1
8b0f8ac
6e761ce
e4f8e82
1fa3d05
133d30d
761b8ba
ca0eb64
c73b116
476faf2
ca78344
3d1235c
ca1be3f
1f4e4bf
d2aada1
dcfa6ba
8a9069c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,9 +31,9 @@ def time_function(f, N=1): | |
for n_jobs in range(1, 8): | ||
def f_forward(): | ||
return diffcp.solve_and_derivative_batch(As, bs, cs, Ks, | ||
n_jobs_forward=n_jobs, n_jobs_backward=n_jobs, solver="ECOS", verbose=False) | ||
n_jobs_forward=n_jobs, n_jobs_backward=n_jobs, solve_method="ECOS", verbose=False) | ||
xs, ys, ss, D_batch, DT_batch = diffcp.solve_and_derivative_batch(As, bs, cs, Ks, | ||
n_jobs_forward=1, n_jobs_backward=n_jobs, solver="ECOS", verbose=False) | ||
n_jobs_forward=1, n_jobs_backward=n_jobs, solve_method="ECOS", verbose=False) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ;) |
||
|
||
def f_backward(): | ||
DT_batch(xs, ys, ss, mode="lsqr") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import diffcp | ||
import utils | ||
import IPython as ipy | ||
import time | ||
import numpy as np | ||
|
||
m = 100 | ||
n = 50 | ||
|
||
batch_size = 16 | ||
n_jobs = 1 | ||
|
||
As, bs, cs, Ks = [], [], [], [] | ||
for _ in range(batch_size): | ||
A, b, c, K = diffcp.utils.least_squares_eq_scs_data(m, n) | ||
As += [A] | ||
bs += [b] | ||
cs += [c] | ||
Ks += [K] | ||
|
||
|
||
def time_function(f, N=1): | ||
result = [] | ||
for i in range(N): | ||
tic = time.time() | ||
f() | ||
toc = time.time() | ||
result += [toc - tic] | ||
return np.mean(result), np.std(result) | ||
|
||
for n_jobs in range(1, 8): | ||
def f_forward(): | ||
return diffcp.solve_and_derivative_batch(As, bs, cs, Ks, | ||
n_jobs_forward=n_jobs, n_jobs_backward=n_jobs, solve_method="ECOS", verbose=False, | ||
mode="lpgd", derivative_kwargs=dict(tau=1e-3, rho=0.0)) | ||
xs, ys, ss, D_batch, DT_batch = diffcp.solve_and_derivative_batch(As, bs, cs, Ks, | ||
n_jobs_forward=1, n_jobs_backward=n_jobs, solve_method="ECOS", verbose=False, | ||
mode="lpgd", derivative_kwargs=dict(tau=1e-3, rho=0.0)) | ||
|
||
def f_backward(): | ||
DT_batch(xs, ys, ss) | ||
|
||
mean_forward, std_forward = time_function(f_forward) | ||
mean_backward, std_backward = time_function(f_backward) | ||
print("%03d | %4.4f +/- %2.2f | %4.4f +/- %2.2f" % | ||
(n_jobs, mean_forward, std_forward, mean_backward, std_backward)) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import diffcp | ||
import utils | ||
import IPython as ipy | ||
import time | ||
import numpy as np | ||
|
||
m = 100 | ||
n = 50 | ||
|
||
batch_size = 16 | ||
n_jobs = 1 | ||
|
||
As, bs, cs, Ks = [], [], [], [] | ||
for _ in range(batch_size): | ||
A, b, c, K = diffcp.utils.least_squares_eq_scs_data(m, n) | ||
As += [A] | ||
bs += [b] | ||
cs += [c] | ||
Ks += [K] | ||
|
||
def time_function(f, N=1): | ||
result = [] | ||
for i in range(N): | ||
tic = time.time() | ||
f() | ||
toc = time.time() | ||
result += [toc-tic] | ||
return np.mean(result), np.std(result) | ||
|
||
for n_jobs in range(1, 5): | ||
def f_forward(): | ||
return diffcp.solve_and_derivative_batch(As, bs, cs, Ks, | ||
n_jobs_forward=n_jobs, n_jobs_backward=n_jobs) | ||
xs, ys, ss, D_batch, DT_batch = diffcp.solve_and_derivative_batch(As, bs, cs, Ks, | ||
n_jobs_forward=1, n_jobs_backward=n_jobs, mode='lpgd_left') | ||
def f_backward(): | ||
DT_batch(xs, ys, ss, tau=0.1, rho=0.1) | ||
|
||
mean_forward, std_forward = time_function(f_forward) | ||
mean_backward, std_backward = time_function(f_backward) | ||
print ("%03d | %4.4f +/- %2.2f | %4.4f +/- %2.2f" % | ||
(n_jobs, mean_forward, std_forward, mean_backward, std_backward)) |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -9,7 +9,7 @@ | |||||
# defined as a product of a 3-d fixed cone, 3-d positive orthant cone, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
# and a 5-d second order cone. | ||||||
K = { | ||||||
'f': 3, | ||||||
'z': 3, | ||||||
'l': 3, | ||||||
'q': [5] | ||||||
} | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import diffcp | ||
|
||
import numpy as np | ||
import utils | ||
np.set_printoptions(precision=5, suppress=True) | ||
|
||
|
||
# We generate a random cone program with a cone | ||
# defined as a product of a 3-d fixed cone, 3-d positive orthant cone, | ||
# and a 5-d second order cone. | ||
K = { | ||
'z': 3, | ||
'l': 3, | ||
'q': [5] | ||
} | ||
|
||
m = 3 + 3 + 5 | ||
n = 5 | ||
|
||
np.random.seed(0) | ||
|
||
A, b, c = utils.random_cone_prog(m, n, K) | ||
|
||
# We solve the cone program and get the derivative and its adjoint | ||
x, y, s, derivative, adjoint_derivative = diffcp.solve_and_derivative( | ||
A, b, c, K, eps=1e-10, mode="lpgd", derivative_kwargs=dict(tau=1e-3, rho=0.1)) | ||
|
||
print("x =", x) | ||
print("y =", y) | ||
print("s =", s) | ||
|
||
# We evaluate the gradient of the objective with respect to A, b and c. | ||
dA, db, dc = adjoint_derivative(c, np.zeros(m), np.zeros(m)) | ||
|
||
# The gradient of the objective with respect to b should be | ||
# equal to minus the dual variable y (see, e.g., page 268 of Convex Optimization by | ||
# Boyd & Vandenberghe). | ||
print("db =", db) | ||
print("-y =", -y) |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -9,7 +9,7 @@ | |||||
# defined as a product of a 3-d fixed cone, 3-d positive orthant cone, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
# and a 5-d second order cone. | ||||||
K = { | ||||||
'f': 3, | ||||||
'z': 3, | ||||||
'l': 3, | ||||||
'q': [5] | ||||||
} | ||||||
|
@@ -23,7 +23,7 @@ | |||||
|
||||||
# We solve the cone program and get the derivative and its adjoint | ||||||
x, y, s, derivative, adjoint_derivative = diffcp.solve_and_derivative( | ||||||
A, b, c, K, solver="ECOS", verbose=False) | ||||||
A, b, c, K, solve_method="ECOS", verbose=False) | ||||||
|
||||||
print("x =", x) | ||||||
print("y =", y) | ||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,39 @@ | ||||||
import diffcp | ||||||
|
||||||
import numpy as np | ||||||
import utils | ||||||
np.set_printoptions(precision=5, suppress=True) | ||||||
|
||||||
|
||||||
# We generate a random cone program with a cone | ||||||
# defined as a product of a 3-d fixed cone, 3-d positive orthant cone, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
# and a 5-d second order cone. | ||||||
K = { | ||||||
'z': 3, | ||||||
'l': 3, | ||||||
'q': [5] | ||||||
} | ||||||
|
||||||
m = 3 + 3 + 5 | ||||||
n = 5 | ||||||
|
||||||
np.random.seed(0) | ||||||
|
||||||
A, b, c = utils.random_cone_prog(m, n, K) | ||||||
|
||||||
# We solve the cone program and get the derivative and its adjoint | ||||||
x, y, s, derivative, adjoint_derivative = diffcp.solve_and_derivative( | ||||||
A, b, c, K, solve_method="ECOS", verbose=False, mode="lpgd", derivative_kwargs=dict(tau=0.1, rho=0.0)) | ||||||
|
||||||
print("x =", x) | ||||||
print("y =", y) | ||||||
print("s =", s) | ||||||
|
||||||
# We evaluate the gradient of the objective with respect to A, b and c. | ||||||
dA, db, dc = adjoint_derivative(c, np.zeros(m), np.zeros(m)) | ||||||
|
||||||
# The gradient of the objective with respect to b should be | ||||||
# equal to minus the dual variable y (see, e.g., page 268 of Convex Optimization by | ||||||
# Boyd & Vandenberghe). | ||||||
print("db =", db) | ||||||
print("-y =", -y) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does Clarabel use a warmstart?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does not seem like they support it, currently in diffcp the warm_start is also not passed to the Clarabel solver so it definitely is not used at the moment. Probably better to make this explicit, I will add a check to throw an error when trying to use warmstarteing with Clarabel.