Skip to content

Commit

Permalink
ss
Browse files Browse the repository at this point in the history
  • Loading branch information
bzhangcw committed Apr 7, 2024
1 parent 0cb8efb commit fdfd3dc
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 59 deletions.
95 changes: 41 additions & 54 deletions src/utilities/homogeneous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ function _inner_homogeneous_eigenvalue(
B::Symmetric{Q,F}, iter, state
) where {Q<:Real,F<:Union{SparseMatrixCSC{Float64,Int64},Matrix{Float64}}}
n = length(state.x)
vals, vecs, info = eigenvalue(B, iter, state)
vals, vecs, info = _eigenvalue(B, iter, state)
λ₁ = vals |> real
ξ = vecs[:, 1] |> real

Expand All @@ -229,7 +229,7 @@ end

function _inner_homogeneous_eigenvalue(f::Function, iter, state)
n = length(state.x)
vals, vecs, info = eigenvalue(f, iter, state)
vals, vecs, info = _eigenvalue(f, iter, state)

λ₁ = vals[1]
ξ = vecs[1]
Expand All @@ -254,13 +254,14 @@ end

homogeneous_eigenvalue = Counting(_inner_homogeneous_eigenvalue)

function eigenvalue(
function _eigenvalue(
B::Symmetric{Float64,F}, iter, state; bg=:arnoldi
) where {F<:Union{SparseMatrixCSC{Float64,Int64},Matrix{Float64}}}

n = length(state.x)
_reltol = state.ϵ > 1e-4 ? 1e-5 : iter.eigtol
_tol = _reltol # * state.ϵ
# _reltol = state.ϵ > 1e-4 ? 1e-5 : iter.eigtol
# _tol = _reltol * state.ϵ
_tol = state.ϵ > 1e-3 ? 1e-5 : max(iter.eigtol, 1e-3 * state.ϵ)
if bg == :krylov
if iter.direction == :cold
vals, vecs, info = KrylovKit.eigsolve(B, n + 1, 1, :SR, Float64; tol=_tol, issymmetric=true, eager=true)
Expand All @@ -283,8 +284,8 @@ function eigenvalue(
end


function eigenvalue(f::Function, iter, state; bg=:krylov)
_tol = (state.ϵ > 1e-4 ? 1e-5 : iter.eigtol) # * state.ϵ
function _eigenvalue(f::Function, iter, state; bg=:krylov)
_tol = state.ϵ > 1e-3 ? 1e-5 : max(iter.eigtol, 1e-3 * state.ϵ)
if bg == :krylov
if iter.direction == :cold
n = length(state.x)
Expand Down Expand Up @@ -333,61 +334,47 @@ function NewtonStep(iter::I, μ, g, state; verbose::Bool=false

n = g |> length
gn = (g |> norm)
opH = LinearOperator(Float64, n, n, true, true, (y, v) -> iter.ff(y, v))
d, _info = cg(
opH, -Vector(g);
# rtol=state.ϵ > 1e-4 ? 1e-7 : iter.eigtol,
rtol=state.ϵ > 1e-4 ? gn * 1e-4 : gn * 1e-6,
itmax=200,
verbose=verbose ? 3 : 0
)

return _info.niter, 1, d, norm(d), d' * state.∇f, d' * state.∇fb
# f(v) = iter.ff(state.∇fb, v)
# d, _info = KrylovKit.linsolve(
# f, -Vector(g), -Vector(g),
# # isposdef=true,
# # issymmetric=true,
# opH = LinearOperator(Float64, n, n, true, true, (y, v) -> iter.ff(y, v))
# d, _info = cg(
# opH, -Vector(g);
# # rtol=state.ϵ > 1e-4 ? 1e-7 : iter.eigtol,
# # maxiter=200,
# # verbosity=3
# CG(;
# # rtol=state.ϵ > 1e-4 ? 1e-7 : iter.eigtol,
# tol=1e-4 * (g |> norm),
# maxiter=n * 2,
# verbosity=verbose ? 3 : 0
# ),
# rtol=state.ϵ > 1e-4 ? gn * 1e-4 : gn * 1e-6,
# itmax=200,
# verbose=verbose ? 3 : 0
# )
# return _info.numops, 1, d, norm(d), d' * state.∇f, d' * state.∇fb
# return _info.niter, 1, d, norm(d), d' * state.∇f, d' * state.∇fb
f(v) = iter.ff(state.∇fb, v)
d, _info = KrylovKit.linsolve(
f, -Vector(g), -Vector(g),
CG(;
tol=min(gn, 1e-4),
maxiter=n * 2,
verbosity=verbose ? 3 : 0
),
)
return _info.numops, 1, d, norm(d), d' * state.∇f, d' * state.∇fb
end

function NewtonStep(opH::LinearOperator, g, state; verbose::Bool=false)
@debug "started Newton-step @" Dates.now()
# @debug "started Newton-step @" Dates.now()
n = g |> length
gn = (g |> norm)
d, _info = cg(
opH, -Vector(g);
rtol=state.ϵ > 1e-4 ? gn * 1e-4 : gn * 1e-6,
itmax=200,
verbose=verbose ? 3 : 0
)

return _info.niter, 1, d, norm(d), d' * state.∇f, d' * state.∇fb
# f(v) = iter.ff(state.∇fb, v)
# d, _info = KrylovKit.linsolve(
# f, -Vector(g), -Vector(g),
# # isposdef=true,
# # issymmetric=true,
# # rtol=state.ϵ > 1e-4 ? 1e-7 : iter.eigtol,
# # maxiter=200,
# # verbosity=3
# CG(;
# # rtol=state.ϵ > 1e-4 ? 1e-7 : iter.eigtol,
# tol=1e-4*(g|>norm),
# maxiter=n*2,
# verbosity=3
# ),
# d, _info = cg(
# opH, -Vector(g);
# rtol=state.ϵ > 1e-4 ? gn * 1e-4 : gn * 1e-6,
# itmax=200,
# verbose=verbose ? 3 : 0
# )
# return _info.niter, 1, d, norm(d), d' * state.∇f, d' * state.∇fb
f(v) = iter.ff(state.∇fb, v)
d, _info = KrylovKit.linsolve(
f, -Vector(g), -Vector(g),
CG(;
tol=min(gn, 1e-4),
maxiter=n * 2,
verbosity=verbose ? 3 : 0
),
)

end

8 changes: 4 additions & 4 deletions test/convex/test_logistic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ using .LP
using LoopVectorization
using LIBSVMFileIO

bool_q_preprocessed = bool_opt = false
bool_q_preprocessed = bool_opt = true
bool_plot = true
f1(A, d=2) = sqrt.(sum(abs2.(A), dims=d))

ε = 1.5e-8 # * max(g(x0) |> norm, 1)
λ = 5e-6
λ = 1e-6
if bool_q_preprocessed
# name = "a4a"
# name = "a9a"
# name = "w4a"
# name = "covtype"
# name = "rcv1"
name = "news20"
name = "rcv1"
# name = "news20"

X, y = libsvmread("test/instances/libsvm/$name.libsvm"; dense=false)
Xv = hcat(X...)'
Expand Down
3 changes: 2 additions & 1 deletion test/cutest-benchmark/test_setup.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ filter_optimization_method(k) = k == :HSODMhvp
# PROBLEMS = UNC_PROBLEMS_4to200
# PROBLEMS = UNC_PROBLEMS_200to5000
# PROBLEMS = UNC_PROBLEMS_GOOD
PROBLEMS = UNC_PROBLEMS_COMB[155:end]
# PROBLEMS = UNC_PROBLEMS_COMB[155:end]
PROBLEMS = UNC_PROBLEMS_COMB
# PROBLEMS = UNC_PROBLEM_NO_PARAMS

if test_before_start
Expand Down

0 comments on commit fdfd3dc

Please sign in to comment.