/
MPC.jl
410 lines (329 loc) · 11.9 KB
/
MPC.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
"""
MPC
Implements Mehrotra's Predictor-Corrector interior-point algorithm.
"""
mutable struct MPC{T, Tv, Tb, Ta, Tk} <: AbstractIPMOptimizer{T}
# Problem data, in standard form
dat::IPMData{T, Tv, Tb, Ta}
# =================
# Book-keeping
# =================
niter::Int # Number of IPM iterations
solver_status::TerminationStatus # Optimization status
primal_status::SolutionStatus # Primal solution status
dual_status::SolutionStatus # Dual solution status
primal_objective::T # Primal bound: c'x
dual_objective::T # Dual bound: b'y + l' zl - u'zu
timer::TimerOutput
#=====================
Working memory
=====================#
pt::Point{T, Tv} # Current primal-dual iterate
res::Residuals{T, Tv} # Residuals at current iterate
Δ::Point{T, Tv} # Predictor
Δc::Point{T, Tv} # Corrector
# Step sizes
αp::T
αd::T
# Newton system RHS
ξp::Tv
ξl::Tv
ξu::Tv
ξd::Tv
ξxzl::Tv
ξxzu::Tv
# KKT solver
kkt::Tk
regP::Tv # Primal regularization
regD::Tv # Dual regularization
function MPC(
dat::IPMData{T, Tv, Tb, Ta}, kkt_options::KKTOptions{T}
) where{T, Tv<:AbstractVector{T}, Tb<:AbstractVector{Bool}, Ta<:AbstractMatrix{T}}
m, n = dat.nrow, dat.ncol
p = sum(dat.lflag) + sum(dat.uflag)
# Working memory
pt = Point{T, Tv}(m, n, p, hflag=false)
res = Residuals(
tzeros(Tv, m), tzeros(Tv, n), tzeros(Tv, n),
tzeros(Tv, n), zero(T),
zero(T), zero(T), zero(T), zero(T), zero(T)
)
Δ = Point{T, Tv}(m, n, p, hflag=false)
Δc = Point{T, Tv}(m, n, p, hflag=false)
# Newton RHS
ξp = tzeros(Tv, m)
ξl = tzeros(Tv, n)
ξu = tzeros(Tv, n)
ξd = tzeros(Tv, n)
ξxzl = tzeros(Tv, n)
ξxzu = tzeros(Tv, n)
# Initial regularizations
regP = tones(Tv, n)
regD = tones(Tv, m)
kkt = KKT.setup(dat.A, kkt_options.System, kkt_options.Backend)
Tk = typeof(kkt)
return new{T, Tv, Tb, Ta, Tk}(dat,
0, Trm_Unknown, Sln_Unknown, Sln_Unknown,
T(Inf), T(-Inf),
TimerOutput(),
pt, res, Δ, Δc, zero(T), zero(T),
ξp, ξl, ξu, ξd, ξxzl, ξxzu,
kkt, regP, regD
)
end
end
include("step.jl")
"""
compute_residuals!(::MPC)
In-place computation of primal-dual residuals at point `pt`.
"""
function compute_residuals!(mpc::MPC{T}) where{T}
pt, res = mpc.pt, mpc.res
dat = mpc.dat
# Primal residual
# rp = b - A*x
res.rp .= dat.b
mul!(res.rp, dat.A, pt.x, -one(T), one(T))
# Lower-bound residual
# rl_j = l_j - (x_j - xl_j) if l_j ∈ R
# = 0 if l_j = -∞
@. res.rl = ((dat.l + pt.xl) - pt.x) * dat.lflag
# Upper-bound residual
# ru_j = u_j - (x_j + xu_j) if u_j ∈ R
# = 0 if u_j = +∞
@. res.ru = (dat.u - (pt.x + pt.xu)) * dat.uflag
# Dual residual
# rd = c - (A'y + zl - zu)
res.rd .= dat.c
mul!(res.rd, transpose(dat.A), pt.y, -one(T), one(T))
@. res.rd += pt.zu .* dat.uflag - pt.zl .* dat.lflag
# Residuals norm
res.rp_nrm = norm(res.rp, Inf)
res.rl_nrm = norm(res.rl, Inf)
res.ru_nrm = norm(res.ru, Inf)
res.rd_nrm = norm(res.rd, Inf)
# Compute primal and dual bounds
mpc.primal_objective = dot(dat.c, pt.x) + dat.c0
mpc.dual_objective = (
dot(dat.b, pt.y)
+ dot(dat.l .* dat.lflag, pt.zl)
- dot(dat.u .* dat.uflag, pt.zu)
) + dat.c0
return nothing
end
"""
update_solver_status!()
Update status and return true if solver should stop.
"""
function update_solver_status!(mpc::MPC{T}, ϵp::T, ϵd::T, ϵg::T, ϵi::T) where{T}
mpc.solver_status = Trm_Unknown
pt, res = mpc.pt, mpc.res
dat = mpc.dat
ρp = max(
res.rp_nrm / (one(T) + norm(dat.b, Inf)),
res.rl_nrm / (one(T) + norm(dat.l .* dat.lflag, Inf)),
res.ru_nrm / (one(T) + norm(dat.u .* dat.uflag, Inf))
)
ρd = res.rd_nrm / (one(T) + norm(dat.c, Inf))
ρg = abs(mpc.primal_objective - mpc.dual_objective) / (one(T) + abs(mpc.primal_objective))
# Check for feasibility
if ρp <= ϵp
mpc.primal_status = Sln_FeasiblePoint
else
mpc.primal_status = Sln_Unknown
end
if ρd <= ϵd
mpc.dual_status = Sln_FeasiblePoint
else
mpc.dual_status = Sln_Unknown
end
# Check for optimal solution
if ρp <= ϵp && ρd <= ϵd && ρg <= ϵg
mpc.primal_status = Sln_Optimal
mpc.dual_status = Sln_Optimal
mpc.solver_status = Trm_Optimal
return nothing
end
# TODO: Primal/Dual infeasibility detection
# Check for infeasibility certificates
if max(
norm(dat.A * pt.x, Inf),
norm((pt.x .- pt.xl) .* dat.lflag, Inf),
norm((pt.x .+ pt.xu) .* dat.uflag, Inf)
) * (norm(dat.c, Inf) / max(1, norm(dat.b, Inf))) < - ϵi * dot(dat.c, pt.x)
# Dual infeasible, i.e., primal unbounded
mpc.primal_status = Sln_InfeasibilityCertificate
mpc.solver_status = Trm_DualInfeasible
return nothing
end
δ = dat.A' * pt.y .+ (pt.zl .* dat.lflag) .- (pt.zu .* dat.uflag)
if norm(δ, Inf) * max(
norm(dat.l .* dat.lflag, Inf),
norm(dat.u .* dat.uflag, Inf),
norm(dat.b, Inf)
) / (max(one(T), norm(dat.c, Inf))) < (dot(dat.b, pt.y) + dot(dat.l .* dat.lflag, pt.zl)- dot(dat.u .* dat.uflag, pt.zu)) * ϵi
# Primal infeasible
mpc.dual_status = Sln_InfeasibilityCertificate
mpc.solver_status = Trm_PrimalInfeasible
return nothing
end
return nothing
end
"""
optimize!
"""
function ipm_optimize!(mpc::MPC{T}, params::IPMOptions{T}) where{T}
# TODO: pre-check whether model needs to be re-optimized.
# This should happen outside of this function
dat = mpc.dat
# Initialization
TimerOutputs.reset_timer!(mpc.timer)
tstart = time()
mpc.niter = 0
# Print information about the problem
if params.OutputLevel > 0
@printf "\nOptimizer info (MPC)\n"
@printf "Constraints : %d\n" dat.nrow
@printf "Variables : %d\n" dat.ncol
bmin, bmax = extrema(dat.b)
@printf "RHS : [%+.2e, %+.2e]\n" bmin bmax
lmin, lmax = extrema(dat.l .* dat.lflag)
@printf "Lower bounds : [%+.2e, %+.2e]\n" lmin lmax
lmin, lmax = extrema(dat.u .* dat.uflag)
@printf "Upper bounds : [%+.2e, %+.2e]\n" lmin lmax
@printf "\nLinear solver options\n"
@printf " %-12s : %s\n" "Arithmetic" KKT.arithmetic(mpc.kkt)
@printf " %-12s : %s\n" "Backend" KKT.backend(mpc.kkt)
@printf " %-12s : %s\n" "System" KKT.linear_system(mpc.kkt)
end
# IPM LOG
if params.OutputLevel > 0
@printf "\n%4s %14s %14s %8s %8s %8s %7s %4s\n" "Itn" "PObj" "DObj" "PFeas" "DFeas" "GFeas" "Mu" "Time"
end
# Set starting point
@timeit mpc.timer "Initial point" compute_starting_point(mpc)
# Main loop
# Iteration 0 corresponds to the starting point.
# Therefore, there is no numerical factorization before the first log is printed.
# If the maximum number of iterations is set to 0, the only computation that occurs
# is computing the residuals at the initial point.
@timeit mpc.timer "Main loop" while(true)
# I.A - Compute residuals at current iterate
@timeit mpc.timer "Residuals" compute_residuals!(mpc)
update_mu!(mpc.pt)
# I.B - Log
# TODO: Put this in a logging function
ttot = time() - tstart
if params.OutputLevel > 0
# Display log
@printf "%4d" mpc.niter
# Objectives
ϵ = dat.objsense ? one(T) : -one(T)
@printf " %+14.7e" ϵ * mpc.primal_objective
@printf " %+14.7e" ϵ * mpc.dual_objective
# Residuals
@printf " %8.2e" max(mpc.res.rp_nrm, mpc.res.rl_nrm, mpc.res.ru_nrm)
@printf " %8.2e" mpc.res.rd_nrm
@printf " %8s" "--"
# Mu
@printf " %7.1e" mpc.pt.μ
# Time
@printf " %.2f" ttot
print("\n")
end
# TODO: check convergence status
# TODO: first call an `compute_convergence status`,
# followed by a check on the solver status to determine whether to stop
# In particular, user limits should be checked last (if an optimal solution is found,
# we want to report optimal, not user limits)
@timeit mpc.timer "update status" update_solver_status!(mpc,
params.TolerancePFeas,
params.ToleranceDFeas,
params.ToleranceRGap,
params.ToleranceIFeas
)
if (
mpc.solver_status == Trm_Optimal
|| mpc.solver_status == Trm_PrimalInfeasible
|| mpc.solver_status == Trm_DualInfeasible
)
break
elseif mpc.niter >= params.IterationsLimit
mpc.solver_status = Trm_IterationLimit
break
elseif ttot >= params.TimeLimit
mpc.solver_status = Trm_TimeLimit
break
end
# TODO: step
# For now, include the factorization in the step function
# Q: should we use more arguments here?
try
@timeit mpc.timer "Step" compute_step!(mpc, params)
catch err
if isa(err, PosDefException) || isa(err, SingularException)
# Numerical trouble while computing the factorization
mpc.solver_status = Trm_NumericalProblem
elseif isa(err, OutOfMemoryError)
# Out of memory
mpc.solver_status = Trm_MemoryLimit
elseif isa(err, InterruptException)
mpc.solver_status = Trm_Unknown
else
# Unknown error: rethrow
rethrow(err)
end
break
end
mpc.niter += 1
end
# TODO: print message based on termination status
params.OutputLevel > 0 && println("Solver exited with status $((mpc.solver_status))")
return nothing
end
function compute_starting_point(mpc::MPC{T}) where{T}
pt = mpc.pt
dat = mpc.dat
m, n, p = pt.m, pt.n, pt.p
KKT.update!(mpc.kkt, zeros(T, n), ones(T, n), T(1e-6) .* ones(T, m))
# Get initial iterate
KKT.solve!(zeros(T, n), pt.y, mpc.kkt, false .* mpc.dat.b, mpc.dat.c) # For y
KKT.solve!(pt.x, zeros(T, m), mpc.kkt, mpc.dat.b, false .* mpc.dat.c) # For x
# I. Recover positive primal-dual coordinates
δx = one(T) + max(
zero(T),
(-3 // 2) * minimum((pt.x .- dat.l) .* dat.lflag),
(-3 // 2) * minimum((dat.u .- pt.x) .* dat.uflag)
)
@. pt.xl = ((pt.x - dat.l) + δx) * dat.lflag
@. pt.xu = ((dat.u - pt.x) + δx) * dat.uflag
z = dat.c - dat.A' * pt.y
#=
We set zl, zu such that `z = zl - zu`
lⱼ | uⱼ | zˡⱼ | zᵘⱼ |
----+-----+--------+---------+
yes | yes | ¹/₂ zⱼ | ⁻¹/₂ zⱼ |
yes | no | zⱼ | 0 |
no | yes | 0 | -zⱼ |
no | no | 0 | 0 |
----+-----+--------+---------+
=#
@. pt.zl = ( z / (dat.lflag + dat.uflag)) * dat.lflag
@. pt.zu = (-z / (dat.lflag + dat.uflag)) * dat.uflag
δz = one(T) + max(zero(T), (-3 // 2) * minimum(pt.zl), (-3 // 2) * minimum(pt.zu))
pt.zl[dat.lflag] .+= δz
pt.zu[dat.uflag] .+= δz
mpc.pt.τ = one(T)
mpc.pt.κ = zero(T)
# II. Balance complementarity products
μ = dot(pt.xl, pt.zl) + dot(pt.xu, pt.zu)
dx = μ / ( 2 * (sum(pt.zl) + sum(pt.zu)))
dz = μ / ( 2 * (sum(pt.xl) + sum(pt.xu)))
pt.xl[dat.lflag] .+= dx
pt.xu[dat.uflag] .+= dx
pt.zl[dat.lflag] .+= dz
pt.zu[dat.uflag] .+= dz
# Update centrality parameter
update_mu!(mpc.pt)
return nothing
end