************************************************************************ * Section : Mathematiques Fonctions ************************************************************************ ************************************************************************ * Test de la procédure NELDMEAD : * * optimisation par Nelder-Mead Simplex * * Application à la fonction "banane" de Rosenbrock * * * * Le problème s'écrit : * * Minimiser : f0(x) = (1-x1)² + 100*(x2-x1²)² * * sur x * * * * La solution est : x1 = 1 x2 = 1 f0(1,1) = 0 * ************************************************************************ **----------------------------- Préambule ----------------------------** * Options OPTI 'ECHO' 0 ; itrac = FAUX ; * Procédure pour calculer f0 selon 2 arguments quelconques DEBP F00 x1 x2 ; f0 = ((1. - x1) ** 2) + (100. * ((x2 - (x1 ** 2)) ** 2)) ; FINP f0 ; * Procédure pour calculer la fonction sur un LISTREEL DEBP FONC lx*'LISTREEL' ; x1 = EXTR lx 1 ; x2 = EXTR lx 2 ; f0 = F00 x1 x2 ; FINP f0 ; * Procédure pour tracer la fonction et le simplexe DEBP PLOT sim*'LISTOBJE' f*'LISTREEL' tit*'MOT' ; SI itrac ; x1 = EXTR sim 1 ; x2 = EXTr sim 2 ; x3 = EXTR sim 3 ; pt1 = (EXTR x1 1) (EXTr x1 2) ; pt2 = (EXTR x2 1) (EXTr x2 2) ; pt3 = (EXTR x3 1) (EXTr x3 2) ; mshs = QUEL 'SEG2' pt1 pt2 pt3 pt1 ; cpt1 = CERC 10 'ROTA' 360. (pt1 PLUS (0.05 0.)) pt1 ; f1 = EXTR f 1 ; ann1 = ANNO 'ETIQ' pt1 'NOIR' 'NE' 0.5 VRAI (CHAI 'f0 =' f1) ; liso = PROG 0. 'PAS' 1. 10. 'PAS' 10. 100. ; TRAC f0msh msh (cmsh ET mshs ET cpt1) ann1 'TITR' tit liso ; FINSI ; FINP ; * Maillage + champ (pour la visualisation) OPTI 'DIME' 2 'ELEM' 'QUA8' 'DENS' 0.1 ; msh = (DROI (-3. -2.) (3. -2.)) TRAN (0. 6.) ; cmsh = CONT msh ; x y = COOR msh ; f0msh = F00 x y ; * Solution de référence xref = PROG 1. 1. ; fref = FONC xref ; MESS ; MESS 'Solution de reference' ; MESS '---------------------' ; MESS ' x1 x2 f0' ; MESS (CHAI 'FORMAT' '(F10.5)' (EXTR xref 1) /4 (EXTR xref 2) /16 fref /28) ; MESS ; MESS ; **------------- Calcul 1 : on donne le simplexe initial --------------** MESS 'Optimisation par Nelder-Mead Simplex (depuis un simplexe initial)' ; MESS '-----------------------------------------------------------------' ; MESS ; * Simplexe initial s0 = ENUM (PROG -2.8 0.) (PROG 0. 3.5) (PROG 2.5 -1.9) ; fs0 = PROG (FONC (EXTR s0 1)) (FONC (EXTR s0 2)) (FONC (EXTR s0 3)) ; * Ici, on fait "nit" appels à NELDMEAD, en les limitant à 1 seule itération * interne, afin de tracer les l'évolution des résultats nit = 100 ; x0 = EXTR s0 1 ; f0 = EXTR fs0 1 ; lit = LECT 0 ; lx1 = PROG (EXTR x0 1) ; lx2 = PROG (EXTR x0 2) ; MESS 'It x1 x2 f0' ; MESS (CHAI 'FORMAT' '(F10.5)' 0 (EXTR x0 1) /4 (EXTR x0 2) /16 f0 /28) ; PLOT s0 fs0 'Simplxe initial' ; REPE b1 nit ; SI (EGA &b1 1) ; s1 fs1 = NELDMEAD FONC s0 1 ; SINON ; s1 fs1 = NELDMEAD FONC s1 1 ; FINSI ; * Affichage du simplexe et des valeurs x0 = EXTR s1 1 ; f0 = EXTR fs1 1 ; lit = lit ET &b1 ; lx1 = lx1 ET (EXTR x0 1) ; lx2 = lx2 ET (EXTR x0 2) ; MESS (CHAI 'FORMAT' '(F10.5)' &b1 (EXTR x0 1) /4 (EXTR x0 2) /16 f0 /28) ; PLOT s1 fs1 (CHAI 'Simplexe a l''iteration' ' ' &b1) ; FIN b1 ; MESS ; * Vérification du resultat final err1 = MAXI 'ABS' ((x0 - xref) / xref) ; MESS 'Erreur relative max (sur x)' ; MESS err1 ; MESS ; MESS ; MESS ; * Courbes de convergence evo = (EVOL 'ROUG' 'MANU' 'LEGE' 'x1 Calcul' 'Iteration' lit lx1) ET (EVOL 'ORAN' 'MANU' 'LEGE' 'x2 Calcul' 'Iteration' lit lx2) ET (EVOL 'GRIS' 'MANU' 'LEGE' 'x1/x2 Reference' 'STYL' 'TIRR' 'Iteration' (LECT 0 nit) (PROG 2*(EXTR xref 1))) ; SI itrac ; DESS evo 'LEGE' 'SE' 'TITR' 'Coordonnees du point vs Iterations' ; FINSI ; **--------------- Calcul 2 : on donne un point initial ---------------** * + changement des paramètres * MESS 'Optimisation par Nelder-Mead Simplex (depuis un point initial)' ; MESS '--------------------------------------------------------------' ; MESS ' x1 x2 f0' ; MESS ; * Point initial x0 = PROG -2.8 0. ; fs0 = FONC x0 ; * Ici les itérations sont faites en interne de la procédure NELDMEAD * jusqu'à atteindre le critère s1 fs1 = NELDMEAD FONC x0 'PAS' 1. 'ALPH' 0.7 'BETA' 3.2 'GAMM' 0.3 'DELT' 0.2 'CRIT' 1.E-20 ; x0 = EXTR s1 1 ; f0 = EXTR fs1 1 ; MESS (CHAI 'FORMAT' '(F10.5)' (EXTR x0 1) /4 (EXTR x0 2) /16 f0 /28) ; MESS ; * Vérification du resultat final err2 = MAXI 'ABS' ((x0 - xref) / xref) ; MESS 'Erreur relative max (sur x)' ; MESS err2 ; MESS ; MESS ; MESS ; * Sortie en erreur si l'écart aux valeurs de références est trop important errmax = MAXI err1 err2 ; SI (errmax > 1.E-8) ; ERRE 'Erreur dans le calcul d''optimisation' ; SINON ; MESS 'Cas test passe avec succes !' ; FINSI ; FIN ;