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

87 lines
3.2 KiB
Julia
Raw Normal View History

2021-11-17 17:30:44 +00:00
@doc doc"""
Tester l'algorithme du gradient conjugué tronqué
# Entrées :
* afficher : (Bool) affichage ou non des résultats de chaque test
# Les cas de test (dans l'ordre)
* la quadratique 1
* la quadratique 2
* la quadratique 3
* la quadratique 4
* la quadratique 5
* la quadratique 6
"""
2021-12-04 17:43:26 +00:00
function tester_gct(afficher::Bool, Gradient_Conjugue_Tronque::Function)
2021-11-17 17:30:44 +00:00
tol = 1e-7
max_iter = 100
# Tolérance utilisé dans les tests
tol_test = 1e-3
@testset "Gradient-CT" begin
# le cas de test 1
2021-12-04 17:43:26 +00:00
grad = [0; 0]
Hess = [7 0; 0 2]
2021-11-17 17:30:44 +00:00
delta = 1
2021-12-04 17:43:26 +00:00
s = Gradient_Conjugue_Tronque(grad, Hess, [delta; max_iter; tol])
@test s [0.0; 0.0] atol = tol_test
2021-11-17 17:30:44 +00:00
# le cas de test 2 H definie positive
2021-12-04 17:43:26 +00:00
grad = [6; 2]
Hess = [7 0; 0 2]
2021-11-17 17:30:44 +00:00
delta = 0.5 # sol = pas de Cauchy
2021-12-04 17:43:26 +00:00
s = Gradient_Conjugue_Tronque(grad, Hess, [delta; max_iter; tol])
@test s -delta * grad / norm(grad) atol = tol_test
2021-11-17 17:30:44 +00:00
delta = 1.2 # saturation à la 2ieme itération
2021-12-04 17:43:26 +00:00
s = Gradient_Conjugue_Tronque(grad, Hess, [delta; max_iter; tol])
@test s [-0.8740776099190263, -0.8221850958502244] atol = tol_test
2021-11-17 17:30:44 +00:00
delta = 3 # sol = min global
2021-12-04 17:43:26 +00:00
s = Gradient_Conjugue_Tronque(grad, Hess, [delta; max_iter; tol])
@test s -Hess \ grad atol = tol_test
2021-11-17 17:30:44 +00:00
# le cas test 2 bis matrice avec 1 vp < 0 et 1 vp > 0
2021-12-04 17:43:26 +00:00
grad = [1, 2]
Hess = [1 0; 0 -1]
delta = 1.0 # g^T H g < 0 première direction concave
s = Gradient_Conjugue_Tronque(grad, Hess, [delta; max_iter; tol])
@test s -delta * grad / norm(grad) atol = tol_test
grad = [1, 0]
2021-11-17 17:30:44 +00:00
delta = 0.5 # g^T H g > 0 sol pas de Cauchy
2021-12-04 17:43:26 +00:00
s = Gradient_Conjugue_Tronque(grad, Hess, [delta; max_iter; tol])
@test s -delta * grad / norm(grad) atol = tol_test
grad = [2, 1] # g^T H g > 0 sol à l'itération 2, saturation
2021-11-17 17:30:44 +00:00
delta = 6
2021-12-04 17:43:26 +00:00
s = Gradient_Conjugue_Tronque(grad, Hess, [delta; max_iter; tol])
2021-12-08 07:51:27 +00:00
@test isapprox(s, [0.48997991959774634, 5.979959839195494], atol = tol_test) || isapprox(s, [-4.489979919597747, -3.979959839195493], atol = tol_test)
2021-11-17 17:30:44 +00:00
# le cas de test 3
#grad = [-2 ; 1]
#Hess = [-2 0 ; 0 10]
#delta = 10
#s = Gradient_Conjugue_Tronque(grad,Hess,[delta;max_iter;tol])
#@test s ≈ [9.102342582478453; -4.140937032991381] atol = tol_test
# le cas de test 4
#grad = [0 ; 0]
#Hess = [-2 0 ; 0 10]
#delta = 1
#s = Gradient_Conjugue_Tronque(grad,Hess,[delta;max_iter;tol])
#@test s ≈ [0.0 ; 0.0] atol = tol_test
# le cas de test 5
2021-12-04 17:43:26 +00:00
grad = [2; 3]
Hess = [4 6; 6 5]
2021-11-17 17:30:44 +00:00
delta = 3
2021-12-04 17:43:26 +00:00
s = Gradient_Conjugue_Tronque(grad, Hess, [delta; max_iter; tol])
@test s [1.9059020876695578; -2.3167946029410595] atol = tol_test
2021-11-17 17:30:44 +00:00
# le cas de test 6
# Le pas de Cauchy conduit à un gradient nul en 1 itération
2021-12-04 17:43:26 +00:00
grad = [2; 0]
Hess = [4 0; 0 -15]
2021-11-17 17:30:44 +00:00
delta = 2
2021-12-04 17:43:26 +00:00
s = Gradient_Conjugue_Tronque(grad, Hess, [delta; max_iter; tol])
@test s [-0.5; 0.0] atol = tol_test
2021-11-17 17:30:44 +00:00
end
end