bfgs_b2.f90 12.8 KB
Newer Older
Gregor Michalicek's avatar
Gregor Michalicek committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
!--------------------------------------------------------------------------------
! Copyright (c) 2019 Peter Grünberg Institut, Forschungszentrum Jülich, Germany
! This file is part of FLEUR and available as free software under the conditions
! of the MIT license as expressed in the LICENSE file in more detail.
!--------------------------------------------------------------------------------

MODULE m_bfgs_b2

CONTAINS

! The subroutine bfgs_b2 is an implementation of a BFGS algorithm with
! box constraints. It is motivated by the paper:
! Byrd et al., "A Limited Memory Algorithm for Bound Constrained Optimization", 
! SIAM Journal on Scientific Computing 16, 1190 (1995)
! However, it is substantially different from that paper.
!
!                            GM'2019

! Note: lastGradient and lastParameters have to be 0.0 if this is the first call to the subroutine and these
!       vectors don't exist yet. Also historyLength has to be 0 in that case.

SUBROUTINE bfgs_b2(vecLength,gradient,lastGradient,minConstraints,maxConstraints,enabledConstraints,parameters,&
                   lastParameters,equalityLinCombi,equalityCriterion,maxHistoryLength,paramCorrections,&
                   gradientCorrections,iStep,mixParam,l_converged,convCrit)

   IMPLICIT NONE

   INTEGER,        INTENT(IN)    :: vecLength
   INTEGER,        INTENT(IN)    :: maxHistoryLength
   INTEGER,        INTENT(INOUT) :: iStep ! The index of the bfgs_b iteration
   REAL,           INTENT(IN)    :: mixParam
   REAL,           INTENT(IN)    :: equalityCriterion
   REAL,           INTENT(IN)    :: gradient(vecLength)
   REAL,           INTENT(INOUT) :: lastGradient(vecLength)
   REAL,           INTENT(IN)    :: minConstraints(vecLength)
   REAL,           INTENT(IN)    :: maxConstraints(vecLength)
   LOGICAL,        INTENT(IN)    :: enabledConstraints(vecLength)
   REAL,           INTENT(INOUT) :: parameters(vecLength)
   REAL,           INTENT(INOUT) :: lastParameters(vecLength)
   REAL,           INTENT(IN)    :: equalityLinCombi(vecLength)
   REAL,           INTENT(INOUT) :: paramCorrections(vecLength,maxHistoryLength)
   REAL,           INTENT(INOUT) :: gradientCorrections(vecLength,maxHistoryLength)
   LOGICAL,        INTENT(OUT)   :: l_converged
   REAL,           INTENT(IN)    :: convCrit

   INTEGER                :: i, j, stepIndex, numFreeParams, numFixedParams
   INTEGER                :: historyLength
   REAL, PARAMETER        :: eps = 1.0e-10
   REAL, PARAMETER        :: largeEps = 1.0e-6
   REAL                   :: temp, norm, subspaceEqualityCrit
   REAL                   :: ddot

   LOGICAL                :: converged

   REAL                   :: lastGradCorrection(vecLength)
   REAL                   :: lastParamCorrection(vecLength)
   REAL                   :: prelimNextPoint(vecLength)
   REAL                   :: prelimGradient(vecLength)
   REAL                   :: tempVecA(vecLength)
   REAL                   :: subspaceGrad(vecLength)
   REAL                   :: subspaceNextPoint(vecLength)
   REAL                   :: subspaceLinCombi(vecLength)
   REAL                   :: diffPoint(vecLength)
   REAL                   :: diffGrad(vecLength)

   LOGICAL                :: freeParams(vecLength)

   REAL, ALLOCATABLE      :: hessMat(:,:), invHessMat(:,:)
   REAL, ALLOCATABLE      :: tempMatB(:,:), tempMatC(:,:)
   REAL, ALLOCATABLE      :: subspaceBasis(:,:)
   REAL, ALLOCATABLE      :: subspaceInvHessMat(:,:)

