Télécharger banane.dgibi

Retour à la liste

Numérotation des lignes :

  1. ************************************************************************
  2. ************************************************************************
  3.  
  4. ************************************************************************
  5. * Test de la procédure NELDMEAD : *
  6. * optimisation par Nelder-Mead Simplex *
  7. * Application à la fonction "banane" de Rosenbrock *
  8. * *
  9. * Le problème s'écrit : *
  10. * Minimiser : f0(x) = (1-x1)² + 100*(x2-x1²)² *
  11. * sur x *
  12. * *
  13. * La solution est : x1 = 1 x2 = 1 f0(1,1) = 0 *
  14. ************************************************************************
  15.  
  16.  
  17. **----------------------------- Préambule ----------------------------**
  18.  
  19. * Options
  20. OPTI 'ECHO' 0 ;
  21. itrac = FAUX ;
  22.  
  23. * Procédure pour calculer f0 selon 2 arguments quelconques
  24. DEBP F00 x1 x2 ;
  25. f0 = ((1. - x1) ** 2) + (100. * ((x2 - (x1 ** 2)) ** 2)) ;
  26. FINP f0 ;
  27.  
  28. * Procédure pour calculer la fonction sur un LISTREEL
  29. DEBP FONC lx*'LISTREEL' ;
  30. x1 = EXTR lx 1 ;
  31. x2 = EXTR lx 2 ;
  32. f0 = F00 x1 x2 ;
  33. FINP f0 ;
  34.  
  35. * Procédure pour tracer la fonction et le simplexe
  36. DEBP PLOT sim*'LISTOBJE' f*'LISTREEL' tit*'MOT' ;
  37. SI itrac ;
  38. x1 = EXTR sim 1 ;
  39. x2 = EXTr sim 2 ;
  40. x3 = EXTR sim 3 ;
  41. pt1 = (EXTR x1 1) (EXTr x1 2) ;
  42. pt2 = (EXTR x2 1) (EXTr x2 2) ;
  43. pt3 = (EXTR x3 1) (EXTr x3 2) ;
  44. mshs = QUEL 'SEG2' pt1 pt2 pt3 pt1 ;
  45. cpt1 = CERC 10 'ROTA' 360. (pt1 PLUS (0.05 0.)) pt1 ;
  46. f1 = EXTR f 1 ;
  47. ann1 = ANNO 'ETIQ' pt1 'NOIR' 'NE' 0.5 VRAI (CHAI 'f0 =' f1) ;
  48. liso = PROG 0. 'PAS' 1. 10. 'PAS' 10. 100. ;
  49. TRAC f0msh msh (cmsh ET mshs ET cpt1) ann1 'TITR' tit liso ;
  50. FINSI ;
  51. FINP ;
  52.  
  53. * Maillage + champ (pour la visualisation)
  54. OPTI 'DIME' 2 'ELEM' 'QUA8' 'DENS' 0.1 ;
  55. msh = (DROI (-3. -2.) (3. -2.)) TRAN (0. 6.) ;
  56. cmsh = CONT msh ;
  57. x y = COOR msh ;
  58. f0msh = F00 x y ;
  59.  
  60. * Solution de référence
  61. xref = PROG 1. 1. ;
  62. fref = FONC xref ;
  63. MESS 'Solution de reference' ;
  64. MESS '---------------------' ;
  65. MESS ' x1 x2 f0' ;
  66. MESS (CHAI 'FORMAT' '(F10.5)' (EXTR xref 1) /4 (EXTR xref 2) /16 fref /28) ;
  67.  
  68.  
  69.  
  70. **------------- Calcul 1 : on donne le simplexe initial --------------**
  71.  
  72. MESS 'Optimisation par Nelder-Mead Simplex (depuis un simplexe initial)' ;
  73. MESS '-----------------------------------------------------------------' ;
  74.  
  75. * Simplexe initial
  76. s0 = ENUM (PROG -2.8 0.) (PROG 0. 3.5) (PROG 2.5 -1.9) ;
  77. fs0 = PROG (FONC (EXTR s0 1)) (FONC (EXTR s0 2)) (FONC (EXTR s0 3)) ;
  78.  
  79. * Ici, on fait "nit" appels à NELDMEAD, en les limitant à 1 seule itération
  80. * interne, afin de tracer les l'évolution des résultats
  81. nit = 100 ;
  82. x0 = EXTR s0 1 ;
  83. f0 = EXTR fs0 1 ;
  84. lit = LECT 0 ;
  85. lx1 = PROG (EXTR x0 1) ;
  86. lx2 = PROG (EXTR x0 2) ;
  87. MESS 'It x1 x2 f0' ;
  88. MESS (CHAI 'FORMAT' '(F10.5)' 0 (EXTR x0 1) /4 (EXTR x0 2) /16 f0 /28) ;
  89. PLOT s0 fs0 'Simplxe initial' ;
  90. REPE b1 nit ;
  91. SI (EGA &b1 1) ;
  92. s1 fs1 = NELDMEAD FONC s0 1 ;
  93. SINON ;
  94. s1 fs1 = NELDMEAD FONC s1 1 ;
  95. FINSI ;
  96. * Affichage du simplexe et des valeurs
  97. x0 = EXTR s1 1 ;
  98. f0 = EXTR fs1 1 ;
  99. lit = lit ET &b1 ;
  100. lx1 = lx1 ET (EXTR x0 1) ;
  101. lx2 = lx2 ET (EXTR x0 2) ;
  102. MESS (CHAI 'FORMAT' '(F10.5)' &b1 (EXTR x0 1) /4 (EXTR x0 2) /16 f0 /28) ;
  103. PLOT s1 fs1 (CHAI 'Simplexe a l''iteration' ' ' &b1) ;
  104. FIN b1 ;
  105.  
  106. * Vérification du resultat final
  107. err1 = MAXI 'ABS' ((x0 - xref) / xref) ;
  108. MESS 'Erreur relative max (sur x)' ;
  109. MESS err1 ;
  110. MESS ; MESS ; MESS ;
  111.  
  112. * Courbes de convergence
  113. evo = (EVOL 'ROUG' 'MANU' 'LEGE' 'x1 Calcul' 'Iteration' lit lx1) ET
  114. (EVOL 'ORAN' 'MANU' 'LEGE' 'x2 Calcul' 'Iteration' lit lx2) ET
  115. (EVOL 'GRIS' 'MANU' 'LEGE' 'x1/x2 Reference' 'STYL' 'TIRR' 'Iteration' (LECT 0 nit) (PROG 2*(EXTR xref 1))) ;
  116. SI itrac ;
  117. DESS evo 'LEGE' 'SE' 'TITR' 'Coordonnees du point vs Iterations' ;
  118. FINSI ;
  119.  
  120.  
  121.  
  122.  
  123. **--------------- Calcul 2 : on donne un point initial ---------------**
  124. * + changement des paramètres *
  125.  
  126. MESS 'Optimisation par Nelder-Mead Simplex (depuis un point initial)' ;
  127. MESS '--------------------------------------------------------------' ;
  128. MESS ' x1 x2 f0' ;
  129.  
  130. * Point initial
  131. x0 = PROG -2.8 0. ;
  132. fs0 = FONC x0 ;
  133.  
  134. * Ici les itérations sont faites en interne de la procédure NELDMEAD
  135. * jusqu'à atteindre le critère
  136. s1 fs1 = NELDMEAD FONC x0 'PAS' 1. 'ALPH' 0.7 'BETA' 3.2 'GAMM' 0.3 'DELT' 0.2 'CRIT' 1.E-20 ;
  137. x0 = EXTR s1 1 ;
  138. f0 = EXTR fs1 1 ;
  139. MESS (CHAI 'FORMAT' '(F10.5)' (EXTR x0 1) /4 (EXTR x0 2) /16 f0 /28) ;
  140.  
  141. * Vérification du resultat final
  142. err2 = MAXI 'ABS' ((x0 - xref) / xref) ;
  143. MESS 'Erreur relative max (sur x)' ;
  144. MESS err2 ;
  145. MESS ; MESS ; MESS ;
  146.  
  147.  
  148.  
  149. * Sortie en erreur si l'écart aux valeurs de références est trop important
  150. errmax = MAXI err1 err2 ;
  151. SI (errmax > 1.E-8) ;
  152. ERRE 'Erreur dans le calcul d''optimisation' ;
  153. SINON ;
  154. MESS 'Cas test passe avec succes !' ;
  155. FINSI ;
  156.  
  157.  
  158.  
  159. FIN ;
  160.  
  161.  
  162.  

© Cast3M 2003 - Tous droits réservés.
Mentions légales