Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
update notebooks to fix latex expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpython committed Sep 28, 2015
1 parent d7636fa commit e1ac8cf
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 80 deletions.
8 changes: 3 additions & 5 deletions _doc/notebooks/td1a/td1a_cenonce_session7.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,7 @@
"\n",
"Après avoir avoir trié les skieurs et les paires par tailles croissantes. On définit :\n",
"\n",
"$$\n",
"p(n,m) = \\sum_{i=1}^{n} \\left| t_i - s_{\\sigma_m^*(i)} \\right| \n",
"$$ \n",
"$p(n,m) = \\sum_{i=1}^{n} \\left| t_i - s_{\\sigma_m^*(i)} \\right|$ \n",
"\n",
"Où $\\sigma_m^*$ est le meilleur choix possible de $n$ paires de skis parmi les $m$ premières. Exprimer $p(n,m)$ par récurrence (en fonction de $p(n,m-1)$ et $p(n-1,m-1)$. On suppose qu'un skieur sans paire de ski correspond au cas où la paire est de taille nulle."
]
Expand Down Expand Up @@ -572,11 +570,11 @@
"\n",
"**Etape 0**\n",
"\n",
"$$d(v) = \\infty, \\, \\forall v \\in V$$\n",
"$d(v) = \\infty, \\, \\forall v \\in V$\n",
"\n",
"**Etape $n$**\n",
"\n",
"$$ d(v) = \\left \\{ \\begin{array}{ll} 0 & \\text{si } v = \\text{Charleville-Mezieres} \\\\ \\min \\{ d(w) + e(v,w) \\, | \\, w \\in V \\} & \\text{sinon} \\end{array} \\right . $$\n",
"$ d(v) = \\left \\{ \\begin{array}{ll} 0 & \\text{si } v = \\text{Charleville-Mezieres} \\\\ \\min \\{ d(w) + e(v,w) \\, | \\, w \\in V \\} & \\text{sinon} \\end{array} \\right . $\n",
"\n",
"\n",
"Tant que l'étape $n$ continue à faire des mises à jour ($\\sum_v d(v)$ diminue), on répète l'étape $n$. Ce même algorithme peut être appliqué pour déterminer le [degré de séparation](http://www.atlantico.fr/decryptage/theorie-six-degres-separation-relations-entre-individus-facebook-nombre-amis-229803.html) dans un réseau social. L'agorithme s'applique presque tel quel à condition de définir ce que sont une ville et une distance entre villes dans ce nouveau graphe. Vous pouvez tester vos idées sur cet exemple de graphe [Social circles: Facebook](http://snap.stanford.edu/data/egonets-Facebook.html). L'algorithme de [Dikjstra](http://fr.wikipedia.org/wiki/Algorithme_de_Dijkstra) calcule le plus court chemin entre deux noeuds d'un graphe, l'algorithme de [Bellman-Ford](http://fr.wikipedia.org/wiki/Algorithme_de_Bellman-Ford) est une variante qui calcule toutes les distances des plus courts chemin entre deux noeuds d'un graphe."
Expand Down
6 changes: 3 additions & 3 deletions _doc/notebooks/td1a/td1a_cenonce_session8.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 1,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -39,7 +39,7 @@
" var memo_level = 1;\n",
" var href;\n",
" var tags = [];\n",
" for (i = lfirst; i <= llast; i++) {\n",
" for (i = 0; i <= llast; i++) {\n",
" tags.push(\"h\" + i);\n",
" }\n",
"\n",
Expand Down Expand Up @@ -122,7 +122,7 @@
"<IPython.core.display.HTML object>"
]
},
"execution_count": 4,
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
Expand Down
172 changes: 147 additions & 25 deletions _doc/notebooks/td1a/td1a_cenonce_session9.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,149 @@
"# TD 9 : Optimisation sous contrainte"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
"<b>Plan</b>\n",
"<div id=\"my_menu_id\">run previous cell, wait for 2 seconds</div>\n",
"<script>\n",
"function repeat_indent_string(n){\n",
" var a = \"\" ;\n",
" for ( ; n > 0 ; --n) {\n",
" a += \" \";\n",
" }\n",
" return a;\n",
"}\n",
"var update_menu_string = function(begin, lfirst, llast, sformat, send) {\n",
" var anchors = document.getElementsByClassName(\"section\");\n",
" if (anchors.length == 0) {\n",
" anchors = document.getElementsByClassName(\"text_cell_render rendered_html\");\n",
" }\n",
" var i,t;\n",
" var text_menu = begin;\n",
" var text_memo = \"<pre>\\nlength:\" + anchors.length + \"\\n\";\n",
" var ind = \"\";\n",
" var memo_level = 1;\n",
" var href;\n",
" var tags = [];\n",
" for (i = 0; i <= llast; i++) {\n",
" tags.push(\"h\" + i);\n",
" }\n",
"\n",
" for (i = 0; i < anchors.length; i++) {\n",
" text_memo += \"**\" + anchors[i].id + \"--\\n\";\n",
"\n",
" var child = null;\n",
" for(t = 0; t < tags.length; t++) {\n",
" var r = anchors[i].getElementsByTagName(tags[t]);\n",
" if (r.length > 0) {\n",
"child = r[0];\n",
"break;\n",
" }\n",
" }\n",
" if (child == null){\n",
" text_memo += \"null\\n\";\n",
" continue;\n",
" }\n",
"\n",
" if (anchors[i].hasAttribute(\"id\")) {\n",
" // when converted in RST\n",
" href = anchors[i].id;\n",
" text_memo += \"#1-\" + href;\n",
" // passer à child suivant (le chercher)\n",
" }\n",
" else if (child.hasAttribute(\"id\")) {\n",
" // in a notebook\n",
" href = child.id;\n",
" text_memo += \"#2-\" + href;\n",
" }\n",
" else {\n",
" text_memo += \"#3-\" + \"*\" + \"\\n\";\n",
" continue;\n",
" }\n",
" var title = child.textContent;\n",
" var level = parseInt(child.tagName.substring(1,2));\n",
"\n",
" text_memo += \"--\" + level + \"?\" + lfirst + \"--\" + title + \"\\n\";\n",
"\n",
" if ((level < lfirst) || (level > llast)) {\n",
" continue ;\n",
" }\n",
" if (title.endsWith('¶')) {\n",
" title = title.substring(0,title.length-1).replace(\"<\", \"&lt;\").replace(\">\", \"&gt;\").replace(\"&\", \"&amp;\")\n",
" }\n",
"\n",
" if (title.length == 0) {\n",
" continue;\n",
" }\n",
" while (level > memo_level) {\n",
" text_menu += \"<ul>\\n\";\n",
" memo_level += 1;\n",
" }\n",
" while (level < memo_level) {\n",
" text_menu += \"</ul>\\n\";\n",
" memo_level -= 1;\n",
" }\n",
" text_menu += repeat_indent_string(level-2) + sformat.replace(\"__HREF__\", href).replace(\"__TITLE__\", title);\n",
" }\n",
" while (1 < memo_level) {\n",
" text_menu += \"</ul>\\n\";\n",
" memo_level -= 1;\n",
" }\n",
" text_menu += send;\n",
" //text_menu += \"\\n\" + text_memo;\n",
" return text_menu;\n",
"};\n",
"var update_menu = function() {\n",
" var sbegin = \"\";\n",
" var sformat = '<li><a href=\"#__HREF__\">__TITLE__</a></li>';\n",
" var send = \"\";\n",
" var text_menu = update_menu_string(sbegin, 2, 4, sformat, send);\n",
" var menu = document.getElementById(\"my_menu_id\");\n",
" menu.innerHTML=text_menu;\n",
"};\n",
"window.setTimeout(update_menu,2000);\n",
" </script>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import pyensae\n",
"%nb_menu"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Plan\n",
"\n",
"* [Premier problème](#p1)\n",
"* [L'algorithme de Arrow-Hurwicz](#arrow)\n",
"* [Le lagrangien augmenté](#lag)\n",
"* [Prolongement 1 : inégalité](#prol1)\n",
"* [Prolongement 2 : optimisation d'une fonction linéaire](#prol2)\n",
" \n",
"Le langage Python propose des modules qui permettent de résoudre des problèmes d'optimisation sous contraintes et il n'est pas forcément nécessaire de connaître la théorie derrière les algorithmes de résolution pour s'en servir. Au cours de cette séance, on verra comment faire. Même si comprendre comment utiliser une fonction d'un module tel que [cvxopt](http://cvxopt.org/) requiert parfois un peu de temps et de lecture. \n",
"\n",
"On verra également un algorithme simple d'optimisation. C'est une bonne façon de comprendre que cela prend du temps si on veut implémenter soi-même ce type de solution tout en étant aussi rapide et efficace.\n",
"\n",
"<h2 id=\"p1\">Premier problème</h2>\n",
"On verra également un algorithme simple d'optimisation. C'est une bonne façon de comprendre que cela prend du temps si on veut implémenter soi-même ce type de solution tout en étant aussi rapide et efficace."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercice 1 : optimisation avec cvxopt\n",
"\n",
"On souhaite résoudre le problème d'optimisation suivant :\n",
"\n",
"$$ \\left \\{ \\begin{array}{l} \\min_{x,y} \\left \\{ x^2 + y^2 - xy + y \\right \\} \\\\ \\text{sous contrainte} \\; x + 2y = 1 \\end{array}\\right . $$\n",
"$ \\left \\{ \\begin{array}{l} \\min_{x,y} \\left \\{ x^2 + y^2 - xy + y \\right \\} \\\\ \\text{sous contrainte} \\; x + 2y = 1 \\end{array}\\right . $\n",
"\n",
"Le module [cvxopt](http://cvxopt.org/userguide/solvers.html\\#problems-with-nonlinear-objectives) est un des modules les plus indiqués pour résoudre ce problème. Voici quelques instructions qui l'utilisent :"
]
Expand Down Expand Up @@ -155,7 +277,7 @@
"source": [
"Cet exemple résoud le problème de minimisation suivant :\n",
"\n",
"$$ \\left \\{ \\begin{array}{l} \\min_{X} \\left\\{ - \\sum_{i=1}^n \\ln x_i \\right \\}\\\\ \\text{sous contrainte} \\; AX = b \\end{array}\\right . $$\n",
"$ \\left \\{ \\begin{array}{l} \\min_{X} \\left\\{ - \\sum_{i=1}^n \\ln x_i \\right \\}\\\\ \\text{sous contrainte} \\; AX = b \\end{array}\\right . $\n",
"\n",
"Les deux modules [numpy](http://www.numpy.org/) et [cvxopt](http://cvxopt.org/) n'utilisent pas les mêmes matrices (les mêmes objets ``matrix``) bien qu'elles portent le même nom dans les deux modules. Les fonctions de ``cvxopt`` ne fonctionnent qu'avec les matrices de ce module. Il ne faut pas oublier de convertir la matrice quand elle est décrite par une autre classe."
]
Expand Down Expand Up @@ -194,25 +316,25 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"arrow\">L'algorithme de Arrow-Hurwicz</h2>\n",
"## Exercice 2 : l'algorithme de Arrow-Hurwicz\n",
"\n",
"On cherche à résoudre le même problème avec l'algorithme de [Arrow-Hurwicz](http://dido.wss.yale.edu/P/ccdp/ec/e-0296.pdf) (tiré de [Introduction à l'optimisation](http://www.editions-ellipses.fr/product_info.php?products_id=8830) de Jean-Christophe Culioli, voir également l'[algorithme d'Uzawa](http://en.wikipedia.org/wiki/Uzawa_iteration)) et les notations suivantes :\n",
"\n",
"$$ \\left \\{ \\begin{array}{l} \\min_U J(U)=u_1^2 + u_2^2 - u_1 u_2 + u_2 \\\\ \\text{sous contrainte} \\; \\theta(U) = u_1 + 2u_2 - 1 = 0 \\end{array}\\right . $$\n",
"$ \\left \\{ \\begin{array}{l} \\min_U J(U)=u_1^2 + u_2^2 - u_1 u_2 + u_2 \\\\ \\text{sous contrainte} \\; \\theta(U) = u_1 + 2u_2 - 1 = 0 \\end{array}\\right . $\n",
"\n",
"L'algorithme est le suivant :\n",
"\n",
"\n",
"- On choisit $\\epsilon > 0$, $\\rho > 0$, des vecteurs aléatoires $U_0 \\in \\mathbb{R}^2$ et $P_0 \\in \\mathbb{R}^d$ ($d$ est le nombre de contraintes).\n",
"- A l'itération $k$, on met à jour :\n",
"$$ \\begin{array}{l} U_{t+1} = U_t - \\epsilon \\left( \\nabla J (U_t) + \\left[ \\nabla \\theta(U_t) \\right] ' P_t \\right) \\\\ P_{t+1} = P_t + \\rho \\theta( U_{t+1} ) \\end{array} $$\n",
" $ \\begin{array}{l} U_{t+1} = U_t - \\epsilon \\left( \\nabla J (U_t) + \\left[ \\nabla \\theta(U_t) \\right] ' P_t \\right) \\\\ P_{t+1} = P_t + \\rho \\theta( U_{t+1} ) \\end{array} $\n",
"- On retourne à l'étape précédente jusqu'à ce que la suite $(U_k)$ n'évolue plus.\n",
"\n",
"\n",
"\n",
"Le coefficient $P_t$ correspond au coefficient de Lagrange pour un Lagrangien défini comme suit :\n",
"\n",
"$$ L(U,P) = J(U) + P' \\theta(U) $$\n",
"$ L(U,P) = J(U) + P' \\theta(U) $\n",
"\n",
"La suite $(U_t)_t$ converge vers la solution en se déplaçant le long du gradient de la fonction $J$ lorsque la contrainte est vérifiée. Lorsqu'elle ne l'est pas, cette direction est un mélange du gradient de la fonction à optimiser et de celui de la contrainte à respecter.\n",
"\n",
Expand All @@ -223,11 +345,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"lag\">Le lagrangien augmenté</h2>\n",
"## Exercice 3 : Le lagrangien augmenté\n",
"\n",
"On reprend l'algorithme précédent mais appliqué à la fonction :\n",
"\n",
"$$ A(U) = J(U) + \\frac{c}{2} \\theta^2(U) $$\n",
"$ A(U) = J(U) + \\frac{c}{2} \\theta^2(U) $\n",
"\n",
"L'objectif est ici de remplacer la fonction $J$ par la fonction $A$ (ou Lagrangien augmenté) dans l'algorithme précédent et de vérifier qu'il converge en un nombre moindre d'itérations lorsqu'on choisit les mêmes conditions initiales."
]
Expand All @@ -236,19 +358,19 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"prol1\">Prolongement 1 : inégalité</h2>\n",
"## Prolongement 1 : inégalité\n",
"\n",
"On souhaite maintenant ajouter une contrainte d'inégalité : $Du -d \\leqslant 0$. L'idée est de créer une matrice $\\Theta$ définie par bloc :\n",
"\n",
"$$ \\Theta = \\left( \\begin{array}{c} \\theta \\\\ D \\end{array} \\right) \\;,\\; \\Pi = \\left(\\begin{array}{c} P \\\\ D \\end{array} \\right) \\;,\\; F = diag \\left( \\begin{array}{c} 1 \\\\ r_i \\end{array} \\right) $$\n",
"$ \\Theta = \\left( \\begin{array}{c} \\theta \\\\ D \\end{array} \\right) \\;,\\; \\Pi = \\left(\\begin{array}{c} P \\\\ D \\end{array} \\right) \\;,\\; F = diag \\left( \\begin{array}{c} 1 \\\\ r_i \\end{array} \\right) $\n",
"\n",
"On considère que $\\Theta$ est une matrice. On étend ce découpage en bloc au formule de mise à jour de l'algorithme de Arrow-Hurwicz :\n",
"\n",
"$$ \\begin{array}{l} U_{t+1} = U_t - \\epsilon \\left( \\nabla J (U_t) + \\left[ \\nabla \\Theta(U_t)\\right] ' Fi_t \\right) \\\\ \\Pi_{t+1} = F \\left( \\Pi_t + \\rho \\theta ( U_{t+1} ) \\right) \\end{array} $$\n",
"$ \\begin{array}{l} U_{t+1} = U_t - \\epsilon \\left( \\nabla J (U_t) + \\left[ \\nabla \\Theta(U_t)\\right] ' Fi_t \\right) \\\\ \\Pi_{t+1} = F \\left( \\Pi_t + \\rho \\theta ( U_{t+1} ) \\right) \\end{array} $\n",
"\n",
"Les coefficients $r_i$ valent 0 ou 1 selon que la contrainte d'inégalité $i$ est respectée ou non. On applique la modification pour le problème :\n",
"\n",
"$$ \\left \\{ \\begin{array}{l} \\min_U J(U)=u_1^2 + u_2^2 - u_1 u_2 + u_2 \\\\ \\text{sous contrainte} \\; \\theta(U) = u_1 + 2u_2 - 1 = 0 \\text{ et } u_1 \\geqslant 0.3 \\end{array}\\right . $$\n",
"$ \\left \\{ \\begin{array}{l} \\min_U J(U)=u_1^2 + u_2^2 - u_1 u_2 + u_2 \\\\ \\text{sous contrainte} \\; \\theta(U) = u_1 + 2u_2 - 1 = 0 \\text{ et } u_1 \\geqslant 0.3 \\end{array}\\right . $\n",
"\n",
"\n"
]
Expand All @@ -257,11 +379,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"prol2\">Prolongement 2 : optimisation d'une fonction linéaire</h2>\n",
"## Prolongement 2 : optimisation d'une fonction linéaire\n",
"\n",
"On s'intéresse à un problème différent :\n",
"\n",
"$$ \\left \\{ \\begin{array}{l} \\min_X C'X \\\\ \\text{sous contrainte} \\; AX \\leqslant B \\end{array}\\right . $$\n",
"$ \\left \\{ \\begin{array}{l} \\min_X C'X \\\\ \\text{sous contrainte} \\; AX \\leqslant B \\end{array}\\right . $\n",
"\n",
"Une façon de résoudre ce problème est l'[algorithme de Karmarkar](http://fr.wikipedia.org/wiki/Algorithme\\_de\\_Karmarkar). On optimise maintenant une fonction linéaire et non une fonction quadratique. Par conséquent, la solution est forcément sur un bord : des contraintes sont saturées.\n",
"\n",
Expand Down
6 changes: 3 additions & 3 deletions _doc/notebooks/td1a/td1a_cenonce_session_10.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 1,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -46,7 +46,7 @@
" var memo_level = 1;\n",
" var href;\n",
" var tags = [];\n",
" for (i = lfirst; i <= llast; i++) {\n",
" for (i = 0; i <= llast; i++) {\n",
" tags.push(\"h\" + i);\n",
" }\n",
"\n",
Expand Down Expand Up @@ -129,7 +129,7 @@
"<IPython.core.display.HTML object>"
]
},
"execution_count": 11,
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
Expand Down
20 changes: 7 additions & 13 deletions _doc/notebooks/td1a/td1a_cenonce_session_11.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 1,
"metadata": {
"collapsed": false
},
Expand Down Expand Up @@ -39,7 +39,7 @@
" var memo_level = 1;\n",
" var href;\n",
" var tags = [];\n",
" for (i = lfirst; i <= llast; i++) {\n",
" for (i = 0; i <= llast; i++) {\n",
" tags.push(\"h\" + i);\n",
" }\n",
"\n",
Expand Down Expand Up @@ -122,7 +122,7 @@
"<IPython.core.display.HTML object>"
]
},
"execution_count": 3,
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
Expand Down Expand Up @@ -231,21 +231,15 @@
"\n",
"Le filtre de [Sobel](http://fr.wikipedia.org/wiki/Filtre_de_Sobel) est un filtre qu'on applique à une image pour calculer le gradient d'une image afin de déterminer les contours qui s'y trouve. Le filtre de [Canny](http://fr.wikipedia.org/wiki/Filtre_de_Canny) permet de flouter une image. Dans un premier temps, on cherchera à appliquer un filtre 3x3 : \n",
"\n",
"$$\n",
"\\left( \\begin{array}{ccc} 1&1&1 \\\\ 1&1&1 \\\\ 1&1&1 \\end{array} \\right)\n",
"$$\n",
"$\\left( \\begin{array}{ccc} 1&1&1 \\\\ 1&1&1 \\\\ 1&1&1 \\end{array} \\right)$\n",
"\n",
"Qu'on applique au voisinage 3x3 du pixel $p_5$ :\n",
"\n",
"$$\n",
"\\left( \\begin{array}{ccc} p_1&p_2&p_3 \\\\ p_4&p_5&p_6 \\\\ p_7&p_8&p_9 \\end{array} \\right)\n",
"$$\n",
"$\\left( \\begin{array}{ccc} p_1&p_2&p_3 \\\\ p_4&p_5&p_6 \\\\ p_7&p_8&p_9 \\end{array} \\right)$\n",
"\n",
"Après l'application du filtre à ce pixel, le résultat devient :\n",
"\n",
"$$\n",
"\\left( \\begin{array}{ccc} ?&?&? \\\\ ?& \\sum_{i=1}^9 p_i &? \\\\ ?&?&? \\end{array} \\right)\n",
"$$\n",
"$\\left( \\begin{array}{ccc} ?&?&? \\\\ ?& \\sum_{i=1}^9 p_i &? \\\\ ?&?&? \\end{array} \\right)$\n",
"\n",
"On veut maintenant appliquer ce filtre sur l'image suivante :"
]
Expand Down Expand Up @@ -442,7 +436,7 @@
},
"outputs": [],
"source": [
"%load_ext cythonmagic"
"%load_ext cython"
]
},
{
Expand Down
Loading

0 comments on commit e1ac8cf

Please sign in to comment.