Skip to content
Snippets Groups Projects
Commit b7be7c3a authored by Olivier Cots's avatar Olivier Cots
Browse files

up 2024-2025

parent 6a9e324f
Branches
No related tags found
No related merge requests found
Manifest.toml
/docs/Manifest.toml
.ipynb_checkpoints
.DS_Store
......@@ -13,7 +13,7 @@ Les sujets liés au projet se trouvent dans les notebooks du répertoire `notebo
* Régions de confiance
* Lagrangien augmenté
Pour réaliser le projet vous aurez besoin de cette [documentation](https://gitlab.irit.fr/toc/etu-n7/projet-optinum/-/raw/master/doc-projet.pdf) qui décrit les différents algorithmes à implémenter.
Pour réaliser le projet vous aurez besoin de cette [documentation]([doc-projet.pdf](https://gitlab.irit.fr/toc/etu-n7/projet-optinum/-/raw/master/doc-projet.pdf)) qui décrit les différents algorithmes à implémenter.
## Installation de Julia
......
No preview for this file type
......@@ -62,15 +62,15 @@
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.8.2",
"display_name": "Julia 1.9.0",
"language": "julia",
"name": "julia-1.8"
"name": "julia-1.9"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.8.2"
"version": "1.9.0"
}
},
"nbformat": 4,
......
%% Cell type:markdown id: tags:
<center>
<h1> TP-Projet d'optimisation numérique </h1>
<h1> Algorithme de Newton </h1>
</center>
%% Cell type:markdown id: tags:
## Implémentation
1. Coder l’algorithme de Newton dans le fichier `src/newton.jl` en respectant la spécification donnée dans ce même fichier ;
2. Exécuter les tests ci-dessous et vérifier qu'ils passent.
Pour les tests, nous avons défini les fonctions suivantes $f_1 \colon \mathbb{R}^3 \to \mathbb{R}$
et $f_2 \colon \mathbb{R}^2 \to \mathbb{R}$.
$$
f_{1}(x_1, x_2, x_3) = 2 (x_1 +x_2 + x_3 -3)^2 + (x_1-x_2)^2 + (x_2 - x_3)^2
$$
et
$$
f_{2}(x_1,x_2) = 100(x_2-x_1^2)^2 + (1-x_1)^2.
$$
**Remarque.** On peut retrouver ces fonctions dans le fichier `test/fonctions_de_tests.jl`.
%% Cell type:code id: tags:
``` julia
include("../src/newton.jl") # votre algorithme de Newton
include("../test/tester_newton.jl") # la fonction pour tester votre algorithme de Newton
#
afficher = false # si true, alors affiche les résultats des algorithmes
#
tester_newton(newton, afficher); # tester l'algorithme de Newton
```
%% Output
Affichage des résultats des algorithmes : false
Test Summary: | Pass Total Time
algorithme de Newton |  19  19 0.6s
%% Cell type:code id: tags:
``` julia
include("../src/newton.jl") # votre algorithme de Newton
include("../test/fonctions_de_tests.jl") # pour avoir la fonction d'affichage des résultats
# Fonction f0
# -----------
f0(x) = sin(x)
grad_f0(x) = cos(x) # la gradient de la fonction f0
hess_f0(x) = -sin(x) # la hessienne de la fonction f0
solution = -pi/2
x0 = solution
x_sol, f_sol, flag, nb_iters = newton(f0, grad_f0, hess_f0, x0)
afficher_resultats("Newton", "f0", x0, x_sol, f_sol, flag, nb_iters, solution)
x0 = -pi/2+0.5
x_sol, f_sol, flag, nb_iters = newton(f0, grad_f0, hess_f0, x0)
afficher_resultats("Newton", "f0", x0, x_sol, f_sol, flag, nb_iters, solution)
x0 = pi/2
x_sol, f_sol, flag, nb_iters = newton(f0, grad_f0, hess_f0, x0)
afficher_resultats("Newton", "f0", x0, x_sol, f_sol, flag, nb_iters, solution)
```
%% Output
-------------------------------------------------------------------------
Résultats de : Newton appliqué à f0:
* x0 = -1.5707963267948966
* x_sol = -1.5707963267948966
* f(x_sol) = -1.0
* nb_iters = 0
* flag = 0
* solution = -1.5707963267948966
-------------------------------------------------------------------------
Résultats de : Newton appliqué à f0:
* x0 = -1.0707963267948966
* x_sol = -1.5707963267949088
* f(x_sol) = -1.0
* nb_iters = 3
* flag = 0
* solution = -1.5707963267948966
-------------------------------------------------------------------------
Résultats de : Newton appliqué à f0:
* x0 = 1.5707963267948966
* x_sol = 1.5707963267948966
* f(x_sol) = 1.0
* nb_iters = 0
* flag = 0
* solution = -1.5707963267948966
%% Cell type:markdown id: tags:
## Interprétation
1. Justifier les résultats obtenus pour l'exemple $f_0$ ci-dessus;
2. Justifier que l’algorithme implémenté converge en une itération pour $f_{1}$;
3. Justifier que l’algorithme puisse ne pas converger pour $f_{2}$ avec certains points initiaux.
**Remarque.** Vous pouvez mettre `affiche=true` dans les tests de l'algorithme de Newton pour
vous aider.
%% Cell type:code id: tags:
``` julia
```
......
......@@ -171,15 +171,15 @@
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.8.2",
"display_name": "Julia 1.9.0",
"language": "julia",
"name": "julia-1.8"
"name": "julia-1.9"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.8.2"
"version": "1.9.0"
}
},
"nbformat": 4,
......
......@@ -18,7 +18,7 @@ x011 = [1; 0; 0]
x012 = [10; 3; -2.2]
x021 = [-1.2; 1]
x022 = [10; 0]
x023 = [0; 1/200 + 1/10^12]
x023 = [0; 1/200 + 1/10^18]
# les points initiaux utilisés dans les problèmes sans contraintes
pts1 = Pts_sans_contraintes(x011,x012,x021,x022,x023)
......
......@@ -3,8 +3,8 @@ using Test
function tester_cauchy(cauchy::Function)
@testset "Pas de Cauchy" begin
@test 1==1
Test.@testset "Pas de Cauchy" begin
Test.@test 1==1
end
end
\ No newline at end of file
......@@ -18,48 +18,48 @@ function tester_gct(gct::Function)
# Tolérance utilisé dans les tests
tol_test = 1e-3
@testset "Gradient conjugué tronqué" begin
Test.@testset "Gradient conjugué tronqué" begin
# le cas de test 1
g = [0 ; 0]
H = [7 0 ; 0 2]
Δ = 1
s = gct(g,H,Δ)
@test s [0.0 ; 0.0] atol = tol_test
Test.@test s [0.0 ; 0.0] atol = tol_test
# le cas de test 2 H definie positive
g = [6 ; 2]
H = [7 0 ; 0 2]
Δ = 0.5 # sol = pas de Cauchy
s = gct(g,H,Δ)
@test s -Δ*g/norm(g) atol = tol_test
Test.@test s -Δ*g/norm(g) atol = tol_test
Δ = 1.2 # saturation à la 2ieme itération
s = gct(g,H,Δ)
@test s [-0.8740776099190263, -0.8221850958502244] atol = tol_test
Test.@test s [-0.8740776099190263, -0.8221850958502244] atol = tol_test
Δ = 3 # sol = min global
s = gct(g,H,Δ)
@test s -H\g atol = tol_test
Test.@test s -H\g atol = tol_test
# le cas test 2 bis matrice avec 1 vp < 0 et 1 vp > 0
g = [1,2]
H = [1 0 ; 0 -1]
Δ = 1. # g^T H g < 0 première direction concave
s = gct(g,H,Δ)
@test s -Δ*g/norm(g) atol = tol_test
Test.@test s -Δ*g/norm(g) atol = tol_test
g = [1,0]
Δ = 0.5 # g^T H g > 0 sol pas de Cauchy
s = gct(g,H,Δ)
@test s -Δ*g/norm(g) atol = tol_test
Test.@test s -Δ*g/norm(g) atol = tol_test
g = [2,1] # g^T H g > 0 sol à l'itération 2, saturation
Δ = 6
s = gct(g,H,Δ)
@test isapprox(s, [0.48997991959774634, 5.979959839195494], atol = tol_test) || isapprox(s, [-4.489979919597747, -3.979959839195493], atol = tol_test)
Test.@test isapprox(s, [0.48997991959774634, 5.979959839195494], atol = tol_test) || isapprox(s, [-4.489979919597747, -3.979959839195493], atol = tol_test)
# le cas de test 5
g = [2 ; 3]
H = [4 6 ; 6 5]
Δ = 3
s = gct(g,H,Δ)
@test s [1.9059020876695578 ; -2.3167946029410595] atol = tol_test
Test.@test s [1.9059020876695578 ; -2.3167946029410595] atol = tol_test
# le cas de test 6
# Le pas de Cauchy conduit à un gient nul en 1 itération
......@@ -67,6 +67,6 @@ function tester_gct(gct::Function)
H = [4 0 ; 0 -15]
Δ = 2
s = gct(g,H,Δ)
@test s [-0.5 ; 0.0] atol = tol_test
Test.@test s [-0.5 ; 0.0] atol = tol_test
end
end
......@@ -42,9 +42,9 @@ function tester_lagrangien_augmente(LA::Function, afficher::Bool)
x03 = pts2.x03
x04 = pts2.x04
@testset "Lagrangien augmenté " begin
Test.@testset "Lagrangien augmenté " begin
@testset "Avec $algo" for algo in algos
Test.@testset "Avec $algo" for algo in algos
# le cas de test 1
x0 = x01
......@@ -52,7 +52,7 @@ function tester_lagrangien_augmente(LA::Function, afficher::Bool)
if (afficher)
afficher_resultats("LA et " * algo, "f1", x0, x_sol, f_sol, flag, nb_iters, sol_fct1_augm)
end
@test x_sol sol_fct1_augm atol=tol_erreur
Test.@test x_sol sol_fct1_augm atol=tol_erreur
# le cas de test 2
x0 = x02
......@@ -60,7 +60,7 @@ function tester_lagrangien_augmente(LA::Function, afficher::Bool)
if (afficher)
afficher_resultats("LA et " * algo, "f1", x0, x_sol, f_sol, flag, nb_iters, sol_fct1_augm)
end
@test x_sol sol_fct1_augm atol=tol_erreur
Test.@test x_sol sol_fct1_augm atol=tol_erreur
# le cas de test 3
x0 = x03
......@@ -68,7 +68,7 @@ function tester_lagrangien_augmente(LA::Function, afficher::Bool)
if (afficher)
afficher_resultats("LA et " * algo, "f2", x0, x_sol, f_sol, flag, nb_iters, sol_fct2_augm)
end
@test x_sol sol_fct2_augm atol=tol_erreur
Test.@test x_sol sol_fct2_augm atol=tol_erreur
# le cas de test 4
x0 = x04
......@@ -76,7 +76,7 @@ function tester_lagrangien_augmente(LA::Function, afficher::Bool)
if (afficher)
afficher_resultats("LA et " * algo, "f2", x0, x_sol, f_sol, flag, nb_iters, sol_fct2_augm)
end
@test x_sol sol_fct2_augm atol=tol_erreur
Test.@test x_sol sol_fct2_augm atol=tol_erreur
end
......
using Test
using LinearAlgebra
include("../test/fonctions_de_tests.jl")
"""
......@@ -28,104 +29,104 @@ function tester_newton(newton::Function, afficher::Bool)
epsilon = 1.0
#
@testset "algorithme de Newton" begin
@testset "test f1 : x0 = solution" begin
Test.@testset "algorithme de Newton" begin
Test.@testset "test f1 : x0 = solution" begin
x_sol, f_sol, flag, nb_iters, _ = newton(fct1, grad_fct1, hess_fct1, sol_exacte_fct1,
max_iter=max_iter, tol_abs=tol_abs, tol_rel=tol_rel, epsilon=epsilon)
if (afficher)
afficher_resultats("newton", "f1", sol_exacte_fct1, x_sol, f_sol, flag, nb_iters, sol_exacte_fct1)
end
@testset "solution" begin
@test x_sol sol_exacte_fct1 atol=tol_erreur
Test.@testset "solution" begin
Test.@test x_sol sol_exacte_fct1 atol=tol_erreur
end
@testset "itération" begin
@test nb_iters == 0
Test.@testset "itération" begin
Test.@test nb_iters == 0
end
@testset "flag" begin
@test flag == 0
Test.@testset "flag" begin
Test.@test flag == 0
end
end
@testset "test f1 : x0 = x011" begin
Test.@testset "test f1 : x0 = x011" begin
x_sol, f_sol, flag, nb_iters, _ = newton(fct1, grad_fct1, hess_fct1, pts1.x011,
max_iter=max_iter, tol_abs=tol_abs, tol_rel=tol_rel, epsilon=epsilon)
if (afficher)
afficher_resultats("newton", "f1", pts1.x011, x_sol, f_sol, flag, nb_iters, sol_exacte_fct1)
end
@testset "solution" begin
@test x_sol sol_exacte_fct1 atol=tol_erreur
Test.@testset "solution" begin
Test.@test x_sol sol_exacte_fct1 atol=tol_erreur
end
@testset "itération" begin
@test nb_iters == 1
Test.@testset "itération" begin
Test.@test nb_iters == 1
end
@testset "flag" begin
@test flag == 0
Test.@testset "flag" begin
Test.@test flag == 0
end
end
@testset "test f1 : x0 = x012" begin
Test.@testset "test f1 : x0 = x012" begin
x_sol, f_sol, flag, nb_iters, _ = newton(fct1, grad_fct1, hess_fct1, pts1.x012,
max_iter=max_iter, tol_abs=tol_abs, tol_rel=tol_rel, epsilon=epsilon)
if (afficher)
afficher_resultats("newton", "f1", pts1.x012, x_sol, f_sol, flag, nb_iters, sol_exacte_fct1)
end
@testset "solution" begin
@test x_sol sol_exacte_fct1 atol = tol_erreur
Test.@testset "solution" begin
Test.@test x_sol sol_exacte_fct1 atol = tol_erreur
end
@testset "itération" begin
@test nb_iters == 1
Test.@testset "itération" begin
Test.@test nb_iters == 1
end
@testset "flag" begin
@test flag == 0
Test.@testset "flag" begin
Test.@test flag == 0
end
end
@testset "test f2 : x0 = x021" begin
Test.@testset "test f2 : x0 = x021" begin
x_sol, f_sol, flag, nb_iters, _ = newton(fct2, grad_fct2, hess_fct2, pts1.x021,
max_iter=max_iter, tol_abs=tol_abs, tol_rel=tol_rel, epsilon=epsilon)
if (afficher)
afficher_resultats("newton", "f2", pts1.x021, x_sol, f_sol, flag, nb_iters, sol_exacte_fct2)
end
@testset "solution" begin
@test x_sol sol_exacte_fct2 atol = tol_erreur
Test.@testset "solution" begin
Test.@test x_sol sol_exacte_fct2 atol = tol_erreur
end
@testset "itération" begin
@test nb_iters == 6
Test.@testset "itération" begin
Test.@test nb_iters == 6
end
@testset "flag" begin
@test flag == 0
Test.@testset "flag" begin
Test.@test flag == 0
end
end
@testset "test f2 : x0 = x022" begin
Test.@testset "test f2 : x0 = x022" begin
x_sol, f_sol, flag, nb_iters, _ = newton(fct2, grad_fct2, hess_fct2, pts1.x022,
max_iter=max_iter, tol_abs=tol_abs, tol_rel=tol_rel, epsilon=epsilon)
if (afficher)
afficher_resultats("newton", "f2", pts1.x022, x_sol, f_sol, flag, nb_iters, sol_exacte_fct2)
end
@testset "solution" begin
@test x_sol sol_exacte_fct2 atol = tol_erreur
Test.@testset "solution" begin
Test.@test x_sol sol_exacte_fct2 atol = tol_erreur
end
@testset "itération" begin
@test nb_iters == 5
Test.@testset "itération" begin
Test.@test nb_iters == 5
end
@testset "flag" begin
@test flag == 0
Test.@testset "flag" begin
Test.@test flag == 0
end
end
@testset "test f2 : x0 = x023" begin
Test.@testset "test f2 : x0 = x023" begin
x_sol, f_sol, flag, nb_iters, _ = newton(fct2, grad_fct2, hess_fct2, pts1.x023,
max_iter=1, tol_abs=tol_abs, tol_rel=tol_rel, epsilon=epsilon)
if (afficher)
afficher_resultats("newton", "f2", pts1.x023, x_sol, f_sol, flag, nb_iters, sol_exacte_fct2)
end
@testset "solution" begin
@test x_sol [-4.99999958629818e9, 8.673617379884035e-19] atol = tol_erreur
Test.@testset "solution" begin
Test.@test norm(x_sol) > 1e6
end
@testset "flag" begin
@test flag == 3
Test.@testset "flag" begin
Test.@test flag == 3
end
@testset "itération" begin
@test nb_iters == 1
Test.@testset "itération" begin
Test.@test nb_iters == 1
end
@testset "exception" begin
@test_throws SingularException _, _, _, _ = newton(fct2, grad_fct2, hess_fct2, pts1.x023,
Test.@testset "exception" begin
Test.@test_throws SingularException _, _, _, _ = newton(fct2, grad_fct2, hess_fct2, pts1.x023,
max_iter=max_iter, tol_abs=tol_abs, tol_rel=tol_rel, epsilon=epsilon)
end
end
......
......@@ -25,52 +25,52 @@ function tester_rc_cauchy(rc::Function, afficher::Bool)
algo_pas = "cauchy"
# l'ensemble de tests
@testset "RC et pas de Cauchy " begin
Test.@testset "RC et pas de Cauchy " begin
# cas de test 1
x_sol, f_sol, flag, nb_iters, _ = rc(fct1, grad_fct1, hess_fct1, pts1.x011, algo_pas=algo_pas)
if (afficher)
afficher_resultats("RC et " * algo_pas, "f1", pts1.x011, x_sol, f_sol, flag, nb_iters, sol_exacte_fct1)
end
@test x_sol sol_exacte_fct1 atol=tol_erreur
@test flag == 2
@test nb_iters == 33
Test.@test x_sol sol_exacte_fct1 atol=tol_erreur
Test.@test flag == 2
Test.@test nb_iters == 33
# cas de test 2
x_sol, f_sol, flag, nb_iters = rc(fct1, grad_fct1, hess_fct1, pts1.x012, algo_pas=algo_pas)
if (afficher)
afficher_resultats("RC et " * algo_pas, "f1", pts1.x012, x_sol, f_sol, flag, nb_iters, sol_exacte_fct1)
end
@test x_sol sol_exacte_fct1 atol=tol_erreur
@test flag == 2
@test nb_iters == 34
Test.@test x_sol sol_exacte_fct1 atol=tol_erreur
Test.@test flag == 2
Test.@test nb_iters == 34
# cas de test 3
x_sol, f_sol, flag, nb_iters, _ = rc(fct2, grad_fct2, hess_fct2, pts1.x021, algo_pas=algo_pas)
if (afficher)
afficher_resultats("RC et " * algo_pas, "f2", pts1.x021, x_sol, f_sol, flag, nb_iters, sol_exacte_fct2)
end
@test x_sol sol_exacte_fct2 atol=tol_erreur
@test flag == 3
@test nb_iters == 5000
Test.@test x_sol sol_exacte_fct2 atol=tol_erreur
Test.@test flag == 3
Test.@test nb_iters == 5000
# cas de test 4
x_sol, f_sol, flag, nb_iters, _ = rc(fct2, grad_fct2, hess_fct2, pts1.x022, algo_pas=algo_pas)
if (afficher)
afficher_resultats("RC et " * algo_pas, "f2", pts1.x022, x_sol, f_sol, flag, nb_iters, sol_exacte_fct2)
end
@test x_sol sol_exacte_fct2 atol=tol_erreur
@test flag == 0
@test nb_iters == 864
Test.@test x_sol sol_exacte_fct2 atol=tol_erreur
Test.@test flag == 0
Test.@test nb_iters == 864
# cas de test 5
x_sol, f_sol, flag, nb_iters, _ = rc(fct2, grad_fct2, hess_fct2, pts1.x023, algo_pas=algo_pas)
if (afficher)
afficher_resultats("RC et " * algo_pas, "f2", pts1.x023, x_sol, f_sol, flag, nb_iters, sol_exacte_fct2)
end
@test x_sol sol_exacte_fct2 atol=tol_erreur
@test flag == 2
@test nb_iters == 4512
Test.@test x_sol sol_exacte_fct2 atol=tol_erreur
Test.@test flag == 2
Test.@test nb_iters == 4512
end
end
......@@ -25,52 +25,52 @@ function tester_rc_gct(rc::Function, afficher::Bool)
algo_pas = "gct"
# l'ensemble de tests
@testset "RC et gct" begin
Test.@testset "RC et gct" begin
# cas de test 1
x_sol, f_sol, flag, nb_iters, _ = rc(fct1, grad_fct1, hess_fct1, pts1.x011, algo_pas=algo_pas)
if (afficher)
afficher_resultats("RC et " * algo_pas, "f1", pts1.x011, x_sol, f_sol, flag, nb_iters, sol_exacte_fct1)
end
@test x_sol sol_exacte_fct1 atol=tol_erreur
@test flag == 0
@test nb_iters == 1
Test.@test x_sol sol_exacte_fct1 atol=tol_erreur
Test.@test flag == 0
Test.@test nb_iters == 1
# cas de test 2
x_sol, f_sol, flag, nb_iters = rc(fct1, grad_fct1, hess_fct1, pts1.x012, algo_pas=algo_pas)
if (afficher)
afficher_resultats("RC et " * algo_pas, "f1", pts1.x012, x_sol, f_sol, flag, nb_iters, sol_exacte_fct1)
end
@test x_sol sol_exacte_fct1 atol=tol_erreur
@test flag == 0
@test nb_iters == 3
Test.@test x_sol sol_exacte_fct1 atol=tol_erreur
Test.@test flag == 0
Test.@test nb_iters == 3
# cas de test 3
x_sol, f_sol, flag, nb_iters, _ = rc(fct2, grad_fct2, hess_fct2, pts1.x021, algo_pas=algo_pas)
if (afficher)
afficher_resultats("RC et " * algo_pas, "f2", pts1.x021, x_sol, f_sol, flag, nb_iters, sol_exacte_fct2)
end
@test x_sol sol_exacte_fct2 atol=tol_erreur
@test flag == 0
@test nb_iters == 31
Test.@test x_sol sol_exacte_fct2 atol=tol_erreur
Test.@test flag == 0
Test.@test nb_iters == 31
# cas de test 4
x_sol, f_sol, flag, nb_iters, _ = rc(fct2, grad_fct2, hess_fct2, pts1.x022, algo_pas=algo_pas)
if (afficher)
afficher_resultats("RC et " * algo_pas, "f2", pts1.x022, x_sol, f_sol, flag, nb_iters, sol_exacte_fct2)
end
@test x_sol sol_exacte_fct2 atol=tol_erreur
@test flag == 0
@test nb_iters == 44
Test.@test x_sol sol_exacte_fct2 atol=tol_erreur
Test.@test flag == 0
Test.@test nb_iters == 44
# cas de test 5
x_sol, f_sol, flag, nb_iters, _ = rc(fct2, grad_fct2, hess_fct2, pts1.x023, algo_pas=algo_pas)
if (afficher)
afficher_resultats("RC et " * algo_pas, "f2", pts1.x023, x_sol, f_sol, flag, nb_iters, sol_exacte_fct2)
end
@test x_sol sol_exacte_fct2 atol=tol_erreur
@test flag == 0
@test nb_iters == 19
Test.@test x_sol sol_exacte_fct2 atol=tol_erreur
Test.@test flag == 0
Test.@test nb_iters == 19
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment