style: formattage automatique

This commit is contained in:
Laureηt 2021-12-16 10:36:21 +01:00
parent c071194284
commit bd31a72072
No known key found for this signature in database
GPG key ID: D88C6B294FD40994
6 changed files with 215 additions and 253 deletions

View file

@ -7,12 +7,12 @@ using TestOptinum
include("Algorithme_De_Newton.jl")
export Algorithme_De_Newton
export Algorithme_De_Newton
include("Pas_De_Cauchy.jl")
export Pas_De_Cauchy
include("Gradient_Conjugue_Tronque.jl")
include("Gradient_Conjugue_Tronque.jl")
export Gradient_Conjugue_Tronque
include("Regions_De_Confiance.jl")

View file

@ -1,38 +0,0 @@
#using Pkg; Pkg.add("LinearAlgebra"); Pkg.add("Markdown")
# using Documenter
using LinearAlgebra
using Markdown # Pour que les docstrings en début des fonctions ne posent
# pas de soucis. Ces docstrings sont utiles pour générer
# la documentation sous GitHub
include("Algorithme_De_Newton.jl")
# Affichage les sorties de l'algorithme des Régions de confiance
function my_afficher_resultats(algo,nom_fct,point_init,xmin,fxmin,flag,sol_exacte,nbiters)
println("-------------------------------------------------------------------------")
printstyled("Résultats de : ",algo, " appliqué à ",nom_fct, " au point initial ", point_init, ":\n",bold=true,color=:blue)
println(" * xsol = ",xmin)
println(" * f(xsol) = ",fxmin)
println(" * nb_iters = ",nbiters)
println(" * flag = ",flag)
println(" * sol_exacte : ", sol_exacte)
end
# Fonction f0
# -----------
f0(x) = sin(x)
# la gradient de la fonction f0
grad_f0(x) = cos(x)
# la hessienne de la fonction f0
hess_f0(x) = -sin(x)
sol_exacte = -pi/2
options = []
x0 = sol_exacte
xmin,f_min,flag,nb_iters = Algorithme_De_Newton(f0,grad_f0,hess_f0,x0,options)
my_afficher_resultats("Newton","f0",x0,xmin,f_min,flag,sol_exacte,nb_iters)
x0 = -pi/2+0.5
xmin,f_min,flag,nb_iters = Algorithme_De_Newton(f0,grad_f0,hess_f0,x0,options)
my_afficher_resultats("Newton","f0",x0,xmin,f_min,flag,sol_exacte,nb_iters)
x0 = pi/2
xmin,f_min,flag,nb_iters = Algorithme_De_Newton(f0,grad_f0,hess_f0,x0,options)
my_afficher_resultats("Newton","f0",x0,xmin,f_min,flag,sol_exacte,nb_iters)

View file

