-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial infrastructure for Krylov-based linear solvers #56
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
456d069
Initial infrastructure for Krylov-based linear solvers
mtanneau 1b80c02
Add unit tests for krylov solvers
mtanneau 584cb14
Add SQD solver
mtanneau 218161c
Remove use of opDiagonal
mtanneau f63da44
Use more efficient linear operator
mtanneau e932015
Add unit tests for SQD solvers
mtanneau 82cdd08
Fix typo
mtanneau 5fb6432
Update docs
mtanneau 28c7802
Allow for user-specified tolerances
mtanneau 2003908
Add compat entry for Krylov
mtanneau f0e35a6
Krylov solver for symmetric indefinite systems
mtanneau 3857ac4
Update docs and unify naming conventions
mtanneau 012163b
Fix form of 2x2 SQD system
mtanneau b530162
Tighten default tolerances
mtanneau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,239 @@ | ||
import Krylov | ||
const LO = Krylov.LinearOperators | ||
|
||
abstract type KrylovSolver{T} <: AbstractKKTSolver{T} end | ||
|
||
# ============================================================================== | ||
# KrylovPDSolver: | ||
# ============================================================================== | ||
|
||
""" | ||
KrylovPDSolver{T, F, Tv, Ta} | ||
|
||
Solves normal equations system with an iterative method `f::F`. | ||
|
||
```julia | ||
model.params.KKTOptions = Tulip.KKT.SolverOptions( | ||
KrylovPDSolver, method=Krylov.cg | ||
) | ||
``` | ||
""" | ||
mutable struct KrylovPDSolver{T, F, Tv, Ta} <: KrylovSolver{T} | ||
m::Int | ||
n::Int | ||
|
||
f::F # Krylov function | ||
atol::T # absolute tolerance | ||
rtol::T # relative tolerance | ||
|
||
# Problem data | ||
A::Ta # Constraint matrix | ||
θ::Tv # scaling | ||
regP::Tv # primal regularization | ||
regD::Tv # dual regularization | ||
|
||
function KrylovPDSolver(f::Function, A::Ta; | ||
atol::T=sqrt(eps(T)), | ||
rtol::T=sqrt(eps(T)) | ||
) where{T, Ta <: AbstractMatrix{T}} | ||
F = typeof(f) | ||
m, n = size(A) | ||
|
||
return new{T, F, Vector{T}, Ta}( | ||
m, n, | ||
f, atol, rtol, | ||
A, ones(T, n), ones(T, n), ones(T, m) | ||
) | ||
end | ||
end | ||
|
||
setup(::Type{KrylovPDSolver}, A; method, kwargs...) = KrylovPDSolver(method, A; kwargs...) | ||
|
||
backend(kkt::KrylovPDSolver) = "Krylov ($(kkt.f))" | ||
linear_system(::KrylovPDSolver) = "Normal equations" | ||
|
||
|
||
""" | ||
update!(kkt::KrylovPDSolver, θ, regP, regD) | ||
|
||
Update diagonal scaling ``θ`` and primal-dual regularizations. | ||
""" | ||
function update!( | ||
kkt::KrylovPDSolver{Tv}, | ||
θ::AbstractVector{Tv}, | ||
regP::AbstractVector{Tv}, | ||
regD::AbstractVector{Tv} | ||
) where{Tv<:Real} | ||
# Sanity checks | ||
length(θ) == kkt.n || throw(DimensionMismatch( | ||
"θ has length $(length(θ)) but linear solver is for n=$(kkt.n)." | ||
)) | ||
length(regP) == kkt.n || throw(DimensionMismatch( | ||
"regP has length $(length(regP)) but linear solver has n=$(kkt.n)" | ||
)) | ||
length(regD) == kkt.m || throw(DimensionMismatch( | ||
"regD has length $(length(regD)) but linear solver has m=$(kkt.m)" | ||
)) | ||
|
||
# Update diagonal scaling | ||
kkt.θ .= θ | ||
# Update regularizers | ||
kkt.regP .= regP | ||
kkt.regD .= regD | ||
|
||
return nothing | ||
end | ||
|
||
""" | ||
solve!(dx, dy, kkt::KrylovPDSolver, ξp, ξd) | ||
|
||
Solve the normal equations system using an iterative method. | ||
""" | ||
function solve!( | ||
dx::Vector{Tv}, dy::Vector{Tv}, | ||
kkt::KrylovPDSolver{Tv}, | ||
ξp::Vector{Tv}, ξd::Vector{Tv} | ||
) where{Tv<:Real} | ||
m, n = kkt.m, kkt.n | ||
|
||
# Setup | ||
d = one(Tv) ./ (kkt.θ .+ kkt.regP) | ||
D = Diagonal(d) | ||
|
||
# Set-up right-hand side | ||
ξ_ = ξp .+ kkt.A * (D * ξd) | ||
|
||
# Form linear operator S = A * D * A' + Rd | ||
# Here we pre-allocate the intermediary and final vectors | ||
v1 = zeros(Tv, n) | ||
v2 = zeros(Tv, m) | ||
Comment on lines
+128
to
+129
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should move |
||
opS = LO.LinearOperator(Tv, m, m, true, true, | ||
w -> begin | ||
mul!(v1, kkt.A', w) | ||
v1 .*= d | ||
mul!(v2, kkt.A, v1) | ||
v2 .+= kkt.regD .* w | ||
v2 | ||
end | ||
) | ||
|
||
# Solve normal equations | ||
_dy, stats = kkt.f(opS, ξ_, atol=kkt.atol, rtol=kkt.rtol) | ||
dy .= _dy | ||
|
||
# Recover dx | ||
dx .= D * (kkt.A' * dy - ξd) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could reuse mul!(v1, kkt.A', dy)
v1 .-= ξd
mul!(v2, D, v1) |
||
|
||
return nothing | ||
end | ||
|
||
|
||
# ============================================================================== | ||
# KrylovSQDSolver: | ||
# ============================================================================== | ||
|
||
""" | ||
KrylovSQDSolver{T, F, Tv, Ta} | ||
|
||
Solves the augmented system with an iterative method `f::F`. | ||
|
||
```julia | ||
model.params.KKTOptions = Tulip.KKT.SolverOptions( | ||
KrylovSQDSolver, method=Krylov.trimr | ||
) | ||
``` | ||
""" | ||
mutable struct KrylovSQDSolver{T, F, Tv, Ta} <: KrylovSolver{T} | ||
m::Int | ||
n::Int | ||
|
||
f::F # Krylov function | ||
atol::T # absolute tolerance | ||
rtol::T # relative tolerance | ||
|
||
# Problem data | ||
A::Ta # Constraint matrix | ||
θ::Tv # scaling | ||
regP::Tv # primal regularization | ||
regD::Tv # dual regularization | ||
|
||
function KrylovSQDSolver(f::Function, A::Ta; | ||
atol::T=sqrt(eps(T)), | ||
rtol::T=sqrt(eps(T)) | ||
mtanneau marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) where{T, Ta <: AbstractMatrix{T}} | ||
F = typeof(f) | ||
m, n = size(A) | ||
|
||
return new{T, F, Vector{T}, Ta}( | ||
m, n, | ||
f, atol, rtol, | ||
A, ones(T, n), ones(T, n), ones(T, m) | ||
) | ||
end | ||
end | ||
|
||
setup(::Type{KrylovSQDSolver}, A; method, kwargs...) = KrylovSQDSolver(method, A; kwargs...) | ||
|
||
backend(kkt::KrylovSQDSolver) = "Krylov ($(kkt.f))" | ||
linear_system(::KrylovSQDSolver) = "Augmented system" | ||
|
||
|
||
""" | ||
update!(kkt::KrylovSQDSolver, θ, regP, regD) | ||
|
||
Update diagonal scaling ``θ`` and primal-dual regularizations. | ||
""" | ||
function update!( | ||
kkt::KrylovSQDSolver{Tv}, | ||
θ::AbstractVector{Tv}, | ||
regP::AbstractVector{Tv}, | ||
regD::AbstractVector{Tv} | ||
) where{Tv<:Real} | ||
# Sanity checks | ||
length(θ) == kkt.n || throw(DimensionMismatch( | ||
"θ has length $(length(θ)) but linear solver is for n=$(kkt.n)." | ||
)) | ||
length(regP) == kkt.n || throw(DimensionMismatch( | ||
"regP has length $(length(regP)) but linear solver has n=$(kkt.n)" | ||
)) | ||
length(regD) == kkt.m || throw(DimensionMismatch( | ||
"regD has length $(length(regD)) but linear solver has m=$(kkt.m)" | ||
)) | ||
|
||
# Update diagonal scaling | ||
kkt.θ .= θ | ||
# Update regularizers | ||
kkt.regP .= regP | ||
kkt.regD .= regD | ||
|
||
return nothing | ||
end | ||
|
||
""" | ||
solve!(dx, dy, kkt::KrylovSQDSolver, ξp, ξd) | ||
|
||
Solve the normal equations system using an iterative method. | ||
""" | ||
function solve!( | ||
dx::Vector{Tv}, dy::Vector{Tv}, | ||
kkt::KrylovSQDSolver{Tv}, | ||
ξp::Vector{Tv}, ξd::Vector{Tv} | ||
) where{Tv<:Real} | ||
m, n = kkt.m, kkt.n | ||
|
||
# Setup | ||
d = one(Tv) ./ (kkt.θ .+ kkt.regP) | ||
D = Diagonal(d) | ||
|
||
# Solve augmented system | ||
# Currently assumes that Rd is a multiple of the identity matrix | ||
_dy, _dx, stats = kkt.f(kkt.A, ξp, ξd, | ||
N=D, τ = kkt.regD[1], | ||
atol=kkt.atol, rtol=kkt.rtol | ||
) | ||
|
||
dx .= _dx | ||
dy .= _dy | ||
|
||
return nothing | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import Krylov | ||
|
||
@testset "Krylov" begin | ||
for T in TvTYPES | ||
@testset "$T" begin | ||
for f in [Krylov.cg, Krylov.minres, Krylov.minres_qlp] | ||
@testset "$f" begin | ||
A = SparseMatrixCSC{T, Int}([ | ||
1 0 1 0; | ||
0 1 0 1 | ||
]) | ||
kkt = KKT.KrylovPDSolver(f, A) | ||
KKT.run_ls_tests(A, kkt) | ||
end | ||
end | ||
for f in [Krylov.tricg, Krylov.trimr] | ||
@testset "$f" begin | ||
A = SparseMatrixCSC{T, Int}([ | ||
1 0 1 0; | ||
0 1 0 1 | ||
]) | ||
kkt = KKT.KrylovSQDSolver(f, A) | ||
KKT.run_ls_tests(A, kkt) | ||
end | ||
end | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use
v1
andv2
here to avoid 2 new allocations at eachsolve!
pass: