altmov.f 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. SUBROUTINE ALTMOV (N,NPT,XPT,XOPT,BMAT,ZMAT,NDIM,SL,SU,KOPT,
  2. 1 KNEW,ADELT,XNEW,XALT,ALPHA,CAUCHY,GLAG,HCOL,W)
  3. IMPLICIT REAL*8 (A-H,O-Z)
  4. DIMENSION XPT(NPT,*),XOPT(*),BMAT(NDIM,*),ZMAT(NPT,*),SL(*),
  5. 1 SU(*),XNEW(*),XALT(*),GLAG(*),HCOL(*),W(*)
  6. C
  7. C The arguments N, NPT, XPT, XOPT, BMAT, ZMAT, NDIM, SL and SU all have
  8. C the same meanings as the corresponding arguments of BOBYQB.
  9. C KOPT is the index of the optimal interpolation point.
  10. C KNEW is the index of the interpolation point that is going to be moved.
  11. C ADELT is the current trust region bound.
  12. C XNEW will be set to a suitable new position for the interpolation point
  13. C XPT(KNEW,.). Specifically, it satisfies the SL, SU and trust region
  14. C bounds and it should provide a large denominator in the next call of
  15. C UPDATE. The step XNEW-XOPT from XOPT is restricted to moves along the
  16. C straight lines through XOPT and another interpolation point.
  17. C XALT also provides a large value of the modulus of the KNEW-th Lagrange
  18. C function subject to the constraints that have been mentioned, its main
  19. C difference from XNEW being that XALT-XOPT is a constrained version of
  20. C the Cauchy step within the trust region. An exception is that XALT is
  21. C not calculated if all components of GLAG (see below) are zero.
  22. C ALPHA will be set to the KNEW-th diagonal element of the H matrix.
  23. C CAUCHY will be set to the square of the KNEW-th Lagrange function at
  24. C the step XALT-XOPT from XOPT for the vector XALT that is returned,
  25. C except that CAUCHY is set to zero if XALT is not calculated.
  26. C GLAG is a working space vector of length N for the gradient of the
  27. C KNEW-th Lagrange function at XOPT.
  28. C HCOL is a working space vector of length NPT for the second derivative
  29. C coefficients of the KNEW-th Lagrange function.
  30. C W is a working space vector of length 2N that is going to hold the
  31. C constrained Cauchy step from XOPT of the Lagrange function, followed
  32. C by the downhill version of XALT when the uphill step is calculated.
  33. C
  34. C Set the first NPT components of W to the leading elements of the
  35. C KNEW-th column of the H matrix.
  36. C
  37. HALF=0.5D0
  38. ONE=1.0D0
  39. ZERO=0.0D0
  40. CONST=ONE+DSQRT(2.0D0)
  41. DO 10 K=1,NPT
  42. 10 HCOL(K)=ZERO
  43. DO 20 J=1,NPT-N-1
  44. TEMP=ZMAT(KNEW,J)
  45. DO 20 K=1,NPT
  46. 20 HCOL(K)=HCOL(K)+TEMP*ZMAT(K,J)
  47. ALPHA=HCOL(KNEW)
  48. HA=HALF*ALPHA
  49. C
  50. C Calculate the gradient of the KNEW-th Lagrange function at XOPT.
  51. C
  52. DO 30 I=1,N
  53. 30 GLAG(I)=BMAT(KNEW,I)
  54. DO 50 K=1,NPT
  55. TEMP=ZERO
  56. DO 40 J=1,N
  57. 40 TEMP=TEMP+XPT(K,J)*XOPT(J)
  58. TEMP=HCOL(K)*TEMP
  59. DO 50 I=1,N
  60. 50 GLAG(I)=GLAG(I)+TEMP*XPT(K,I)
  61. C
  62. C Search for a large denominator along the straight lines through XOPT
  63. C and another interpolation point. SLBD and SUBD will be lower and upper
  64. C bounds on the step along each of these lines in turn. PREDSQ will be
  65. C set to the square of the predicted denominator for each line. PRESAV
  66. C will be set to the largest admissible value of PREDSQ that occurs.
  67. C
  68. PRESAV=ZERO
  69. DO 80 K=1,NPT
  70. IF (K .EQ. KOPT) GOTO 80
  71. DDERIV=ZERO
  72. DISTSQ=ZERO
  73. DO 60 I=1,N
  74. TEMP=XPT(K,I)-XOPT(I)
  75. DDERIV=DDERIV+GLAG(I)*TEMP
  76. 60 DISTSQ=DISTSQ+TEMP*TEMP
  77. SUBD=ADELT/DSQRT(DISTSQ)
  78. SLBD=-SUBD
  79. ILBD=0
  80. IUBD=0
  81. SUMIN=DMIN1(ONE,SUBD)
  82. C
  83. C Revise SLBD and SUBD if necessary because of the bounds in SL and SU.
  84. C
  85. DO 70 I=1,N
  86. TEMP=XPT(K,I)-XOPT(I)
  87. IF (TEMP .GT. ZERO) THEN
  88. IF (SLBD*TEMP .LT. SL(I)-XOPT(I)) THEN
  89. SLBD=(SL(I)-XOPT(I))/TEMP
  90. ILBD=-I
  91. END IF
  92. IF (SUBD*TEMP .GT. SU(I)-XOPT(I)) THEN
  93. SUBD=DMAX1(SUMIN,(SU(I)-XOPT(I))/TEMP)
  94. IUBD=I
  95. END IF
  96. ELSE IF (TEMP .LT. ZERO) THEN
  97. IF (SLBD*TEMP .GT. SU(I)-XOPT(I)) THEN
  98. SLBD=(SU(I)-XOPT(I))/TEMP
  99. ILBD=I
  100. END IF
  101. IF (SUBD*TEMP .LT. SL(I)-XOPT(I)) THEN
  102. SUBD=DMAX1(SUMIN,(SL(I)-XOPT(I))/TEMP)
  103. IUBD=-I
  104. END IF
  105. END IF
  106. 70 CONTINUE
  107. C
  108. C Seek a large modulus of the KNEW-th Lagrange function when the index
  109. C of the other interpolation point on the line through XOPT is KNEW.
  110. C
  111. IF (K .EQ. KNEW) THEN
  112. DIFF=DDERIV-ONE
  113. STEP=SLBD
  114. VLAG=SLBD*(DDERIV-SLBD*DIFF)
  115. ISBD=ILBD
  116. TEMP=SUBD*(DDERIV-SUBD*DIFF)
  117. IF (DABS(TEMP) .GT. DABS(VLAG)) THEN
  118. STEP=SUBD
  119. VLAG=TEMP
  120. ISBD=IUBD
  121. END IF
  122. TEMPD=HALF*DDERIV
  123. TEMPA=TEMPD-DIFF*SLBD
  124. TEMPB=TEMPD-DIFF*SUBD
  125. IF (TEMPA*TEMPB .LT. ZERO) THEN
  126. TEMP=TEMPD*TEMPD/DIFF
  127. IF (DABS(TEMP) .GT. DABS(VLAG)) THEN
  128. STEP=TEMPD/DIFF
  129. VLAG=TEMP
  130. ISBD=0
  131. END IF
  132. END IF
  133. C
  134. C Search along each of the other lines through XOPT and another point.
  135. C
  136. ELSE
  137. STEP=SLBD
  138. VLAG=SLBD*(ONE-SLBD)
  139. ISBD=ILBD
  140. TEMP=SUBD*(ONE-SUBD)
  141. IF (DABS(TEMP) .GT. DABS(VLAG)) THEN
  142. STEP=SUBD
  143. VLAG=TEMP
  144. ISBD=IUBD
  145. END IF
  146. IF (SUBD .GT. HALF) THEN
  147. IF (DABS(VLAG) .LT. 0.25D0) THEN
  148. STEP=HALF
  149. VLAG=0.25D0
  150. ISBD=0
  151. END IF
  152. END IF
  153. VLAG=VLAG*DDERIV
  154. END IF
  155. C
  156. C Calculate PREDSQ for the current line search and maintain PRESAV.
  157. C
  158. TEMP=STEP*(ONE-STEP)*DISTSQ
  159. PREDSQ=VLAG*VLAG*(VLAG*VLAG+HA*TEMP*TEMP)
  160. IF (PREDSQ .GT. PRESAV) THEN
  161. PRESAV=PREDSQ
  162. KSAV=K
  163. STPSAV=STEP
  164. IBDSAV=ISBD
  165. END IF
  166. 80 CONTINUE
  167. C
  168. C Construct XNEW in a way that satisfies the bound constraints exactly.
  169. C
  170. DO 90 I=1,N
  171. TEMP=XOPT(I)+STPSAV*(XPT(KSAV,I)-XOPT(I))
  172. 90 XNEW(I)=DMAX1(SL(I),DMIN1(SU(I),TEMP))
  173. IF (IBDSAV .LT. 0) XNEW(-IBDSAV)=SL(-IBDSAV)
  174. IF (IBDSAV .GT. 0) XNEW(IBDSAV)=SU(IBDSAV)
  175. C
  176. C Prepare for the iterative method that assembles the constrained Cauchy
  177. C step in W. The sum of squares of the fixed components of W is formed in
  178. C WFIXSQ, and the free components of W are set to BIGSTP.
  179. C
  180. BIGSTP=ADELT+ADELT
  181. IFLAG=0
  182. 100 WFIXSQ=ZERO
  183. GGFREE=ZERO
  184. DO 110 I=1,N
  185. W(I)=ZERO
  186. TEMPA=DMIN1(XOPT(I)-SL(I),GLAG(I))
  187. TEMPB=DMAX1(XOPT(I)-SU(I),GLAG(I))
  188. IF (TEMPA .GT. ZERO .OR. TEMPB .LT. ZERO) THEN
  189. W(I)=BIGSTP
  190. GGFREE=GGFREE+GLAG(I)**2
  191. END IF
  192. 110 CONTINUE
  193. IF (GGFREE .EQ. ZERO) THEN
  194. CAUCHY=ZERO
  195. GOTO 200
  196. END IF
  197. C
  198. C Investigate whether more components of W can be fixed.
  199. C
  200. 120 TEMP=ADELT*ADELT-WFIXSQ
  201. IF (TEMP .GT. ZERO) THEN
  202. WSQSAV=WFIXSQ
  203. STEP=DSQRT(TEMP/GGFREE)
  204. GGFREE=ZERO
  205. DO 130 I=1,N
  206. IF (W(I) .EQ. BIGSTP) THEN
  207. TEMP=XOPT(I)-STEP*GLAG(I)
  208. IF (TEMP .LE. SL(I)) THEN
  209. W(I)=SL(I)-XOPT(I)
  210. WFIXSQ=WFIXSQ+W(I)**2
  211. ELSE IF (TEMP .GE. SU(I)) THEN
  212. W(I)=SU(I)-XOPT(I)
  213. WFIXSQ=WFIXSQ+W(I)**2
  214. ELSE
  215. GGFREE=GGFREE+GLAG(I)**2
  216. END IF
  217. END IF
  218. 130 CONTINUE
  219. IF (WFIXSQ .GT. WSQSAV .AND. GGFREE .GT. ZERO) GOTO 120
  220. END IF
  221. C
  222. C Set the remaining free components of W and all components of XALT,
  223. C except that W may be scaled later.
  224. C
  225. GW=ZERO
  226. DO 140 I=1,N
  227. IF (W(I) .EQ. BIGSTP) THEN
  228. W(I)=-STEP*GLAG(I)
  229. XALT(I)=DMAX1(SL(I),DMIN1(SU(I),XOPT(I)+W(I)))
  230. ELSE IF (W(I) .EQ. ZERO) THEN
  231. XALT(I)=XOPT(I)
  232. ELSE IF (GLAG(I) .GT. ZERO) THEN
  233. XALT(I)=SL(I)
  234. ELSE
  235. XALT(I)=SU(I)
  236. END IF
  237. 140 GW=GW+GLAG(I)*W(I)
  238. C
  239. C Set CURV to the curvature of the KNEW-th Lagrange function along W.
  240. C Scale W by a factor less than one if that can reduce the modulus of
  241. C the Lagrange function at XOPT+W. Set CAUCHY to the final value of
  242. C the square of this function.
  243. C
  244. CURV=ZERO
  245. DO 160 K=1,NPT
  246. TEMP=ZERO
  247. DO 150 J=1,N
  248. 150 TEMP=TEMP+XPT(K,J)*W(J)
  249. 160 CURV=CURV+HCOL(K)*TEMP*TEMP
  250. IF (IFLAG .EQ. 1) CURV=-CURV
  251. IF (CURV .GT. -GW .AND. CURV .LT. -CONST*GW) THEN
  252. SCALE=-GW/CURV
  253. DO 170 I=1,N
  254. TEMP=XOPT(I)+SCALE*W(I)
  255. 170 XALT(I)=DMAX1(SL(I),DMIN1(SU(I),TEMP))
  256. CAUCHY=(HALF*GW*SCALE)**2
  257. ELSE
  258. CAUCHY=(GW+HALF*CURV)**2
  259. END IF
  260. C
  261. C If IFLAG is zero, then XALT is calculated as before after reversing
  262. C the sign of GLAG. Thus two XALT vectors become available. The one that
  263. C is chosen is the one that gives the larger value of CAUCHY.
  264. C
  265. IF (IFLAG .EQ. 0) THEN
  266. DO 180 I=1,N
  267. GLAG(I)=-GLAG(I)
  268. 180 W(N+I)=XALT(I)
  269. CSAVE=CAUCHY
  270. IFLAG=1
  271. GOTO 100
  272. END IF
  273. IF (CSAVE .GT. CAUCHY) THEN
  274. DO 190 I=1,N
  275. 190 XALT(I)=W(N+I)
  276. CAUCHY=CSAVE
  277. END IF
  278. 200 RETURN
  279. END