ajout du fichier notebook pour les etudiants
This commit is contained in:
parent
8cbc9b7723
commit
bad7d52d64
343
src/TP-Projet.ipynb
Normal file
343
src/TP-Projet.ipynb
Normal file
|
@ -0,0 +1,343 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"<center>\n",
|
||||
"<h1> TP-Projet d'optimisation numérique </h1>\n",
|
||||
"<h1> Année 2020-2021 - 2e année département Sciences du Numérique </h1>\n",
|
||||
"<h1> Nom: </h1>\n",
|
||||
"<h1> Prénom: </h1> \n",
|
||||
"</center>"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Algorithme de Newton\n",
|
||||
"## Implémentation \n",
|
||||
" \n",
|
||||
"1. Coder l’algorithme de Newton local tel que décrit dans la section *Algorithme de Newton* (fichier `Algorithme_De_Newton.jl`)\n",
|
||||
"\n",
|
||||
"2. Tester l’algorithme sur les fonctions $f_{1}$ , $f_{2}$ avec les points initiaux $x_{011}$ , $x_{012}$ (pour $f_{1}$ ) et $x_{021}$ , $x_{022}$ , $x_{023}$ (pour $f_{2}$ ) donnés en Annexe A."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 28,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"-------------------------------------------------------------------------\n",
|
||||
"\u001b[34m\u001b[1mRésultats de : Newton appliqué à f2 au point initial [0.0, 0.0050000000010000005]:\u001b[22m\u001b[39m\n",
|
||||
" * xsol = [-4.999999586298179e9, 2.499999586298196e19]\n",
|
||||
" * f(xsol) = 2.499999587298196e19\n",
|
||||
" * nb_iters = 1\n",
|
||||
" * flag = 3\n",
|
||||
" * sol_exacte : [1, 1]\n",
|
||||
"hess_f2(xmin) = [1.9999996690385565e22 1.9999998345192715e12; 1.9999998345192715e12 200.0]\n",
|
||||
"cond(H) = 7.036873253299342e35\n",
|
||||
"-------------------------------------------------------------------------\n",
|
||||
"\u001b[34m\u001b[1mRésultats de : Newton appliqué à f2 au point initial [0.0, 0.0050000000010000005]:\u001b[22m\u001b[39m\n",
|
||||
" * xsol = [-5.00000310473568e9, 2.5000031047354057e19]\n",
|
||||
" * f(xsol) = 2.5015352823034896e19\n",
|
||||
" * nb_iters = 2\n",
|
||||
" * flag = 3\n",
|
||||
" * sol_exacte : [1, 1]\n",
|
||||
"hess_f2(xmin) = [2.0000024837898096e22 2.000001241894272e12; 2.000001241894272e12 200.0]\n",
|
||||
"cond(H) = 4.0441857223131525e32\n",
|
||||
"-------------------------------------------------------------------------\n",
|
||||
"\u001b[34m\u001b[1mRésultats de : Newton appliqué à f2 au point initial [0.0, 0.0050000000010000005]:\u001b[22m\u001b[39m\n",
|
||||
" * xsol = [-5.00000310271359e9, 2.5000031027145523e19]\n",
|
||||
" * f(xsol) = 2.500003103714553e19\n",
|
||||
" * nb_iters = 3\n",
|
||||
" * flag = 3\n",
|
||||
" * sol_exacte : [1, 1]\n",
|
||||
"hess_f2(xmin) = [2.0000024821716417e22 2.0000012410854358e12; 2.0000012410854358e12 200.0]\n",
|
||||
"cond(H) = Inf\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"ename": "SingularException",
|
||||
"evalue": "SingularException(2)",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"SingularException(2)",
|
||||
"",
|
||||
"Stacktrace:",
|
||||
" [1] checknonsingular at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/factorization.jl:19 [inlined]",
|
||||
" [2] checknonsingular at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/factorization.jl:21 [inlined]",
|
||||
" [3] lu!(::Array{Float64,2}, ::Val{true}; check::Bool) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/lu.jl:85",
|
||||
" [4] #lu#142 at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/lu.jl:273 [inlined]",
|
||||
" [5] lu at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/lu.jl:272 [inlined] (repeats 2 times)",
|
||||
" [6] \\(::Array{Float64,2}, ::Array{Float64,1}) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.4/LinearAlgebra/src/generic.jl:1116",
|
||||
" [7] Algorithme_De_Newton(::typeof(f2), ::typeof(grad_f2), ::typeof(hess_f2), ::Array{Float64,1}, ::Array{Float64,1}) at /Users/gergaud/ENS/optinum/TP/OptinumProf/src/Algorithme_De_Newton.jl:61",
|
||||
" [8] top-level scope at ./In[28]:70"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"#using Pkg; Pkg.add(\"LinearAlgebra\"); Pkg.add(\"Markdown\")\n",
|
||||
"# using Documenter\n",
|
||||
"using LinearAlgebra\n",
|
||||
"using Markdown # Pour que les docstrings en début des fonctions ne posent\n",
|
||||
" # pas de soucis. Ces docstrings sont utiles pour générer \n",
|
||||
" # la documentation sous GitHub\n",
|
||||
"include(\"Algorithme_De_Newton.jl\")\n",
|
||||
"\n",
|
||||
"# Affichage les sorties de l'algorithme des Régions de confiance\n",
|
||||
"function my_afficher_resultats(algo,nom_fct,point_init,xmin,fxmin,flag,sol_exacte,nbiters)\n",
|
||||
"\tprintln(\"-------------------------------------------------------------------------\")\n",
|
||||
"\tprintstyled(\"Résultats de : \",algo, \" appliqué à \",nom_fct, \" au point initial \", point_init, \":\\n\",bold=true,color=:blue)\n",
|
||||
"\tprintln(\" * xsol = \",xmin)\n",
|
||||
"\tprintln(\" * f(xsol) = \",fxmin)\n",
|
||||
"\tprintln(\" * nb_iters = \",nbiters)\n",
|
||||
"\tprintln(\" * flag = \",flag)\n",
|
||||
"\tprintln(\" * sol_exacte : \", sol_exacte)\n",
|
||||
"end\n",
|
||||
"\n",
|
||||
"# Fonction f0\n",
|
||||
"# -----------\n",
|
||||
"f0(x) = sin(x)\n",
|
||||
"# la gradient de la fonction f0\n",
|
||||
"grad_f0(x) = cos(x)\n",
|
||||
"# la hessienne de la fonction f0\n",
|
||||
"hess_f0(x) = -sin(x)\n",
|
||||
"sol_exacte = -pi/2\n",
|
||||
"x0 = -pi/2+0.5\n",
|
||||
"options = []\n",
|
||||
"xmin,f_min,flag,nb_iters = Algorithme_De_Newton(f0,grad_f0,hess_f0,x0,options)\n",
|
||||
"#my_afficher_resultats(\"Newton\",\"f0\",x0,xmin,f_min,flag,sol_exacte,nb_iters)\n",
|
||||
"\n",
|
||||
"# Fonction f1\n",
|
||||
"# -----------\n",
|
||||
"f1(x) = 2*(x[1]+x[2]+x[3]-3)^2 + (x[1]-x[2])^2 + (x[2]-x[3])^2\n",
|
||||
"# la gradient de la fonction fct1\n",
|
||||
"function grad_f1(x)\n",
|
||||
" y1 = 4*(x[1]+x[2]+x[3]-3) + 2*(x[1]-x[2])\n",
|
||||
" y2 = 4*(x[1]+x[2]+x[3]-3) - 2*(x[1]-x[2]) +2*(x[2]-x[3])\n",
|
||||
" y3 = 4*(x[1]+x[2]+x[3]-3) - 2*(x[2]-x[3])\n",
|
||||
" return [y1;y2;y3]\n",
|
||||
"end\n",
|
||||
"# la hessienne de la fonction fct1\n",
|
||||
"hess_f1(x) = [6 2 4;2 8 2;4 2 6]\n",
|
||||
"sol_exacte = [1;1;1]\n",
|
||||
"options = []\n",
|
||||
"x0 = [1; 0; 0]\n",
|
||||
"xmin,f_min,flag,nb_iters = Algorithme_De_Newton(f1,grad_f1,hess_f1,x0,options)\n",
|
||||
"#my_afficher_resultats(\"Newton\",\"f1\",x0,xmin,f_min,flag,sol_exacte,nb_iters)\n",
|
||||
"x0 = [10; 3; -2.2]\n",
|
||||
"xmin,f_min,flag,nb_iters = Algorithme_De_Newton(f1,grad_f1,hess_f1,x0,options)\n",
|
||||
"#my_afficher_resultats(\"Newton\",\"f1\",x0,xmin,f_min,flag,sol_exacte,nb_iters)\n",
|
||||
"\n",
|
||||
"f2(x)=100*(x[2]-x[1]^2)^2+(1-x[1])^2\n",
|
||||
"grad_f2(x)=[-400*x[1]*(x[2]-x[1]^2)-2*(1-x[1]) ; 200*(x[2]-x[1]^2)]\n",
|
||||
"hess_f2(x)=[-400*(x[2]-3*x[1]^2)+2 -400*x[1];-400*x[1] 200]\n",
|
||||
"sol_exacte = [1;1]\n",
|
||||
"options = []\n",
|
||||
"x0 = [-1.2; 1]\n",
|
||||
"x0 = sol_exacte\n",
|
||||
"xmin,f_min,flag,nb_iters = Algorithme_De_Newton(f2,grad_f2,hess_f2,x0,options)\n",
|
||||
"#my_afficher_resultats(\"Newton\",\"f2\",x0,xmin,f_min,flag,sol_exacte,nb_iters)\n",
|
||||
"x0 = [10.; 0]\n",
|
||||
"xmin,f_min,flag,nb_iters = Algorithme_De_Newton(f2,grad_f2,hess_f2,x0,options)\n",
|
||||
"#my_afficher_resultats(\"Newton\",\"f2\",x0,xmin,f_min,flag,sol_exacte,nb_iters)\n",
|
||||
"x0 = [0 ; 1/200+1/1.e12]\n",
|
||||
"options = [1 ; sqrt(eps()) ; 1e-15]\n",
|
||||
"for i in 1:5\n",
|
||||
" options[1] = i\n",
|
||||
" xmin,f_min,flag,nb_iters = Algorithme_De_Newton(f2,grad_f2,hess_f2,x0,options)\n",
|
||||
" my_afficher_resultats(\"Newton\",\"f2\",x0,xmin,f_min,flag,sol_exacte,nb_iters)\n",
|
||||
" H = hess_f2(xmin)\n",
|
||||
" println(\"hess_f2(xmin) = \",H)\n",
|
||||
" println(\"cond(H) = \", cond(H))\n",
|
||||
"end\n",
|
||||
"println(grad_f2(x0))\n",
|
||||
"println(hess_f2(x0))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Interprétation \n",
|
||||
"\n",
|
||||
"justifier que\n",
|
||||
"\n",
|
||||
"1. l’algorithme implémenté converge en une itération pour $f_{1}$;\n",
|
||||
"\n",
|
||||
"2. l’algorithme puisse ne pas converger pour $f_{2}$ avec certains points initiaux."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Vos réponses?\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Régions de confiance avec pas de cauchy \n",
|
||||
"\n",
|
||||
"## Implémentation \n",
|
||||
"\n",
|
||||
"1. Coder l'algorithme du pas de Cauchy d’un sous-problème de\n",
|
||||
"régions de confiance (fichier `Pas_De_Cauchy.jl`). Tester sur les quadratiques proposées en Annexe B.\n",
|
||||
"\n",
|
||||
"2. Coder l'algorithme de régions de confiance (fichier `Regions_De_Confiance.jl`). Tester sur les problèmes de l’Annexe A."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 29,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Vos tests"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Interprétation \n",
|
||||
"\n",
|
||||
"1. Quelle relation lie la fonction test $f_1$ et son modèle de Taylor à l’ordre 2 ? Comparer alors les performances de Newton et RC-Pas de Cauchy sur cette fonction.\n",
|
||||
"\n",
|
||||
"2. Le rayon initial de la région de confiance est un paramètre important dans l’analyse\n",
|
||||
"de la performance de l’algorithme. Sur quel(s) autre(s) paramètre(s) peut-on jouer\n",
|
||||
"pour essayer d’améliorer cette performance ? Étudier l’influence d’au moins deux de\n",
|
||||
"ces paramètres."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Vos réponses?\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Régions de confiance avec gradient conjugué tronqué\n",
|
||||
"\n",
|
||||
"## Implémentation \n",
|
||||
"\n",
|
||||
"1. Implémenter l’algorithme du Gradient Conjugué Tronqué, en se basant sur le cours (fichier `Gradient_Conjugue_Tronque.jl`).\n",
|
||||
"On validera les résultats sur les fonctions de l’Annexe C.\n",
|
||||
"\n",
|
||||
"2. Intégrer finalement l’algorithme du Gradient Conjugué Tronqué dans le code de\n",
|
||||
"régions de confiance, et appliquer ce code pour résoudre les exemples proposés en\n",
|
||||
"Annexe A."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 30,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Vos tests"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Interprétation \n",
|
||||
"\n",
|
||||
"1. Comparer la décroissance obtenue avec celle du pas de Cauchy, en retournant, dans\n",
|
||||
"un premier temps le dernier itéré admissible à courbure positive (c’est à dire, que si\n",
|
||||
"l’une ou l’autre des deux conditions (b) ou (d) sont rencontrées dans l’algorithme 3,\n",
|
||||
"alors on ne calcule pas ``σ_{j}`` et on retourne le dernier itéré ``s_{j}`` directement).\n",
|
||||
"\n",
|
||||
"2. Comparer la décroissance obtenue avec celle du pas de Cauchy, en imposant la sortie\n",
|
||||
"dans l’algorithme 3 au bout d’une itération seulement. Que remarquez vous ?\n",
|
||||
"\n",
|
||||
"3. Comparer la décroissance obtenue avec celle du pas de Cauchy dans le cas général.\n",
|
||||
"\n",
|
||||
"4. Quels sont les avantages et inconvénients des deux approches ?"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Vos réponses?\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# Lagrangien augmenté\n",
|
||||
"\n",
|
||||
"## Implémentation\n",
|
||||
"1.Choisir des critères d’arrêt pour la convergence de l'algorithme.\n",
|
||||
"\n",
|
||||
"2.Implémenter l'algorithme du lagrangien augmenté, en utilisant les différentes méthodes\n",
|
||||
"qui ont été vues en première partie pour la résolution de la suite de problémes sans\n",
|
||||
"contraintes (fichier `Lagrangien_Augmente.jl`)\n",
|
||||
" \n",
|
||||
"3.Tester les différentes variantes sur les problèmes en Annexe D.\n",
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 31,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Vos tests"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Interprétation\n",
|
||||
" 1.Commenter les résultats obtenus, en étudiant notamment les valeurs de $\\lambda_k$ et $\\mu_k$.\n",
|
||||
" \n",
|
||||
" 2.Étudier l'influence du paramètre $\\tau$ dans la performance de l'algorithme.\n",
|
||||
" \n",
|
||||
" 3.**Supplémentaire** : \n",
|
||||
" Que proposez-vous comme méthode pour la résolution des problèmes avec\n",
|
||||
" des contraintes à la fois d'égalité et d'inégalité ? Implémenter (si le temps le permet)\n",
|
||||
" ce nouvel algorithme\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Vos réponses?\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Julia 1.4.1",
|
||||
"language": "julia",
|
||||
"name": "julia-1.4"
|
||||
},
|
||||
"language_info": {
|
||||
"file_extension": ".jl",
|
||||
"mimetype": "application/julia",
|
||||
"name": "julia",
|
||||
"version": "1.4.1"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 4
|
||||
}
|
Loading…
Reference in a new issue