@ -6,19 +6,19 @@ Cacher les traces d'appels des tests erronés ou échoués, pour les remettre, d
"""
function cacher_stacktrace()
Test.eval(quote
function record(ts::DefaultTestSet, t::Union{Fail, Error})
if myid() == 1
#printstyled(ts.description, ": ", color=:white) # afficher la description du testset
# ne pas afficher pour les tests interrompus
if !(t isa Error) || t.test_type != :test_interrupted
# print(t) # afficher le resultat et la solution attendu
if !isa(t, Error)
# Base.show_backtrace(stdout, scrub_backtrace(backtrace())) # afficher la trace d'appels
end
end
end
push!(ts.results, t)
t, isa(t, Error) || backtrace()
end
end)
function record(ts::DefaultTestSet, t::Union{Fail,Error})
if myid() == 1
#printstyled(ts.description, ": ", color=:white) # afficher la description du testset
# ne pas afficher pour les tests interrompus
if !(t isa Error) || t.test_type != :test_interrupted
# print(t) # afficher le resultat et la solution attendu
if !isa(t, Error)
# Base.show_backtrace(stdout, scrub_backtrace(backtrace())) # afficher la trace d'appels
end
end
end
push!(ts.results, t)
t, isa(t, Error) || backtrace()
end
end)
end

View file

@ -8,101 +8,101 @@ Tester l'algorithme de Newton local
* fct 1 : x011,x012
* fct 2 : x021,x022
"""
function tester_algo_newton(afficher::Bool,Algorithme_De_Newton::Function)
max_iter = 100
function tester_algo_newton(afficher::Bool, Algorithme_De_Newton::Function)
max_iter = 100
Tol_abs = sqrt(eps())
Tol_rel = 1e-15
options = [max_iter, Tol_abs, Tol_rel]
@testset "L'algo de Newton" begin
@testset "Cas test 1 x0 = solution" begin
# point de départ x011
x_min, fx_min, flag, nb_iters = Algorithme_De_Newton(fct1,grad_fct1,hess_fct1,sol_exacte_fct1,options)
if (afficher)
afficher_resultats("algorithme de Newton ","fct1","x011",x_min,fx_min,flag,sol_exacte_fct1,nb_iters)
end
@testset "solution" begin
@test isapprox(x_min, sol_exacte_fct1 , atol = tol_erreur)
end
@testset "itération" begin
@test nb_iters == 0
end
end
@testset "Cas test 1 x0 = x011" begin
#point de départ x011
x_min, fx_min, flag, nb_iters = Algorithme_De_Newton(fct1,grad_fct1,hess_fct1,pts1.x011,options)
if (afficher)
afficher_resultats("algorithme de Newton ","fct1","x011",x_min,fx_min,flag,sol_exacte_fct1,nb_iters)
end
@testset "solution" begin
@test isapprox(x_min, sol_exacte_fct1 , atol = tol_erreur)
end
@testset "itération" begin
@test nb_iters == 1
end
end
@testset "Cas test 1 x0 = x012" begin
x_min, fx_min, flag, nb_iters = Algorithme_De_Newton(fct1,grad_fct1,hess_fct1,pts1.x012,options)
if (afficher)
afficher_resultats("algorithme de Newton ","fct1","x012",x_min,fx_min,flag,sol_exacte_fct1,nb_iters)
end
@testset "solution" begin
@test x_min sol_exacte_fct1 atol = tol_erreur
options = [max_iter, Tol_abs, Tol_rel]
@testset "L'algo de Newton" begin
@testset "Cas test 1 x0 = solution" begin
# point de départ x011
x_min, fx_min, flag, nb_iters = Algorithme_De_Newton(fct1, grad_fct1, hess_fct1, sol_exacte_fct1, options)
if (afficher)
afficher_resultats("algorithme de Newton ", "fct1", "x011", x_min, fx_min, flag, sol_exacte_fct1, nb_iters)
end
@testset "itération" begin
@test nb_iters == 1
@testset "solution" begin
@test isapprox(x_min, sol_exacte_fct1, atol = tol_erreur)
end
end
@testset "Cas test 2 x0 = solution" begin
x_min, fx_min, flag, nb_iters = Algorithme_De_Newton(fct1,grad_fct1,hess_fct1,sol_exacte_fct1,options)
if (afficher)
afficher_resultats("algorithme de Newton ","fct1","x011",x_min,fx_min,flag,sol_exacte_fct1,nb_iters)
end
@testset "solution" begin
@test isapprox(x_min, sol_exacte_fct1 , atol = tol_erreur)
end
@testset "itération" begin
@test nb_iters == 0
end
end
@testset "Cas test 2 x0 = x021" begin
x_min, fx_min, flag, nb_iters = Algorithme_De_Newton(fct2,grad_fct2,hess_fct2,pts1.x021,options)
if (afficher)
afficher_resultats("algorithme de Newton ","fct2","x021",x_min,fx_min,flag,sol_exacte_fct2,nb_iters)
end
@testset "solution" begin
@test x_min sol_exacte_fct2 atol = tol_erreur
end
@testset "itération" begin
@test nb_iters == 6
@testset "itération" begin
@test nb_iters == 0
end
end
@testset "Cas test 2 x0 = x022" begin
x_min, fx_min, flag, nb_iters = Algorithme_De_Newton(fct2,grad_fct2,hess_fct2,pts1.x022,options)
if (afficher)
afficher_resultats("algorithme de Newton ","fct2","x022",x_min,fx_min,flag,sol_exacte_fct2,nb_iters)
end
@testset "solution" begin
@test x_min sol_exacte_fct2 atol = tol_erreur
end
@testset "Cas test 1 x0 = x011" begin
#point de départ x011
x_min, fx_min, flag, nb_iters = Algorithme_De_Newton(fct1, grad_fct1, hess_fct1, pts1.x011, options)
if (afficher)
afficher_resultats("algorithme de Newton ", "fct1", "x011", x_min, fx_min, flag, sol_exacte_fct1, nb_iters)
end
@testset "itération" begin
@test nb_iters == 5
@testset "solution" begin
@test isapprox(x_min, sol_exacte_fct1, atol = tol_erreur)
end
end
@testset "itération" begin
@test nb_iters == 1
end
end
@testset "Cas test 1 x0 = x012" begin
x_min, fx_min, flag, nb_iters = Algorithme_De_Newton(fct1, grad_fct1, hess_fct1, pts1.x012, options)
if (afficher)
afficher_resultats("algorithme de Newton ", "fct1", "x012", x_min, fx_min, flag, sol_exacte_fct1, nb_iters)
end
@testset "solution" begin
@test x_min sol_exacte_fct1 atol = tol_erreur
end
@testset "itération" begin
@test nb_iters == 1
end
end
@testset "Cas test 2 x0 = solution" begin
x_min, fx_min, flag, nb_iters = Algorithme_De_Newton(fct1, grad_fct1, hess_fct1, sol_exacte_fct1, options)
if (afficher)
afficher_resultats("algorithme de Newton ", "fct1", "x011", x_min, fx_min, flag, sol_exacte_fct1, nb_iters)
end
@testset "solution" begin
@test isapprox(x_min, sol_exacte_fct1, atol = tol_erreur)
end
@testset "itération" begin
@test nb_iters == 0
end
end
@testset "Cas test 2 x0 = x021" begin
x_min, fx_min, flag, nb_iters = Algorithme_De_Newton(fct2, grad_fct2, hess_fct2, pts1.x021, options)
if (afficher)
afficher_resultats("algorithme de Newton ", "fct2", "x021", x_min, fx_min, flag, sol_exacte_fct2, nb_iters)
end
@testset "solution" begin
@test x_min sol_exacte_fct2 atol = tol_erreur
end
@testset "itération" begin
@test nb_iters == 6
end
end
@testset "Cas test 2 x0 = x022" begin
x_min, fx_min, flag, nb_iters = Algorithme_De_Newton(fct2, grad_fct2, hess_fct2, pts1.x022, options)
if (afficher)
afficher_resultats("algorithme de Newton ", "fct2", "x022", x_min, fx_min, flag, sol_exacte_fct2, nb_iters)
end
@testset "solution" begin
@test x_min sol_exacte_fct2 atol = tol_erreur
end
@testset "itération" begin
@test nb_iters == 5
end
end
@testset "Cas test 2 x0 = x023" begin
options[1] = 1
sol = [-4.99999958629818e9, 8.673617379884035e-19]
x_min, fx_min, flag, nb_iters = Algorithme_De_Newton(fct2,grad_fct2,hess_fct2,pts1.x023,options)
if (afficher)
afficher_resultats("algorithme de Newton ","fct2","x022",x_min,fx_min,flag,sol_exacte_fct2,nb_iters)
end
@testset "solution" begin
@test x_min sol atol = tol_erreur
@testset "Cas test 2 x0 = x023" begin
options[1] = 1
sol = [-4.99999958629818e9, 8.673617379884035e-19]
x_min, fx_min, flag, nb_iters = Algorithme_De_Newton(fct2, grad_fct2, hess_fct2, pts1.x023, options)
if (afficher)
afficher_resultats("algorithme de Newton ", "fct2", "x022", x_min, fx_min, flag, sol_exacte_fct2, nb_iters)
end
@testset "exception" begin
options[1] = 100
@test_throws SingularException x_min, fx_min, flag, nb_iters = Algorithme_De_Newton(fct2,grad_fct2,hess_fct2,pts1.x023,options)
end
end
end
@testset "solution" begin
@test x_min sol atol = tol_erreur
end
@testset "exception" begin
options[1] = 100
@test_throws SingularException x_min, fx_min, flag, nb_iters = Algorithme_De_Newton(fct2, grad_fct2, hess_fct2, pts1.x023, options)
end
end
end
end

