-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmilalonga.f
2137 lines (1700 loc) · 52.8 KB
/
milalonga.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
subroutine milonga (n, m, x, f, g, a, ifree, ele, u,
* gaux, aux, d, otro, ap, gamma, beta, ktot, nftot, ngtot,
* nintot, nstot, nptot, nbtot, netot, nctot,
* eps, maxtang, maxcg, tol, costol, distol, iopt, iophes, iopfac,
* iflag, ambda, rhoinic, rhofac, rho, h, aj,
* konout, maxout, ipmil, ipt, ier, qcib, alin, ata, nlin,
* evalf, evalg, hessi, hrest, jacobh)
c Subroutine Milonga:
c
c This subroutine solves the problem
c Minimize f(x)
c subject to h(x) = 0
c l \leq x \leq u
c
c where f: R^n ---> R and h:R^n ---> R^m are smooth.
c
c The subroutine has been written (beginning september 2002) by
c J. M. Martinez with didactical purposes. It uses an augmented
c Lagrangian approach, which means that, at each outer iteration,
c the problem
c Minimize f(x) + h(x)^T y + (rho/2) ||h(x)||^2
c subject to l \leq x \leq u
c
c and the Lagrange multipliers (y) and the penalty parameter (rho)
c are updated before the next outer iteration.
c For solving the subproblem above, the subroutine Tango is used.
c Tango solves minimization problems with box-constraints using
c the approach of
c
c E. G. Birgin and J. M. Mart\'{\i}nez. Large-scale active-set
c box-constrained optimization method with spectral projected gradients.
c {\it Computational Optimization and Applications} 23, pp. 101-125 (2002).
c
c However, for minimization within the faces of the box, a
c different algorithm from the one described in this reference is used.
c
c Calling sequence:
c
c call milonga (n, m, x, f, g, a, ifree, ele, u,
c * gaux, aux, d, otro, ap, gamma, beta, ktot, nftot, ngtot,
c * nintot, nstot, nptot, nbtot, netot, nctot,
c * eps, maxtang, maxcg, tol, costol, distol, iopt, iophes, iopfac,
c * iflag, ambda, rhoinic, rhofac, rho, h, aj,
c * konout, maxout, ipmil, ipt, ier, qcib, alin, ata, nlin)
c
c All real parameters are in double precision.
c
c Parameters:
c n: number of variables
c m: number of constraints in h(x)=0.
c x: (n real positions) Initial point - Final point.
c f: final value of f(x).
c g: auxiliar (n real positions)
c a: auxiliar (n^2 real positions)
c ifree: auxiliar (n integer positions)
c ele: lower bounds (n real positions)
c u: upper bounds (n real positions)
c gaux: auxiliar (n real positions)
c aux: auxiliar (n real positions)
c d: auxiliar (n real positions)
c otro: auxiliar (n real positions)
c ap: auxiliar (n real positions)
c gamma: parameter for Armijo-like searches. Recommended: 1.e-4
c beta: parameter of Armijo-like searches. Recommended: 0.5
c ktot: total number of inner (Tango) iterations.
c nftot: functional evaluations in Tango.
c ngtot: gradient evaluations in Tango.
c nintot: number of internal iterations in Tango (iterations where
c the point remains in the same closed face).
c nstot: number of spg iterations in Tango (iterations where a
c closed face is abandoned).
c nptot: number of pure alfa-beta iterations in Tango ( internal iterations
c where both Armijo (alfa) condition and beta condition
c were satisfied with step = 1.
c nbtot: number of internal iterations in Tango where backtracking was
c necessary.
c netot: number of internal
c iterations in Tango where extrapolation was tried.
c nctot: number of internal iterations in Tango where it was detected
c that the (approx) Hessian is not positive definite.
c eps: small positive parameter for declaring convergence (success).
c Milonga converges when both the sup-norm of the constraints
c and the continuous projected gradient is less than or equal to
c eps. Tango runs are declared convergent when the supnorm of
c the projected gradient is less than or equal to eps.
c maxtang: maximum number of iterations allowed at each call of Tango.
c maxcg: maximum number of iterations allowed at each call of the
c conjugate-gradient method, when this is used. If maxcg=-1,
c the maximum number of cg-iterations is automatically set to
c be the number of free variables.
c tol: parameter between 0 and 1 used to decide to leave a closed face
c (using spg) or staying in the face. The face is abandoned when
c the quotient ginor/gpnor is smaller than tol, where gpnor here
c is the euclidian norm of the projected gradient and and ginor
c is the euclidian norm of its projection on the current face.
c costol: tolerance for cosine. If the cosine of the angle between the
c search direction and the negative gradient is less than costol, then
c the search direction is replaced by the negative gradient.
c distol: when the relative difference between the current point and
c the backtracked point is smaller than distol, the algorithm
c Tango returns. This may be because you are using a very
c strict criterion (eps) for the projected gradient.
c iopt: parameter to decide what to do when the Hessian is not positive
c definite. iopt=1 modifies the diagonal to make it diagonally
c dominant. iopt=2 uses conjugate gradients.
c iophes: Parameter that indicates what type of Hessian is being used.
c iophes=1 indicates Discrete Hessian
c iophes=2 indicates that the user provides a subroutine for
c computing the analytic Hessian at an arbitrary given
c point. In this case, the Hessian of the augmented
c Lagrangian used is
c Hessian of the function + rho * A^T A
c where A is the Jacobian of the constraints.
c This is known as the Gauss-Newton approach in
c N. Kreji\'c, J. M. Mart\'{\i}nez, M. P. Mello and E. A. Pilotta.
c Validation of an augmented Lagrangian algorithm with a Gauss-Newton
c Hessian approximation using a set of hard-spheres problems.
c {\it Computational Optimization and
c Applications}~16,~pp.~247-263~(2000).
c See comment of subroutine hessi.
c iophes=3 indicates "quadratic programming problem" where you
c supply the (constant) Hessian and the matrix of constraints
c (when m > 0).
c iopfac: If iopfac=1, an initial Tango iteration is performed with
c only the sum of squares of constraints as objective function.
c This option merely changes the initial point to a more feasible
c one.
c iflag: output parameter that says what happened in the last iteration
c of Tango. If iflag=0, Tango returned with the eps-convergence
c diagnostic. If iflag=1 the maximum allowed Tango-iterations was
c exhausted. If iflag=2, Tango returned because the relative
c difference between a current point and the backtracked point
c was smaller than distol.
c ambda: (m real positions) On input, initial approximation to the
c Lagrange multipliers. On output, final estimates of the
c Lagrange multipliers.
c rhoinic: initial value for the penalty parameter.
c rhofac: factor for which the penalty parameter is multiplied after
c each outer iteration, if necessary. By mystical reasons one
c usually sets rhofac = 10.
c rho: final value of the penalty parameter used.
c h: (m real positions) On output, values of the constraints h(x).
c aj: (m * n real positions) Auxiliar array, used to stored the
c Jacobian of the constraints. Please, when m=0, declare in the
c main program dimension aj(1) (or any other natural number
c greater than 1, instead of 1) in order to avoid compiling errors.
c konout: effective number of outer (milonga) iterations performed.
c maxout: maximum number of outer iterations allowed. Many times, when
c this number is exceeded the reason is that the problem is
c infeasible.
c ipmil: parameter that commands printing of milonga. Milonga prints
c in the screen and in the file milonga.out. If ipmil < 0, nothing
c is printed. If ipmil=0 only initial and final information is
c printed. If ipmil > 0 information of milonga is printed every
c ipmil outer iterations. If m=0 the value of ipmil is irrelevant
c and all printings are commanded by ipt.
c ipt: parameter that commands printing of Tango. Tango prints
c in the screen and in the file milonga.out. If ipt < 0, nothing
c is printed. If ipt=0 only initial and final information is
c printed. If ipt > 0 information of milonga is printed every
c ipt Tango-iterations.
c ier: output parameter that says what happened. If ier=0 convergence
c occurred in the sense that both the supnorm of the constraints
c and the supnorm of the projected gradient are smaller than or
c equal to eps. If ier=1 the number of outer iterations was
c exhausted.
c qcib: Assume that your problem is of the form
c Minimize x^T Q x + c^T x subject to A x = b.
c You can deal with this problem as an ordinary problem with
c ordinary objective function and constraints. But Milonga
c allows you to take advantage with (part of) the structure
c of the problem. If you want to take such advantage, set
c iophes = 3 (see comment of iophes above) and provide the
c matrix Q in the array qcib. So, qcib must be a symmetric
c matrix that must be declared in the main program as having
c a number of rows that also must be informed to Milonga as
c being equal to nlin. That is, the parameter nlin must be
c the exact number of rows declared for qcib in the main program.
c Of course, qcib must have at least n columns.
c alin: In the case that described in the comment of qcib you must
c also provide the matrix A. This matrix must be given in the
c array alin. However, unlike the storage of qcib, alin must be
c stored as a single vector in the main program. The vector
c must store the elementes of A, in columnwise form. Of course,
c alin must have at least m*n entries.
c ata: Again in the case described in the comment of qcib, Milonga
c needs an auxiliary matrix called ata. You must declare this
c matrix as having exactly nlin rows and at least n columns.
c nlin: on entry, this must be the exact number of rows declared for
c qcib and ata in the main program.
c
c The user must also provide four subroutines that give,
c respectively, the objective function value, its gradient,
c the constraints and the Jacobian of the constraints.
c Let us now describe how these subroutines must be.
c
c Subroutine that computes the objective function value:
c Must have the form:
c
c Subroutine evalf (n, x, f)
c implicit double precision (a-h,o-z)
c dimension x(n)
c .....
c return
c end
c
c where n is the number of variables, x is the point where f(x) must
c be computed and f (the output) is the functional value. The user
c must code the subroutine. However, in the case "iophes=3" the user
c can take advantage of the situation merely coding
c
c Subroutine evalf (n, x, f)
c implicit double precision (a-h,o-z)
c dimension x(n)
c dimension aux(134) (say)
c ..............
c nlin = 58 (say)
c call funquad (n, qcib, x, ccib, f, aux, nlin)
c return
c end
c
c In fact, funquad is an internal routine of Milonga. Observe that the
c information qcib and ccib must be passed through common. I'm
c sorry. This will be improved in the future.
c
c Subroutine that computes the gradient of the objective function:
c Must have the form:
c
c Subroutine evalg (n, x, g)
c implicit double precision (a-h, o-z)
c dimension x(n), g(n)
c ...............
c return
c end
c
c In this subroutine, n is the number of variables, x is the point where
c the gradient must be computed and g is the computed gradient.
c
c As in the case of evalf, in the case "iophes=3" the user
c can take advantage of the situation merely coding
c
c Subroutine evalg (n, x, g)
c implicit double precision (a-h,o-z)
c dimension x(n)
c ..............
c nlin = 58 (say)
c call graquad (n, x, qcib, ccib, g, nlin)
c return
c end
c
c In fact, graquad is an internal routine of Milonga. Observe that the
c information qcib and ccib must be passed through common. I'm
c sorry. This will be improved in the future.
c
c Subroutine that computes the constraints h(x):
c Must have the form:
c Subroutine hrest (m, n, x, h)
c implicit double precision (a-h,o-z)
c dimension x(n), h(m)
c .....
c return
c end
c
c where n is the number of variables, m is the number of constraints
c x is the point where the constraints must be computed and h
c is the vector of constraints. No provision is taken, up to now, to
c take advantage of linear constraints here.
c
c Subroutine that computes the Jacobian of the constraints:
c Must have the form:
c subroutine jacobh (m, n, x, aj)
c implicit double precision (a-h, o-z)
c dimension x(n), aj(m, n)
c
c where n is the number of variables, m is the number of constraints
c x is the point where the constraints must be computed and the matrix
c aj must be computed in order to store the derivatives of the constraints.
c aj(i, j) must be the derivative of the i-th constraint with respect
c to the j-th variable. No provision is taken, up to now, to
c take advantage of linear constraints here.
c
c Subroutine that computes the Hessian of the objective function:
c The user must code this subroutine only if iophes=2.
c Must have the form:
c subroutine hessi (n, x, hess)
c implicit double precision (a-h,o-z)
c dimension x(n), hess(n, n)
c ---------
c return
c end
c In fact, observe that this subroutine, exactly in the way described
c above, is part of this package. You must fill the dashed lines if
c you provide the Hessian.
c
implicit double precision (a-h,o-z)
external funtan,gratan,sumsq,grasum
external evalf,evalg,hrest,hessi, jacobh
dimension a(n, n)
dimension ifree(n)
dimension ele(n), u(n)
dimension x(n), g(n)
dimension gaux(n), aux(n)
dimension d(n)
dimension otro(n)
dimension ap(n)
dimension ambda(m)
dimension h(m)
dimension aj(m, n)
dimension qcib(nlin, *)
dimension alin(*)
dimension ata(nlin, *)
n10 = min0(n, 10)
m10 = min0(m, 10)
if(m.gt.0) then
if(rhoinic.eq.0) then
write(*, *)' Error. Initial penalty parameter null.'
stop
endif
rho = rhoinic
if(iophes.eq.3) then
do i = 1, n
do j = 1, n
ata(i, j) = 0.d0
do k = 1, m
ata(i, j) = ata(i, j) + alin((i-1)*m+k) * alin((j-1)*m+k)
end do
end do
end do
end if
endif
c Checking box
do i = 1, n
x(i) = dmax1(ele(i), dmin1(x(i), u(i)))
end do
if(m.eq.0) then
call tango (funtan, gratan, n, x, f, g, a, ifree, ele, u,
* gaux, aux, d, otro, ap, gamma, beta, kon, nef, neg, nint,
* nspg, npur, nback, nextr, ncor, nfsmal,
* eps, maxtang, maxcg, tol, costol, distol, iopt, iophes,
* iflag, gpnor, m, ambda, rho, h, aj, ipt, qcib, ata, nlin,
* evalf, evalg, hessi, hrest, jacobh)
return
endif
c Find a better initial point
if(iopfac.eq.1) then
call tango (sumsq, grasum, n, x, f, g, a, ifree, ele, u,
* gaux, aux, d, otro, ap, gamma, beta, kon, nef, neg, nint,
* nspg, npur, nback, nextr, ncor, nfsmal,
* eps, maxtang, maxcg, tol, costol, distol, iopt, iophes,
* iflag, gpnor, m, ambda, rho, h, aj, ipt, qcib, ata, nlin,
* evalf, evalg, hessi, hrest, jacobh)
endif
ktot = 0
nftot = 0
ngtot = 0
nintot = 0
nstot = 0
nptot = 0
nbtot = 0
netot = 0
nctot = 0
nfstot = 0
konout = 0
c Initial evaluation of constraints
call hrest(m, n, x, h)
c Compute sup norm of constraints
hnor = 0.d0
do i = 1, m
hnor = dmax1(hnor, dabs(h(i)))
end do
call jacobh (m, n, x, aj)
call evalg(n, x, g)
do j = 1, n
do i = 1, m
g(j) = g(j) + aj(i, j) * ambda(i)
end do
end do
gpnor = 0.d0
do i = 1, n
z = x(i) - g(i)
z = dmax1(ele(i), dmin1(u(i), z))
z = dabs(z - x(i))
gpnor = dmax1(z, gpnor)
end do
1 continue
call evalf (n, x, f)
if(ipmil.ge.0) then
write(10, *)
write(10, *)' Milonga (Outer) iteration:', konout
write(10, *)' Current point:'
write(10, *)(x(i), i = 1, n10)
write(10, *)' Objective function:', f
write(10, *)' Sup norm of the constraints:', hnor
write(10, *)' Multipliers:'
write(10, *) (ambda(i),i=1,m10)
write(10, *)' Sup norm of projected gradient of Lagrangian:',
* gpnor
write(*, *)
write(*, *)' Milonga (Outer) iteration:', konout
write(*, *)' Current point:'
write(*, *)(x(i), i = 1, n10)
write(*, *)' Objective function:', f
write(*, *)' Sup norm of the constraints:', hnor
write(*, *)' Multipliers:'
write(*, *) (ambda(i),i=1,m10)
write(*, *)' Sup norm of projected gradient of Lagrangian:',
* gpnor
endif
if(dmax1(hnor, gpnor).le.eps) then
ier = 0
if(ipmil.ge.0) then
write(10, *)
write(10, *)' Solution found. f(x)=',f
write(*, *)
write(*, *)' Solution found. f(x)=',f
write(10, *)' Number of inner (Tango) iterations:', ktot
write(10, *)' Function evaluations in Tango:', nftot
write(10, *)' Gradient evaluations in Tango:', ngtot
write(10, *)' Number of internal iterations:', nintot
write(10, *)' Number of spg iterations:', nstot
write(10, *)' Current points where nfree < m:', nfstot
write(10, *)' Number of pure alfa-beta iterations:', nptot
write(10, *)' Number of internals with backtracking:',nbtot
write(10, *)' Number of extrapolations tried:', netot
write(10, *)' Number of nonsemiposdef Hessians:', nctot
write(*, *)' Number of inner (Tango) iterations:', ktot
write(*, *)' Function evaluations in Tango:', nftot
write(*, *)' Gradient evaluations in Tango:', ngtot
write(*, *)' Number of internal iterations:', nintot
write(*, *)' Number of spg iterations:', nstot
write(*, *)' Current points where nfree < m:', nfstot
write(*, *)' Number of pure alfa-beta iterations:', nptot
write(*, *)' Number of internals with backtracking:',nbtot
write(*, *)' Number of extrapolations tried:', netot
write(*, *)' Number of non semiposdef Hessians:', nctot
endif
return
endif
if(konout.ge.maxout) then
ier = 1
if(ipmil.ge.0) then
write(10, *)
write(10, *)' Exceeded allowed outer iterations'
write(*, *)
write(*, *)' Exceeded allowed outer iterations'
write(10, *)' Number of inner (Tango) iterations:', ktot
write(10, *)' Function evaluations in Tango:', nftot
write(10, *)' Gradient evaluations in Tango:', ngtot
write(10, *)' Number of internal iterations:', nintot
write(10, *)' Current points where to nfree < m:', nstot
write(10, *)' Spg iterations due to nfree small:', nfstot
write(10, *)' Number of pure alfa-beta iterations:', nptot
write(10, *)' Number of internals with backtracking:',nbtot
write(10, *)' Number of extrapolations tried:', netot
write(10, *)' Number of nonsemiposdef Hessians:', nctot
write(*, *)' Number of inner (Tango) iterations:', ktot
write(*, *)' Function evaluations in Tango:', nftot
write(*, *)' Gradient evaluations in Tango:', ngtot
write(*, *)' Number of internal iterations:', nintot
write(*, *)' Number of spg iterations:', nstot
write(*, *)' Current points where to nfree < m:', nfstot
write(*, *)' Number of pure alfa-beta iterations:', nptot
write(*, *)' Number of internals with backtracking:',nbtot
write(*, *)' Number of extrapolations tried:', netot
write(*, *)' Number of non semiposdef Hessians:', nctot
endif
return
endif
if(ipmil.ge.0) then
write(10, *)' Penalty parameter for the next iteration:',rho
write(*, *)' Penalty parameter for the next iteration:',rho
endif
hnoran = hnor
call tango (funtan, gratan, n, x, f, g, a, ifree, ele, u,
* gaux, aux, d, otro, ap, gamma, beta, kon, nef, neg, nint,
* nspg, npur, nback, nextr, ncor, nfsmal,
* eps, maxtang, maxcg, tol, costol, distol, iopt, iophes,
* iflag, gpnor, m, ambda, rho, h, aj, ipt, qcib, ata, nlin,
* evalf, evalg, hessi, hrest, jacobh)
ktot = ktot + kon
nftot = nftot + nef
ngtot = ngtot + neg
nintot = nintot + nint
nstot = nstot + nspg
nptot = nptot + npur
nbtot = nbtot + nback
netot = netot + nextr
nctot = nctot + ncor
nfstot = nfstot + nfsmal
call hrest (m, n, x, h)
hnor = 0.d0
do i = 1, m
hnor = dmax1(hnor, dabs(h(i)))
end do
if(gpnor.le.eps) then
if(ipmil.ge.0) then
write(10, *)' Tango ran with success'
write(*, *)' Tango ran with success'
endif
do i = 1, m
ambda(i) = ambda(i) + rho * h(i)
end do
endif
if(hnor.gt.hnoran/10.d0) then
rho = rhofac * rho
endif
konout = konout + 1
go to 1
end
subroutine tango (funtan, gratan, n, x, f, g, a, ifree, ele, u,
* gaux, aux, d, otro, ap, gamma, beta, kon, nef, neg, nint,
* nspg, npur, nback, nextr, ncor, nfsmal,
* eps, max, maxcg, tol, costol, distol, iopt, iophes,
* iflag, gpnor, m, ambda, rho, h, aj, ipt, qcib, ata, nlin,
* evalf, evalg, hessi, hrest, jacobh)
implicit double precision (a-h,o-z)
external funtan, gratan, evalf, evalg, hessi, hrest, jacobh
dimension a(n, n)
dimension ifree(n)
dimension ele(n), u(n)
dimension x(n), g(n)
dimension gaux(n), aux(n)
dimension d(n)
dimension otro(n)
dimension ap(n)
dimension ambda(m)
dimension h(m)
dimension aj(m, n)
dimension ata(nlin, *), qcib(nlin, *)
c
c Tango is a subroutine for box-constrained minimization written
c by J. M. Martinez with didactical purposes in the context of
c the course Metodos Computacionais de Otimizacao at the
c University of Campinas
c The purpose is to minimize f(x) subject to bounds on the variables
c The subroutine uses the active set strategy and line searches
c described in
c E. G. Birgin and J. M. Martinez (2002) ``Large-scale active set
c box-constrained optimization method with spectral projected
c gradients'', Computational Optimization and Applications 23,
c pp. 101-125 (2002).
c However, the search directions are different from the ones defined
c in that paper, which is devoted to large-scale problems.
c In Tango the search directions are Newton directions inside the
c faces and are corrected in different ways when the Hessians are not
c positive definite.
c Parameters:
c n: number of variables
c x: initial point - final point
c f: final value of the objective function
c g: final gradient
c a: auxiliar array of n**2 positions for internal storage of the Hessian
c ifree: auxiliar integer array of n positions (for free variables)
c ele, u: lower and upper bounds
c gaux, aux, d, otro, ap: auxiliar double precision arrays n positions
c beta: parameter for line search. Default: 0.5
c kon: number of iterations
c nef: number of function evaluations
c neg: number of gradient evaluations
c nint: number of iterations within the faces
c nspg: number of iterations that leave the current face (spg iterations)
c npur: number of iterations where the line search stopped at the first
c trial (alfa-beta iterations)
c nback: number of iterations where backtracking was necessary
c nextr: number of iterations where extrapolation was tried
c ncor: number of iterations where the Hessian was not positive definite
c eps: small number for convergence (projected gradient small)
c max: maximum number of iterations allowed
c tol: parameter to decide leaving faces. Default: 0.1
c costol: tolerance for cosine. If the cosine of the angle between the
c search direction and the negative gradient is less than costol, then
c the search direction is replaced by the negative gradient.
c iopt: parameter to decide what to do when the Hessian is not positive
c definite. iopt=1 modifies the diagonal to make it diagonally
c dominant. iopt=2 uses conjugate gradients.
c iflag: output parameter that says what happened.
c
c write(10, *)' In tango'
c write(10, *)' Half-Hessian of the quadratic:'
c do i = 1, n
c write(10, *) (qcib(i, j), j=1,n)
c end do
c
c Initialization
tol2 = tol * tol
c itipo is the type of iterate. 0 means initial point
itipo = 0
kon = 0
nint = 0
nspg = 0
npur = 0
nback = 0
nextr = 0
ncor = 0
nfsmal = 0
c Initial evaluation of function and gradient
call funtan(m, n, x, ambda, rho, h, f,evalf,hrest)
call gratan (m, n, x, ambda, rho, h, aj, g, evalg,hrest,jacobh)
nef = 1
neg = 1
c Here begins the loop
c Compute free variables
1 nfree = 0
c Project initial point on the box
do i = 1, n
x(i) = dmin1(u(i), dmax1(ele(i), x(i)))
if(x(i).gt.ele(i).and.x(i).lt.u(i)) then
nfree = nfree + 1
ifree(nfree) = i
end if
end do
c Compute norms of projected gradient and internal gradient
gpnor = 0.d0
ginor2 = 0.d0
gpnor2 = 0.d0
do i = 1, n
z = x(i) - g(i)
z = dmin1(u(i), dmax1(ele(i), z))
z = z - x(i)
z2 = z * z
gpnor = dmax1(gpnor, dabs(z))
gpnor2 = gpnor2 + z2
if(x(i).gt.ele(i).and.x(i).lt.u(i)) then
ginor2 = ginor2 + z2
endif
end do
c Printing
if(ipt.gt.0.and.mod(kon,ipt).eq.0) then
write(10, *)
write(10, *)' Tango Iteration ', kon
if(itipo.ne.0) then
if(itipo.eq.1) then
write(10, *)' Point obtained in the same closed face'
else
write(10, *)' Point obtained by SPG iteration'
endif
endif
write(10, *)' Point:'
write(10, *) (x(i),i=1,n)
write(10, *)' f(x) =', f
write(10, *)' Gradient:'
write(10, *) (g(i),i=1,n)
write(10, *)' Sup norm of projected gradient:', gpnor
write(10, *)' Number of free variables:', nfree
if(nfree.gt.0) then
write(10, *)' Free variables:', (ifree(i),i=1,nfree)
endif
write(10, *)' Function evaluations:', nef,
* ' Gradient evaluations:', neg
write(10, *)' Number of internal iterations:', nint
write(10, *)' Number of spg iterations:', nspg
write(10, *)' Current points where to nfree < m:', nfsmal
write(10, *)' Number of pure alfa-beta iterations:', npur
write(10, *)' Number of internals with backtracking:',nback
write(10, *)' Number of extrapolations tried:', nextr
write(10, *)' Number of nonsemiposdef Hessians:', ncor
write(10, *)
write(*, *)
write(*, *)' Tango Iteration ', kon
if(itipo.ne.0) then
if(itipo.eq.1) then
write(*, *)' Point obtained in the same closed face'
else
write(*, *)' Point obtained by SPG iteration'
endif
endif
write(*, *)' Point:'
write(*, *) (x(i),i=1,n)
write(*, *)' f(x) =', f
write(*, *)' Gradient:'
write(*, *) (g(i),i=1,n)
write(*, *)' Sup norm of projected gradient:', gpnor
write(*, *)' Number of free variables:', nfree
if(nfree.gt.0) then
write(*, *)' Free variables:', (ifree(i),i=1,nfree)
endif
write(*, *)' Function evaluations:', nef,
* ' Gradient evaluations:', neg
write(*, *)' Number of internal iterations:', nint
write(*, *)' Number of spg iterations:', nspg
write(*, *)' Current points where to nfree < m:', nfsmal
write(*, *)' Number of pure alfa-beta iterations:', npur
write(*, *)' Number of internals with backtracking:',nback
write(*, *)' Number of extrapolations tried:', nextr
write(*, *)' Number of non semiposdef Hessians:', ncor
write(*, *)
endif
c Stopping criteria
itipo = 1
if(gpnor.le.eps) then
iflag = 0
if(ipt.ge.0) then
write(10, *) ' Convergence: small sup norm of proj. gradient'
write(*, *) ' Convergence: small sup norm of proj. gradient'
endif
return
endif
if(kon.gt.max) then
iflag = 1
if(ipt.ge.0) then
write(10, *)' Maximum number of iterations exhausted'
write(*, *)' Maximum number of iterations exhausted'
endif
return
endif
c Abandon the face if the number of free variables is smaller than
c the number of constraints
if(m.gt.nfree) nfsmal = nfsmal + 1
c Decision about giving up the current face or not
c If the norm of internal gradient is large in comparison to the
c projected gradient we keep the same face
c
if(ginor2.le.tol2*gpnor2) then
c Abandon the face using an spg iteration
c itipo = 2 means that the following point leaves the current face
itipo = 2
c call the subroutine that abandons the face (spg)
call giveup (funtan, gratan,
* n, x, f, g, ele, u, nef, neg, gamma,
* aux, gaux, d, m, ambda, rho, h, aj, evalf,evalg,hrest,
* jacobh)
kon = kon + 1
nspg = nspg + 1
go to 1
endif
c Check if we have at least one free variable
if(nfree.lt.1) then
write(*, *)' There are no free variables. Something wrong.'
write(*, *)' Probably, the gradient was evaluated at a'
write(*, *)' Forbidden point, given not-a-number as result.'
stop
endif
c Check if x is in the box
do i = 1, n
if(x(i).gt.u(i).or.x(i).lt.ele(i)) then
write(*, *) ' x out of bounds'
stop
endif
end do
c Check free variables
do i = 1, nfree
ii = ifree(i)
if(x(ii).ge.u(ii).or.x(ii).le.ele(ii)) then
write(*, *)' The free variable', ii, ' is not free'
stop
endif
end do
nint = nint + 1
if(iophes.eq.2) then
call hessi (n, x, a)
c write(10, *)' Hessian:'
c do i = 1, n
c write(10, *) (a(i, j), j=1,n)
c end do
do i = 1, nfree
do j = 1, nfree
ii = ifree(i)
jj = ifree(j)
qcib(i, j) = a(ii, jj)
end do
end do
c write (10, *)' Free variables:'
c write(10, *) (ifree(i), i=1,nfree)
c write(10, *)' Reduced Hessian:'
c do i = 1, nfree
c write(10, *) (qcib(i, j), j=1,nfree)
c end do
if(m.gt.0) then
call jacobh(m, n, x, aj)
c write(10, *)' Point where I compute Jacobian:'
c write(10, *) (x(i), i=1,n)
c write(10, *) ' Jacobian:'
c do i = 1, m
c write(10, *) (aj(i, j), j=1,n)
c end do
do i = 1, n
do j = 1, n
ata(i, j) = 0.d0
do k = 1, m
ata(i, j) = ata(i, j) + aj(k, i) * aj(k, j)
end do
end do
end do
c write(10, *)' Reduced Hessian (qcib) de nuevo:'
c do i = 1, nfree
c write(10, *) (qcib(i, j), j=1,nfree)
c end do
c write(10, *) ' J^T J (ata):'
c do i = 1, n
c write(10, *) (ata(i, j), j=1,n)
c end do
c write(10, *)' rho =', rho
do i = 1, nfree
do j = 1, nfree
ii = ifree(i)
jj = ifree(j)
c write(10, *)' i = ', i, ' j =', j
c write(10, *)' ii = ', ii,' jj = ', jj
c write(10, *)' qcib(i, j)=', qcib(i, j)
c write(10, *)' rho = ', rho
c write(10, *)' ata(ii, jj) =', ata(ii, jj)
qcib(i, j) = qcib(i, j) + rho * ata(ii, jj)
c write(10, *)' qcib(i,j)+rho*ata(ii,jj)=',qcib(i,j)
c write(10, *)' qcib(2, 1) =', qcib(2, 1)
end do
end do
c write(10, *) ' Hessian gaussnewton (nuevo qcib):'
c do i = 1, nfree
c write(10, *) (qcib(i, j), j=1,nfree)
c end do
end if
c This endif corresponds to if(m.gt.0)
do i = 1, nfree
do j = 1, nfree
a(i, j) = qcib(i, j)
end do
end do
c write(10, *) ' Hessian gaussnewton:'
c
c do i = 1, nfree
c write(10, *) (a(i, j), j=1,nfree)
c end do
c
c stop
endif
c This endif corresponds to if(iophes.eq.2n)
if(iophes.eq.3) then
if(m.gt.0) then
do i = 1, nfree
do j = 1, nfree
ii = ifree(i)
jj = ifree(j)
a(i, j) = 2.d0 * qcib(ii, jj) + rho * ata(ii, jj)
end do
end do
else
do i = 1, nfree
do j = 1, nfree
ii = ifree(i)
jj = ifree(j)
a(i, j) = 2.d0 * qcib(ii, jj)
end do
end do
endif
c write(10, *)' Half-Hessian of the quadratic:'
c do i = 1, n
c write(10, *) (qcib(i, j), j=1,n)
c end do
c
c write(10, *)' quadratic hessian of the augmented lag.:'
c do i = 1, nfree
c write(10, *)(a(i, j), j=1,nfree)
c end do
endif
if(iophes.eq.1) then
c Compute the discrete Hessian
c write(10, *)' Point where I compute discrete Hessian:'
c write(10, *) (x(i), i = 1, n)
c write(10, *)' Lagrange multipliers:'
c write(10, *) (ambda(i), i=1,m)
c write(10, *)' gradient in basic point:'
c write(10, *) (g(i),i=1,n)
do j = 1, nfree
jj = ifree(j)
hh = dmax1(1.d-10, dabs(x(jj))*1.d-7)
if(x(jj).lt.0.d0) hh = - hh
save = x(jj)