-
Notifications
You must be signed in to change notification settings - Fork 4
/
lbfgs.f
731 lines (729 loc) · 23.8 KB
/
lbfgs.f
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
73
74
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
132
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
206
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
SUBROUTINE LBFGS_UPD(NWORK,NHSIZE,N, K,M,JOB,
; NNPINV,NVINV,
; IPIV, X,X0, G,G0, D, P, S,Y, IERR)
!
! Drives the L-BFGS method. Nocedal and Wright page 197
! - B Baker March 2013
!
! INPUT MEANING
! ----- -------
! D block diagonal hessian
! G current gradient
! G0 gradient direction from previous iteration
! JOB = 1, update P via L-BFGS two loop recursion
! = 2, update memory in L-BFGS
! IPIV nvinv > 1 holds pivots for D
! K iteration number 1,2,3,...
! M memory size of L-BFGS
! N number of parameters to invert for
! NHSIZE size of D
! NNPINV number of nodes in inversion (nnpinv*nvinv = n)
! NVINV number of variables in inversion
! NWORK size of S and Y
! S past search directions
! X current model
! X0 model from previous iteration
! Y past differenced gradient directions
!
! OUTPUT MEANING
! ------ -------
! S if JOB = 2 past m search directions
! Y if JOB = 2 past m differenced gradient directions
!
!.... variable declarations
IMPLICIT NONE
REAL*8, INTENT(INOUT) :: Y(NWORK), S(NWORK)
REAL*8, INTENT(IN) :: D(NHSIZE), G(N), G0(N), X(N), X0(N)
INTEGER*4, INTENT(IN) :: IPIV(N), NWORK, N, K, M, JOB,
; NHSIZE, NNPINV, NVINV
REAL*8, INTENT(OUT) :: P(N)
INTEGER*4, INTENT(OUT) :: IERR
!.... local variables
REAL*8, ALLOCATABLE :: WORK(:)
INTEGER*4 I, I1, I2, J1, J2
!
!----------------------------------------------------------------------!
!
!.... compute D = inv(B)*G
IF (JOB.EQ.1) THEN
ALLOCATE(WORK(N))
DO 1 I=1,N
IF (D(I).LE.0.D0) THEN
WRITE(*,*) 'lbfgs_upd: Error diagonal <= 0.!'
IERR = 1
RETURN
ENDIF
WORK(I) = D(I)*G(I)
1 CONTINUE
CALL DPREGRAD(NHSIZE,N, NNPINV,NVINV, IPIV,D,G,
; WORK,IERR)
IF (IERR.NE.0) THEN
WRITE(*,*) 'lbfgs_upd: Error calling DPREGRAD!'
RETURN
ENDIF
CALL TWO_LOOP(NWORK, N,M, K, WORK,S,Y, P)
DEALLOCATE(WORK)
ELSE !updating memory
IF (K.GT.M) THEN
WRITE(*,*) 'lbfgs_upd: Shifting memory...'
DO 2 I=1,M-1
I1 = I*N + 1
I2 = I1 + N - 1
J1 = (I - 1)*N + 1
J2 = J1 + N - 1
CALL DCOPY(N,S(I1:I2),1,S(J1:J2),1)
CALL DCOPY(N,Y(I1:I2),1,Y(J1:J2),1)
2 CONTINUE
ENDIF
IF (K.LT.M) THEN
I1 = (K - 1)*N + 1
I2 = I1 + N - 1
ELSE
I1 = (M - 1)*N + 1
I2 = I1 + N - 1
ENDIF
S(I1:I2) = X(1:N) - X0(1:N)
Y(I1:I2) = G(1:N) - G0(1:N)
ENDIF
RETURN
END
! !
!======================================================================!
! !
SUBROUTINE TWO_LOOP(NWORK, N,M, K, D,S,Y, P)
!
! Updates model perturbation by 2 loop recursion. Page 179 of
! Nocedal and Wright 2006. - B Baker March 2013
!
! INPUT MEANING
! ----- -------
! K iteration number 1,2,3,...
! M memory size of L-BFGS
! N number of parameters to invert for
! NWORK size of S and Y
! S saved search directions
! Y saved differenced gradient directions
!
! OUTPUT MEANING
! ------ -------
! P new search direction (minimizing)
!
!.... variable declarations
REAL*8, INTENT(IN) :: D(N), S(NWORK), Y(NWORK)
INTEGER*4, INTENT(IN) :: NWORK, K, N, M
REAL*8, INTENT(OUT) :: P(N)
!.... local variables
REAL*8, ALLOCATABLE :: Q(:), RHO(:), ALPHA(:)
REAL*8 DDOT, GAMMA1, BETA
INTEGER*4 KNDX, I, I1, I2
!
!----------------------------------------------------------------------!
!
!.... maybe just an easy return
IF (K.EQ.1) THEN
DO 1 I=1,N
P(I) =-D(I)
1 CONTINUE
RETURN
ENDIF
ALLOCATE(Q(N))
ALLOCATE(RHO(M))
ALLOCATE(ALPHA(M))
CALL DCOPY(N,D,1,Q,1) !q = D
KNDX = MIN(K - 1,M) + 1
DO 2 I=MIN(K-1,M),1,-1
KNDX = KNDX - 1
I1 = (KNDX - 1)*N + 1
I2 = I1 + N - 1
RHO(I) = 1.D0/DDOT(N,Y(I1:I2),1,S(I1:I2),1)
ALPHA(I) = RHO(I)*DDOT(N,S(I1:I2),1,Q,1)
CALL DAXPY(N,-ALPHA(I),Y(I1:I2),1,Q,1) !q = q - alpha y
2 CONTINUE
I = MIN(K-1,M)
I1 = (I - 1)*N + 1
I2 = I1 + N - 1
GAMMA1 = DDOT(N,S(I1:I2),1,Y(I1:I2),1)
; /DDOT(N,Y(I1:I2),1,Y(I1:I2),1)
CALL DSCAL(N,GAMMA1,Q,1) !r <- gamma q
DO 3 I=1,MIN(K-1,M)
I1 = (I - 1)*N + 1
I2 = I1 + N - 1
BETA = RHO(I)*DDOT(N,Y(I1:I2),1,Q,1) !beta = rho_i y^T r
CALL DAXPY(N,(ALPHA(I) - BETA),S(I1:I2),1,Q,1)
3 CONTINUE
CALL DCOPY(N,Q,1,P,1)
CALL DSCAL(N,-1.D0,P,1)
DEALLOCATE(Q)
DEALLOCATE(RHO)
DEALLOCATE(ALPHA)
RETURN
END
C ------------------------------------------------------------------
C
C **************************
C LINE SEARCH ROUTINE MCSRCH
C **************************
C
SUBROUTINE MCSRCH(N,X,F,G,S,STP,FTOL,GTOL,XTOL,STPMIN,STPMAX,
; MAXFEV,INFO,NFEV,WA,LP)
INTEGER N,MAXFEV,INFO,NFEV
DOUBLE PRECISION F,STP,FTOL,GTOL,XTOL,STPMIN,STPMAX
DOUBLE PRECISION X(N),G(N),S(N),WA(N)
! COMMON /LB3/MP,LP,GTOL,STPMIN,STPMAX
SAVE
C
C SUBROUTINE MCSRCH
C
C A slight modification of the subroutine CSRCH of More' and Thuente.
C The changes are to allow reverse communication, and do not affect
C the performance of the routine.
C
C THE PURPOSE OF MCSRCH IS TO FIND A STEP WHICH SATISFIES
C A SUFFICIENT DECREASE CONDITION AND A CURVATURE CONDITION.
C
C AT EACH STAGE THE SUBROUTINE UPDATES AN INTERVAL OF
C UNCERTAINTY WITH ENDPOINTS STX AND STY. THE INTERVAL OF
C UNCERTAINTY IS INITIALLY CHOSEN SO THAT IT CONTAINS A
C MINIMIZER OF THE MODIFIED FUNCTION
C
C F(X+STP*S) - F(X) - FTOL*STP*(GRADF(X)'S).
C
C IF A STEP IS OBTAINED FOR WHICH THE MODIFIED FUNCTION
C HAS A NONPOSITIVE FUNCTION VALUE AND NONNEGATIVE DERIVATIVE,
C THEN THE INTERVAL OF UNCERTAINTY IS CHOSEN SO THAT IT
C CONTAINS A MINIMIZER OF F(X+STP*S).
C
C THE ALGORITHM IS DESIGNED TO FIND A STEP WHICH SATISFIES
C THE SUFFICIENT DECREASE CONDITION
C
C F(X+STP*S) .LE. F(X) + FTOL*STP*(GRADF(X)'S),
C
C AND THE CURVATURE CONDITION
C
C ABS(GRADF(X+STP*S)'S)) .LE. GTOL*ABS(GRADF(X)'S).
C
C IF FTOL IS LESS THAN GTOL AND IF, FOR EXAMPLE, THE FUNCTION
C IS BOUNDED BELOW, THEN THERE IS ALWAYS A STEP WHICH SATISFIES
C BOTH CONDITIONS. IF NO STEP CAN BE FOUND WHICH SATISFIES BOTH
C CONDITIONS, THEN THE ALGORITHM USUALLY STOPS WHEN ROUNDING
C ERRORS PREVENT FURTHER PROGRESS. IN THIS CASE STP ONLY
C SATISFIES THE SUFFICIENT DECREASE CONDITION.
C
C THE SUBROUTINE STATEMENT IS
C
C SUBROUTINE MCSRCH(N,X,F,G,S,STP,FTOL,XTOL, MAXFEV,INFO,NFEV,WA)
C WHERE
C
C N IS A POSITIVE INTEGER INPUT VARIABLE SET TO THE NUMBER
C OF VARIABLES.
C
C X IS AN ARRAY OF LENGTH N. ON INPUT IT MUST CONTAIN THE
C BASE POINT FOR THE LINE SEARCH. ON OUTPUT IT CONTAINS
C X + STP*S.
C
C F IS A VARIABLE. ON INPUT IT MUST CONTAIN THE VALUE OF F
C AT X. ON OUTPUT IT CONTAINS THE VALUE OF F AT X + STP*S.
C
C G IS AN ARRAY OF LENGTH N. ON INPUT IT MUST CONTAIN THE
C GRADIENT OF F AT X. ON OUTPUT IT CONTAINS THE GRADIENT
C OF F AT X + STP*S.
C
C S IS AN INPUT ARRAY OF LENGTH N WHICH SPECIFIES THE
C SEARCH DIRECTION.
C
C STP IS A NONNEGATIVE VARIABLE. ON INPUT STP CONTAINS AN
C INITIAL ESTIMATE OF A SATISFACTORY STEP. ON OUTPUT
C STP CONTAINS THE FINAL ESTIMATE.
C
C FTOL AND GTOL ARE NONNEGATIVE INPUT VARIABLES. (In this reverse
C communication implementation GTOL is defined in a COMMON
C statement.) TERMINATION OCCURS WHEN THE SUFFICIENT DECREASE
C CONDITION AND THE DIRECTIONAL DERIVATIVE CONDITION ARE
C SATISFIED.
C
C XTOL IS A NONNEGATIVE INPUT VARIABLE. TERMINATION OCCURS
C WHEN THE RELATIVE WIDTH OF THE INTERVAL OF UNCERTAINTY
C IS AT MOST XTOL.
C
C STPMIN AND STPMAX ARE NONNEGATIVE INPUT VARIABLES WHICH
C SPECIFY LOWER AND UPPER BOUNDS FOR THE STEP. (In this reverse
C communication implementatin they are defined in a COMMON
C statement).
C
C MAXFEV IS A POSITIVE INTEGER INPUT VARIABLE. TERMINATION
C OCCURS WHEN THE NUMBER OF CALLS TO FCN IS AT LEAST
C MAXFEV BY THE END OF AN ITERATION.
C
C INFO IS AN INTEGER OUTPUT VARIABLE SET AS FOLLOWS:
C
C INFO = 0 IMPROPER INPUT PARAMETERS.
C
C INFO =-1 A RETURN IS MADE TO COMPUTE THE FUNCTION AND GRADIENT.
C
C INFO = 1 THE SUFFICIENT DECREASE CONDITION AND THE
C DIRECTIONAL DERIVATIVE CONDITION HOLD.
C
C INFO = 2 RELATIVE WIDTH OF THE INTERVAL OF UNCERTAINTY
C IS AT MOST XTOL.
C
C INFO = 3 NUMBER OF CALLS TO FCN HAS REACHED MAXFEV.
C
C INFO = 4 THE STEP IS AT THE LOWER BOUND STPMIN.
C
C INFO = 5 THE STEP IS AT THE UPPER BOUND STPMAX.
C
C INFO = 6 ROUNDING ERRORS PREVENT FURTHER PROGRESS.
C THERE MAY NOT BE A STEP WHICH SATISFIES THE
C SUFFICIENT DECREASE AND CURVATURE CONDITIONS.
C TOLERANCES MAY BE TOO SMALL.
C
C NFEV IS AN INTEGER OUTPUT VARIABLE SET TO THE NUMBER OF
C CALLS TO FCN.
C
C WA IS A WORK ARRAY OF LENGTH N.
C
C SUBPROGRAMS CALLED
C
C MCSTEP
C
C FORTRAN-SUPPLIED...ABS,MAX,MIN
C
C ARGONNE NATIONAL LABORATORY. MINPACK PROJECT. JUNE 1983
C JORGE J. MORE', DAVID J. THUENTE
C
C **********
INTEGER INFOC,J
LOGICAL BRACKT,STAGE1
DOUBLE PRECISION DG,DGM,DGINIT,DGTEST,DGX,DGXM,DGY,DGYM,
* FINIT,FTEST1,FM,FX,FXM,FY,FYM,P5,P66,STX,STY,
* STMIN,STMAX,WIDTH,WIDTH1,XTRAPF,ZERO
DATA P5,P66,XTRAPF,ZERO /0.5D0,0.66D0,4.0D0,0.0D0/
IF(INFO.EQ.-1) GO TO 45
INFOC = 1
C
C CHECK THE INPUT PARAMETERS FOR ERRORS.
C
IF (N .LE. 0) THEN
WRITE(*,*) 'MCSRCH ERROR: N <= 0'
RETURN
ENDIF
IF (STP .LE. ZERO) THEN
WRITE(*,*) 'MCSRCH ERROR: STP <= 0'
RETURN
ENDIF
IF (FTOL .LT. ZERO) THEN
WRITE(*,*) 'MCSRCH ERROR: FTOL < 0'
RETURN
ENDIF
IF (GTOL .LT. ZERO) THEN
WRITE(*,*) 'MCSRCH ERROR: GTOL < 0'
RETURN
ENDIF
IF (XTOL .LT. ZERO) THEN
WRITE(*,*) 'MCSRCH ERROR: XTOL < 0'
RETURN
ENDIF
IF (STPMIN .LT. ZERO) THEN
WRITE(*,*) 'MCSRCH ERROR: STPMIN < 0'
RETURN
ENDIF
IF (STPMAX .LT. STPMIN) THEN
WRITE(*,*) 'MCSRCH STPMAX < STPMIN'
RETURN
ENDIF
IF (MAXFEV .LE. 0) THEN
WRITE(*,*) 'MCSRCH MAXFEV <= 0'
RETURN
ENDIF
C
C COMPUTE THE INITIAL GRADIENT IN THE SEARCH DIRECTION
C AND CHECK THAT S IS A DESCENT DIRECTION.
C
DGINIT = ZERO
DO 10 J = 1, N
DGINIT = DGINIT + G(J)*S(J)
10 CONTINUE
IF (DGINIT .GE. ZERO) then
write(LP,15)
15 FORMAT(/' THE SEARCH DIRECTION IS NOT A DESCENT DIRECTION')
RETURN
ENDIF
C
C INITIALIZE LOCAL VARIABLES.
C
BRACKT = .FALSE.
STAGE1 = .TRUE.
NFEV = 0
FINIT = F
DGTEST = FTOL*DGINIT
WIDTH = STPMAX - STPMIN
WIDTH1 = WIDTH/P5
DO 20 J = 1, N
WA(J) = X(J)
20 CONTINUE
C
C THE VARIABLES STX, FX, DGX CONTAIN THE VALUES OF THE STEP,
C FUNCTION, AND DIRECTIONAL DERIVATIVE AT THE BEST STEP.
C THE VARIABLES STY, FY, DGY CONTAIN THE VALUE OF THE STEP,
C FUNCTION, AND DERIVATIVE AT THE OTHER ENDPOINT OF
C THE INTERVAL OF UNCERTAINTY.
C THE VARIABLES STP, F, DG CONTAIN THE VALUES OF THE STEP,
C FUNCTION, AND DERIVATIVE AT THE CURRENT STEP.
C
STX = ZERO
FX = FINIT
DGX = DGINIT
STY = ZERO
FY = FINIT
DGY = DGINIT
C
C START OF ITERATION.
C
30 CONTINUE
C
C SET THE MINIMUM AND MAXIMUM STEPS TO CORRESPOND
C TO THE PRESENT INTERVAL OF UNCERTAINTY.
C
IF (BRACKT) THEN
STMIN = MIN(STX,STY)
STMAX = MAX(STX,STY)
ELSE
STMIN = STX
STMAX = STP + XTRAPF*(STP - STX)
END IF
C
C FORCE THE STEP TO BE WITHIN THE BOUNDS STPMAX AND STPMIN.
C
STP = MAX(STP,STPMIN)
STP = MIN(STP,STPMAX)
C
C IF AN UNUSUAL TERMINATION IS TO OCCUR THEN LET
C STP BE THE LOWEST POINT OBTAINED SO FAR.
C
IF ((BRACKT .AND. (STP .LE. STMIN .OR. STP .GE. STMAX))
* .OR. NFEV .GE. MAXFEV-1 .OR. INFOC .EQ. 0
* .OR. (BRACKT .AND. STMAX-STMIN .LE. XTOL*STMAX)) STP = STX
C
C EVALUATE THE FUNCTION AND GRADIENT AT STP
C AND COMPUTE THE DIRECTIONAL DERIVATIVE.
C We return to main program to obtain F and G.
C
DO 40 J = 1, N
X(J) = WA(J) + STP*S(J)
40 CONTINUE
INFO=-1
RETURN
C
45 INFO=0
NFEV = NFEV + 1
DG = ZERO
DO 50 J = 1, N
DG = DG + G(J)*S(J)
50 CONTINUE
FTEST1 = FINIT + STP*DGTEST
C
C TEST FOR CONVERGENCE.
C
IF ((BRACKT .AND. (STP .LE. STMIN .OR. STP .GE. STMAX))
* .OR. INFOC .EQ. 0) INFO = 6
IF (STP .EQ. STPMAX .AND.
* F .LE. FTEST1 .AND. DG .LE. DGTEST) INFO = 5
IF (STP .EQ. STPMIN .AND.
* (F .GT. FTEST1 .OR. DG .GE. DGTEST)) INFO = 4
IF (NFEV .GE. MAXFEV) INFO = 3
IF (BRACKT .AND. STMAX-STMIN .LE. XTOL*STMAX) INFO = 2
IF (F .LE. FTEST1 .AND. ABS(DG) .LE. GTOL*(-DGINIT)) INFO = 1
C
C CHECK FOR TERMINATION.
C
IF (INFO .NE. 0) RETURN
C
C IN THE FIRST STAGE WE SEEK A STEP FOR WHICH THE MODIFIED
C FUNCTION HAS A NONPOSITIVE VALUE AND NONNEGATIVE DERIVATIVE.
C
IF (STAGE1 .AND. F .LE. FTEST1 .AND.
* DG .GE. MIN(FTOL,GTOL)*DGINIT) STAGE1 = .FALSE.
C
C A MODIFIED FUNCTION IS USED TO PREDICT THE STEP ONLY IF
C WE HAVE NOT OBTAINED A STEP FOR WHICH THE MODIFIED
C FUNCTION HAS A NONPOSITIVE FUNCTION VALUE AND NONNEGATIVE
C DERIVATIVE, AND IF A LOWER FUNCTION VALUE HAS BEEN
C OBTAINED BUT THE DECREASE IS NOT SUFFICIENT.
C
IF (STAGE1 .AND. F .LE. FX .AND. F .GT. FTEST1) THEN
C
C DEFINE THE MODIFIED FUNCTION AND DERIVATIVE VALUES.
C
FM = F - STP*DGTEST
FXM = FX - STX*DGTEST
FYM = FY - STY*DGTEST
DGM = DG - DGTEST
DGXM = DGX - DGTEST
DGYM = DGY - DGTEST
C
C CALL CSTEP TO UPDATE THE INTERVAL OF UNCERTAINTY
C AND TO COMPUTE THE NEW STEP.
C
CALL MCSTEP(STX,FXM,DGXM,STY,FYM,DGYM,STP,FM,DGM,
* BRACKT,STMIN,STMAX,INFOC)
C
C RESET THE FUNCTION AND GRADIENT VALUES FOR F.
C
FX = FXM + STX*DGTEST
FY = FYM + STY*DGTEST
DGX = DGXM + DGTEST
DGY = DGYM + DGTEST
ELSE
C
C CALL MCSTEP TO UPDATE THE INTERVAL OF UNCERTAINTY
C AND TO COMPUTE THE NEW STEP.
C
CALL MCSTEP(STX,FX,DGX,STY,FY,DGY,STP,F,DG,
* BRACKT,STMIN,STMAX,INFOC)
END IF
C
C FORCE A SUFFICIENT DECREASE IN THE SIZE OF THE
C INTERVAL OF UNCERTAINTY.
C
IF (BRACKT) THEN
IF (ABS(STY-STX) .GE. P66*WIDTH1)
* STP = STX + P5*(STY - STX)
WIDTH1 = WIDTH
WIDTH = ABS(STY-STX)
END IF
C
C END OF ITERATION.
C
GO TO 30
C
C LAST LINE OF SUBROUTINE MCSRCH.
C
END
SUBROUTINE MCSTEP(STX,FX,DX,STY,FY,DY,STP,FP,DP,BRACKT,
* STPMIN,STPMAX,INFO)
INTEGER INFO
DOUBLE PRECISION STX,FX,DX,STY,FY,DY,STP,FP,DP,STPMIN,STPMAX
LOGICAL BRACKT,BOUND
C
C SUBROUTINE MCSTEP
C
C THE PURPOSE OF MCSTEP IS TO COMPUTE A SAFEGUARDED STEP FOR
C A LINESEARCH AND TO UPDATE AN INTERVAL OF UNCERTAINTY FOR
C A MINIMIZER OF THE FUNCTION.
C
C THE PARAMETER STX CONTAINS THE STEP WITH THE LEAST FUNCTION
C VALUE. THE PARAMETER STP CONTAINS THE CURRENT STEP. IT IS
C ASSUMED THAT THE DERIVATIVE AT STX IS NEGATIVE IN THE
C DIRECTION OF THE STEP. IF BRACKT IS SET TRUE THEN A
C MINIMIZER HAS BEEN BRACKETED IN AN INTERVAL OF UNCERTAINTY
C WITH ENDPOINTS STX AND STY.
C
C THE SUBROUTINE STATEMENT IS
C
C SUBROUTINE MCSTEP(STX,FX,DX,STY,FY,DY,STP,FP,DP,BRACKT,
C STPMIN,STPMAX,INFO)
C
C WHERE
C
C STX, FX, AND DX ARE VARIABLES WHICH SPECIFY THE STEP,
C THE FUNCTION, AND THE DERIVATIVE AT THE BEST STEP OBTAINED
C SO FAR. THE DERIVATIVE MUST BE NEGATIVE IN THE DIRECTION
C OF THE STEP, THAT IS, DX AND STP-STX MUST HAVE OPPOSITE
C SIGNS. ON OUTPUT THESE PARAMETERS ARE UPDATED APPROPRIATELY.
C
C STY, FY, AND DY ARE VARIABLES WHICH SPECIFY THE STEP,
C THE FUNCTION, AND THE DERIVATIVE AT THE OTHER ENDPOINT OF
C THE INTERVAL OF UNCERTAINTY. ON OUTPUT THESE PARAMETERS ARE
C UPDATED APPROPRIATELY.
C
C STP, FP, AND DP ARE VARIABLES WHICH SPECIFY THE STEP,
C THE FUNCTION, AND THE DERIVATIVE AT THE CURRENT STEP.
C IF BRACKT IS SET TRUE THEN ON INPUT STP MUST BE
C BETWEEN STX AND STY. ON OUTPUT STP IS SET TO THE NEW STEP.
C
C BRACKT IS A LOGICAL VARIABLE WHICH SPECIFIES IF A MINIMIZER
C HAS BEEN BRACKETED. IF THE MINIMIZER HAS NOT BEEN BRACKETED
C THEN ON INPUT BRACKT MUST BE SET FALSE. IF THE MINIMIZER
C IS BRACKETED THEN ON OUTPUT BRACKT IS SET TRUE.
C
C STPMIN AND STPMAX ARE INPUT VARIABLES WHICH SPECIFY LOWER
C AND UPPER BOUNDS FOR THE STEP.
C
C INFO IS AN INTEGER OUTPUT VARIABLE SET AS FOLLOWS:
C IF INFO = 1,2,3,4,5, THEN THE STEP HAS BEEN COMPUTED
C ACCORDING TO ONE OF THE FIVE CASES BELOW. OTHERWISE
C INFO = 0, AND THIS INDICATES IMPROPER INPUT PARAMETERS.
C
C SUBPROGRAMS CALLED
C
C FORTRAN-SUPPLIED ... ABS,MAX,MIN,SQRT
C
C ARGONNE NATIONAL LABORATORY. MINPACK PROJECT. JUNE 1983
C JORGE J. MORE', DAVID J. THUENTE
C
DOUBLE PRECISION GAMMA,P,Q,R,S,SGND,STPC,STPF,STPQ,THETA
INFO = 0
C
C CHECK THE INPUT PARAMETERS FOR ERRORS.
C
IF ((BRACKT .AND. (STP .LE. MIN(STX,STY) .OR.
* STP .GE. MAX(STX,STY))) .OR.
* DX*(STP-STX) .GE. 0.D0 .OR. STPMAX .LT. STPMIN) RETURN
C
C DETERMINE IF THE DERIVATIVES HAVE OPPOSITE SIGN.
C
SGND = DP*(DX/ABS(DX))
C
C FIRST CASE. A HIGHER FUNCTION VALUE.
C THE MINIMUM IS BRACKETED. IF THE CUBIC STEP IS CLOSER
C TO STX THAN THE QUADRATIC STEP, THE CUBIC STEP IS TAKEN,
C ELSE THE AVERAGE OF THE CUBIC AND QUADRATIC STEPS IS TAKEN.
C
IF (FP .GT. FX) THEN
INFO = 1
BOUND = .TRUE.
THETA = 3*(FX - FP)/(STP - STX) + DX + DP
S = MAX(ABS(THETA),ABS(DX),ABS(DP))
GAMMA = S*SQRT((THETA/S)**2 - (DX/S)*(DP/S))
IF (STP .LT. STX) GAMMA = -GAMMA
P = (GAMMA - DX) + THETA
Q = ((GAMMA - DX) + GAMMA) + DP
R = P/Q
STPC = STX + R*(STP - STX)
STPQ = STX + ((DX/((FX-FP)/(STP-STX)+DX))/2)*(STP - STX)
IF (ABS(STPC-STX) .LT. ABS(STPQ-STX)) THEN
STPF = STPC
ELSE
STPF = STPC + (STPQ - STPC)/2
END IF
BRACKT = .TRUE.
C
C SECOND CASE. A LOWER FUNCTION VALUE AND DERIVATIVES OF
C OPPOSITE SIGN. THE MINIMUM IS BRACKETED. IF THE CUBIC
C STEP IS CLOSER TO STX THAN THE QUADRATIC (SECANT) STEP,
C THE CUBIC STEP IS TAKEN, ELSE THE QUADRATIC STEP IS TAKEN.
C
ELSE IF (SGND .LT. 0.D0) THEN
INFO = 2
BOUND = .FALSE.
THETA = 3*(FX - FP)/(STP - STX) + DX + DP
S = MAX(ABS(THETA),ABS(DX),ABS(DP))
GAMMA = S*SQRT((THETA/S)**2 - (DX/S)*(DP/S))
IF (STP .GT. STX) GAMMA = -GAMMA
P = (GAMMA - DP) + THETA
Q = ((GAMMA - DP) + GAMMA) + DX
R = P/Q
STPC = STP + R*(STX - STP)
STPQ = STP + (DP/(DP-DX))*(STX - STP)
IF (ABS(STPC-STP) .GT. ABS(STPQ-STP)) THEN
STPF = STPC
ELSE
STPF = STPQ
END IF
BRACKT = .TRUE.
C
C THIRD CASE. A LOWER FUNCTION VALUE, DERIVATIVES OF THE
C SAME SIGN, AND THE MAGNITUDE OF THE DERIVATIVE DECREASES.
C THE CUBIC STEP IS ONLY USED IF THE CUBIC TENDS TO INFINITY
C IN THE DIRECTION OF THE STEP OR IF THE MINIMUM OF THE CUBIC
C IS BEYOND STP. OTHERWISE THE CUBIC STEP IS DEFINED TO BE
C EITHER STPMIN OR STPMAX. THE QUADRATIC (SECANT) STEP IS ALSO
C COMPUTED AND IF THE MINIMUM IS BRACKETED THEN THE THE STEP
C CLOSEST TO STX IS TAKEN, ELSE THE STEP FARTHEST AWAY IS TAKEN.
C
ELSE IF (ABS(DP) .LT. ABS(DX)) THEN
INFO = 3
BOUND = .TRUE.
THETA = 3*(FX - FP)/(STP - STX) + DX + DP
S = MAX(ABS(THETA),ABS(DX),ABS(DP))
C
C THE CASE GAMMA = 0 ONLY ARISES IF THE CUBIC DOES NOT TEND
C TO INFINITY IN THE DIRECTION OF THE STEP.
C
GAMMA = S*SQRT(MAX(0.0D0,(THETA/S)**2 - (DX/S)*(DP/S)))
IF (STP .GT. STX) GAMMA = -GAMMA
P = (GAMMA - DP) + THETA
Q = (GAMMA + (DX - DP)) + GAMMA
R = P/Q
IF (R .LT. 0.0 .AND. GAMMA .NE. 0.0) THEN
STPC = STP + R*(STX - STP)
ELSE IF (STP .GT. STX) THEN
STPC = STPMAX
ELSE
STPC = STPMIN
END IF
STPQ = STP + (DP/(DP-DX))*(STX - STP)
IF (BRACKT) THEN
IF (ABS(STP-STPC) .LT. ABS(STP-STPQ)) THEN
STPF = STPC
ELSE
STPF = STPQ
END IF
ELSE
IF (ABS(STP-STPC) .GT. ABS(STP-STPQ)) THEN
STPF = STPC
ELSE
STPF = STPQ
END IF
END IF
C
C FOURTH CASE. A LOWER FUNCTION VALUE, DERIVATIVES OF THE
C SAME SIGN, AND THE MAGNITUDE OF THE DERIVATIVE DOES
C NOT DECREASE. IF THE MINIMUM IS NOT BRACKETED, THE STEP
C IS EITHER STPMIN OR STPMAX, ELSE THE CUBIC STEP IS TAKEN.
C
ELSE
INFO = 4
BOUND = .FALSE.
IF (BRACKT) THEN
THETA = 3*(FP - FY)/(STY - STP) + DY + DP
S = MAX(ABS(THETA),ABS(DY),ABS(DP))
GAMMA = S*SQRT((THETA/S)**2 - (DY/S)*(DP/S))
IF (STP .GT. STY) GAMMA = -GAMMA
P = (GAMMA - DP) + THETA
Q = ((GAMMA - DP) + GAMMA) + DY
R = P/Q
STPC = STP + R*(STY - STP)
STPF = STPC
ELSE IF (STP .GT. STX) THEN
STPF = STPMAX
ELSE
STPF = STPMIN
END IF
END IF
C
C UPDATE THE INTERVAL OF UNCERTAINTY. THIS UPDATE DOES NOT
C DEPEND ON THE NEW STEP OR THE CASE ANALYSIS ABOVE.
C
IF (FP .GT. FX) THEN
STY = STP
FY = FP
DY = DP
ELSE
IF (SGND .LT. 0.0) THEN
STY = STX
FY = FX
DY = DX
END IF
STX = STP
FX = FP
DX = DP
END IF
C
C COMPUTE THE NEW STEP AND SAFEGUARD IT.
C
STPF = MIN(STPMAX,STPF)
STPF = MAX(STPMIN,STPF)
STP = STPF
IF (BRACKT .AND. BOUND) THEN
IF (STY .GT. STX) THEN
STP = MIN(STX+0.66D0*(STY-STX),STP)
ELSE
STP = MAX(STX+0.66D0*(STY-STX),STP)
END IF
END IF
RETURN
C
C LAST LINE OF SUBROUTINE MCSTEP.
C
END