prelim.f 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. SUBROUTINE PRELIM (N,NPT,X,XL,XU,RHOBEG,IPRINT,MAXFUN,XBASE,
  2. 1 XPT,FVAL,GOPT,HQ,PQ,BMAT,ZMAT,NDIM,SL,SU,NF,KOPT)
  3. IMPLICIT REAL*8 (A-H,O-Z)
  4. DIMENSION X(*),XL(*),XU(*),XBASE(*),XPT(NPT,*),FVAL(*),GOPT(*),
  5. 1 HQ(*),PQ(*),BMAT(NDIM,*),ZMAT(NPT,*),SL(*),SU(*)
  6. C
  7. C The arguments N, NPT, X, XL, XU, RHOBEG, IPRINT and MAXFUN are the
  8. C same as the corresponding arguments in SUBROUTINE BOBYQA.
  9. C The arguments XBASE, XPT, FVAL, HQ, PQ, BMAT, ZMAT, NDIM, SL and SU
  10. C are the same as the corresponding arguments in BOBYQB, the elements
  11. C of SL and SU being set in BOBYQA.
  12. C GOPT is usually the gradient of the quadratic model at XOPT+XBASE, but
  13. C it is set by PRELIM to the gradient of the quadratic model at XBASE.
  14. C If XOPT is nonzero, BOBYQB will change it to its usual value later.
  15. C NF is maintaned as the number of calls of CALFUN so far.
  16. C KOPT will be such that the least calculated value of F so far is at
  17. C the point XPT(KOPT,.)+XBASE in the space of the variables.
  18. C
  19. C SUBROUTINE PRELIM sets the elements of XBASE, XPT, FVAL, GOPT, HQ, PQ,
  20. C BMAT and ZMAT for the first iteration, and it maintains the values of
  21. C NF and KOPT. The vector X is also changed by PRELIM.
  22. C
  23. C Set some constants.
  24. C
  25. HALF=0.5D0
  26. ONE=1.0D0
  27. TWO=2.0D0
  28. ZERO=0.0D0
  29. RHOSQ=RHOBEG*RHOBEG
  30. RECIP=ONE/RHOSQ
  31. NP=N+1
  32. C
  33. C Set XBASE to the initial vector of variables, and set the initial
  34. C elements of XPT, BMAT, HQ, PQ and ZMAT to zero.
  35. C
  36. DO 20 J=1,N
  37. XBASE(J)=X(J)
  38. DO 10 K=1,NPT
  39. 10 XPT(K,J)=ZERO
  40. DO 20 I=1,NDIM
  41. 20 BMAT(I,J)=ZERO
  42. DO 30 IH=1,(N*NP)/2
  43. 30 HQ(IH)=ZERO
  44. DO 40 K=1,NPT
  45. PQ(K)=ZERO
  46. DO 40 J=1,NPT-NP
  47. 40 ZMAT(K,J)=ZERO
  48. C
  49. C Begin the initialization procedure. NF becomes one more than the number
  50. C of function values so far. The coordinates of the displacement of the
  51. C next initial interpolation point from XBASE are set in XPT(NF+1,.).
  52. C
  53. NF=0
  54. 50 NFM=NF
  55. NFX=NF-N
  56. NF=NF+1
  57. IF (NFM .LE. 2*N) THEN
  58. IF (NFM .GE. 1 .AND. NFM .LE. N) THEN
  59. STEPA=RHOBEG
  60. IF (SU(NFM) .EQ. ZERO) STEPA=-STEPA
  61. XPT(NF,NFM)=STEPA
  62. ELSE IF (NFM .GT. N) THEN
  63. STEPA=XPT(NF-N,NFX)
  64. STEPB=-RHOBEG
  65. IF (SL(NFX) .EQ. ZERO) STEPB=DMIN1(TWO*RHOBEG,SU(NFX))
  66. IF (SU(NFX) .EQ. ZERO) STEPB=DMAX1(-TWO*RHOBEG,SL(NFX))
  67. XPT(NF,NFX)=STEPB
  68. END IF
  69. ELSE
  70. ITEMP=(NFM-NP)/N
  71. JPT=NFM-ITEMP*N-N
  72. IPT=JPT+ITEMP
  73. IF (IPT .GT. N) THEN
  74. ITEMP=JPT
  75. JPT=IPT-N
  76. IPT=ITEMP
  77. END IF
  78. XPT(NF,IPT)=XPT(IPT+1,IPT)
  79. XPT(NF,JPT)=XPT(JPT+1,JPT)
  80. END IF
  81. C
  82. C Calculate the next value of F. The least function value so far and
  83. C its index are required.
  84. C
  85. DO 60 J=1,N
  86. X(J)=DMIN1(DMAX1(XL(J),XBASE(J)+XPT(NF,J)),XU(J))
  87. IF (XPT(NF,J) .EQ. SL(J)) X(J)=XL(J)
  88. IF (XPT(NF,J) .EQ. SU(J)) X(J)=XU(J)
  89. 60 CONTINUE
  90. CALL CALFUN (N,X,F)
  91. IF (IPRINT .EQ. 3) THEN
  92. PRINT 70, NF,F,(X(I),I=1,N)
  93. 70 FORMAT (/4X,'Function number',I6,' F =',1PD18.10,
  94. 1 ' The corresponding X is:'/(2X,5D15.6))
  95. END IF
  96. FVAL(NF)=F
  97. IF (NF .EQ. 1) THEN
  98. FBEG=F
  99. KOPT=1
  100. ELSE IF (F .LT. FVAL(KOPT)) THEN
  101. KOPT=NF
  102. END IF
  103. C
  104. C Set the nonzero initial elements of BMAT and the quadratic model in the
  105. C cases when NF is at most 2*N+1. If NF exceeds N+1, then the positions
  106. C of the NF-th and (NF-N)-th interpolation points may be switched, in
  107. C order that the function value at the first of them contributes to the
  108. C off-diagonal second derivative terms of the initial quadratic model.
  109. C
  110. IF (NF .LE. 2*N+1) THEN
  111. IF (NF .GE. 2 .AND. NF .LE. N+1) THEN
  112. GOPT(NFM)=(F-FBEG)/STEPA
  113. IF (NPT .LT. NF+N) THEN
  114. BMAT(1,NFM)=-ONE/STEPA
  115. BMAT(NF,NFM)=ONE/STEPA
  116. BMAT(NPT+NFM,NFM)=-HALF*RHOSQ
  117. END IF
  118. ELSE IF (NF .GE. N+2) THEN
  119. IH=(NFX*(NFX+1))/2
  120. TEMP=(F-FBEG)/STEPB
  121. DIFF=STEPB-STEPA
  122. HQ(IH)=TWO*(TEMP-GOPT(NFX))/DIFF
  123. GOPT(NFX)=(GOPT(NFX)*STEPB-TEMP*STEPA)/DIFF
  124. IF (STEPA*STEPB .LT. ZERO) THEN
  125. IF (F .LT. FVAL(NF-N)) THEN
  126. FVAL(NF)=FVAL(NF-N)
  127. FVAL(NF-N)=F
  128. IF (KOPT .EQ. NF) KOPT=NF-N
  129. XPT(NF-N,NFX)=STEPB
  130. XPT(NF,NFX)=STEPA
  131. END IF
  132. END IF
  133. BMAT(1,NFX)=-(STEPA+STEPB)/(STEPA*STEPB)
  134. BMAT(NF,NFX)=-HALF/XPT(NF-N,NFX)
  135. BMAT(NF-N,NFX)=-BMAT(1,NFX)-BMAT(NF,NFX)
  136. ZMAT(1,NFX)=DSQRT(TWO)/(STEPA*STEPB)
  137. ZMAT(NF,NFX)=DSQRT(HALF)/RHOSQ
  138. ZMAT(NF-N,NFX)=-ZMAT(1,NFX)-ZMAT(NF,NFX)
  139. END IF
  140. C
  141. C Set the off-diagonal second derivatives of the Lagrange functions and
  142. C the initial quadratic model.
  143. C
  144. ELSE
  145. IH=(IPT*(IPT-1))/2+JPT
  146. ZMAT(1,NFX)=RECIP
  147. ZMAT(NF,NFX)=RECIP
  148. ZMAT(IPT+1,NFX)=-RECIP
  149. ZMAT(JPT+1,NFX)=-RECIP
  150. TEMP=XPT(NF,IPT)*XPT(NF,JPT)
  151. HQ(IH)=(FBEG-FVAL(IPT+1)-FVAL(JPT+1)+F)/TEMP
  152. END IF
  153. IF (NF .LT. NPT .AND. NF .LT. MAXFUN) GOTO 50
  154. RETURN
  155. END