73 74
   WRITE(*,*) 'bfgs_b2 (input): parameters(:vecLength): ', parameters(:vecLength)
   WRITE(*,*) 'bfgs_b2 (input): gradient(:vecLength): ', gradient(:vecLength)
Gregor Michalicek's avatar
Gregor Michalicek committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

   l_converged = .FALSE.

   ! 0. Add last data pair to history iff it is not obviously destructive for the positive definiteness of the Hessian matrix

   IF(ANY(lastGradient(:).NE.0.0).OR.ANY(lastParameters(:).NE.0.0)) THEN
      lastGradCorrection(:) = gradient(:) - lastGradient(:)
      lastParamCorrection(:) = parameters(:) - lastParameters(:)
      temp = ddot(vecLength,lastParamCorrection(:),1,lastGradCorrection(:),1)
      IF (temp.GT.eps) THEN
         ! Add data pair to history
         istep = istep + 1
         stepIndex = MOD(iStep - 1,maxHistoryLength) + 1
         paramCorrections(:,stepIndex) = lastParamCorrection(:)
         gradientCorrections(:,stepIndex) = lastGradCorrection(:)
      END IF

      ! Check for convergence and return if converged
      IF(ALL(ABS(lastGradCorrection(:)).LT.convCrit).AND.ALL(ABS(lastParamCorrection(:)).LT.convCrit)) THEN
         l_converged = .TRUE.
         RETURN
      END IF
   END IF

   ! 1. Construct Hessian matrix and its inverse.

   historyLength = MIN(istep,maxHistoryLength)

   ALLOCATE(hessMat(vecLength,vecLength))
   ALLOCATE(invHessMat(vecLength,vecLength))
   ALLOCATE(tempMatB(vecLength,vecLength))

   hessMat(:,:) = 0.0
   invHessMat(:,:) = 0.0
   tempMatB(:,:) = 0.0

   DO i = 1, vecLength
      hessMat(i,i) = 1.0
      invHessMat(i,i) = 1.0
   END DO

   IF(historyLength.EQ.0) THEN
      DO i = 1, vecLength
         hessMat(i,i) = 1.0 / mixParam
         invHessMat(i,i) = mixParam
      END DO
   END IF

   DO i = 1, historyLength
      stepIndex = MOD(iStep - historyLength + i - 1,maxHistoryLength) + 1

      ! First the Hessian matrix
      ! second correction term
      tempMatB(:,:) = 0.0
      tempVecA(:) = 0.0
      CALL dgemv('N',vecLength,vecLength,1.0,hessMat(:,:),vecLength,paramCorrections(:,stepIndex),1,0.0,tempVecA(:),1)
      norm = ddot(vecLength,paramCorrections(:,stepIndex),1,tempVecA(:),1)
Gregor Michalicek's avatar
Gregor Michalicek committed
132
      CALL dgemm('N','N',vecLength,vecLength,1,-1.0/norm,tempVecA(:),vecLength,tempVecA(:),1,0.0,tempMatB,vecLength)