View file

@ -15,55 +15,55 @@ Tester l'algorithme du Lagrangien augmenté
* fct1 : x01, x02
* fct2 : x03, x04
"""
function tester_lagrangien_augmente(afficher::Bool,Lagrangien_Augmente::Function)
function tester_lagrangien_augmente(afficher::Bool, Lagrangien_Augmente::Function)
# initialisation des paramètres
lambda0 = 2
mu0 = 10
tho = 2
epsilon = 1e-8
tol = 1e-5
max_iters = 1000
options = [epsilon, tol, max_iters, lambda0, mu0, tho]
# La tolérance utilisée dans les tests
tol_erreur = 1e-4
# Les trois algorithmes d'optimisations sans contraintes utlisés
algos = ["newton", "gct", "cauchy"]
# initialisation des paramètres
lambda0 = 2
mu0 = 10
tho = 2
epsilon = 1e-8
tol = 1e-5
max_iters = 1000
options = [epsilon, tol, max_iters, lambda0, mu0, tho]
# La tolérance utilisée dans les tests
tol_erreur = 1e-4
@testset "Lagrangien augmenté " begin
@testset "Avec $algo" for algo in algos
# le cas de test 1
xmin,fxmin,flag,nbiters = Lagrangien_Augmente(algo,fct1,contrainte1,grad_fct1,hess_fct1,grad_contrainte1,
hess_contrainte1,pts2.x01,options)
if (afficher)
afficher_resultats("Lagrangien augmenté avec "*algo,"fonction 1","x01",xmin,fxmin,flag,sol_fct1_augm,nbiters)
end
@test isapprox(xmin,sol_fct1_augm ,atol=tol_erreur)
# Les trois algorithmes d'optimisations sans contraintes utlisés
algos = ["newton", "gct", "cauchy"]
# le cas de test 2
xmin ,fxmin,flag,nbiters = Lagrangien_Augmente(algo,fct1,contrainte1,grad_fct1,hess_fct1,grad_contrainte1,
hess_contrainte1,pts2.x02,options)
if (afficher)
afficher_resultats("Lagrangien augmenté avec "*algo,"fonction 1","x02",xmin,fxmin,flag,sol_fct1_augm,nbiters)
end
@test xmin sol_fct1_augm atol=tol_erreur
@testset "Lagrangien augmenté " begin
@testset "Avec $algo" for algo in algos
# le cas de test 1
xmin, fxmin, flag, nbiters = Lagrangien_Augmente(algo, fct1, contrainte1, grad_fct1, hess_fct1, grad_contrainte1,
hess_contrainte1, pts2.x01, options)
if (afficher)
afficher_resultats("Lagrangien augmenté avec " * algo, "fonction 1", "x01", xmin, fxmin, flag, sol_fct1_augm, nbiters)
end
@test isapprox(xmin, sol_fct1_augm, atol = tol_erreur)
# le cas de test 3
xmin,fxmin,flag,nbiters = Lagrangien_Augmente(algo,fct2,contrainte2,grad_fct2,hess_fct2,grad_contrainte2,
hess_contrainte2,pts2.x03,options)
if (afficher)
afficher_resultats("Lagrangien augmenté avec "*algo,"fonction 2","x03",xmin,fxmin,flag,sol_fct2_augm,nbiters)
end
@test xmin sol_fct2_augm atol=tol_erreur
# le cas de test 2
xmin, fxmin, flag, nbiters = Lagrangien_Augmente(algo, fct1, contrainte1, grad_fct1, hess_fct1, grad_contrainte1,
hess_contrainte1, pts2.x02, options)
if (afficher)
afficher_resultats("Lagrangien augmenté avec " * algo, "fonction 1", "x02", xmin, fxmin, flag, sol_fct1_augm, nbiters)
end
@test xmin sol_fct1_augm atol = tol_erreur
# le cas de test 4
xmin ,fxmin,flag,nbiters = Lagrangien_Augmente(algo,fct2,contrainte2,grad_fct2,hess_fct2,grad_contrainte2,
hess_contrainte2,pts2.x04,options)
if (afficher)
afficher_resultats("Lagrangien augmenté avec "*algo,"fonction 2","x04",xmin,fxmin,flag,sol_fct2_augm,nbiters)
end
@test xmin sol_fct2_augm atol=tol_erreur
end
end
# le cas de test 3
xmin, fxmin, flag, nbiters = Lagrangien_Augmente(algo, fct2, contrainte2, grad_fct2, hess_fct2, grad_contrainte2,
hess_contrainte2, pts2.x03, options)
if (afficher)
afficher_resultats("Lagrangien augmenté avec " * algo, "fonction 2", "x03", xmin, fxmin, flag, sol_fct2_augm, nbiters)
end
@test xmin sol_fct2_augm atol = tol_erreur
# le cas de test 4
xmin, fxmin, flag, nbiters = Lagrangien_Augmente(algo, fct2, contrainte2, grad_fct2, hess_fct2, grad_contrainte2,
hess_contrainte2, pts2.x04, options)
if (afficher)
afficher_resultats("Lagrangien augmenté avec " * algo, "fonction 2", "x04", xmin, fxmin, flag, sol_fct2_augm, nbiters)
end
@test xmin sol_fct2_augm atol = tol_erreur
end
end
end

View file

@ -9,72 +9,72 @@ Tester l'algorithme de pas de Cauchy
* quadratique 2
* quadratique 3
"""
function tester_pas_de_cauchy(afficher::Bool,Pas_De_Cauchy::Function)
function tester_pas_de_cauchy(afficher::Bool, Pas_De_Cauchy::Function)
tol_erreur = 1e-6
tol_erreur = 1e-6
@testset "Pas de Cauchy" begin
"# Pour la quadratique 1"
@testset "Pas de Cauchy" begin
"# Pour la quadratique 1"
@testset "g = 0" begin
g = [0; 0]
H = [7 0 ; 0 2]
delta = 1
s, e = Pas_De_Cauchy(g,H,delta)
@test (e == 0) && (isapprox(s,[0; 0],atol=tol_erreur))
end
@testset "g = 0" begin
g = [0; 0]
H = [7 0; 0 2]
delta = 1
s, e = Pas_De_Cauchy(g, H, delta)
@test (e == 0) && (isapprox(s, [0; 0], atol = tol_erreur))
end
@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
@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
@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
println("Cauchy 4 = ", sol)
@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
println("Cauchy 5= ", sol)
@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
println("Cauchy 6= ", sol)
@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
println("Cauchy 6= ", sol)
@test (e == -1) && (isapprox(s,sol,atol=tol_erreur)) # sol = [-0.9230769230769234; -0.30769230769230776]
end
end
@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
@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
@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
println("Cauchy 4 = ", sol)
@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
println("Cauchy 5= ", sol)
@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
println("Cauchy 6= ", sol)
@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
println("Cauchy 6= ", sol)
@test (e == -1) && (isapprox(s, sol, atol = tol_erreur)) # sol = [-0.9230769230769234; -0.30769230769230776]
end
end
end