-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbobyqa_ArraysSpecified.f
2100 lines (2084 loc) · 72.8 KB
/
bobyqa_ArraysSpecified.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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
c 6 Aug 2011
c I have gone through and given bounds on dummy arrays when possible to try and determine
c memeory overwrite problem. Have basically replaced assumed size arrays with explicit-shaped arrays.
c 10 Mar 2012
c Recalling from aug 2011, I believe the problem was not this routine, but rather the TSPACK routine used to
C determine spline fits.
SUBROUTINE BOBYQA (N,NPT,X,XL,XU,RHOBEG,RHOEND,IPRINT,
1 MAXFUN,W)
IMPLICIT REAL*8 (A-H,O-Z)
c DIMENSION X(*),XL(*),XU(*),W(*)
DIMENSION X(n),XL(n),XU(n),W(*)
C
C This subroutine seeks the least value of a function of many variables,
C by applying a trust region method that forms quadratic models by
C interpolation. There is usually some freedom in the interpolation
C conditions, which is taken up by minimizing the Frobenius norm of
C the change to the second derivative of the model, beginning with the
C zero matrix. The values of the variables are constrained by upper and
C lower bounds. The arguments of the subroutine are as follows.
C
C N must be set to the number of variables and must be at least two.
C NPT is the number of interpolation conditions. Its value must be in
C the interval [N+2,(N+1)(N+2)/2]. Choices that exceed 2*N+1 are not
C recommended.
C Initial values of the variables must be set in X(1),X(2),...,X(N). They
C will be changed to the values that give the least calculated F.
C For I=1,2,...,N, XL(I) and XU(I) must provide the lower and upper
C bounds, respectively, on X(I). The construction of quadratic models
C requires XL(I) to be strictly less than XU(I) for each I. Further,
C the contribution to a model from changes to the I-th variable is
C damaged severely by rounding errors if XU(I)-XL(I) is too small.
C RHOBEG and RHOEND must be set to the initial and final values of a trust
C region radius, so both must be positive with RHOEND no greater than
C RHOBEG. Typically, RHOBEG should be about one tenth of the greatest
C expected change to a variable, while RHOEND should indicate the
C accuracy that is required in the final values of the variables. An
C error return occurs if any of the differences XU(I)-XL(I), I=1,...,N,
C is less than 2*RHOBEG.
C The value of IPRINT should be set to 0, 1, 2 or 3, which controls the
C amount of printing. Specifically, there is no output if IPRINT=0 and
C there is output only at the return if IPRINT=1. Otherwise, each new
C value of RHO is printed, with the best vector of variables so far and
C the corresponding value of the objective function. Further, each new
C value of F with its variables are output if IPRINT=3.
C MAXFUN must be set to an upper bound on the number of calls of CALFUN.
C The array W will be used for working space. Its length must be at least
C (NPT+5)*(NPT+N)+3*N*(N+5)/2.
C
C SUBROUTINE CALFUN (N,X,F) has to be provided by the user. It must set
C F to the value of the objective function for the current values of the
C variables X(1),X(2),...,X(N), which are generated automatically in a
C way that satisfies the bounds given in XL and XU.
C
C Return if the value of NPT is unacceptable.
C
NP=N+1
IF (NPT .LT. N+2 .OR. NPT .GT. ((N+2)*NP)/2) THEN
PRINT 10
10 FORMAT (/4X,'Return from BOBYQA because NPT is not in',
1 ' the required interval')
GO TO 40
END IF
C
C Partition the working space array, so that different parts of it can
C be treated separately during the calculation of BOBYQB. The partition
C requires the first (NPT+2)*(NPT+N)+3*N*(N+5)/2 elements of W plus the
C space that is taken by the last array in the argument list of BOBYQB.
C
NDIM=NPT+N
IXB=1
IXP=IXB+N
IFV=IXP+N*NPT
IXO=IFV+NPT
IGO=IXO+N
IHQ=IGO+N
IPQ=IHQ+(N*NP)/2
IBMAT=IPQ+NPT
IZMAT=IBMAT+NDIM*N
ISL=IZMAT+NPT*(NPT-NP)
ISU=ISL+N
IXN=ISU+N
IXA=IXN+N
ID=IXA+N
IVL=ID+N
IW=IVL+NDIM
C
C Return if there is insufficient space between the bounds. Modify the
C initial X if necessary in order to avoid conflicts between the bounds
C and the construction of the first quadratic model. The lower and upper
C bounds on moves from the updated X are set now, in the ISL and ISU
C partitions of W, in order to provide useful and exact information about
C components of X that become within distance RHOBEG from their bounds.
C
ZERO=0.0D0
DO 30 J=1,N
TEMP=XU(J)-XL(J)
IF (TEMP .LT. RHOBEG+RHOBEG) THEN
PRINT 20
20 FORMAT (/4X,'Return from BOBYQA because one of the',
1 ' differences XU(I)-XL(I)'/6X,' is less than 2*RHOBEG.')
GO TO 40
END IF
JSL=ISL+J-1
JSU=JSL+N
W(JSL)=XL(J)-X(J)
W(JSU)=XU(J)-X(J)
IF (W(JSL) .GE. -RHOBEG) THEN
IF (W(JSL) .GE. ZERO) THEN
X(J)=XL(J)
W(JSL)=ZERO
W(JSU)=TEMP
ELSE
X(J)=XL(J)+RHOBEG
W(JSL)=-RHOBEG
W(JSU)=DMAX1(XU(J)-X(J),RHOBEG)
END IF
ELSE IF (W(JSU) .LE. RHOBEG) THEN
IF (W(JSU) .LE. ZERO) THEN
X(J)=XU(J)
W(JSL)=-TEMP
W(JSU)=ZERO
ELSE
X(J)=XU(J)-RHOBEG
W(JSL)=DMIN1(XL(J)-X(J),-RHOBEG)
W(JSU)=RHOBEG
END IF
END IF
30 CONTINUE
C
C Make the call of BOBYQB.
C
CALL BOBYQB (N,NPT,X,XL,XU,RHOBEG,RHOEND,IPRINT,MAXFUN,W(IXB),
1 W(IXP),W(IFV),W(IXO),W(IGO),W(IHQ),W(IPQ),W(IBMAT),W(IZMAT),
2 NDIM,W(ISL),W(ISU),W(IXN),W(IXA),W(ID),W(IVL),W(IW))
40 RETURN
END
SUBROUTINE BOBYQB (N,NPT,X,XL,XU,RHOBEG,RHOEND,IPRINT,
1 MAXFUN,XBASE,XPT,FVAL,XOPT,GOPT,HQ,PQ,BMAT,ZMAT,NDIM,
2 SL,SU,XNEW,XALT,D,VLAG,W)
IMPLICIT REAL*8 (A-H,O-Z)
c DIMENSION X(*),XL(*),XU(*),XBASE(*),XPT(NPT,*),FVAL(*),
c 1 XOPT(*),GOPT(*),HQ(*),PQ(*),BMAT(NDIM,*),ZMAT(NPT,*),
c 2 SL(*),SU(*),XNEW(*),XALT(*),D(*),VLAG(*),W(*)
DIMENSION X(n),XL(n),XU(n),XBASE(*),XPT(NPT,*),FVAL(*),
1 XOPT(*),GOPT(*),HQ(*),PQ(*),BMAT(NDIM,*),ZMAT(NPT,*),
2 SL(*),SU(*),XNEW(*),XALT(*),D(*),VLAG(*),W(*)
C
C The arguments N, NPT, X, XL, XU, RHOBEG, RHOEND, IPRINT and MAXFUN
C are identical to the corresponding arguments in SUBROUTINE BOBYQA.
C XBASE holds a shift of origin that should reduce the contributions
C from rounding errors to values of the model and Lagrange functions.
C XPT is a two-dimensional array that holds the coordinates of the
C interpolation points relative to XBASE.
C FVAL holds the values of F at the interpolation points.
C XOPT is set to the displacement from XBASE of the trust region centre.
C GOPT holds the gradient of the quadratic model at XBASE+XOPT.
C HQ holds the explicit second derivatives of the quadratic model.
C PQ contains the parameters of the implicit second derivatives of the
C quadratic model.
C BMAT holds the last N columns of H.
C ZMAT holds the factorization of the leading NPT by NPT submatrix of H,
C this factorization being ZMAT times ZMAT^T, which provides both the
C correct rank and positive semi-definiteness.
C NDIM is the first dimension of BMAT and has the value NPT+N.
C SL and SU hold the differences XL-XBASE and XU-XBASE, respectively.
C All the components of every XOPT are going to satisfy the bounds
C SL(I) .LEQ. XOPT(I) .LEQ. SU(I), with appropriate equalities when
C XOPT is on a constraint boundary.
C XNEW is chosen by SUBROUTINE TRSBOX or ALTMOV. Usually XBASE+XNEW is the
C vector of variables for the next call of CALFUN. XNEW also satisfies
C the SL and SU constraints in the way that has just been mentioned.
C XALT is an alternative to XNEW, chosen by ALTMOV, that may replace XNEW
C in order to increase the denominator in the updating of UPDATE.
C D is reserved for a trial step from XOPT, which is usually XNEW-XOPT.
C VLAG contains the values of the Lagrange functions at a new point X.
C They are part of a product that requires VLAG to be of length NDIM.
C W is a one-dimensional array that is used for working space. Its length
C must be at least 3*NDIM = 3*(NPT+N).
C
C Set some constants.
C
HALF=0.5D0
ONE=1.0D0
TEN=10.0D0
TENTH=0.1D0
TWO=2.0D0
ZERO=0.0D0
NP=N+1
NPTM=NPT-NP
NH=(N*NP)/2
C
C The call of PRELIM sets the elements of XBASE, XPT, FVAL, GOPT, HQ, PQ,
C BMAT and ZMAT for the first iteration, with the corresponding values of
C of NF and KOPT, which are the number of calls of CALFUN so far and the
C index of the interpolation point at the trust region centre. Then the
C initial XOPT is set too. The branch to label 720 occurs if MAXFUN is
C less than NPT. GOPT will be updated if KOPT is different from KBASE.
C
CALL PRELIM (N,NPT,X,XL,XU,RHOBEG,IPRINT,MAXFUN,XBASE,XPT,
1 FVAL,GOPT,HQ,PQ,BMAT,ZMAT,NDIM,SL,SU,NF,KOPT)
XOPTSQ=ZERO
DO 10 I=1,N
XOPT(I)=XPT(KOPT,I)
10 XOPTSQ=XOPTSQ+XOPT(I)**2
FSAVE=FVAL(1)
IF (NF .LT. NPT) THEN
IF (IPRINT .GT. 0) PRINT 390
GOTO 720
END IF
KBASE=1
C
C Complete the settings that are required for the iterative procedure.
C
RHO=RHOBEG
DELTA=RHO
NRESC=NF
NTRITS=0
DIFFA=ZERO
DIFFB=ZERO
ITEST=0
NFSAV=NF
C
C Update GOPT if necessary before the first iteration and after each
C call of RESCUE that makes a call of CALFUN.
C
20 IF (KOPT .NE. KBASE) THEN
IH=0
DO 30 J=1,N
DO 30 I=1,J
IH=IH+1
IF (I .LT. J) GOPT(J)=GOPT(J)+HQ(IH)*XOPT(I)
30 GOPT(I)=GOPT(I)+HQ(IH)*XOPT(J)
IF (NF .GT. NPT) THEN
DO 50 K=1,NPT
TEMP=ZERO
DO 40 J=1,N
40 TEMP=TEMP+XPT(K,J)*XOPT(J)
TEMP=PQ(K)*TEMP
DO 50 I=1,N
50 GOPT(I)=GOPT(I)+TEMP*XPT(K,I)
END IF
END IF
C
C Generate the next point in the trust region that provides a small value
C of the quadratic model subject to the constraints on the variables.
C The integer NTRITS is set to the number "trust region" iterations that
C have occurred since the last "alternative" iteration. If the length
C of XNEW-XOPT is less than HALF*RHO, however, then there is a branch to
C label 650 or 680 with NTRITS=-1, instead of calculating F at XNEW.
C
60 CALL TRSBOX (N,NPT,XPT,XOPT,GOPT,HQ,PQ,SL,SU,DELTA,XNEW,D,
1 W,W(NP),W(NP+N),W(NP+2*N),W(NP+3*N),DSQ,CRVMIN)
DNORM=DMIN1(DELTA,DSQRT(DSQ))
IF (DNORM .LT. HALF*RHO) THEN
NTRITS=-1
DISTSQ=(TEN*RHO)**2
IF (NF .LE. NFSAV+2) GOTO 650
C
C The following choice between labels 650 and 680 depends on whether or
C not our work with the current RHO seems to be complete. Either RHO is
C decreased or termination occurs if the errors in the quadratic model at
C the last three interpolation points compare favourably with predictions
C of likely improvements to the model within distance HALF*RHO of XOPT.
C
ERRBIG=DMAX1(DIFFA,DIFFB,DIFFC)
FRHOSQ=0.125D0*RHO*RHO
IF (CRVMIN .GT. ZERO .AND. ERRBIG .GT. FRHOSQ*CRVMIN)
1 GOTO 650
BDTOL=ERRBIG/RHO
DO 80 J=1,N
BDTEST=BDTOL
IF (XNEW(J) .EQ. SL(J)) BDTEST=W(J)
IF (XNEW(J) .EQ. SU(J)) BDTEST=-W(J)
IF (BDTEST .LT. BDTOL) THEN
CURV=HQ((J+J*J)/2)
DO 70 K=1,NPT
70 CURV=CURV+PQ(K)*XPT(K,J)**2
BDTEST=BDTEST+HALF*CURV*RHO
IF (BDTEST .LT. BDTOL) GOTO 650
END IF
80 CONTINUE
GOTO 680
END IF
NTRITS=NTRITS+1
C
C Severe cancellation is likely to occur if XOPT is too far from XBASE.
C If the following test holds, then XBASE is shifted so that XOPT becomes
C zero. The appropriate changes are made to BMAT and to the second
C derivatives of the current model, beginning with the changes to BMAT
C that do not depend on ZMAT. VLAG is used temporarily for working space.
C
90 IF (DSQ .LE. 1.0D-3*XOPTSQ) THEN
FRACSQ=0.25D0*XOPTSQ
SUMPQ=ZERO
DO 110 K=1,NPT
SUMPQ=SUMPQ+PQ(K)
SUM=-HALF*XOPTSQ
DO 100 I=1,N
100 SUM=SUM+XPT(K,I)*XOPT(I)
W(NPT+K)=SUM
TEMP=FRACSQ-HALF*SUM
DO 110 I=1,N
W(I)=BMAT(K,I)
VLAG(I)=SUM*XPT(K,I)+TEMP*XOPT(I)
IP=NPT+I
DO 110 J=1,I
110 BMAT(IP,J)=BMAT(IP,J)+W(I)*VLAG(J)+VLAG(I)*W(J)
C
C Then the revisions of BMAT that depend on ZMAT are calculated.
C
DO 150 JJ=1,NPTM
SUMZ=ZERO
SUMW=ZERO
DO 120 K=1,NPT
SUMZ=SUMZ+ZMAT(K,JJ)
VLAG(K)=W(NPT+K)*ZMAT(K,JJ)
120 SUMW=SUMW+VLAG(K)
DO 140 J=1,N
SUM=(FRACSQ*SUMZ-HALF*SUMW)*XOPT(J)
DO 130 K=1,NPT
130 SUM=SUM+VLAG(K)*XPT(K,J)
W(J)=SUM
DO 140 K=1,NPT
140 BMAT(K,J)=BMAT(K,J)+SUM*ZMAT(K,JJ)
DO 150 I=1,N
IP=I+NPT
TEMP=W(I)
DO 150 J=1,I
150 BMAT(IP,J)=BMAT(IP,J)+TEMP*W(J)
C
C The following instructions complete the shift, including the changes
C to the second derivative parameters of the quadratic model.
C
IH=0
DO 170 J=1,N
W(J)=-HALF*SUMPQ*XOPT(J)
DO 160 K=1,NPT
W(J)=W(J)+PQ(K)*XPT(K,J)
160 XPT(K,J)=XPT(K,J)-XOPT(J)
DO 170 I=1,J
IH=IH+1
HQ(IH)=HQ(IH)+W(I)*XOPT(J)+XOPT(I)*W(J)
170 BMAT(NPT+I,J)=BMAT(NPT+J,I)
DO 180 I=1,N
XBASE(I)=XBASE(I)+XOPT(I)
XNEW(I)=XNEW(I)-XOPT(I)
SL(I)=SL(I)-XOPT(I)
SU(I)=SU(I)-XOPT(I)
180 XOPT(I)=ZERO
XOPTSQ=ZERO
END IF
IF (NTRITS .EQ. 0) GOTO 210
GOTO 230
C
C XBASE is also moved to XOPT by a call of RESCUE. This calculation is
C more expensive than the previous shift, because new matrices BMAT and
C ZMAT are generated from scratch, which may include the replacement of
C interpolation points whose positions seem to be causing near linear
C dependence in the interpolation conditions. Therefore RESCUE is called
C only if rounding errors have reduced by at least a factor of two the
C denominator of the formula for updating the H matrix. It provides a
C useful safeguard, but is not invoked in most applications of BOBYQA.
C
190 NFSAV=NF
KBASE=KOPT
CALL RESCUE (N,NPT,XL,XU,IPRINT,MAXFUN,XBASE,XPT,FVAL,
1 XOPT,GOPT,HQ,PQ,BMAT,ZMAT,NDIM,SL,SU,NF,DELTA,KOPT,
2 VLAG,W,W(N+NP),W(NDIM+NP))
C
C XOPT is updated now in case the branch below to label 720 is taken.
C Any updating of GOPT occurs after the branch below to label 20, which
C leads to a trust region iteration as does the branch to label 60.
C
XOPTSQ=ZERO
IF (KOPT .NE. KBASE) THEN
DO 200 I=1,N
XOPT(I)=XPT(KOPT,I)
200 XOPTSQ=XOPTSQ+XOPT(I)**2
END IF
IF (NF .LT. 0) THEN
NF=MAXFUN
IF (IPRINT .GT. 0) PRINT 390
GOTO 720
END IF
NRESC=NF
IF (NFSAV .LT. NF) THEN
NFSAV=NF
GOTO 20
END IF
IF (NTRITS .GT. 0) GOTO 60
C
C Pick two alternative vectors of variables, relative to XBASE, that
C are suitable as new positions of the KNEW-th interpolation point.
C Firstly, XNEW is set to the point on a line through XOPT and another
C interpolation point that minimizes the predicted value of the next
C denominator, subject to ||XNEW - XOPT|| .LEQ. ADELT and to the SL
C and SU bounds. Secondly, XALT is set to the best feasible point on
C a constrained version of the Cauchy step of the KNEW-th Lagrange
C function, the corresponding value of the square of this function
C being returned in CAUCHY. The choice between these alternatives is
C going to be made when the denominator is calculated.
C
210 CALL ALTMOV (N,NPT,XPT,XOPT,BMAT,ZMAT,NDIM,SL,SU,KOPT,
1 KNEW,ADELT,XNEW,XALT,ALPHA,CAUCHY,W,W(NP),W(NDIM+1))
DO 220 I=1,N
220 D(I)=XNEW(I)-XOPT(I)
C
C Calculate VLAG and BETA for the current choice of D. The scalar
C product of D with XPT(K,.) is going to be held in W(NPT+K) for
C use when VQUAD is calculated.
C
230 DO 250 K=1,NPT
SUMA=ZERO
SUMB=ZERO
SUM=ZERO
DO 240 J=1,N
SUMA=SUMA+XPT(K,J)*D(J)
SUMB=SUMB+XPT(K,J)*XOPT(J)
240 SUM=SUM+BMAT(K,J)*D(J)
W(K)=SUMA*(HALF*SUMA+SUMB)
VLAG(K)=SUM
250 W(NPT+K)=SUMA
BETA=ZERO
DO 270 JJ=1,NPTM
SUM=ZERO
DO 260 K=1,NPT
260 SUM=SUM+ZMAT(K,JJ)*W(K)
BETA=BETA-SUM*SUM
DO 270 K=1,NPT
270 VLAG(K)=VLAG(K)+SUM*ZMAT(K,JJ)
DSQ=ZERO
BSUM=ZERO
DX=ZERO
DO 300 J=1,N
DSQ=DSQ+D(J)**2
SUM=ZERO
DO 280 K=1,NPT
280 SUM=SUM+W(K)*BMAT(K,J)
BSUM=BSUM+SUM*D(J)
JP=NPT+J
DO 290 I=1,N
290 SUM=SUM+BMAT(JP,I)*D(I)
VLAG(JP)=SUM
BSUM=BSUM+SUM*D(J)
300 DX=DX+D(J)*XOPT(J)
BETA=DX*DX+DSQ*(XOPTSQ+DX+DX+HALF*DSQ)+BETA-BSUM
VLAG(KOPT)=VLAG(KOPT)+ONE
C
C If NTRITS is zero, the denominator may be increased by replacing
C the step D of ALTMOV by a Cauchy step. Then RESCUE may be called if
C rounding errors have damaged the chosen denominator.
C
IF (NTRITS .EQ. 0) THEN
DENOM=VLAG(KNEW)**2+ALPHA*BETA
IF (DENOM .LT. CAUCHY .AND. CAUCHY .GT. ZERO) THEN
DO 310 I=1,N
XNEW(I)=XALT(I)
310 D(I)=XNEW(I)-XOPT(I)
CAUCHY=ZERO
GO TO 230
END IF
IF (DENOM .LE. HALF*VLAG(KNEW)**2) THEN
IF (NF .GT. NRESC) GOTO 190
IF (IPRINT .GT. 0) PRINT 320
320 FORMAT (/5X,'Return from BOBYQA because of much',
1 ' cancellation in a denominator.')
GOTO 720
END IF
C
C Alternatively, if NTRITS is positive, then set KNEW to the index of
C the next interpolation point to be deleted to make room for a trust
C region step. Again RESCUE may be called if rounding errors have damaged
C the chosen denominator, which is the reason for attempting to select
C KNEW before calculating the next value of the objective function.
C
ELSE
DELSQ=DELTA*DELTA
SCADEN=ZERO
BIGLSQ=ZERO
KNEW=0
DO 350 K=1,NPT
IF (K .EQ. KOPT) GOTO 350
HDIAG=ZERO
DO 330 JJ=1,NPTM
330 HDIAG=HDIAG+ZMAT(K,JJ)**2
DEN=BETA*HDIAG+VLAG(K)**2
DISTSQ=ZERO
DO 340 J=1,N
340 DISTSQ=DISTSQ+(XPT(K,J)-XOPT(J))**2
TEMP=DMAX1(ONE,(DISTSQ/DELSQ)**2)
IF (TEMP*DEN .GT. SCADEN) THEN
SCADEN=TEMP*DEN
KNEW=K
DENOM=DEN
END IF
BIGLSQ=DMAX1(BIGLSQ,TEMP*VLAG(K)**2)
350 CONTINUE
IF (SCADEN .LE. HALF*BIGLSQ) THEN
IF (NF .GT. NRESC) GOTO 190
IF (IPRINT .GT. 0) PRINT 320
GOTO 720
END IF
END IF
C
C Put the variables for the next calculation of the objective function
C in XNEW, with any adjustments for the bounds.
C
C
C Calculate the value of the objective function at XBASE+XNEW, unless
C the limit on the number of calculations of F has been reached.
C
360 DO 380 I=1,N
X(I)=DMIN1(DMAX1(XL(I),XBASE(I)+XNEW(I)),XU(I))
IF (XNEW(I) .EQ. SL(I)) X(I)=XL(I)
IF (XNEW(I) .EQ. SU(I)) X(I)=XU(I)
380 CONTINUE
IF (NF .GE. MAXFUN) THEN
IF (IPRINT .GT. 0) PRINT 390
390 FORMAT (/4X,'Return from BOBYQA because CALFUN has been',
1 ' called MAXFUN times.')
GOTO 720
END IF
NF=NF+1
CALL CALFUN (N,X,F)
IF (IPRINT .EQ. 3) THEN
PRINT 400, NF,F,(X(I),I=1,N)
400 FORMAT (/4X,'Function number',I6,' F =',1PD18.10,
1 ' The corresponding X is:'/(2X,5D15.6))
END IF
IF (NTRITS .EQ. -1) THEN
FSAVE=F
GOTO 720
END IF
C
C Use the quadratic model to predict the change in F due to the step D,
C and set DIFF to the error of this prediction.
C
FOPT=FVAL(KOPT)
VQUAD=ZERO
IH=0
DO 410 J=1,N
VQUAD=VQUAD+D(J)*GOPT(J)
DO 410 I=1,J
IH=IH+1
TEMP=D(I)*D(J)
IF (I .EQ. J) TEMP=HALF*TEMP
410 VQUAD=VQUAD+HQ(IH)*TEMP
DO 420 K=1,NPT
420 VQUAD=VQUAD+HALF*PQ(K)*W(NPT+K)**2
DIFF=F-FOPT-VQUAD
DIFFC=DIFFB
DIFFB=DIFFA
DIFFA=DABS(DIFF)
IF (DNORM .GT. RHO) NFSAV=NF
C
C Pick the next value of DELTA after a trust region step.
C
IF (NTRITS .GT. 0) THEN
IF (VQUAD .GE. ZERO) THEN
IF (IPRINT .GT. 0) PRINT 430
430 FORMAT (/4X,'Return from BOBYQA because a trust',
1 ' region step has failed to reduce Q.')
GOTO 720
END IF
RATIO=(F-FOPT)/VQUAD
IF (RATIO .LE. TENTH) THEN
DELTA=DMIN1(HALF*DELTA,DNORM)
ELSE IF (RATIO. LE. 0.7D0) THEN
DELTA=DMAX1(HALF*DELTA,DNORM)
ELSE
DELTA=DMAX1(HALF*DELTA,DNORM+DNORM)
END IF
IF (DELTA .LE. 1.5D0*RHO) DELTA=RHO
C
C Recalculate KNEW and DENOM if the new F is less than FOPT.
C
IF (F .LT. FOPT) THEN
KSAV=KNEW
DENSAV=DENOM
DELSQ=DELTA*DELTA
SCADEN=ZERO
BIGLSQ=ZERO
KNEW=0
DO 460 K=1,NPT
HDIAG=ZERO
DO 440 JJ=1,NPTM
440 HDIAG=HDIAG+ZMAT(K,JJ)**2
DEN=BETA*HDIAG+VLAG(K)**2
DISTSQ=ZERO
DO 450 J=1,N
450 DISTSQ=DISTSQ+(XPT(K,J)-XNEW(J))**2
TEMP=DMAX1(ONE,(DISTSQ/DELSQ)**2)
IF (TEMP*DEN .GT. SCADEN) THEN
SCADEN=TEMP*DEN
KNEW=K
DENOM=DEN
END IF
460 BIGLSQ=DMAX1(BIGLSQ,TEMP*VLAG(K)**2)
IF (SCADEN .LE. HALF*BIGLSQ) THEN
KNEW=KSAV
DENOM=DENSAV
END IF
END IF
END IF
C
C Update BMAT and ZMAT, so that the KNEW-th interpolation point can be
C moved. Also update the second derivative terms of the model.
C
CALL UPDATE (N,NPT,BMAT,ZMAT,NDIM,VLAG,BETA,DENOM,KNEW,W)
IH=0
PQOLD=PQ(KNEW)
PQ(KNEW)=ZERO
DO 470 I=1,N
TEMP=PQOLD*XPT(KNEW,I)
DO 470 J=1,I
IH=IH+1
470 HQ(IH)=HQ(IH)+TEMP*XPT(KNEW,J)
DO 480 JJ=1,NPTM
TEMP=DIFF*ZMAT(KNEW,JJ)
DO 480 K=1,NPT
480 PQ(K)=PQ(K)+TEMP*ZMAT(K,JJ)
C
C Include the new interpolation point, and make the changes to GOPT at
C the old XOPT that are caused by the updating of the quadratic model.
C
FVAL(KNEW)=F
DO 490 I=1,N
XPT(KNEW,I)=XNEW(I)
490 W(I)=BMAT(KNEW,I)
DO 520 K=1,NPT
SUMA=ZERO
DO 500 JJ=1,NPTM
500 SUMA=SUMA+ZMAT(KNEW,JJ)*ZMAT(K,JJ)
SUMB=ZERO
DO 510 J=1,N
510 SUMB=SUMB+XPT(K,J)*XOPT(J)
TEMP=SUMA*SUMB
DO 520 I=1,N
520 W(I)=W(I)+TEMP*XPT(K,I)
DO 530 I=1,N
530 GOPT(I)=GOPT(I)+DIFF*W(I)
C
C Update XOPT, GOPT and KOPT if the new calculated F is less than FOPT.
C
IF (F .LT. FOPT) THEN
KOPT=KNEW
XOPTSQ=ZERO
IH=0
DO 540 J=1,N
XOPT(J)=XNEW(J)
XOPTSQ=XOPTSQ+XOPT(J)**2
DO 540 I=1,J
IH=IH+1
IF (I .LT. J) GOPT(J)=GOPT(J)+HQ(IH)*D(I)
540 GOPT(I)=GOPT(I)+HQ(IH)*D(J)
DO 560 K=1,NPT
TEMP=ZERO
DO 550 J=1,N
550 TEMP=TEMP+XPT(K,J)*D(J)
TEMP=PQ(K)*TEMP
DO 560 I=1,N
560 GOPT(I)=GOPT(I)+TEMP*XPT(K,I)
END IF
C
C Calculate the parameters of the least Frobenius norm interpolant to
C the current data, the gradient of this interpolant at XOPT being put
C into VLAG(NPT+I), I=1,2,...,N.
C
IF (NTRITS .GT. 0) THEN
DO 570 K=1,NPT
VLAG(K)=FVAL(K)-FVAL(KOPT)
570 W(K)=ZERO
DO 590 J=1,NPTM
SUM=ZERO
DO 580 K=1,NPT
580 SUM=SUM+ZMAT(K,J)*VLAG(K)
DO 590 K=1,NPT
590 W(K)=W(K)+SUM*ZMAT(K,J)
DO 610 K=1,NPT
SUM=ZERO
DO 600 J=1,N
600 SUM=SUM+XPT(K,J)*XOPT(J)
W(K+NPT)=W(K)
610 W(K)=SUM*W(K)
GQSQ=ZERO
GISQ=ZERO
DO 630 I=1,N
SUM=ZERO
DO 620 K=1,NPT
620 SUM=SUM+BMAT(K,I)*VLAG(K)+XPT(K,I)*W(K)
IF (XOPT(I) .EQ. SL(I)) THEN
GQSQ=GQSQ+DMIN1(ZERO,GOPT(I))**2
GISQ=GISQ+DMIN1(ZERO,SUM)**2
ELSE IF (XOPT(I) .EQ. SU(I)) THEN
GQSQ=GQSQ+DMAX1(ZERO,GOPT(I))**2
GISQ=GISQ+DMAX1(ZERO,SUM)**2
ELSE
GQSQ=GQSQ+GOPT(I)**2
GISQ=GISQ+SUM*SUM
END IF
630 VLAG(NPT+I)=SUM
C
C Test whether to replace the new quadratic model by the least Frobenius
C norm interpolant, making the replacement if the test is satisfied.
C
ITEST=ITEST+1
IF (GQSQ .LT. TEN*GISQ) ITEST=0
IF (ITEST .GE. 3) THEN
DO 640 I=1,MAX0(NPT,NH)
IF (I .LE. N) GOPT(I)=VLAG(NPT+I)
IF (I .LE. NPT) PQ(I)=W(NPT+I)
IF (I .LE. NH) HQ(I)=ZERO
ITEST=0
640 CONTINUE
END IF
END IF
C
C If a trust region step has provided a sufficient decrease in F, then
C branch for another trust region calculation. The case NTRITS=0 occurs
C when the new interpolation point was reached by an alternative step.
C
IF (NTRITS .EQ. 0) GOTO 60
IF (F .LE. FOPT+TENTH*VQUAD) GOTO 60
C
C Alternatively, find out if the interpolation points are close enough
C to the best point so far.
C
DISTSQ=DMAX1((TWO*DELTA)**2,(TEN*RHO)**2)
650 KNEW=0
DO 670 K=1,NPT
SUM=ZERO
DO 660 J=1,N
660 SUM=SUM+(XPT(K,J)-XOPT(J))**2
IF (SUM .GT. DISTSQ) THEN
KNEW=K
DISTSQ=SUM
END IF
670 CONTINUE
C
C If KNEW is positive, then ALTMOV finds alternative new positions for
C the KNEW-th interpolation point within distance ADELT of XOPT. It is
C reached via label 90. Otherwise, there is a branch to label 60 for
C another trust region iteration, unless the calculations with the
C current RHO are complete.
C
IF (KNEW .GT. 0) THEN
DIST=DSQRT(DISTSQ)
IF (NTRITS .EQ. -1) THEN
DELTA=DMIN1(TENTH*DELTA,HALF*DIST)
IF (DELTA .LE. 1.5D0*RHO) DELTA=RHO
END IF
NTRITS=0
ADELT=DMAX1(DMIN1(TENTH*DIST,DELTA),RHO)
DSQ=ADELT*ADELT
GOTO 90
END IF
IF (NTRITS .EQ. -1) GOTO 680
IF (RATIO .GT. ZERO) GOTO 60
IF (DMAX1(DELTA,DNORM) .GT. RHO) GOTO 60
C
C The calculations with the current value of RHO are complete. Pick the
C next values of RHO and DELTA.
C
680 IF (RHO .GT. RHOEND) THEN
DELTA=HALF*RHO
RATIO=RHO/RHOEND
IF (RATIO .LE. 16.0D0) THEN
RHO=RHOEND
ELSE IF (RATIO .LE. 250.0D0) THEN
RHO=DSQRT(RATIO)*RHOEND
ELSE
RHO=TENTH*RHO
END IF
DELTA=DMAX1(DELTA,RHO)
IF (IPRINT .GE. 2) THEN
IF (IPRINT .GE. 3) PRINT 690
690 FORMAT (5X)
PRINT 700, RHO,NF
700 FORMAT (/4X,'New RHO =',1PD11.4,5X,'Number of',
1 ' function values =',I6)
PRINT 710, FVAL(KOPT),(XBASE(I)+XOPT(I),I=1,N)
710 FORMAT (4X,'Least value of F =',1PD23.15,9X,
1 'The corresponding X is:'/(2X,5D15.6))
END IF
NTRITS=0
NFSAV=NF
GOTO 60
END IF
C
C Return from the calculation, after another Newton-Raphson step, if
C it is too short to have been tried before.
C
IF (NTRITS .EQ. -1) GOTO 360
720 IF (FVAL(KOPT) .LE. FSAVE) THEN
DO 730 I=1,N
X(I)=DMIN1(DMAX1(XL(I),XBASE(I)+XOPT(I)),XU(I))
IF (XOPT(I) .EQ. SL(I)) X(I)=XL(I)
IF (XOPT(I) .EQ. SU(I)) X(I)=XU(I)
730 CONTINUE
F=FVAL(KOPT)
END IF
IF (IPRINT .GE. 1) THEN
PRINT 740, NF
740 FORMAT (/4X,'At the return from BOBYQA',5X,
1 'Number of function values =',I6)
PRINT 710, F,(X(I),I=1,N)
END IF
RETURN
END
SUBROUTINE ALTMOV (N,NPT,XPT,XOPT,BMAT,ZMAT,NDIM,SL,SU,KOPT,
1 KNEW,ADELT,XNEW,XALT,ALPHA,CAUCHY,GLAG,HCOL,W)
IMPLICIT REAL*8 (A-H,O-Z)
DIMENSION XPT(NPT,*),XOPT(*),BMAT(NDIM,*),ZMAT(NPT,*),SL(*),
1 SU(*),XNEW(*),XALT(*),GLAG(*),HCOL(*),W(*)
C
C The arguments N, NPT, XPT, XOPT, BMAT, ZMAT, NDIM, SL and SU all have
C the same meanings as the corresponding arguments of BOBYQB.
C KOPT is the index of the optimal interpolation point.
C KNEW is the index of the interpolation point that is going to be moved.
C ADELT is the current trust region bound.
C XNEW will be set to a suitable new position for the interpolation point
C XPT(KNEW,.). Specifically, it satisfies the SL, SU and trust region
C bounds and it should provide a large denominator in the next call of
C UPDATE. The step XNEW-XOPT from XOPT is restricted to moves along the
C straight lines through XOPT and another interpolation point.
C XALT also provides a large value of the modulus of the KNEW-th Lagrange
C function subject to the constraints that have been mentioned, its main
C difference from XNEW being that XALT-XOPT is a constrained version of
C the Cauchy step within the trust region. An exception is that XALT is
C not calculated if all components of GLAG (see below) are zero.
C ALPHA will be set to the KNEW-th diagonal element of the H matrix.
C CAUCHY will be set to the square of the KNEW-th Lagrange function at
C the step XALT-XOPT from XOPT for the vector XALT that is returned,
C except that CAUCHY is set to zero if XALT is not calculated.
C GLAG is a working space vector of length N for the gradient of the
C KNEW-th Lagrange function at XOPT.
C HCOL is a working space vector of length NPT for the second derivative
C coefficients of the KNEW-th Lagrange function.
C W is a working space vector of length 2N that is going to hold the
C constrained Cauchy step from XOPT of the Lagrange function, followed
C by the downhill version of XALT when the uphill step is calculated.
C
C Set the first NPT components of W to the leading elements of the
C KNEW-th column of the H matrix.
C
HALF=0.5D0
ONE=1.0D0
ZERO=0.0D0
CONST=ONE+DSQRT(2.0D0)
DO 10 K=1,NPT
10 HCOL(K)=ZERO
DO 20 J=1,NPT-N-1
TEMP=ZMAT(KNEW,J)
DO 20 K=1,NPT
20 HCOL(K)=HCOL(K)+TEMP*ZMAT(K,J)
ALPHA=HCOL(KNEW)
HA=HALF*ALPHA
C
C Calculate the gradient of the KNEW-th Lagrange function at XOPT.
C
DO 30 I=1,N
30 GLAG(I)=BMAT(KNEW,I)
DO 50 K=1,NPT
TEMP=ZERO
DO 40 J=1,N
40 TEMP=TEMP+XPT(K,J)*XOPT(J)
TEMP=HCOL(K)*TEMP
DO 50 I=1,N
50 GLAG(I)=GLAG(I)+TEMP*XPT(K,I)
C
C Search for a large denominator along the straight lines through XOPT
C and another interpolation point. SLBD and SUBD will be lower and upper
C bounds on the step along each of these lines in turn. PREDSQ will be
C set to the square of the predicted denominator for each line. PRESAV
C will be set to the largest admissible value of PREDSQ that occurs.
C
PRESAV=ZERO
DO 80 K=1,NPT
IF (K .EQ. KOPT) GOTO 80
DDERIV=ZERO
DISTSQ=ZERO
DO 60 I=1,N
TEMP=XPT(K,I)-XOPT(I)
DDERIV=DDERIV+GLAG(I)*TEMP
60 DISTSQ=DISTSQ+TEMP*TEMP
SUBD=ADELT/DSQRT(DISTSQ)
SLBD=-SUBD
ILBD=0
IUBD=0
SUMIN=DMIN1(ONE,SUBD)
C
C Revise SLBD and SUBD if necessary because of the bounds in SL and SU.
C
DO 70 I=1,N
TEMP=XPT(K,I)-XOPT(I)
IF (TEMP .GT. ZERO) THEN
IF (SLBD*TEMP .LT. SL(I)-XOPT(I)) THEN
SLBD=(SL(I)-XOPT(I))/TEMP
ILBD=-I
END IF
IF (SUBD*TEMP .GT. SU(I)-XOPT(I)) THEN
SUBD=DMAX1(SUMIN,(SU(I)-XOPT(I))/TEMP)
IUBD=I
END IF
ELSE IF (TEMP .LT. ZERO) THEN
IF (SLBD*TEMP .GT. SU(I)-XOPT(I)) THEN
SLBD=(SU(I)-XOPT(I))/TEMP
ILBD=I
END IF
IF (SUBD*TEMP .LT. SL(I)-XOPT(I)) THEN
SUBD=DMAX1(SUMIN,(SL(I)-XOPT(I))/TEMP)
IUBD=-I
END IF
END IF
70 CONTINUE
C
C Seek a large modulus of the KNEW-th Lagrange function when the index
C of the other interpolation point on the line through XOPT is KNEW.
C
IF (K .EQ. KNEW) THEN
DIFF=DDERIV-ONE
STEP=SLBD
VLAG=SLBD*(DDERIV-SLBD*DIFF)
ISBD=ILBD
TEMP=SUBD*(DDERIV-SUBD*DIFF)
IF (DABS(TEMP) .GT. DABS(VLAG)) THEN
STEP=SUBD
VLAG=TEMP
ISBD=IUBD
END IF
TEMPD=HALF*DDERIV
TEMPA=TEMPD-DIFF*SLBD
TEMPB=TEMPD-DIFF*SUBD
IF (TEMPA*TEMPB .LT. ZERO) THEN
TEMP=TEMPD*TEMPD/DIFF
IF (DABS(TEMP) .GT. DABS(VLAG)) THEN
STEP=TEMPD/DIFF
VLAG=TEMP
ISBD=0
END IF
END IF
C
C Search along each of the other lines through XOPT and another point.
C
ELSE
STEP=SLBD
VLAG=SLBD*(ONE-SLBD)
ISBD=ILBD
TEMP=SUBD*(ONE-SUBD)
IF (DABS(TEMP) .GT. DABS(VLAG)) THEN
STEP=SUBD
VLAG=TEMP
ISBD=IUBD
END IF
IF (SUBD .GT. HALF) THEN
IF (DABS(VLAG) .LT. 0.25D0) THEN
STEP=HALF
VLAG=0.25D0
ISBD=0
END IF
END IF
VLAG=VLAG*DDERIV
END IF
C
C Calculate PREDSQ for the current line search and maintain PRESAV.
C
TEMP=STEP*(ONE-STEP)*DISTSQ
PREDSQ=VLAG*VLAG*(VLAG*VLAG+HA*TEMP*TEMP)
IF (PREDSQ .GT. PRESAV) THEN
PRESAV=PREDSQ
KSAV=K
STPSAV=STEP
IBDSAV=ISBD
END IF
80 CONTINUE
C
C Construct XNEW in a way that satisfies the bound constraints exactly.
C
DO 90 I=1,N
TEMP=XOPT(I)+STPSAV*(XPT(KSAV,I)-XOPT(I))
90 XNEW(I)=DMAX1(SL(I),DMIN1(SU(I),TEMP))
IF (IBDSAV .LT. 0) XNEW(-IBDSAV)=SL(-IBDSAV)
IF (IBDSAV .GT. 0) XNEW(IBDSAV)=SU(IBDSAV)
C
C Prepare for the iterative method that assembles the constrained Cauchy
C step in W. The sum of squares of the fixed components of W is formed in
C WFIXSQ, and the free components of W are set to BIGSTP.
C
BIGSTP=ADELT+ADELT
IFLAG=0
100 WFIXSQ=ZERO
GGFREE=ZERO
DO 110 I=1,N
W(I)=ZERO
TEMPA=DMIN1(XOPT(I)-SL(I),GLAG(I))
TEMPB=DMAX1(XOPT(I)-SU(I),GLAG(I))