-
Notifications
You must be signed in to change notification settings - Fork 7
/
test.py
81 lines (59 loc) · 2.42 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from newton_admm import newton_admm, problems, PSD
import numpy as np
import cvxpy as cp
np.random.seed(0)
def _s(o1, o2):
return "Final objective values don't match, got {} but expected {}.".format(o1, o2)
def test_least_squares():
""" This test will construct second order cone constraints """
prob = problems.least_squares(10, 5)[1]
data = prob.get_problem_data(cp.SCS)
out = newton_admm(data, data['dims'])
cvx_out = prob.solve()
assert np.allclose(out['info']['fstar'], cvx_out), _s(
out['info']['fstar'], cvx_out)
def test_lp():
""" This test will construct equality, inequality, and second order cone constraints """
prob = problems.lp(30, 60)[1]
data = prob.get_problem_data(cp.SCS)
out = newton_admm(data, data['dims'])
cvx_out = prob.solve()
assert np.allclose(out['info']['fstar'], cvx_out), _s(
out['info']['fstar'], cvx_out)
def test_portfolio_opt():
""" This test will construct equality, inequality, and second order cone constraints """
prob = problems.portfolio_opt(10)[1]
data = prob.get_problem_data(cp.SCS)
out = newton_admm(data, data['dims'])
cvx_out = prob.solve()
assert np.allclose(out['info']['fstar'], cvx_out), _s(
out['info']['fstar'], cvx_out)
def test_logistic_regression():
""" This test will construct inequality, and exponential cone constraints """
prob = problems.logistic_regression(5, 2, 1.0)[1]
data = prob.get_problem_data(cp.SCS)
out = newton_admm(data, data['dims'])
cvx_out = prob.solve()
assert np.allclose(out['info']['fstar'], cvx_out), _s(
out['info']['fstar'], cvx_out)
def test_PSD():
from numdifftools import Jacobian
x = np.random.randn(6)
J = Jacobian(lambda x: PSD.P(x))
assert np.allclose(J(x), PSD.J(x))
def test_robust_pca():
""" This test will construct positive semi-definite cone constraints """
prob = problems.robust_pca(5, 0.5)[1]
data = prob.get_problem_data(cp.SCS)
# solve the problem with ADMM instead for better accuracy
out0 = newton_admm(data, data['dims'], admm_maxiters=4000, maxiters=4000)
out = newton_admm(data, data['dims'])
fstar, fstar0 = out['info']['fstar'], out0['info']['fstar']
assert np.allclose(fstar, fstar0), _s(fstar, fstar0)
if __name__ == '__main__':
test_least_squares()
test_lp()
test_portfolio_opt()
test_logistic_regression()
test_PSD()
test_robust_pca()