-
Notifications
You must be signed in to change notification settings - Fork 269
/
Lec8-IPM_CenteringPath_Primal.py
87 lines (61 loc) · 1.77 KB
/
Lec8-IPM_CenteringPath_Primal.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
import numpy as np;
m=5;
n=7;
c = np.array( [2, 1.5, 0, 0, 0, 0, 0] );
A = np.array( [ [12, 24, -1, 0, 0, 0, 0],
[16, 16, 0, -1, 0, 0, 0],
[30, 12, 0, 0, -1, 0, 0],
[1, 0, 0, 0, 0, 1, 0],
[0, 1, 0, 0, 0, 0, 1] ] );
b = np.array( [ 120, 120, 120, 15, 15] );
x = np.array( [14, 1, 72, 120, 312, 1, 14] );
y = np.zeros( m );
s = np.zeros( n );
e = np.array( [1] * n);
alpha = 0.995;
epsilon = 0.000000001;
beta = 0.3;
num = 0;
theta = 0;
mu = 10;
print(" iter x1 x2 theta max(xi*si) min(xi*si) mu #");
while ( True ):
found = True;
print("============== iter: %d ============" % (num) );
X = np.zeros( shape=(n,n) );
for i in range(n) :
X[i, i] = x[i];
X2 = np.zeros( shape=(n,n) );
for i in range(n) :
X2[i, i] = x[i]*x[i];
AX2c = np.dot( A, np.dot( X2, c) );
AX2Atr = np.dot( A, np.dot( X2, np.transpose(A)) );
muAXe = mu * np.dot( A, np.dot(X, e) );
y = np.linalg.solve( AX2Atr, AX2c - muAXe );
s = c - np.dot( np.transpose(A), y );
maxxs = max( x * s );
minxs = min( x * s );
print(" %d %lf %lf %lf %lf %lf %lf #" % (num, x[0], x[1], theta, maxxs, minxs, mu) );
mu = mu * 0.9;
num = num + 1;
for i in range(n) :
print("x[%d]: %lf s[%d]: %lf x*s[%d]: %lf " % (i, x[i], i, s[i], i, x[i]*s[i] ) );
if ( abs( x[i] * s[i]) > epsilon ):
found = False;
if (found == True ):
break;
X2c = np.dot( X2, c);
X2Atry = np.dot( X2, np.dot( np.transpose(A), y) );
dx = x + 1/mu * ( X2Atry - X2c );
for i in range(n):
print("dx[%d]: %lf" % (i, dx[i] ) );
thetax = -1;
for i in range(n) :
if (dx[i] < 0 ):
t = x[i] / (-dx[i]);
print("t[%d]: %lf" % (i, t) );
if ( t < thetax or thetax < 0 ):
thetax = t;
theta = min( 1, thetax*alpha);
print("thetax: %lf theta: %lf" % (thetax, theta) );
x = x + theta * dx;