TP-optimisation-numerique-2/test/tester_pas_de_cauchy.jl

99 lines
3.2 KiB
Julia
Raw Normal View History

2021-11-17 17:30:44 +00:00
@doc doc"""
Tester l'algorithme de pas de Cauchy
# Entrées :
* afficher : (Bool) affichage ou non des résultats de chaque test
# Les cas de test (dans l'ordre)
* quadratique 1
* quadratique 2
* quadratique 3
"""
2021-12-16 09:36:21 +00:00
function tester_pas_de_cauchy(afficher::Bool, Pas_De_Cauchy::Function)
2021-11-17 17:30:44 +00:00
2021-12-16 09:36:21 +00:00
tol_erreur = 1e-6
2021-11-17 17:30:44 +00:00
2021-12-16 09:36:21 +00:00
@testset "Pas de Cauchy" begin
"# Pour la quadratique 1"
2021-11-17 17:30:44 +00:00
2021-12-16 09:36:21 +00:00
@testset "g = 0" begin
g = [0; 0]
H = [7 0; 0 2]
delta = 1
s, e = Pas_De_Cauchy(g, H, delta)
2021-12-16 15:02:26 +00:00
sol = [0; 0]
if afficher
println("Cauchy 1= ", sol)
end
2021-12-16 09:36:21 +00:00
@test (e == 0) && (isapprox(s, [0; 0], atol = tol_erreur))
end
2021-11-17 17:30:44 +00:00
2021-12-16 09:36:21 +00:00
@testset "quad 2, non saturé" begin
g = [6; 2]
H = [7 0; 0 2]
delta = 1
s, e = Pas_De_Cauchy(g, H, delta)
sol = -(norm(g)^2 / (g' * H * g)) * g
2021-12-16 15:02:26 +00:00
if afficher
println("Cauchy 2= ", sol)
end
2021-12-16 09:36:21 +00:00
@test (e == 1) && (isapprox(s, sol, atol = tol_erreur)) # sol = [-0.9230769230769234; -0.30769230769230776]
end
@testset "quad 2, saturé" begin
g = [6; 2]
H = [7 0; 0 2]
delta = 0.9
s, e = Pas_De_Cauchy(g, H, delta)
sol = -(delta / norm(g)) * g
2021-12-16 15:02:26 +00:00
if afficher
println("Cauchy 3= ", sol)
end
2021-12-16 09:36:21 +00:00
@test (e == -1) && (isapprox(s, sol, atol = tol_erreur)) # sol = [-0.9230769230769234; -0.30769230769230776]
end
@testset "quad 3, non saturé" begin
g = [-2; 1]
H = [-2 0; 0 10]
delta = 6
s, e = Pas_De_Cauchy(g, H, delta)
sol = -(norm(g)^2 / (g' * H * g)) * g
2021-12-16 15:02:26 +00:00
if afficher
println("Cauchy 4= ", sol)
end
2021-12-16 09:36:21 +00:00
@test (e == 1) && (isapprox(s, sol, atol = tol_erreur)) # sol = [-0.9230769230769234; -0.30769230769230776]
end
@testset "quad 3, saturé" begin
g = [-2; 1]
H = [-2 0; 0 10]
delta = 5
s, e = Pas_De_Cauchy(g, H, delta)
sol = -(delta / norm(g)) * g
2021-12-16 15:02:26 +00:00
if afficher
println("Cauchy 5= ", sol)
end
2021-12-16 09:36:21 +00:00
@test (e == -1) && (isapprox(s, sol, atol = tol_erreur)) # sol = [-0.9230769230769234; -0.30769230769230776]
end
@testset "quad 3, g'*H*g <0 saturé" begin
g = [3; 1]
H = [-2 0; 0 10]
delta = 5
s, e = Pas_De_Cauchy(g, H, delta)
sol = -(delta / norm(g)) * g
2021-12-16 15:02:26 +00:00
if afficher
println("Cauchy 6= ", sol)
end
2021-12-16 09:36:21 +00:00
@test (e == -1) && (isapprox(s, sol, atol = tol_erreur)) # sol = [-0.9230769230769234; -0.30769230769230776]
end
@testset "quad 3, g'*H*g = 0 saturé" begin
g = [1, 2]
H = [2 -1; 4 -2]
delta = 5
s, e = Pas_De_Cauchy(g, H, delta)
sol = -(delta / norm(g)) * g
2021-12-16 15:02:26 +00:00
if afficher
println("Cauchy 7= ", sol)
end
2021-12-16 09:36:21 +00:00
@test (e == -1) && (isapprox(s, sol, atol = tol_erreur)) # sol = [-0.9230769230769234; -0.30769230769230776]
end
end
2021-11-17 17:30:44 +00:00
end