Gregor Michalicek's avatar
Gregor Michalicek committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205

      ! first correction term
      norm = ddot(vecLength,gradientCorrections(:,stepIndex),1,paramCorrections(:,stepIndex),1)
      CALL dgemm('N','N',vecLength,vecLength,1,1.0/norm,gradientCorrections(:,stepIndex),vecLength,gradientCorrections(:,stepIndex),1,1.0,tempMatB,vecLength)
      hessMat(:,:) = hessMat(:,:) + tempMatB(:,:)

      ! Now the inverse of the Hessian matrix
      tempMatB(:,:) = 0.0
      tempVecA(:) = 0.0
      CALL dgemv('N',vecLength,vecLength,1.0,invHessMat(:,:),vecLength,gradientCorrections(:,stepIndex),1,0.0,tempVecA(:),1)
      norm = ddot(vecLength,paramCorrections(:,stepIndex),1,gradientCorrections(:,stepIndex),1)

      ! second correction term
      CALL dgemm('N','N',vecLength,vecLength,1,1.0,tempVecA(:),vecLength,paramCorrections(:,stepIndex),1,0.0,tempMatB,vecLength)

      tempMatB(:,:) = tempMatB(:,:) + TRANSPOSE(tempMatB(:,:))
      tempMatB(:,:) = -tempMatB(:,:) / norm

      ! first correction term
      temp = norm + ddot(vecLength,gradientCorrections(:,stepIndex),1,tempVecA(:),1)
      CALL dgemm('N','N',vecLength,vecLength,1,temp/(norm*norm),paramCorrections(:,stepIndex),vecLength,paramCorrections(:,stepIndex),1,1.0,tempMatB,vecLength)
      invHessMat(:,:) = invHessMat(:,:) + tempMatB(:,:)

      !Check whether invHessMat is the inverse of hessMat (for debugging only)
      tempMatB(:,:) = 0.0
      CALL dgemm('N','N',vecLength,vecLength,vecLength,1.0,hessMat(:,:),vecLength,invHessMat(:,:),vecLength,0.0,tempMatB,vecLength)      

   END DO

   ! 2. Determine next point based on the gradient, the inverse of the Hessian, and the box constraints

   freeParams(:) = .TRUE.
   prelimNextPoint(:) = parameters(:)
   prelimGradient(:) = gradient(:)

   converged = .FALSE.
   DO WHILE (.NOT.converged)

      ! Determine free parameters (parameters not on boundaries with a gradient pointing outwards)
      numFixedParams = 0
      DO i = 1, vecLength
         IF(enabledConstraints(i)) THEN
            IF((prelimNextPoint(i).LE.minConstraints(i)+eps).AND.(prelimGradient(i).GT.0.0)) THEN
               freeParams(i) = .FALSE.
               numFixedParams = numFixedParams + 1
            END IF
            IF((prelimNextPoint(i).GE.maxConstraints(i)-eps).AND.(prelimGradient(i).LT.0.0)) THEN
               freeParams(i) = .FALSE.
               numFixedParams = numFixedParams + 1
            END IF
         END IF
      END DO
      numFreeParams = vecLength - numFixedParams

      ! Perform basis transformation to obtain inverse Hessian matrix for the free parameter subspace

      ALLOCATE (subspaceBasis(vecLength,numFreeParams))
      subspaceBasis = 0.0
      subspaceGrad = 0.0
      j = 1
      DO i = 1, vecLength
         IF(freeParams(i)) THEN
            subspaceBasis(i,j) = 1.0
            subspaceGrad(j) = prelimGradient(i)
            j = j + 1
         END IF
      END DO

      ALLOCATE (tempMatC(vecLength,numFreeParams))
      ALLOCATE (subspaceInvHessMat(numFreeParams,numFreeParams))
      tempMatC(:,:) = 0.0
      subspaceInvHessMat(:,:) = 0.0

206
      CALL dgemm('N','N',vecLength,numFreeParams,vecLength,1.0,invHessMat(:,:),vecLength,subspaceBasis(:,:),vecLength,0.0,tempMatC(:,:),vecLength)
