Skip to content

Commit b0c2d5a

Browse files
authored
Merge pull request #21 from MPF-Optimization-Laboratory/updates
update
2 parents 8dcf1a0 + 713d48e commit b0c2d5a

File tree

6 files changed

+234
-86
lines changed

6 files changed

+234
-86
lines changed

src/BPDual.jl

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,13 @@ function bpdual(
100100
# ------------------------------------------------------------------
101101
m, n = size(A)
102102

103+
104+
work = rand(n)
105+
work2 = rand(n)
106+
work3 = rand(n)
107+
work4 = rand(n)
108+
work5 = rand(m)
109+
103110
tracer = ASPTracer(
104111
Int[], # iteration
105112
Float64[], # lambda
@@ -110,8 +117,8 @@ function bpdual(
110117
active = Vector{Int}([])
111118
state = Vector{Int}(zeros(Int, n))
112119
y = Vector{Float64}(zeros(Float64, m))
113-
S = Matrix{Float64}(zeros(Float64, m, 0))
114-
R = Matrix{Float64}(zeros(Float64, 0, 0))
120+
S = Matrix{Float64}(zeros(Float64, m, n))
121+
R = Matrix{Float64}(zeros(Float64, n, n))
115122
end
116123

117124
if homotopy
@@ -171,6 +178,8 @@ function bpdual(
171178
numtrim = 0
172179
nprodA = 0
173180
nprodAt = 0
181+
cur_r_size = 0
182+
174183

175184
# ------------------------------------------------------------------
176185
# Cold/warm-start initialization.
@@ -186,7 +195,7 @@ function bpdual(
186195
end
187196
else
188197
g = b - λ*y # Compute steepest-descent dir
189-
triminf(active, state, S, R, bl, bu, g)
198+
active, state, S, R = triminf!(active, state, S, R, bl, bu, g)
190199
nact = length(active)
191200
x = zeros(nact)
192201
y = restorefeas(y, active, state, S, R, bl, bu)
@@ -211,25 +220,25 @@ function bpdual(
211220
sL, sU = infeasibilities(bl, bu, z)
212221
g = b - λ*y # Steepest-descent direction
213222

214-
if isempty(R)
223+
if isempty((@view R[1:cur_r_size,1:cur_r_size]))
215224
condS = 1
216225
else
217-
rmin = minimum(diag(R))
218-
rmax = maximum(diag(R))
226+
rmin = minimum(diag((@view R[1:cur_r_size, 1:cur_r_size])))
227+
rmax = maximum(diag((@view R[1:cur_r_size, 1:cur_r_size])))
219228
condS = rmax / rmin
220229
end
221230

222231
if condS > 1e+10
223232
eFlag = :EXIT_SINGULAR_LS
224233
# Pad x with enough zeros to make it compatible with S.
225-
npad = size(S, 2) - size(x, 1)
234+
npad = size((@view S[:, 1:cur_r_size]), 2) - size(x, 1)
226235
x = [x; zeros(npad)]
227236
else
228-
dx, dy = newtonstep(S, R, g, x, λ)
237+
dx, dy = newtonstep((@view S[:,1:cur_r_size]), (@view R[1:cur_r_size, 1:cur_r_size]), g, x, λ)
229238
x .+= dx
230239
end
231240

232-
r = b - S*x
241+
r = b - S[:,1:cur_r_size]*x
233242

234243
# Print to log.
235244
yNorm = norm(y, 2)
@@ -273,7 +282,7 @@ function bpdual(
273282
@info "\nOptimal solution found. Trimming multipliers..."
274283
end
275284
g = b - λin*y
276-
trimx(x, S, R, active, state, g, b, λ, feaTol, optTol, loglevel)
285+
trimx(x, (@view S[:, 1:cur_r_size]), (@view R[1:cur_r_size, 1:cur_r_size]), active, state, g, b, λ, feaTol, optTol, loglevel)
277286
numtrim = nact - length(active)
278287
nact = length(active)
279288
end
@@ -288,7 +297,7 @@ function bpdual(
288297
p = q = 0
289298

290299
if homotopy
291-
x, dy, dz, step, λ, p = htpynewlam(active, state, A, R, S, x, y, sL, sU, λ, lamFinal)
300+
x, dy, dz, step, λ, p = htpynewlam(active, state, A, (@view R[1:cur_r_size, 1:cur_r_size]), (@view S[:,1:cur_r_size]), x, y, sL, sU, λ, lamFinal)
292301
nprodAt += 1
293302
else
294303
if norm(dy, Inf) < eps()
@@ -339,8 +348,9 @@ function bpdual(
339348
a = A * zerovec
340349
nprodA += 1
341350
zerovec[p] = 0
342-
R = qraddcol(S, R, a)
343-
S = [S a]
351+
qraddcol!(S, R, a, cur_r_size, work, work2, work3, work4, work5) # Update R
352+
cur_r_size +=1
353+
# S = [S a]
344354
push!(active, p)
345355
push!(x, 0)
346356
else
@@ -358,10 +368,12 @@ function bpdual(
358368
_, qa = findmax(abs.(x .* dropa))
359369
q = active[qa]
360370
state[q] = 0
361-
S = S[:, 1:nact .!= qa]
371+
# S = S[:, 1:nact .!= qa]
362372
deleteat!(active, qa)
363373
deleteat!(x, qa)
364-
R = qrdelcol(R, qa)
374+
# R = qrdelcol(R, qa)
375+
qrdelcol!(S, R, qa)
376+
cur_r_size -=1
365377
else
366378
eFlag = :EXIT_OPTIMAL
367379
end
@@ -390,4 +402,4 @@ function bpdual(
390402
@info "Solution time (sec): $tottime"
391403
end
392404
return tracer
393-
end
405+
end

src/helpers.jl

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -201,37 +201,49 @@ end
201201
# ----------------------------------------------------------------------
202202
# Trim working constraints with "infinite" bounds.
203203
# ----------------------------------------------------------------------
204-
205-
function triminf(active::Vector, state::Vector, S::Matrix, R::Matrix,
206-
bl::Vector, bu::Vector, g::Vector, b::Vector)
207-
204+
function triminf!(
205+
active::Vector{Int},
206+
state::Vector{Int},
207+
S::Matrix{Float64},
208+
R,
209+
bl::Vector{Float64},
210+
bu::Vector{Float64},
211+
)
208212
bigbnd = 1e10
209-
nact = length(active)
210213

211-
tlistbl = find( state[active] .== -1 & bl[active] .< -bigbnd )
212-
tlistbu = find( state[active] .== +1 & bu[active] .> +bigbnd )
213-
tlist = [tlistbl; tlistbu]
214+
tlist = [i for i in active if
215+
(state[i] == -1 && bl[i] < -bigbnd) ||
216+
(state[i] == 1 && bu[i] > bigbnd)
217+
]
214218

215219
if isempty(tlist)
216-
return
220+
return active, state, S, R
217221
end
218222

219-
for q in tlist
220-
qa = active[q]
221-
nact = nact - 1
222-
S = S[:,1:size(S,2) .!= qa] # Delete column from S
223-
deleteat!(active, qa) # Delete index from active set
224-
R = qrdelcol(R, qa) # Recompute new QR factorization
225-
state[q] = 0 # Mark constraint as free
226-
x = csne(R, S, g) # Recompute multipliers
223+
for q in sort(tlist; rev=true)
224+
qa = active[q]
227225

228-
rNorm = norm(b - S*x)
229-
xNorm = norm(x, 1)
226+
if qa == 1
227+
S = S[:, 2:end]
228+
elseif qa == size(S, 2)
229+
S = S[:, 1:end-1]
230+
else
231+
S = hcat(S[:, 1:qa-1], S[:, qa+1:end])
230232
end
231233

234+
deleteat!(active, q)
235+
236+
R = qrdelcol(R, qa)
237+
238+
state[q] = 0
239+
end
240+
241+
return active, state, S, R
232242
end
233243

234244

245+
246+
235247
## G) find_step
236248

237249
# ----------------------------------------------------------------------
@@ -387,4 +399,4 @@ function htpynewlam(active, state, A, R, S, x, y, s1, s2, λ, lamFinal)
387399
end
388400

389401
return x, dy, dz, step, λ, p
390-
end
402+
end

0 commit comments

Comments
 (0)