-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproject2.jl
373 lines (291 loc) · 8.63 KB
/
project2.jl
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
using AMD
using MatrixDepot
using MathProgBase
using Clp
using PyPlot
# to detect unbounded variables
INFINITY = 1.0e308
machine_eps = 1.1102230246251565e-16
# Parameters
eta = 0.99999
type IplpSolution
x::Vector{Float64} # the solution vector
flag::Bool # a true/false flag indicating convergence or not
cs::Vector{Float64} # the objective vector in standard form
As::SparseMatrixCSC{Float64} # the constraint matrix in standard form
bs::Vector{Float64} # the right hand side (b) in standard form
xs::Vector{Float64} # the solution in standard form
lam::Vector{Float64} # the solution lambda in standard form
s::Vector{Float64} # the solution s in standard form
end
type IplpProblem
c::Vector{Float64}
A::SparseMatrixCSC{Float64}
b::Vector{Float64}
lo::Vector{Float64}
hi::Vector{Float64}
end
type IplpProblemStandardForm
c::Vector{Float64}
A::SparseMatrixCSC{Float64}
b::Vector{Float64}
end
# This gives the LP problem
function convert_matrixdepot(mmmeta::Dict{AbstractString,Any})
key_base = sort(collect(keys(mmmeta)))[1]
return IplpProblem(
vec(mmmeta[key_base*"_c"]),
mmmeta[key_base],
vec(mmmeta[key_base*"_b"]),
vec(mmmeta[key_base*"_lo"]),
vec(mmmeta[key_base*"_hi"]))
end
#Convert Problem into Standard Form
function convert_to_standard_form(Problem)
n = length(Problem.c)
A = Problem.A
m = size(A,1)
b = Problem.b
c = Problem.c
hi = Problem.hi
lo = Problem.lo
As = sparse(A)
bs = b
cs = Problem.c
if length(find(lo)) != 0
b = b - A*lo
hi = hi - lo
bs = b
As = sparse(A)
end
if length(find(hi .!= INFINITY)) != 0
Jhigh = find(hi .!= INFINITY);
Vhigh = hi[Jhigh];
jh = length(Jhigh);
B1 = zeros(m,jh);
B2 = eye(jh);
B3 = zeros(jh,n);
B3[:,Jhigh] = B2;
As = [A B1;B3 B2];
As = sparse(As);
cs = vec([c; zeros(jh,1)]);
bs = vec([b;Vhigh]);
end
return IplpProblemStandardForm(
cs,
As,
bs)
end
#check for unboundedness
function unboundedness_check(A,c,x_k,s_k)
X_k = diagm([x_k;s_k])
X = A*(X_k^2)*A'
L = get_cholesky(X)
p_k = L*L'\(A*(X_k^2)*c)
r_k = c - A'*p_k
if -(X_k^2)*r_k >=0
return true
end
return false
end
function get_min_for_negative_delta(v_k, delta_v)
min_val = Inf
n = size(v_k,1)
for i = 1:n
if delta_v[i] < 0
val = -v_k[i]/delta_v[i]
min_val = min(min_val, val)
end
end
return min_val
end
function get_alpha_affine(x_k,s_k,delta_x_aff, delta_s_aff)
n = size(x_k,1)
alpha_aff_prim = min(1,get_min_for_negative_delta(x_k,delta_x_aff))
alpha_aff_dual = min(1,get_min_for_negative_delta(s_k,delta_s_aff))
return alpha_aff_prim,alpha_aff_dual
end
function get_alpha_max(x_k,s_k,delta_x, delta_s)
n = size(x_k,1)
alpha_max_prim = get_min_for_negative_delta(x_k,delta_x)
alpha_max_dual = get_min_for_negative_delta(s_k,delta_s)
return alpha_max_prim,alpha_max_dual
end
function get_alpha(alpha_max_prim,alpha_max_dual)
return min(1,eta*alpha_max_prim),min(1,eta*alpha_max_dual)
end
#Modified Cholesky Implementation
function get_cholesky_factor(M)
ordering = amd(sparse(M))
M = M[ordering,ordering]
n = size(M,1)
diagM = diag(M)
# Get the maximum diagonal element
gamma = maximum(abs(diagM))
# get the maximum off diagonal element
xi = maximum(abs(M - diagm(diagM)))
delta = machine_eps*(maximum([gamma + xi 1]))
beta = sqrt(maximum([gamma xi/n machine_eps]))
D = zeros(n,1)
L = sparse(eye(n))
C = zeros(n,n)
for j = 1:n
K = 1:j-1
C[j,j] = M[j,j]
if !isempty(K)
for s = 1:j-1
C[j,j]= C[j,j]- D[s,1]*L[j,s]*L[j,s]
end
end
if j < n
for i = j+1:n
C[i,j] = M[i,j]
if !isempty(K)
for s = 1:j-1
C[i,j] = C[i,j] - D[s,1]*L[i,s]*L[j,s]
end
end
end
# I can calculate D[j,j] now
theta = maximum(abs(C[j+1:n,j]))
D[j,1] = maximum([abs(C[j,j]) (theta/beta)^2 delta])
for i = j+1:n
L[i,j] = C[i,j]/D[j,1]
end
else
D[j] = maximum([abs(C[j,j]) delta]);
end
end
# Convert to the standard form of Cholesky Factorization
for j = 1:n
L[:,j] = L[:,j]*sqrt(D[j,1])
end
return L,ordering
end
function predictor_corrector(A, c, b, x_0, s_0,lambda_0,m_original,n_original,Problem, tol, max_iter)
# Get system parameters
n = size(A,2)
m = size(A,1)
# initialize variables
k = 0
x_k = copy(x_0)
s_k = copy(s_0)
lambda_k = copy(lambda_0)
normalized_residual_den = norm([b;c])
convergence = false
iter_hist = []
func_val_hist = []
mu_hist = []
norm_res_hist = []
while k <= max_iter
dxs = x_k./s_k
ss = 1./s_k
rc_k = A'*lambda_k + s_k - c
rb_k = A*x_k - b
rxs_k = x_k.*s_k
# Current Duality measure
mu = (x_k.*s_k/n)[1]
normalized_residual = norm([rc_k;rb_k;rxs_k])/normalized_residual_den
if (mu <= tol && normalized_residual <= tol)
convergence = true
break
end
# step length for affine step
M = A*sparse(diagm(vec(dxs)))*A'
L, ordering = get_cholesky_factor(M)
delta_lambda_aff, delta_s_aff, delta_x_aff = solve_linear_systems(L,A,dxs,ss,rc_k, rb_k, rxs_k, ordering)
alpha_aff_prim,alpha_aff_dual = get_alpha_affine(x_k,s_k,delta_x_aff, delta_s_aff)
# Duality measure for affine step
mu_aff = (((x_k + alpha_aff_prim*delta_x_aff).*(s_k + alpha_aff_dual*delta_s_aff))/n)[1]
# Set the Centering Parameter
sigma = (mu_aff/mu)^3
rxs_k = rxs_k + delta_x_aff.*delta_s_aff - sigma*mu
delta_lambda, delta_s, delta_x = solve_linear_systems(L,A,dxs, ss, rc_k, rb_k, rxs_k, ordering)
alpha_max_prim,alpha_max_dual = get_alpha_max(x_k,s_k,delta_x, delta_s)
eta = max(0.995,1 - mu)
alpha_primal_k = min(1,eta*alpha_max_prim)
alpha_dual_k = min(1,eta*alpha_max_dual)
x_k = x_k + alpha_primal_k*delta_x
s_k = s_k + alpha_dual_k*delta_s
lambda_k = lambda_k + alpha_dual_k*delta_lambda
#println("Iteration: \n", k)
#@show((c[1:n_original])'*(Problem.lo + x_k[1:n_original]))
iter_hist = [iter_hist;k]
func_val_hist = [func_val_hist;(c[1:n_original])'*(Problem.lo + x_k[1:n_original])]
mu_hist = [mu_hist;mu]
norm_res_hist = [norm_res_hist; normalized_residual]
k += 1
end
return IplpSolution(
vec(x_k[1:n_original] + Problem.lo),
Bool(convergence),
vec(c),
sparse(A),
vec(b),
vec(x_k),
vec(lambda_k),
vec(s_k)
)
end
function solve_linear_systems(L,A,dxs,ss, rc, rb, rxs, ordering)
lam_rhs = -rb - A*(dxs.*rc - ss.*rxs)
delta_lambda = lam_rhs
delta_lambda[ordering] = L'\(L\lam_rhs[ordering])
delta_s = -rc - A' * delta_lambda
delta_x = -ss.*rxs - dxs.*delta_s
return delta_lambda, delta_s, delta_x
end
function get_starting_point(A, b, c)
#x_hat, lambda_hat, s_hat arre solutions of :
# min 0.5*x'x subject to Ax=b
# min 0.5*s's subject to A'lambda + s = c
L = get_cholesky_factor(A*A')[1]
AA = L*L'
d = AA\b
x_hat = A'*d
lambda_hat = AA\(A*c)
s_hat = c - A'*lambda_hat
delta_x = max(-(3.0/2.0)*minimum(x_hat), 0)
delta_s = max(-(3.0/2.0)*minimum(s_hat), 0)
n = size(x_hat,1)
x_hat = x_hat + delta_x*ones(n,1)
s_hat = s_hat + delta_s*ones(n,1)
delta_x = (0.5 * (x_hat'*s_hat) / (ones(n,1)'*s_hat))[1]
delta_s = (0.5 * (x_hat'*s_hat) / (ones(n,1)'*x_hat))[1]
x_0 = x_hat + delta_x*ones(n,1)
lambda_0 = lambda_hat
s_0 = s_hat + delta_s*ones(n,1)
return x_0, lambda_0, s_0
end
function remove_zero_rows(Problem)
m = size(Problem.A,1)
non_zero_elems = find(Problem.A)
n_non_zero = length(non_zero_elems)
non_zero_rows = zeros(m,1)
for i = 1:n_non_zero
index = non_zero_elems[i] % m
if index == 0
index = m
end
non_zero_rows[index] = 1
end
ind = find(non_zero_rows)
return IplpProblem(
Problem.c,
sparse(Problem.A[ind,:]),
vec(Problem.b[ind,:]),
Problem.lo,
Problem.hi
)
end
# This is the public interface for the problem
function iplp(Problem, tol; maxit=100000)
Problem = remove_zero_rows(Problem)
m_original = size(Problem.A,1)
n_original = size(Problem.A,2)
sol_original = linprog(Problem.c,Problem.A,'=',Problem.b,Problem.lo,Problem.hi,ClpSolver())
standard_P = convert_to_standard_form(Problem)
x_0, lambda_0, s_0 = get_starting_point(standard_P.A,standard_P.b,standard_P.c)
solution = predictor_corrector(standard_P.A, standard_P.c, standard_P.b,x_0, s_0,lambda_0,m_original,n_original,Problem, tol, maxit)
return solution
end