Gregor Michalicek's avatar
Gregor Michalicek committed
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
      CALL dgemm('N','N',numFreeParams,numFreeParams,vecLength,1.0,TRANSPOSE(subspaceBasis(:,:)),numFreeParams,tempMatC(:,:),vecLength,0.0,subspaceInvHessMat(:,:),numFreeParams)
      
      DEALLOCATE(tempMatC)
      DEALLOCATE(subspaceBasis)

      ! Calculate next point (ignoring constraints)

      tempVecA(:) = 0.0
      CALL dgemv('N',numFreeParams,numFreeParams,1.0,subspaceInvHessMat(:,:),numFreeParams,-subspaceGrad(:numFreeParams),1,0.0,tempVecA(:numFreeParams),1)

      j = 1
      DO i = 1, vecLength
         IF(freeParams(i)) THEN
            prelimNextPoint(i) = prelimNextPoint(i) + tempVecA(j)
            j = j + 1
         END IF
      END DO

      DEALLOCATE(subspaceInvHessMat)

      ! Constrain next point to box and check convergence
      converged = .TRUE.
      DO i = 1, vecLength
         IF (enabledConstraints(i).AND.freeParams(i)) THEN
            IF(prelimNextPoint(i).LT.minConstraints(i)-eps) THEN
               converged = .FALSE.
            END IF
            IF(prelimNextPoint(i).GT.maxConstraints(i)+eps) THEN
               converged = .FALSE.
            END IF
            IF(prelimNextPoint(i).LT.minConstraints(i)) THEN
               prelimNextPoint(i) = minConstraints(i)
            END IF
            IF(prelimNextPoint(i).GT.maxConstraints(i)) THEN
               prelimNextPoint(i) = maxConstraints(i)
            END IF
         END IF
      END DO


      ! If not converged calculate prelimGradient for next point according to quadratic model
      IF(.NOT.converged) THEN
         diffPoint(:) = prelimNextPoint(:) - parameters(:)
         diffGrad(:) = 0.0
         CALL dgemv('N',vecLength,vecLength,1.0,hessMat(:,:),vecLength,diffPoint(:),1,0.0,diffGrad(:),1)
         prelimGradient(:) = prelimGradient(:) + diffGrad(:)
      END IF

   END DO

   ! 3. Project prelimNextPoint onto the allowed subspace given by the equality constraints
   ! Note: I only consider a single equality constraint so this part becomes simpler

   ! Determine free parameters (parameters not on boundaries with a gradient pointing outwards)

   converged = .FALSE.
   DO WHILE (.NOT.converged)

      freeParams(:) = .TRUE.
      numFixedParams = 0
      DO i = 1, vecLength
         IF(enabledConstraints(i)) THEN
            IF(prelimNextPoint(i).LT.minConstraints(i) - eps) THEN
               freeParams(i) = .FALSE.
               numFixedParams = numFixedParams + 1
            END IF
            IF(prelimNextPoint(i).GT.maxConstraints(i) + eps) THEN
               freeParams(i) = .FALSE.
               numFixedParams = numFixedParams + 1
            END IF
            IF(prelimNextPoint(i).LT.minConstraints(i)) THEN
               prelimNextPoint(i) = minConstraints(i)
            END IF
            IF(prelimNextPoint(i).GT.maxConstraints(i)) THEN
               prelimNextPoint(i) = maxConstraints(i)
            END IF
         END IF
      END DO
      numFreeParams = vecLength - numFixedParams

      subspaceNextPoint(:) = 0.0
      subspaceLinCombi(:) = 0.0
      subspaceEqualityCrit = equalityCriterion
      j = 1
      DO i = 1, vecLength
         IF(freeParams(i)) THEN
            subspaceNextPoint(j) = prelimNextPoint(i)
            subspaceLinCombi(j) = equalityLinCombi(i)
            j = j + 1
         ELSE
            subspaceEqualityCrit = subspaceEqualityCrit - prelimNextPoint(i) * equalityLinCombi(i)
         END IF
      END DO
      temp = ddot(numFreeParams,subspaceLinCombi(:),1,subspaceNextPoint(:),1)
      converged = .TRUE.
      IF(ABS(temp - subspaceEqualityCrit).GT.largeEps) THEN
         converged = .FALSE.
         subspaceNextPoint(:) = subspaceNextPoint(:) * subspaceEqualityCrit / temp
      END IF
      j = 1
      DO i = 1, vecLength
         IF(freeParams(i)) THEN
            prelimNextPoint(i) = subspaceNextPoint(j)
            j = j + 1
         END IF
      END DO
   END DO

   ! 4. Update lastParameters, lastGradient, parameters

   lastParameters(:) = parameters(:)
   lastGradient(:) = gradient(:)
   parameters(:) = prelimNextPoint(:)

   DEALLOCATE(tempMatB)
   DEALLOCATE(invHessMat)
   DEALLOCATE(hessMat)

325
   WRITE(*,*) 'bfgs_b2 (output): parameters(:vecLength): ', parameters(:vecLength)
Gregor Michalicek's avatar
Gregor Michalicek committed
326 327 328 329

END SUBROUTINE bfgs_b2

END MODULE m_bfgs_b2