-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcg_descent.c
4331 lines (3993 loc) · 147 KB
/
cg_descent.c
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
/* =========================================================================
============================ CG_DESCENT =================================
=========================================================================
________________________________________________________________
| A conjugate gradient method with guaranteed descent |
| C-code Version 1.1 (October 6, 2005) |
| Version 1.2 (November 14, 2005) |
| Version 2.0 (September 23, 2007) |
| Version 3.0 (May 18, 2008) |
| Version 4.0 (March 28, 2011) |
| Version 4.1 (April 8, 2011) |
| Version 4.2 (April 14, 2011) |
| Version 5.0 (May 1, 2011) |
| Version 5.1 (January 31, 2012) |
| Version 5.2 (April 17, 2012) |
| Version 5.3 (May 18, 2012) |
| Version 6.0 (November 6, 2012) |
| Version 6.1 (January 27, 2013) |
| Version 6.2 (February 2, 2013) |
| Version 6.3 (April 21, 2013) |
| Version 6.4 (April 29, 2013) |
| Version 6.5 (April 30, 2013) |
| Version 6.6 (May 28, 2013) |
| Version 6.7 (April 7, 2014) |
| Version 6.8 (March 7, 2015) |
| |
| William W. Hager and Hongchao Zhang |
| Department of Mathematics |
| University of Florida |
| Gainesville, Florida 32611 USA |
| 352-392-0281 x 244 |
| |
| Copyright by William W. Hager |
| |
| http://www.math.ufl.edu/~hager/papers/CG |
| |
| Disclaimer: The views expressed are those of the authors and |
| do not reflect the official policy or position of |
| the Department of Defense or the U.S. Government. |
| |
| Approved for Public Release, Distribution Unlimited |
|________________________________________________________________|
________________________________________________________________
|This program is free software; you can redistribute it and/or |
|modify it under the terms of the GNU General Public License as |
|published by the Free Software Foundation; either version 2 of |
|the License, or (at your option) any later version. |
|This program is distributed in the hope that it will be useful, |
|but WITHOUT ANY WARRANTY; without even the implied warranty of |
|MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|GNU General Public License for more details. |
| |
|You should have received a copy of the GNU General Public |
|License along with this program; if not, write to the Free |
|Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
|MA 02110-1301 USA |
|________________________________________________________________|
References:
1. W. W. Hager and H. Zhang, A new conjugate gradient method
with guaranteed descent and an efficient line search,
SIAM Journal on Optimization, 16 (2005), 170-192.
2. W. W. Hager and H. Zhang, Algorithm 851: CG_DESCENT,
A conjugate gradient method with guaranteed descent,
ACM Transactions on Mathematical Software, 32 (2006), 113-137.
3. W. W. Hager and H. Zhang, A survey of nonlinear conjugate gradient
methods, Pacific Journal of Optimization, 2 (2006), pp. 35-58.
4. W. W. Hager and H. Zhang, Limited memory conjugate gradients,
SIAM Journal on Optimization, 23 (2013), 2150-2168. */
#include "cg_user.h"
#include "cg_descent.h"
#include "cg_blas.h"
/* begin external variables */
double one [1], zero [1] ;
BLAS_INT blas_one [1] ;
/* end external variables */
#define UPDATE_STATS_AND_CALLBACK() \
IterStats.iter = iter; \
IterStats.alpha = alpha; \
IterStats.x = x; \
IterStats.f = f; \
IterStats.g = g; \
IterStats.d = d; \
if (callback(&IterStats, User) == 0) { status = 13; goto Exit; }
int cg_descent /* return status of solution process:
0 (convergence tolerance satisfied)
1 (change in func <= feps*|f|)
2 (total number of iterations exceeded maxit)
3 (slope always negative in line search)
4 (number of line search iterations exceeds nline)
5 (search direction not a descent direction)
6 (excessive updating of eps)
7 (Wolfe conditions never satisfied)
8 (debugger is on and the function value increases)
9 (no cost or gradient improvement in
2n + Parm->nslow iterations)
10 (out of memory)
11 (function nan or +-INF and could not be repaired)
12 (invalid choice for memory parameter)
13 (iteration stopped by user callback) */
(
double *x, /* input: starting guess, output: the solution */
INT n, /* problem dimension */
cg_stats *Stat, /* structure with statistics (can be NULL) */
cg_parameter *UParm, /* user parameters, NULL = use default parameters */
double grad_tol, /* StopRule = 1: |g|_infty <= max (grad_tol,
StopFac*initial |g|_infty) [default]
StopRule = 0: |g|_infty <= grad_tol(1+|f|) */
cg_value_fn value, /* f = value (x, n, User) */
cg_grad_fn grad, /* grad (g, x, n, User) */
cg_valgrad_fn valgrad, /* f = valgrad (g, x, n, User),
NULL = compute value & gradient using value & grad */
cg_callback_fn callback,/* user provided function called at the end of a
(successful) iteration */
double *Work, /* NULL => let code allocate memory
not NULL => use array Work for required memory
The amount of memory needed depends on the value
of the parameter memory in the Parm structure.
memory > 0 => need (mem+6)*n + (3*mem+9)*mem + 5
where mem = MIN(memory, n)
memory = 0 => need 4*n */
void *User /* user provided pointer passed to functions */
)
{
INT i, iter, IterRestart, maxit, n5, nrestart, nrestartsub ;
int nslow, slowlimit, IterQuad, status, PrintLevel, QuadF, StopRule ;
double delta2, Qk, Ck, fbest, gbest,
f, ftemp, gnorm, xnorm, gnorm2, dnorm2, denom,
t, dphi, dphi0, alpha,
ykyk, ykgk, dkyk, beta, QuadTrust, tol,
*d, *g, *xtemp, *gtemp, *work ;
/* new variables added in Version 6.0 */
int l1, l2, j, k, mem, memsq, memk, memk_begin, mlast, mlast_sub,
mp, mp_begin, mpp, nsub, spp, spp1, SkFstart, SkFlast, Subspace,
UseMemory, Restart, LBFGS, InvariantSpace, IterSub, NumSub,
IterSubStart, IterSubRestart, FirstFull, SubSkip, SubCheck,
StartSkip, StartCheck, DenseCol1, NegDiag, memk_is_mem,
d0isg, qrestart ;
double gHg, scale, gsubnorm2, ratio, stgkeep,
alphaold, zeta, yty, ytg, t1, t2, t3, t4,
*Rk, *Re, *Sk, *SkF, *stemp, *Yk, *SkYk,
*dsub, *gsub, *gsubtemp, *gkeep, *tau, *vsub, *wsub ;
cg_iter_stats IterStats;
IterStats.n = n;
cg_parameter *Parm, ParmStruc ;
cg_com Com ;
/* assign values to the external variables */
one [0] = (double) 1 ;
zero [0] = (double) 0 ;
blas_one [0] = (BLAS_INT) 1 ;
/* initialize the parameters */
if ( UParm == NULL )
{
Parm = &ParmStruc ;
cg_default (Parm) ;
}
else Parm = UParm ;
PrintLevel = Parm->PrintLevel ;
qrestart = MIN (n, Parm->qrestart) ;
Com.Parm = Parm ;
Com.eps = Parm->eps ;
Com.PertRule = Parm->PertRule ;
Com.Wolfe = FALSE ; /* initially Wolfe line search not performed */
Com.nf = (INT) 0 ; /* number of function evaluations */
Com.ng = (INT) 0 ; /* number of gradient evaluations */
iter = (INT) 0 ; /* total number of iterations */
QuadF = FALSE ; /* initially function assumed to be nonquadratic */
NegDiag = FALSE ; /* no negative diagonal elements in QR factorization */
mem = Parm->memory ;/* cg_descent corresponds to mem = 0 */
if ( Parm->PrintParms ) cg_printParms (Parm) ;
if ( (mem != 0) && (mem < 3) )
{
status = 12 ;
goto Exit ;
}
/* allocate work array */
mem = MIN (mem, n) ;
if ( Work == NULL )
{
if ( mem == 0 ) /* original CG_DESCENT without memory */
{
work = (double *) malloc (4*n*sizeof (double)) ;
}
else if ( Parm->LBFGS || (mem >= n) ) /* use L-BFGS */
{
work = (double *) malloc ((2*mem*(n+1)+4*n)*sizeof (double)) ;
}
else /* limited memory CG_DESCENT */
{
i = (mem+6)*n + (3*mem+9)*mem + 5 ;
work = (double *) malloc (i*sizeof (double)) ;
}
}
else work = Work ;
if ( work == NULL )
{
status = 10 ;
goto Exit ;
}
/* set up Com structure */
Com.x = x ;
Com.xtemp = xtemp = work ;
Com.d = d = xtemp+n ;
Com.g = g = d+n ;
Com.gtemp = gtemp = g+n ;
Com.n = n ; /* problem dimension */
Com.neps = 0 ; /* number of times eps updated */
Com.AWolfe = Parm->AWolfe ; /* do not touch user's AWolfe */
Com.cg_value = value ;
Com.cg_grad = grad ;
Com.cg_valgrad = valgrad ;
Com.User = User ;
StopRule = Parm->StopRule ;
LBFGS = FALSE ;
UseMemory = FALSE ;/* do not use memory */
Subspace = FALSE ; /* full space, check subspace condition if UseMemory */
FirstFull = FALSE ;/* not first full iteration after leaving subspace */
memk = 0 ; /* number of vectors in current memory */
/* the conjugate gradient algorithm is restarted every nrestart iteration */
nrestart = (INT) (((double) n)*Parm->restart_fac) ;
/* allocate storage connected with limited memory CG */
if ( mem > 0 )
{
if ( (mem == n) || Parm->LBFGS )
{
LBFGS = TRUE ; /* use L-BFGS */
mlast = -1 ;
Sk = gtemp + n ;
Yk = Sk + mem*n ;
SkYk = Yk + mem*n ;
tau = SkYk + mem ;
}
else
{
UseMemory = TRUE ; /* previous search direction will be saved */
SubSkip = 0 ; /* number of iterations to skip checking memory*/
SubCheck = mem*Parm->SubCheck ; /* number of iterations to check */
StartCheck = 0 ; /* start checking memory at iteration 0 */
InvariantSpace = FALSE ; /* iterations not in invariant space */
FirstFull = TRUE ; /* first iteration in full space */
nsub = 0 ; /* initial subspace dimension */
memsq = mem*mem ;
SkF = gtemp+n ; /* directions in memory (x_k+1 - x_k) */
stemp = SkF + mem*n ;/* stores x_k+1 - x_k */
gkeep = stemp + n ; /* store gradient when first direction != -g */
Sk = gkeep + n ; /* Sk = Rk at start of LBFGS in subspace */
Rk = Sk + memsq ; /* upper triangular factor in SkF = Zk*Rk */
/* zero out Rk to ensure lower triangle is 0 */
cg_init (Rk, ZERO, memsq) ;
Re = Rk + memsq ; /* end column of Rk, used for new direction */
Yk = Re + mem+1 ;
SkYk = Yk + memsq+mem+2 ; /* dot products sk'yk in the subspace */
tau = SkYk + mem ; /* stores alpha in Nocedal and Wright */
dsub = tau + mem ; /* direction projection in subspace */
gsub = dsub + mem ; /* gradient projection in subspace */
gsubtemp = gsub + mem+1 ;/* new gsub before update */
wsub = gsubtemp + mem ; /* mem+1 work array for triangular solve */
vsub = wsub + mem+1 ; /* mem work array for triangular solve */
}
}
/* abort when number of iterations reaches maxit */
maxit = Parm->maxit ;
f = ZERO ;
fbest = INF ;
gbest = INF ;
nslow = 0 ;
slowlimit = 2*n + Parm->nslow ;
n5 = n % 5 ;
Ck = ZERO ;
Qk = ZERO ;
/* initial function and gradient evaluations, initial direction */
Com.alpha = ZERO ;
status = cg_evaluate ("fg", "n", &Com) ;
f = Com.f ;
if ( status )
{
if ( PrintLevel > 0 ) printf ("Function undefined at starting point\n");
goto Exit ;
}
Com.f0 = f + f ;
Com.SmallCost = fabs (f)*Parm->SmallCost ;
xnorm = cg_inf (x, n) ;
/* set d = -g, compute gnorm = infinity norm of g and
gnorm2 = square of 2-norm of g */
gnorm = cg_update_inf2 (g, g, d, &gnorm2, n) ;
dnorm2 = gnorm2 ;
/* check if the starting function value is nan */
if ( f != f )
{
status = 11 ;
goto Exit ;
}
if ( Parm->StopRule ) tol = MAX (gnorm*Parm->StopFac, grad_tol) ;
else tol = grad_tol ;
Com.tol = tol ;
if ( PrintLevel >= 1 )
{
printf ("iter: %5i f: %13.6e gnorm: %13.6e memk: %i\n",
(int) 0, f, gnorm, memk) ;
}
if ( cg_tol (gnorm, &Com) )
{
iter = 0 ;
status = 0 ;
goto Exit ;
}
dphi0 = -gnorm2 ;
delta2 = 2*Parm->delta - ONE ;
alpha = Parm->step ;
if ( alpha == ZERO )
{
if ( xnorm == ZERO )
{
if ( f != ZERO ) alpha = 2.*fabs (f)/gnorm2 ;
else alpha = ONE ;
}
else alpha = Parm->psi0*xnorm/gnorm ;
}
alpha = MIN(alpha, Parm->max_step);
Com.df0 = -2.0*fabs(f)/alpha ;
if (callback != NULL)
{
UPDATE_STATS_AND_CALLBACK();
}
Restart = FALSE ; /* do not restart the algorithm */
IterRestart = 0 ; /* counts number of iterations since last restart */
IterSub = 0 ; /* counts number of iterations in subspace */
NumSub = 0 ; /* total number of subspaces */
IterQuad = 0 ; /* counts number of iterations that function change
is close to that of a quadratic */
scale = (double) 1 ; /* scale is the initial approximation to inverse
Hessian in LBFGS; after the initial iteration,
scale is estimated by the BB formula */
/* Start the conjugate gradient iteration.
alpha starts as old step, ends as final step for current iteration
f is function value for alpha = 0
QuadOK = TRUE means that a quadratic step was taken */
for (iter = 1; iter <= maxit; iter++)
{
/* save old alpha to simplify formula computing subspace direction */
alphaold = alpha ;
Com.QuadOK = FALSE ;
alpha = MIN (Parm->psi2*alpha, Parm->max_step) ;
if ( f != ZERO ) t = fabs ((f-Com.f0)/f) ;
else t = ONE ;
Com.UseCubic = TRUE ;
if ( (t < Parm->CubicCutOff) || !Parm->UseCubic ) Com.UseCubic = FALSE ;
if ( Parm->QuadStep )
{
/* test if quadratic interpolation step should be tried */
if ( ((t > Parm->QuadCutOff)&&(fabs(f) >= Com.SmallCost)) || QuadF )
{
if ( QuadF )
{
Com.alpha = MIN(Parm->psi1*alpha, Parm->max_step) ;
status = cg_evaluate ("g", "y", &Com) ;
if ( status ) goto Exit ;
if ( Com.df > dphi0 )
{
alpha = -dphi0/((Com.df-dphi0)/Com.alpha) ;
Com.QuadOK = TRUE ;
}
else if ( LBFGS )
{
if ( memk >= n )
{
alpha = ONE ;
Com.QuadOK = TRUE ;
}
else alpha = 2. ;
}
else if ( Subspace )
{
if ( memk >= nsub )
{
alpha = ONE ;
Com.QuadOK = TRUE ;
}
else alpha = 2. ;
}
}
else
{
t = MAX (Parm->psi_lo, Com.df0/(dphi0*Parm->psi2)) ;
Com.alpha = MIN (MIN (t, Parm->psi_hi)*alpha, Parm->max_step) ;
status = cg_evaluate ("f", "y", &Com) ;
if ( status ) goto Exit ;
ftemp = Com.f ;
denom = 2.*(((ftemp-f)/Com.alpha)-dphi0) ;
if ( denom > ZERO )
{
t = -dphi0*Com.alpha/denom ;
/* safeguard */
if ( ftemp >= f )
alpha = MAX (t, Com.alpha*Parm->QuadSafe) ;
else alpha = t ;
Com.QuadOK = TRUE ;
}
}
alpha = MIN(alpha, Parm->max_step);
if ( PrintLevel >= 1 )
{
if ( denom <= ZERO )
{
printf ("Quad step fails (denom = %14.6e)\n", denom);
}
else if ( Com.QuadOK )
{
printf ("Quad step %14.6e OK\n", alpha);
}
else printf ("Quad step %14.6e done, but not OK\n", alpha) ;
}
}
else if ( PrintLevel >= 1 )
{
printf ("No quad step (chg: %14.6e, cut: %10.2e)\n",
t, Parm->QuadCutOff) ;
}
}
Com.f0 = f ; /* f0 saved as prior value */
Com.df0 = dphi0 ;
/* parameters in Wolfe and approximate Wolfe conditions, and in update*/
Qk = Parm->Qdecay*Qk + ONE ;
Ck = Ck + (fabs (f) - Ck)/Qk ; /* average cost magnitude */
if ( Com.PertRule ) Com.fpert = f + Com.eps*fabs (f) ;
else Com.fpert = f + Com.eps ;
Com.wolfe_hi = Parm->delta*dphi0 ;
Com.wolfe_lo = Parm->sigma*dphi0 ;
Com.awolfe_hi = delta2*dphi0 ;
Com.alpha = alpha ;
/* perform line search */
status = cg_line (&Com) ;
/*try approximate Wolfe line search if ordinary Wolfe fails */
if ( (status > 0) && !Com.AWolfe )
{
if ( PrintLevel >= 1 )
{
printf ("\nWOLFE LINE SEARCH FAILS\n") ;
}
if ( status != 3 )
{
Com.AWolfe = TRUE ;
status = cg_line (&Com) ;
}
}
alpha = Com.alpha ;
f = Com.f ;
dphi = Com.df ;
if ( status ) goto Exit ;
/* Test for convergence to within machine epsilon
[set feps to zero to remove this test] */
if ( -alpha*dphi0 <= Parm->feps*fabs (f) )
{
status = 1 ;
goto Exit ;
}
/* test how close the cost function changes are to that of a quadratic
QuadTrust = 0 means the function change matches that of a quadratic*/
t = alpha*(dphi+dphi0) ;
if ( fabs (t) <= Parm->qeps*MIN (Ck, ONE) ) QuadTrust = ZERO ;
else QuadTrust = fabs((2.0*(f-Com.f0)/t)-ONE) ;
if ( QuadTrust <= Parm->qrule) IterQuad++ ;
else IterQuad = 0 ;
if ( IterQuad == qrestart ) QuadF = TRUE ;
IterRestart++ ;
if ( !Com.AWolfe )
{
if ( fabs (f-Com.f0) < Parm->AWolfeFac*Ck )
{
Com.AWolfe = TRUE ;
if ( Com.Wolfe ) Restart = TRUE ;
}
}
if ( (mem > 0) && !LBFGS )
{
if ( UseMemory )
{
if ( (iter - StartCheck > SubCheck) && !Subspace )
{
StartSkip = iter ;
UseMemory = FALSE ;
if ( SubSkip == 0 ) SubSkip = mem*Parm->SubSkip ;
else SubSkip *= 2 ;
if ( PrintLevel >= 1 )
{
printf ("skip subspace %i iterations\n", SubSkip) ;
}
}
}
else
{
if ( iter - StartSkip > SubSkip )
{
StartCheck = iter ;
UseMemory = TRUE ;
memk = 0 ;
}
}
}
if ( !UseMemory )
{
if ( !LBFGS )
{
if ( (IterRestart >= nrestart) || ((IterQuad == qrestart)
&& (IterQuad != IterRestart)) ) Restart = TRUE ;
}
}
else
{
if ( Subspace ) /* the iteration is in the subspace */
{
IterSubRestart++ ;
/* compute project of g into subspace */
gsubnorm2 = ZERO ;
mp = SkFstart ;
j = nsub - mp ;
/* multiply basis vectors by new gradient */
cg_matvec (wsub, SkF, gtemp, nsub, n, 0) ;
/* rearrange wsub and store in gsubtemp
(elements associated with old vectors should
precede elements associated with newer vectors */
cg_copy0 (gsubtemp, wsub+mp, j) ;
cg_copy0 (gsubtemp+j, wsub, mp) ;
/* solve Rk'y = gsubtemp */
cg_trisolve (gsubtemp, Rk, mem, nsub, 0) ;
gsubnorm2 = cg_dot0 (gsubtemp, gsubtemp, nsub) ;
gnorm2 = cg_dot (gtemp, gtemp, n);
ratio = sqrt(gsubnorm2/gnorm2) ;
if ( ratio < ONE - Parm->eta1 ) /* Exit Subspace */
{
if ( PrintLevel >= 1 )
{
printf ("iter: %i exit subspace\n", (int) iter) ;
}
FirstFull = TRUE ; /* first iteration in full space */
Subspace = FALSE ; /* leave the subspace */
InvariantSpace = FALSE ;
/* check the subspace condition for SubCheck iterations
starting from the current iteration (StartCheck) */
StartCheck = iter ;
if ( IterSubRestart > 1 ) dnorm2 = cg_dot0 (dsub, dsub,nsub);
}
else
{
/* Check if a restart should be done in subspace */
if ( IterSubRestart == nrestartsub ) Restart = TRUE ;
}
}
else /* in full space */
{
if ( (IterRestart == 1) || FirstFull ) memk = 0 ;
if ( (memk == 1) && InvariantSpace )
{
memk = 0 ;
InvariantSpace = FALSE ;
}
if (memk < mem )
{
memk_is_mem = FALSE ;
SkFstart = 0 ;
/* SkF stores basis vector of the form alpha*d
We factor SkF = Zk*Rk where Zk has orthonormal columns
and Rk is upper triangular. Zk is not stored; wherever
it is needed, we use SkF * inv (Rk) */
if (memk == 0)
{
mlast = 0 ; /* starting pointer in the memory */
memk = 1 ; /* dimension of current subspace */
t = sqrt(dnorm2) ;
zeta = alpha*t ;
Rk [0] = zeta ;
cg_scale (SkF, d, alpha, n) ;
Yk [0] = (dphi - dphi0)/t ;
gsub [0] = dphi/t ;
SkYk [0] = alpha*(dphi-dphi0) ;
FirstFull = FALSE ;
if ( IterRestart > 1 )
{
/* Need to save g for later correction of first
column of Yk. Since g does not lie in the
subspace and the first column is dense */
cg_copy (gkeep, g, n) ;
/* Also store dot product of g with the first
direction vector -- this saves a later dot
product when we fix the first column of Yk */
stgkeep = dphi0*alpha ;
d0isg = FALSE ;
}
else d0isg = TRUE ;
}
else
{
mlast = memk ; /* starting pointer in the memory */
memk++ ; /* total number of Rk in the memory */
mpp = mlast*n ;
spp = mlast*mem ;
cg_scale (SkF+mpp, d, alpha, n) ;
/* check if the alphas are far from 1 */
if ((fabs(alpha-5.05)>4.95)||(fabs(alphaold-5.05)>4.95))
{
/* multiply basis vectors by new direction vector */
cg_matvec (Rk+spp, SkF, SkF+mpp, mlast, n, 0) ;
/* solve Rk'y = wsub to obtain the components of the
new direction vector relative to the orthonormal
basis Z in S = ZR, store in next column of Rk */
cg_trisolve (Rk+spp, Rk, mem, mlast, 0) ;
}
else /* alphas are close to 1 */
{
t1 = -alpha ;
t2 = beta*alpha/alphaold ;
for (j = 0; j < mlast; j++)
{
Rk [spp+j] = t1*gsub [j] + t2*Rk [spp-mem+j] ;
}
}
t = alpha*alpha*dnorm2 ;
t1 = cg_dot0 (Rk+spp, Rk+spp, mlast) ;
if (t <= t1)
{
zeta = t*1.e-12 ;
NegDiag = TRUE ;
}
else zeta = sqrt(t-t1);
Rk [spp+mlast] = zeta ;
t = - zeta/alpha ; /* t = cg_dot0 (Zk+mlast*n, g, n)*/
Yk [spp-mem+mlast] = t ;
gsub [mlast] = t ;
/* multiply basis vectors by new gradient */
cg_matvec (wsub, SkF, gtemp, mlast, n, 0) ;
/* exploit dphi for last multiply */
wsub [mlast] = alpha*dphi ;
/* solve for new gsub */
cg_trisolve (wsub, Rk, mem, memk, 0) ;
/* subtract old gsub from new gsub = column of Yk */
cg_Yk (Yk+spp, gsub, wsub, NULL, memk) ;
SkYk [mlast] = alpha*(dphi-dphi0) ;
}
}
else /* memk = mem */
{
memk_is_mem = TRUE ;
mlast = mem-1 ;
cg_scale (stemp, d, alpha, n) ;
/* compute projection of s_k = alpha_k d_k into subspace
check if the alphas are far from 1 */
if ((fabs(alpha-5.05)>4.95)||(fabs(alphaold-5.05)>4.95))
{
mp = SkFstart ;
j = mem - mp ;
/* multiply basis vectors by sk */
cg_matvec (wsub, SkF, stemp, mem, n, 0) ;
/* rearrange wsub and store in Re = end col Rk */
cg_copy0 (Re, wsub+mp, j) ;
cg_copy0 (Re+j, wsub, mp) ;
/* solve Rk'y = Re */
cg_trisolve (Re, Rk, mem, mem, 0) ;
}
else /* alphas close to 1 */
{
t1 = -alpha ;
t2 = beta*alpha/alphaold ;
for (j = 0; j < mem; j++)
{
Re [j] = t1*gsub [j] + t2*Re [j-mem] ;
}
}
/* t = 2-norm squared of s_k */
t = alpha*alpha*dnorm2 ;
/* t1 = 2-norm squared of projection */
t1 = cg_dot0 (Re, Re, mem) ;
if (t <= t1)
{
zeta = t*1.e-12 ;
NegDiag = TRUE ;
}
else zeta = sqrt(t-t1);
/* dist from new search direction to prior subspace*/
Re [mem] = zeta ;
/* projection of prior g on new orthogonal
subspace vector */
t = -zeta/alpha ; /* t = cg_dot(Zk+mpp, g, n)*/
gsub [mem] = t ;
Yk [memsq] = t ; /* also store it in Yk */
spp = memsq + 1 ;
mp = SkFstart ;
j = mem - mp ;
/* multiply basis vectors by gtemp */
cg_matvec (vsub, SkF, gtemp, mem, n, 0) ;
/* rearrange and store in wsub */
cg_copy0 (wsub, vsub+mp, j) ;
cg_copy0 (wsub+j, vsub, mp) ;
/* solve Rk'y = wsub */
cg_trisolve (wsub, Rk, mem, mem, 0) ;
wsub [mem] = (alpha*dphi - cg_dot0 (wsub, Re, mem))/zeta;
/* add new column to Yk, store new gsub */
cg_Yk (Yk+spp, gsub, wsub, NULL, mem+1) ;
/* store sk (stemp) at SkF+SkFstart */
cg_copy (SkF+SkFstart*n, stemp, n) ;
SkFstart++ ;
if ( SkFstart == mem ) SkFstart = 0 ;
mp = SkFstart ;
for (k = 0; k < mem; k++)
{
spp = (k+1)*mem + k ;
t1 = Rk [spp] ;
t2 = Rk [spp+1] ;
t = sqrt(t1*t1 + t2*t2) ;
t1 = t1/t ;
t2 = t2/t ;
/* update Rk */
Rk [k*mem+k] = t ;
for (j = (k+2); j <= mem; j++)
{
spp1 = spp ;
spp = j*mem + k ;
t3 = Rk [spp] ;
t4 = Rk [spp+1] ;
Rk [spp1] = t1*t3 + t2*t4 ;
Rk [spp+1] = t1*t4 - t2*t3 ;
}
/* update Yk */
if ( k < 2 ) /* mem should be greater than 2 */
{
/* first 2 rows are dense */
spp = k ;
for (j = 1; j < mem; j++)
{
spp1 = spp ;
spp = j*mem + k ;
t3 = Yk [spp] ;
t4 = Yk [spp+1] ;
Yk [spp1] = t1*t3 + t2*t4 ;
Yk [spp+1] = t1*t4 -t2*t3 ;
}
spp1 = spp ;
spp = mem*mem + 1 + k ;
t3 = Yk [spp] ;
t4 = Yk [spp+1] ;
Yk [spp1] = t1*t3 + t2*t4 ;
Yk [spp+1] = t1*t4 -t2*t3 ;
}
else if ( (k == 2) && (2 < mem-1))
{
spp = k ;
/* col 1 dense since the oldest direction
vector has been dropped */
j = 1 ;
spp1 = spp ;
spp = j*mem + k ;
/* single nonzero percolates down the column */
t3 = Yk [spp] ; /* t4 = 0. */
Yk [spp1] = t1*t3 ;
Yk [spp+1] = -t2*t3 ;
/* process rows in Hessenberg part of matrix */
for (j = 2; j < mem; j++)
{
spp1 = spp ;
spp = j*mem + k ;
t3 = Yk [spp] ;
t4 = Yk [spp+1] ;
Yk [spp1] = t1*t3 + t2*t4 ;
Yk [spp+1] = t1*t4 -t2*t3 ;
}
spp1 = spp ;
spp = mem*mem + 1 + k ;
t3 = Yk [spp] ;
t4 = Yk [spp+1] ;
Yk [spp1] = t1*t3 + t2*t4 ;
Yk [spp+1] = t1*t4 -t2*t3 ;
}
else if ( k < (mem-1) )
{
spp = k ;
/* process first column */
j = 1 ;
spp1 = spp ;
spp = j*mem + k ;
t3 = Yk [spp] ; /* t4 = 0. */
Yk [spp1] = t1*t3 ;
Yk [spp+1] = -t2*t3 ;
/* process rows in Hessenberg part of matrix */
j = k-1 ;
spp = (j-1)*mem+k ;
spp1 = spp ;
spp = j*mem + k ;
t3 = Yk [spp] ;
Yk [spp1] = t1*t3 ; /* t4 = 0. */
/* Yk [spp+1] = -t2*t3 ;*/
/* Theoretically this element is zero */
for (j = k; j < mem; j++)
{
spp1 = spp ;
spp = j*mem + k ;
t3 = Yk [spp] ;
t4 = Yk [spp+1] ;
Yk [spp1] = t1*t3 + t2*t4 ;
Yk [spp+1] = t1*t4 -t2*t3 ;
}
spp1 = spp ;
spp = mem*mem + 1 + k ;
t3 = Yk [spp] ;
t4 = Yk [spp+1] ;
Yk [spp1] = t1*t3 + t2*t4 ;
Yk [spp+1] = t1*t4 -t2*t3 ;
}
else /* k = mem-1 */
{
spp = k ;
/* process first column */
j = 1 ;
spp1 = spp ;
spp = j*mem + k ;
t3 = Yk [spp] ; /* t4 = 0. */
Yk [spp1] = t1*t3 ;
/* process rows in Hessenberg part of matrix */
j = k-1 ;
spp = (j-1)*mem+k ;
spp1 = spp ;
spp = j*mem + k ;
t3 = Yk [spp] ; /* t4 = 0. */
Yk [spp1] = t1*t3 ;
j = k ;
spp1 = spp ;
spp = j*mem + k ; /* j=mem-1 */
t3 = Yk [spp] ;
t4 = Yk [spp+1] ;
Yk [spp1] = t1*t3 + t2*t4 ;
spp1 = spp ;
spp = mem*mem + 1 + k ; /* j=mem */
t3 = Yk [spp] ;
t4 = Yk [spp+1] ;
Yk [spp1] = t1*t3 + t2*t4 ;
}
/* update g in subspace */
if ( k < (mem-1) )
{
t3 = gsub [k] ;
t4 = gsub [k+1] ;
gsub [k] = t1*t3 + t2*t4 ;
gsub [k+1] = t1*t4 -t2*t3 ;
}
else /* k = mem-1 */
{
t3 = gsub [k] ;
t4 = gsub [k+1] ;
gsub [k] = t1*t3 + t2*t4 ;
}
}
/* update SkYk */
for (k = 0; k < mlast; k++) SkYk [k] = SkYk [k+1] ;
SkYk [mlast] = alpha*(dphi-dphi0) ;
}
/* calculate t = ||gsub|| / ||gtemp|| */
gsubnorm2 = cg_dot0 (gsub, gsub, memk) ;
gnorm2 = cg_dot (gtemp, gtemp, n) ;
ratio = sqrt (gsubnorm2/gnorm2) ;
if ( ratio > ONE-Parm->eta2) InvariantSpace = TRUE ;
/* check to see whether to enter subspace */
if ( ((memk > 1) && InvariantSpace) ||
((memk == mem) && (ratio > ONE-Parm->eta0)) )
{
NumSub++ ;
if ( PrintLevel >= 1 )
{
if ( InvariantSpace )
{
printf ("iter: %i invariant space, "
"enter subspace\n", (int) iter) ;
}
else
{
printf ("iter: %i enter subspace\n", (int) iter) ;
}
}
/* if the first column is dense, we need to correct it
now since we do not know the entries until the basis
is determined */
if ( !d0isg && !memk_is_mem )
{
wsub [0] = stgkeep ;
/* mlast = memk -1 */
cg_matvec (wsub+1, SkF+n, gkeep, mlast, n, 0) ;
/* solve Rk'y = wsub */
cg_trisolve (wsub, Rk, mem, memk, 0) ;
/* corrected first column of Yk */
Yk [1] -= wsub [1] ;
cg_scale0 (Yk+2, wsub+2, -ONE, memk-2) ;
}
if ( d0isg && !memk_is_mem ) DenseCol1 = FALSE ;
else DenseCol1 = TRUE ;
Subspace = TRUE ;
/* reset subspace skipping to 0, need to test invariance */
SubSkip = 0 ;
IterSubRestart = 0 ;
IterSubStart = IterSub ;
nsub = memk ; /* dimension of subspace */
nrestartsub = (int) (((double) nsub)*Parm->restart_fac) ;
mp_begin = mlast ;
memk_begin = nsub ;
SkFlast = (SkFstart+nsub-1) % mem ;
cg_copy0 (gsubtemp, gsub, nsub) ;
/* Rk contains the sk for subspace, initialize Sk = Rk */
cg_copy (Sk, Rk, (int) mem*nsub) ;
}
else
{
if ( (IterRestart == nrestart) ||
((IterQuad == qrestart) && (IterQuad != IterRestart)) )
{
Restart = TRUE ;
}
}
} /* done checking the full space */
} /* done using the memory */
/* compute search direction */
if ( LBFGS )
{