Skip to content

Commit 30111c6

Browse files
committed
some test problems in sandbox
1 parent 5b42601 commit 30111c6

File tree

10 files changed

+284
-0
lines changed

10 files changed

+284
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import numpy as np
2+
import matplotlib.pyplot as plt
3+
import cvxpy as cp
4+
np.random.seed(0)
5+
6+
n, m, k = 5, 4, 2
7+
noise_level = 0.05
8+
X_true = np.random.rand(n, k)
9+
Y_true = np.random.rand(k, m)
10+
A_noisy = X_true @ Y_true + noise_level * np.random.randn(n, m)
11+
A_noisy = np.clip(A_noisy, 0, None)
12+
X = cp.Variable((n, k), nonneg=True)
13+
Y = cp.Variable((k, m), nonneg=True)
14+
obj = cp.sum_squares(A_noisy - X @ Y)
15+
problem = cp.Problem(cp.Minimize(obj))
16+
problem.solve(solver=cp.IPOPT, nlp=True, verbose=True)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import numpy as np
2+
import cvxpy as cp
3+
import pdb
4+
5+
n, m = 100, 25
6+
A = np.random.randn(m, n)
7+
b = np.random.randn(m)
8+
9+
x = cp.Variable(n)
10+
obj = cp.Minimize(cp.sum_squares(A @ x - b))
11+
constraints = [cp.square(x) == 4]
12+
problem = cp.Problem(obj, constraints)
13+
problem.solve(solver=cp.IPOPT, nlp=True, verbose=True)
14+
15+
pdb.set_trace()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import numpy as np
2+
import cvxpy as cp
3+
import pdb
4+
np.random.seed(0)
5+
6+
# Problem dimensions
7+
m, n = 50, 10
8+
x_true = 10 * (np.random.randn(n) + 1j*np.random.randn(n))
9+
A = np.random.randn(m, n) + 1j*np.random.randn(m, n)
10+
y = np.abs(A @ x_true)
11+
12+
x = cp.Variable(n, complex=True)
13+
x0 = np.random.rand(n) + 1j*np.random.rand(n)
14+
x.value = x0
15+
obj = cp.sum_squares(cp.abs(A @ x) - y)
16+
problem = cp.Problem(cp.Minimize(obj))
17+
problem.solve(solver=cp.IPOPT, nlp=True, verbose=True)
18+
19+
print("x0: ", x0)
20+
print("true x: ", x_true)
21+
print("recovered x: ", x.value)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import numpy as np
2+
import cvxpy as cp
3+
import pdb
4+
np.random.seed(0)
5+
6+
m, n = 300, 50
7+
x_true = np.random.randn(n)
8+
A = np.random.randn(m, n)
9+
y = np.abs(A @ x_true)
10+
x = cp.Variable(n)
11+
12+
t = cp.Variable(m)
13+
v = cp.Variable(m)
14+
15+
#obj = cp.norm1(cp.square(A @ x) - y ** 2)
16+
obj = cp.sum(t)
17+
constraints = [cp.square(t) == cp.square(v), v == cp.square(A @ x) - y ** 2, t >= 0]
18+
problem = cp.Problem(cp.Minimize(obj), constraints)
19+
20+
# With this initialization IPOPT terminates after one iteration
21+
x.value = 0 * np.ones(n)
22+
problem.solve(solver=cp.IPOPT, nlp=True, verbose=True)
23+
x_star_one = x.value
24+
25+
# With this initialization IPOPT converges to the x_true
26+
x.value = np.ones(n)
27+
problem.solve(solver=cp.IPOPT, nlp=True, verbose=True)
28+
x_star_two = x.value
29+
30+
print("true x: ", np.round(x_true, 2))
31+
print("recovered x with zero initialization: ", np.round(x_star_one, 2))
32+
print("recovered x with small initialization: ", np.round(x_star_two, 2))
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import numpy as np
2+
import cvxpy as cp
3+
import pdb
4+
np.random.seed(0)
5+
6+
# Problem dimensions
7+
m, n = 50, 10
8+
x_true = 10 * np.random.randn(n)
9+
A = np.random.randn(m, n)
10+
y = np.abs(A @ x_true) #+ np.random.randn(m)
11+
12+
x = cp.Variable(n)
13+
# if I initialize from 0 IPOPT terminates after one iteration...
14+
# this is extremely weird! must look into this
15+
x0 = 2 * x_true
16+
x.value = x0
17+
#obj = cp.sum_squares(cp.square(A @ x) - y ** 2)
18+
#obj = cp.sum_squares(A @ x - y ** 2)
19+
#pdb.set_trace()
20+
#obj = cp.sum_squares(cp.abs(A @ x) - y)
21+
t = cp.Variable(m)
22+
v = cp.Variable(m)
23+
#constraints = [t == cp.abs(v), v == A @ x]
24+
constraints = [cp.square(t) == cp.square(v), v == A @ x, t >= 0]
25+
obj = cp.sum_squares(t - y)
26+
problem = cp.Problem(cp.Minimize(obj), constraints)
27+
problem.solve(solver=cp.IPOPT, nlp=True, verbose=True)
28+
29+
print("x0: ", x0)
30+
print("true x: ", x_true)
31+
print("recovered x: ", x.value)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import numpy as np
2+
import cvxpy as cp
3+
import pdb
4+
np.random.seed(0)
5+
6+
m, n = 10, 5
7+
x_true = np.random.randn(n)
8+
A = np.random.randn(m, n)
9+
y = np.abs(A @ x_true)
10+
x = cp.Variable(n)
11+
12+
13+
obj = cp.sum_squares(cp.square(A @ x) - y ** 2)
14+
problem = cp.Problem(cp.Minimize(obj))
15+
16+
# With this initilaization IPOPT terminates after one iteration
17+
x.value = 0 * np.ones(n)
18+
problem.solve(solver=cp.IPOPT, nlp=True, verbose=True)
19+
x_star_one = x.value
20+
21+
# With this initialization IPOPT converges to the x_true
22+
x.value = 1e-8 * np.ones(n)
23+
problem.solve(solver=cp.IPOPT, nlp=True, verbose=True)
24+
x_star_two = x.value
25+
26+
print("true x: ", np.round(x_true, 2))
27+
print("recovered x with zero initialization: ", np.round(x_star_one, 2))
28+
print("recovered x with small initialization: ", np.round(x_star_two, 2))
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import numpy as np
2+
import pandas as pd
3+
import pdb
4+
5+
import cvxpy as cp
6+
7+
8+
x = cp.Variable(4, name='x')
9+
F = cp.hstack([x[0] + 10 * x[1],
10+
np.sqrt(5) * (x[2] - x[3]),
11+
cp.square(x[1] - 2 * x[2]),
12+
np.sqrt(10) * (x[0] - x[3])])
13+
14+
15+
# formulation 1 - works (95 iterations)
16+
objective = cp.sum_squares(F)
17+
problem = cp.Problem(cp.Minimize(objective))
18+
x.value = np.array([3, -1, 0, 1])
19+
problem.solve(solver=cp.IPOPT, nlp=True, verbose=True)
20+
21+
# formulation 2 - works (41 iterations)
22+
objective = cp.square(x[0] + 10 * x[1]) + \
23+
cp.square(np.sqrt(5) * (x[2] - x[3])) + \
24+
cp.power(x[1] - 2 * x[2], 4) + \
25+
cp.square(np.sqrt(10) * (x[0] - x[3]))
26+
27+
problem = cp.Problem(cp.Minimize(objective))
28+
x.value = np.array([3, -1, 0, 1])
29+
problem.solve(solver=cp.IPOPT, nlp=True, verbose=True)
30+
31+
32+
# formulation 3 - works (18 iterations)
33+
objective = cp.square(x[0] + 10 * x[1]) + \
34+
cp.square(np.sqrt(5) * (x[2] - x[3])) + \
35+
cp.square(cp.square(x[1] - 2 * x[2])) + \
36+
cp.square(np.sqrt(10) * (x[0] - x[3]))
37+
38+
problem = cp.Problem(cp.Minimize(objective))
39+
x.value = np.array([3, -1, 0, 1])
40+
problem.solve(solver=cp.IPOPT, nlp=True, verbose=True)
41+
42+
# formulation 4 - works (18 iterations)
43+
objective = cp.power(x[0] + 10 * x[1], 2) + \
44+
cp.power(np.sqrt(5) * (x[2] - x[3]), 2) + \
45+
cp.square(cp.square(x[1] - 2 * x[2])) + \
46+
cp.power(np.sqrt(10) * (x[0] - x[3]), 2)
47+
48+
problem = cp.Problem(cp.Minimize(objective))
49+
x.value = np.array([3, -1, 0, 1])
50+
problem.solve(solver=cp.IPOPT, nlp=True, verbose=True)
51+
52+
# formulation 5 - canonicalization fails
53+
#objective = cp.norm(F)
54+
#problem = cp.Problem(cp.Minimize(objective))
55+
#problem.solve(solver=cp.IPOPT, nlp=True, verbose=True)
56+
#pdb.set_trace()
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import numpy as np
2+
import pandas as pd
3+
import pdb
4+
5+
import cvxpy as cp
6+
7+
8+
x = cp.Variable(2, name='x')
9+
F = cp.hstack([1e4*x[0]*x[1] - 1,
10+
cp.exp(-x[0]) + cp.exp(-x[1]) - 1.001])
11+
12+
# formulation 1
13+
objective = cp.sum_squares(F)
14+
problem = cp.Problem(cp.Minimize(objective))
15+
x.value = np.array([0, 1])
16+
problem.solve(solver=cp.IPOPT, nlp=True, verbose=True)
17+
18+
# formulation 2 ()
19+
objective = cp.square(1e4*x[0]*x[1] - 1) + cp.square(cp.exp(-x[0]) + cp.exp(-x[1]) - 1.001)
20+
problem = cp.Problem(cp.Minimize(objective))
21+
x.value = np.array([0, 1])
22+
problem.solve(solver=cp.IPOPT, nlp=True, verbose=True)
23+
24+
pdb.set_trace()
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import numpy as np
2+
import cvxpy as cp
3+
import pdb
4+
5+
m = 100
6+
n = 5
7+
Ps = []
8+
Vs = []
9+
x_true = np.random.randn(n)
10+
for i in range(m):
11+
P = np.random.randn(n, n)
12+
P = P + P.T
13+
Ps.append(P)
14+
v = x_true.T @ P @ x_true
15+
Vs.append(v)
16+
17+
x = cp.Variable(n)
18+
obj = 0
19+
for i in range(m):
20+
obj += cp.square(cp.quad_form(x, Ps[i]) - Vs[i])
21+
22+
problem = cp.Problem(cp.Minimize(obj))
23+
24+
# With this initilaization IPOPT terminates after one iteration
25+
x.value = 0 * np.ones(n)
26+
problem.solve(solver=cp.IPOPT, nlp=True, verbose=True)
27+
x_star_one = x.value
28+
29+
# With this initialization IPOPT converges to the x_true
30+
x.value = 1e-8 * np.ones(n)
31+
problem.solve(solver=cp.IPOPT, nlp=True, verbose=True)
32+
x_star_two = x.value
33+
34+
print("true x: ", np.round(x_true, 2))
35+
print("recovered x with zero initialization: ", np.round(x_star_one, 2))
36+
print("recovered x with small initialization: ", np.round(x_star_two, 2))
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import numpy as np
2+
import pandas as pd
3+
import cvxpy as cp
4+
5+
x = cp.Variable(2, name='x')
6+
F = cp.hstack([-13 + x[0] + ((5 - x[1]) * x[1] - 2) * x[1],
7+
-29 + x[0] + ((x[1] + 1) * x[1] - 14) * x[1]])
8+
9+
# formulation 1
10+
objective = cp.sum_squares(F)
11+
problem = cp.Problem(cp.Minimize(objective))
12+
x.value = np.array([0.5, -2])
13+
problem.solve(solver=cp.IPOPT, nlp=True, verbose=True)
14+
x_opt_one = x.value
15+
16+
# formulation 2
17+
objective = cp.square(-13 + x[0] + ((5 - x[1]) * x[1] - 2) * x[1]) + \
18+
cp.square(-29 + x[0] + ((x[1] + 1) * x[1] - 14) * x[1])
19+
problem = cp.Problem(cp.Minimize(objective))
20+
x.value = np.array([0.5, -2])
21+
problem.solve(solver=cp.IPOPT, nlp=True, verbose=True)
22+
x_opt_two = x.value
23+
24+
print(x_opt_one)
25+
print(x_opt_two)

0 commit comments

Comments
 (0)