From 3a9ef152827e184c15ba0a776c367854373ac5d4 Mon Sep 17 00:00:00 2001 From: tmigot Date: Thu, 1 Oct 2020 00:30:39 +0200 Subject: [PATCH] improve coverage --- src/State/GenericStatemod.jl | 2 +- src/Stopping/LinearAlgebraStopping.jl | 2 +- test/test-state/test-unitaire-ListOfStates.jl | 4 ++++ test/test-state/test-unitaire-NLPAtXmod.jl | 4 ++++ .../test-unitaire-generic-stopping.jl | 9 ++++++++ .../test-unitaire-linearalgebrastopping.jl | 22 ++++++++++++++----- 6 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/State/GenericStatemod.jl b/src/State/GenericStatemod.jl index 22c6d2b0..9be4f57c 100644 --- a/src/State/GenericStatemod.jl +++ b/src/State/GenericStatemod.jl @@ -181,7 +181,7 @@ function compress_state!(stateatx :: AbstractState; if typeof(getfield(stateatx, k)) <: AbstractVector katt = getfield(stateatx, k) if (length(katt) > max_vector_size) setfield!(stateatx, k, [norm(katt, pnorm)]) end - elseif typeof(getfield(stateatx, k)) <: Union{AbstractArray, AbstractMatrix} && !save_matrix + elseif typeof(getfield(stateatx, k)) <: Union{AbstractArray, AbstractMatrix} if save_matrix katt = getfield(stateatx, k) if maximum(size(katt)) > max_vector_size setfield!(stateatx, k, norm(getfield(stateatx, k))) end diff --git a/src/Stopping/LinearAlgebraStopping.jl b/src/Stopping/LinearAlgebraStopping.jl index bc60b2e1..b895a91f 100644 --- a/src/Stopping/LinearAlgebraStopping.jl +++ b/src/Stopping/LinearAlgebraStopping.jl @@ -258,6 +258,6 @@ function normal_equation_check(pb :: LLSModel, state :: AbstractState; pnorm :: Float64 = Inf, kwargs...) - nres = jtprod_residual(pb, state.x, jprod_residual(pb, state.x, state.x)) + nres = jtprod_residual(pb, state.x, residual(pb, state.x)) return norm(nres, pnorm) end diff --git a/test/test-state/test-unitaire-ListOfStates.jl b/test/test-state/test-unitaire-ListOfStates.jl index 33b7561a..0c3fa9d8 100644 --- a/test/test-state/test-unitaire-ListOfStates.jl +++ b/test/test-state/test-unitaire-ListOfStates.jl @@ -21,3 +21,7 @@ df1 = print(stest, verbose = false) df2 = print(stest2, verbose = false) @test typeof(df2) <: DataFrame + +stest3 = ListStates(-1, list = [s0, s1, s2], i = 3) + +@test stest3[2] == s1 diff --git a/test/test-state/test-unitaire-NLPAtXmod.jl b/test/test-state/test-unitaire-NLPAtXmod.jl index 601104e2..2444b5ed 100644 --- a/test/test-state/test-unitaire-NLPAtXmod.jl +++ b/test/test-state/test-unitaire-NLPAtXmod.jl @@ -69,6 +69,10 @@ c_uncons_nlp_at_x = copy_compress_state(uncons_nlp_at_x, max_vector_size = 5) @test c_uncons_nlp_at_x.x == [0.0] @test c_uncons_nlp_at_x.lambda == [1.0] +uncons_nlp_at_x.Hx = zeros(10,10) +zip_uncons_nlp_at_x = compress_state!(uncons_nlp_at_x, keep = true, save_matrix = true, max_vector_size = 5, Hx = 1) +zip_uncons_nlp_at_x.Hx == 0.0 + nlp_64 = NLPAtX(ones(10)) nlp_64.x = ones(10) nlp_64.fx = 1.0 diff --git a/test/test-stopping/test-unitaire-generic-stopping.jl b/test/test-stopping/test-unitaire-generic-stopping.jl index 8ca3c99b..5a41c432 100644 --- a/test/test-stopping/test-unitaire-generic-stopping.jl +++ b/test/test-stopping/test-unitaire-generic-stopping.jl @@ -15,6 +15,15 @@ stop0.meta.norm_unbounded_x = 2 stop!(stop0) @test status(stop0, list = true) == [:Optimal, :Unbounded] #indeed ||x||_2 = sqrt(6) !! +#We now test that stop! verifies that: +#- there are no NaN in the score +#- if the listofstates != nothing, stop! increases the list of states with the current_state. +stop0.meta.optimality_check = (a,b) -> NaN +stop0.listofstates = ListStates(state0) +stop!(stop0) +@test :DomainError in status(stop0, list = true) +@test length(stop0.listofstates) == 2 + #Initialize a GenericStopping by default stop_def = GenericStopping(rosenbrock, x0, atol = 1.0) @test stop_def.current_state.x == x0 diff --git a/test/test-stopping/test-unitaire-linearalgebrastopping.jl b/test/test-stopping/test-unitaire-linearalgebrastopping.jl index 445f60ff..3cac9020 100644 --- a/test/test-stopping/test-unitaire-linearalgebrastopping.jl +++ b/test/test-stopping/test-unitaire-linearalgebrastopping.jl @@ -94,9 +94,19 @@ la_stop = LAStopping(A, b, GenericState(x0), max_iter = 150000, rtol = 1e-6, max sa_stop = LAStopping(sparse(A), b, GenericState(sparse(x0)), max_iter = 150000, rtol = 1e-6) op_stop = LAStopping(LinearSystem(LinearOperator(A), b), GenericState(x0), max_iter = 150000, rtol = 1e-6, max_cntrs = Stopping._init_max_counters_linear_operators(nprod = 150000)) -@time RandomizedBlockKaczmarz(la_stop) -@test status(la_stop) == :Optimal -@time RandomizedBlockKaczmarz(sa_stop) -@test status(sa_stop) == :Optimal -@time RandomizedBlockKaczmarz(op_stop) -@test status(op_stop) == :Optimal +try + @time RandomizedBlockKaczmarz(la_stop) + @test status(la_stop) == :Optimal + @time RandomizedBlockKaczmarz(sa_stop) + @test status(sa_stop) == :Optimal + @time RandomizedBlockKaczmarz(op_stop) + @test status(op_stop) == :Optimal +catch + @warn "If LSSModel.A does not exist consider [la_stop.pb.Avals[i,j] for (i) in la_stop.pb.Arows, j in la_stop.pb.Acols]" + #https://github.com/JuliaSmoothOptimizers/NLPModels.jl/blob/master/src/lls_model.jl +end + +update!(la_stop.current_state, x = xref) +@test normal_equation_check(la_stop.pb, la_stop.current_state) <= 1e-10 +update!(op_stop.current_state, x = xref) +@test normal_equation_check(op_stop.pb, op_stop.current_state) <= 1e-10