-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinterior_point.py
167 lines (109 loc) · 4.34 KB
/
interior_point.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import numpy as np
from numpy.linalg import norm
import matplotlib.pyplot as plt
NORM_EPSILON = 0.001
ETTA_EPSILON = 0.1
def bias(n):
return np.ones(n)
def tridiag(n, d1=-1, d2=4, d3=-1):
a = np.zeros((n, n), int)
d1s = np.repeat(d1, n-1)
d3s = np.repeat(d3, n-1)
np.fill_diagonal(a, d2)
np.fill_diagonal(a[1:], d1s)
np.fill_diagonal(a[:, 1:], d3s)
return a
def hilb(n):
a = np.zeros((n, n))
for i in range(n):
for j in range(n):
a[i, j] = (i + j + 1)
return a
def quadratic_grad(A, x, b):
return np.matmul(A, x) - b
def constraint_grad(P):
return P
def quadratic_hessian(A):
return A
def r_dual(A, x, b, P, q, lam):
return quadratic_grad(A, x, b) + np.matmul(P.T, lam)
def r_cent(x, P, q, lam, t):
return - np.matmul(np.diag(np.reshape(lam, -1)), (np.matmul(P, x) - q)) - np.ones([np.shape(P)[0], 1]) / t
def residual(A, x, b, P, q, lam, t):
return norm(np.append(r_dual(A, x, b, P, q, lam), r_cent(x, P, q, lam, t), axis=0))
def sufficient_decrease_condition(A, x, b, alpha, c, P, q, lam, delta_x, delta_lam, t):
next_step_x = x + alpha * delta_x
next_step_lam = lam + alpha * delta_lam
return np.min(next_step_lam) > 0 > np.max(np.matmul(P, next_step_x) - q) and \
residual(A, next_step_x, b, P, q, next_step_lam, t) <= (1 - c * alpha) * residual(A, x, b, P, q, lam, t)
def line_search(A, x, b, P, q, lam, delta_x, delta_lam, t):
alpha = 1
r = 0.5
c = 0.5
backtrack_iteration = 0
while not (sufficient_decrease_condition(A, x, b, alpha, c, P, q, lam, delta_x, delta_lam, t) or alpha < 0.001):
alpha = alpha * r
backtrack_iteration += 1
# print(backtrack_iteration)
return alpha
def primal_dual_interior_point(A, x, b, P, q, lam, mu):
duality_gaps = []
r_feas = []
iterations = []
m, n = np.shape(P)
iteration = 0
while True:
iteration += 1
iterations.append(iteration)
etta = - np.matmul((np.matmul(P, x) - q).T, lam)[0, 0]
t = mu * m / etta
duality_gaps.append(etta)
r_dual_norm = norm(r_dual(A, x, b, P, q, lam))
r_feas.append(r_dual_norm)
factor_matrix = np.vstack([np.hstack([quadratic_hessian(A), P.T]), np.hstack([-np.matmul(np.diag(np.reshape(lam, -1)), P.T), -np.diag(np.reshape(np.matmul(P, x) - q, -1))])])
residual_matrix = - np.vstack([r_dual(A, x, b, P, q, lam), r_cent(x, P, q, lam, t)])
delta_matrix = np.matmul(np.linalg.pinv(factor_matrix), residual_matrix)
delta_x = delta_matrix[:n]
delta_lam = delta_matrix[n:]
s = line_search(A, x, b, P, q, lam, delta_x, delta_lam, t)
x = x + s * delta_x
lam = lam + s * delta_lam
print(iteration)
print(etta)
print(r_dual_norm)
print('----------------')
if r_dual_norm <= NORM_EPSILON or etta <= ETTA_EPSILON:
return x, iterations, duality_gaps, r_feas
n_list = [100, 400]
A_hilb = list(map(lambda j: hilb(j), n_list))
A_tridiag = list(map(lambda j: tridiag(j), n_list))
b_init = list(map(lambda j: bias(j).reshape(1, -1).T, n_list))
x_init = list(map(lambda j: np.random.rand(j, 1) * 10e-3, n_list))
P_init = list(map(lambda j: np.random.rand(j, j), n_list))
q_init = list(map(lambda j: (np.random.rand(j, 1) + np.ones([j, 1])), n_list))
lam_init = list(map(lambda j: np.random.rand(j, 1) + .1, n_list))
for i in range(len(n_list)):
x_star, iters, d_g, r_f = primal_dual_interior_point(A_tridiag[i], x_init[i], b_init[i], P_init[i], q_init[i], lam_init[i], mu=10)
fig1 = plt.figure()
plt.plot(iters, d_g)
plt.xlabel('Iterations')
plt.ylabel('Duality gap')
plt.title('Primal Dual Interior Point')
fig2 = plt.figure()
plt.plot(iters, r_f)
plt.xlabel('Iterations')
plt.ylabel('Feasibility Residual')
plt.title('Primal Dual Interior Point')
for i in range(len(n_list)):
x_star, iters, d_g, r_f = primal_dual_interior_point(A_tridiag[i], x_init[i], b_init[i], P_init[i], q_init[i], lam_init[i], mu=10)
fig1 = plt.figure()
plt.plot(iters, d_g)
plt.xlabel('Iterations')
plt.ylabel('Surrogate Duality gap')
plt.title('Primal Dual Interior Point')
fig2 = plt.figure()
plt.plot(iters, r_f)
plt.xlabel('Iterations')
plt.ylabel('Feasibility Residual')
plt.title('Primal Dual Interior Point')
plt.show()