root / src / Hupdate_MS.f90 @ 3
Historique | Voir | Annoter | Télécharger (2,93 ko)
| 1 | 1 | pfleura2 | ! Hessian - or its inverse - update |
|---|---|---|---|
| 2 | 1 | pfleura2 | ! We use the Murtagh-Sargent update, also called Symmetric Rank 1 |
| 3 | 1 | pfleura2 | ! |
| 4 | 1 | pfleura2 | ! input: |
| 5 | 1 | pfleura2 | ! - nfree: number of coordinates |
| 6 | 1 | pfleura2 | ! - ds: GeomOld-Geom (expressed in the optimization coord.) |
| 7 | 1 | pfleura2 | ! - dgrad: GradOld-Grad (expressed in the optimization coord.) |
| 8 | 1 | pfleura2 | ! |
| 9 | 1 | pfleura2 | ! input/output: |
| 10 | 1 | pfleura2 | ! - Hess: old Hessian in input, replaced by updated Hessian on output |
| 11 | 1 | pfleura2 | ! (or inverse Hessian) |
| 12 | 1 | pfleura2 | ! |
| 13 | 1 | pfleura2 | ! |
| 14 | 1 | pfleura2 | !!!!!!!!!!!!!!!!!!!!!! |
| 15 | 1 | pfleura2 | ! The update formula for the Hessian is: |
| 16 | 1 | pfleura2 | ! (following Fletcher, approximate Hessian is denoted by B) |
| 17 | 1 | pfleura2 | ! B(k+1)=B+[(y-Bs){(y-Bs)T}]/[{(y-Bs)T}s]
|
| 18 | 1 | pfleura2 | ! |
| 19 | 1 | pfleura2 | ! it is self dual, ie for the inverse Hessian, denote by H: |
| 20 | 1 | pfleura2 | ! H(k+1)=H+[(s-Hy){(s-Hy)T}]/[{(s-Hy)T}y]
|
| 21 | 1 | pfleura2 | ! |
| 22 | 1 | pfleura2 | ! |
| 23 | 1 | pfleura2 | ! We decompose the computation in two steps: |
| 24 | 1 | pfleura2 | ! first we compute the update, and then we add it to Hess to get the |
| 25 | 1 | pfleura2 | ! new Hessian. |
| 26 | 1 | pfleura2 | ! This allows us to introduce a scaling factor if needed later. |
| 27 | 1 | pfleura2 | ! |
| 28 | 1 | pfleura2 | !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 29 | 1 | pfleura2 | |
| 30 | 1 | pfleura2 | SUBROUTINE Hupdate_MS(Nfree,ds,dgrad,Hess) |
| 31 | 1 | pfleura2 | |
| 32 | 1 | pfleura2 | IMPLICIT NONE |
| 33 | 1 | pfleura2 | |
| 34 | 1 | pfleura2 | INTEGER, PARAMETER :: KINT=KIND(1) |
| 35 | 1 | pfleura2 | INTEGER, PARAMETER :: KREAL=KIND(1.D0) |
| 36 | 1 | pfleura2 | |
| 37 | 1 | pfleura2 | INTEGER(KINT), INTENT(IN) :: NFREE |
| 38 | 1 | pfleura2 | REAL(KREAL), INTENT(INOUT) :: Hess(Nfree,Nfree) |
| 39 | 1 | pfleura2 | REAL(KREAL), INTENT(IN) :: ds(Nfree) |
| 40 | 1 | pfleura2 | REAL(KREAL), INTENT(IN) :: dGrad(Nfree) |
| 41 | 1 | pfleura2 | |
| 42 | 1 | pfleura2 | |
| 43 | 2 | pfleura2 | REAL(KREAL), ALLOCATABLE :: HessOld(:, :) |
| 44 | 1 | pfleura2 | REAL(KREAL), ALLOCATABLE :: Hup(:,:) |
| 45 | 1 | pfleura2 | REAL(KREAL), ALLOCATABLE :: HupInt(:) |
| 46 | 1 | pfleura2 | REAL(KREAL), ALLOCATABLE :: HS(:), Num(:,:) |
| 47 | 2 | pfleura2 | REAL(KREAL) :: Den, NormDS |
| 48 | 1 | pfleura2 | INTEGER(KINT) :: i,j, idx |
| 49 | 1 | pfleura2 | |
| 50 | 1 | pfleura2 | LOGICAL :: debug |
| 51 | 1 | pfleura2 | |
| 52 | 1 | pfleura2 | interface |
| 53 | 1 | pfleura2 | function valid(string) result (isValid) |
| 54 | 1 | pfleura2 | logical :: isValid |
| 55 | 1 | pfleura2 | character(*), intent(in) :: string |
| 56 | 1 | pfleura2 | end function valid |
| 57 | 1 | pfleura2 | |
| 58 | 1 | pfleura2 | end interface |
| 59 | 1 | pfleura2 | |
| 60 | 1 | pfleura2 | |
| 61 | 1 | pfleura2 | |
| 62 | 1 | pfleura2 | |
| 63 | 1 | pfleura2 | debug=valid("Hupdate").OR.Valid("Hupdate_MS")
|
| 64 | 1 | pfleura2 | |
| 65 | 1 | pfleura2 | if (debug) Call Header("Entering Hupdate_MS")
|
| 66 | 1 | pfleura2 | |
| 67 | 1 | pfleura2 | ALLOCATE(HessOld(nfree,nfree), & |
| 68 | 1 | pfleura2 | Hup(nfree,nfree), & |
| 69 | 1 | pfleura2 | HupInt(nfree), HS(nfree), & |
| 70 | 1 | pfleura2 | Num(nfree,nfree)) |
| 71 | 1 | pfleura2 | |
| 72 | 1 | pfleura2 | HessOld=Hess |
| 73 | 1 | pfleura2 | |
| 74 | 1 | pfleura2 | if (debug) THEN |
| 75 | 1 | pfleura2 | WRITE(*,*) 'DBG Hupdate HessOld' |
| 76 | 1 | pfleura2 | Idx=min(12,Nfree) |
| 77 | 1 | pfleura2 | DO I=1,NFree |
| 78 | 1 | pfleura2 | WRITE(*,'(12(1X,F8.4))') HessOld(I,1:Idx) |
| 79 | 1 | pfleura2 | END DO |
| 80 | 1 | pfleura2 | WRITE(*,*) 'DBG Hupdate ds' |
| 81 | 1 | pfleura2 | WRITE(*,'(12(1X,F8.4))') ds(1:Idx) |
| 82 | 1 | pfleura2 | WRITE(*,*) 'DBG Hupdate dGrad' |
| 83 | 1 | pfleura2 | WRITE(*,'(12(1X,F8.4))') dGrad(1:Idx) |
| 84 | 1 | pfleura2 | END IF |
| 85 | 1 | pfleura2 | |
| 86 | 1 | pfleura2 | NormDS=sqrt(DOT_PRODUCT(DS,DS)) |
| 87 | 1 | pfleura2 | if (NormDS>=1e-6) THEN |
| 88 | 1 | pfleura2 | |
| 89 | 1 | pfleura2 | HS=0 |
| 90 | 1 | pfleura2 | DO I=1,nfree |
| 91 | 1 | pfleura2 | DO J=1,nfree |
| 92 | 1 | pfleura2 | HS(I)=HS(I)+HessOld(i,j)*Ds(j) |
| 93 | 1 | pfleura2 | END DO |
| 94 | 1 | pfleura2 | END DO |
| 95 | 1 | pfleura2 | |
| 96 | 1 | pfleura2 | if (debug) WRITE(*,*) "HS:",HS |
| 97 | 1 | pfleura2 | HupInt=DGrad-HS |
| 98 | 1 | pfleura2 | |
| 99 | 1 | pfleura2 | ! WE calculate the MS update |
| 100 | 1 | pfleura2 | Num=0. |
| 101 | 1 | pfleura2 | Den=0. |
| 102 | 1 | pfleura2 | DO I=1,nfree |
| 103 | 1 | pfleura2 | DO J=1,nfree |
| 104 | 1 | pfleura2 | Num(I,J)=HUpInt(i)*HupInt(j) |
| 105 | 1 | pfleura2 | END DO |
| 106 | 1 | pfleura2 | Den=Den+Hupint(i)*Ds(i) |
| 107 | 1 | pfleura2 | END DO |
| 108 | 1 | pfleura2 | if (debug) WRITE(*,*) "Den:",Den,1./Den |
| 109 | 1 | pfleura2 | |
| 110 | 1 | pfleura2 | Hup=Num/Den |
| 111 | 1 | pfleura2 | |
| 112 | 1 | pfleura2 | Hess=HessOld+Hup |
| 113 | 1 | pfleura2 | ELSE |
| 114 | 1 | pfleura2 | Hess=HessOld |
| 115 | 1 | pfleura2 | END IF |
| 116 | 1 | pfleura2 | ! WRITE(*,*) 'New hess' |
| 117 | 1 | pfleura2 | ! DO I=1,nfree |
| 118 | 1 | pfleura2 | ! WRITE(*,fmt) Hess(i,:) |
| 119 | 1 | pfleura2 | ! END DO |
| 120 | 1 | pfleura2 | |
| 121 | 1 | pfleura2 | DEALLOCATE(HessOld,Hup, HupInt, HS, Num ) |
| 122 | 1 | pfleura2 | |
| 123 | 1 | pfleura2 | if (debug) Call Header("Hupdate_MS Over")
|
| 124 | 1 | pfleura2 | |
| 125 | 1 | pfleura2 | END SUBROUTINE Hupdate_MS |