forked from deepmodeling/abacus-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopt_DCsrch.cpp
More file actions
732 lines (689 loc) · 24.2 KB
/
opt_DCsrch.cpp
File metadata and controls
732 lines (689 loc) · 24.2 KB
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
#include "opt_DCsrch.h"
#include <math.h>
#include <string.h>
// This file is translated from fortran codes dcstep.f of scipy.
// The structure and all annotation of the original file have been retained.
// See original source at https://github.com/scipy/scipy/blob/main/scipy/optimize/minpack2/dcstep.f.
// sunliang 2022-05-30
namespace ModuleBase
{
int dcsrch(double& stp,
double& f,
double& g,
double& ftol,
double& gtol,
double& xtol,
char* task,
double& stpmin,
double& stpmax,
int* isave,
double* dsave)
{
// c **********
// c
// c Subroutine dcsrch
// c
// c This subroutine finds a step that satisfies a sufficient
// c decrease condition and a curvature condition.
// c
// c Each call of the subroutine updates an interval with
// c endpoints stx and sty. The interval is initially chosen
// c so that it contains a minimizer of the modified function
// c
// c psi(stp) = f(stp) - f(0) - ftol*stp*f'(0).
// c
// c If psi(stp) <= 0 and f'(stp) >= 0 for some step, then the
// c interval is chosen so that it contains a minimizer of f.
// c
// c The algorithm is designed to find a step that satisfies
// c the sufficient decrease condition
// c
// c f(stp) <= f(0) + ftol*stp*f'(0),
// c
// c and the curvature condition
// c
// c abs(f'(stp)) <= gtol*abs(f'(0)).
// c
// c If ftol is less than gtol and if, for example, the function
// c is bounded below, then there is always a step which satisfies
// c both conditions.
// c
// c If no step can be found that satisfies both conditions, then
// c the algorithm stops with a warning. In this case stp only
// c satisfies the sufficient decrease condition.
// c
// c A typical invocation of dcsrch has the following outline:
// c
// c Evaluate the function at stp = 0.0d0; store in f.
// c Evaluate the gradient at stp = 0.0d0; store in g.
// c Choose a starting step stp.
// c
// c task = 'START'
// c 10 continue
// c call dcsrch(stp,f,g,ftol,gtol,xtol,task,stpmin,stpmax,
// c + isave,dsave)
// c if (task .eq. 'FG') then
// c Evaluate the function and the gradient at stp
// c go to 10
// c end if
// c
// c NOTE: The user must not alter work arrays between calls.
// c
// c The subroutine statement is
// c
// c subroutine dcsrch(f,g,stp,ftol,gtol,xtol,stpmin,stpmax,
// c task,isave,dsave)
// c where
// c
// c stp is a double precision variable.
// c On entry stp is the current estimate of a satisfactory
// c step. On initial entry, a positive initial estimate
// c must be provided.
// c On exit stp is the current estimate of a satisfactory step
// c if task = 'FG'. If task = 'CONV' then stp satisfies
// c the sufficient decrease and curvature condition.
// c
// c f is a double precision variable.
// c On initial entry f is the value of the function at 0.
// c On subsequent entries f is the value of the
// c function at stp.
// c On exit f is the value of the function at stp.
// c
// c g is a double precision variable.
// c On initial entry g is the derivative of the function at 0.
// c On subsequent entries g is the derivative of the
// c function at stp.
// c On exit g is the derivative of the function at stp.
// c
// c ftol is a double precision variable.
// c On entry ftol specifies a nonnegative tolerance for the
// c sufficient decrease condition.
// c On exit ftol is unchanged.
// c
// c gtol is a double precision variable.
// c On entry gtol specifies a nonnegative tolerance for the
// c curvature condition.
// c On exit gtol is unchanged.
// c
// c xtol is a double precision variable.
// c On entry xtol specifies a nonnegative relative tolerance
// c for an acceptable step. The subroutine exits with a
// c warning if the relative difference between sty and stx
// c is less than xtol.
// c On exit xtol is unchanged.
// c
// c task is a character variable of length at least 60.
// c On initial entry task must be set to 'START'.
// c On exit task indicates the required action:
// c
// c If task(1:2) = 'FG' then evaluate the function and
// c derivative at stp and call dcsrch again.
// c
// c If task(1:4) = 'CONV' then the search is successful.
// c
// c If task(1:4) = 'WARN' then the subroutine is not able
// c to satisfy the convergence conditions. The exit value of
// c stp contains the best point found during the search.
// c
// c If task(1:5) = 'ERROR' then there is an error in the
// c input arguments.
// c
// c On exit with convergence, a warning or an error, the
// c variable task contains additional information.
// c
// c stpmin is a double precision variable.
// c On entry stpmin is a nonnegative lower bound for the step.
// c On exit stpmin is unchanged.
// c
// c stpmax is a double precision variable.
// c On entry stpmax is a nonnegative upper bound for the step.
// c On exit stpmax is unchanged.
// c
// c isave is an integer work array of dimension 2.
// c
// c dsave is a double precision work array of dimension 13.
// c
// c Subprograms called
// c
// c MINPACK-2 ... dcstep
// c
// c MINPACK-1 Project. June 1983.
// c Argonne National Laboratory.
// c Jorge J. More' and David J. Thuente.
// c
// c MINPACK-2 Project. November 1993.
// c Argonne National Laboratory and University of Minnesota.
// c Brett M. Averick, Richard G. Carter, and Jorge J. More'.
// c
// c **********
double zero = 0.0;
double p5 = 0.5;
double p66 = 0.66;
double xtrapl = 1.1;
double xtrapu = 4.0;
bool brackt = false;
int stage = 0;
double finit = 0.0, ftest = 0.0, fm = 0.0, fx = 0.0, fxm = 0.0, fy = 0.0, fym = 0.0;
double ginit = 0.0, gtest = 0.0, gm = 0.0, gx = 0.0, gxm = 0.0, gy = 0.0, gym = 0.0;
double stx = 0.0, sty = 0.0, stmin = 0.0, stmax = 0.0, width = 0.0, width1 = 0.0;
extern /* Subroutine */ void dcstep(double&,
double&,
double&,
double&,
double&,
double&,
double&,
double&,
double&,
bool&,
double&,
double&);
// c Initialization block.
if (strncmp(task, "START", 5) == 0)
{
// c Check the input arguments for errors.
if (stp < stpmin)
{
strcpy(task, "ERROR: STP .LT. STPMIN");
}
if (stp > stpmax)
{
strcpy(task, "ERROR: STP .GT. STPMAX");
}
if (g >= 0.)
{
strcpy(task, "ERROR: INITIAL G .GE. ZERO");
}
if (ftol < 0.)
{
strcpy(task, "ERROR: FTOL .LT. ZERO");
}
if (gtol < 0.)
{
strcpy(task, "ERROR: GTOL .LT. ZERO");
}
if (xtol < 0.)
{
strcpy(task, "ERROR: XTOL .LT. ZERO");
}
if (stpmin < 0.)
{
strcpy(task, "ERROR: STPMIN .LT. ZERO");
}
if (stpmax < stpmin)
{
strcpy(task, "ERROR: STPMAX .LT. STPMIN");
}
// c Exit if there are errors on input.
if (strncmp(task, "ERROR", 5) == 0)
{
return 0;
}
// c Initialize local variables.
brackt = false;
stage = 1;
finit = f;
ginit = g;
gtest = ftol * ginit;
width = stpmax - stpmin;
width1 = width / p5;
// c The variables stx, fx, gx contain the values of the step,
// c function, and derivative at the best step.
// c The variables sty, fy, gy contain the value of the step,
// c function, and derivative at sty.
// c The variables stp, f, g contain the values of the step,
// c function, and derivative at stp.
stx = zero;
fx = finit;
gx = ginit;
sty = zero;
fy = finit;
gy = ginit;
stmin = zero;
stmax = stp + stp * xtrapu;
strcpy(task, "FG");
goto L10;
}
else
{
// c Restore local variables.
if (isave[1] == 1)
{
brackt = true;
}
else
{
brackt = false;
}
stage = isave[2];
ginit = dsave[1];
gtest = dsave[2];
gx = dsave[3];
gy = dsave[4];
finit = dsave[5];
fx = dsave[6];
fy = dsave[7];
stx = dsave[8];
sty = dsave[9];
stmin = dsave[10];
stmax = dsave[11];
width = dsave[12];
width1 = dsave[13];
}
// c If psi(stp) <= 0 and f'(stp) >= 0 for some step, then the
// c algorithm enters the second stage.
ftest = finit + stp * gtest;
if (stage == 1 && f <= ftest && g >= zero)
stage = 2;
// c Test for warnings.
if (brackt && (stp <= stmin || stp >= stmax))
{
strcpy(task, "WARNING: ROUNDING ERRORS PREVENT PROGRESS");
}
if (brackt && stmax - stmin <= xtol * stmax)
{
strcpy(task, "WARNING: XTOL TEST SATISFIED");
}
if (stp == stpmax && f <= ftest && g <= gtest)
{
strcpy(task, "WARNING: STP = STPMAX");
}
if (stp == stpmin && (f > ftest || g >= gtest))
{
strcpy(task, "WARNING: STP = STPMIN");
}
// c Test for convergence.
if (f <= ftest && std::abs(g) <= gtol * (-ginit))
{
strcpy(task, "CONVERGENCE");
// strcpy(task, "CONVERGENCE", 11);
}
// c Test for termination.
if (strncmp(task, "WARN", 4) == 0 || strncmp(task, "CONV", 4) == 0)
{
goto L10;
}
// c A modified function is used to predict the step during the
// c first stage if a lower function value has been obtained but
// c the decrease is not sufficient.
if (stage == 1 && f <= fx && f > ftest)
{
// c Define the modified function and derivative values.
fm = f - stp * gtest;
fxm = fx - stx * gtest;
fym = fy - sty * gtest;
gm = g - gtest;
gxm = gx - gtest;
gym = gy - gtest;
// c Call dcstep to update stx, sty, and to compute the new step.
dcstep(stx, fxm, gxm, sty, fym, gym, stp, fm, gm, brackt, stmin, stmax);
// c Reset the function and derivative values for f.
fx = fxm + stx * gtest;
fy = fym + sty * gtest;
gx = gxm + gtest;
gy = gym + gtest;
}
else
{
// c Call dcstep to update stx, sty, and to compute the new step.
dcstep(stx, fx, gx, sty, fy, gy, stp, f, g, brackt, stmin, stmax);
}
// c Decide if a bisection step is needed.
if (brackt)
{
if (std::abs(sty - stx) >= p66 * width1)
stp = stx + p5 * (sty - stx);
width1 = width;
width = std::abs(sty - stx);
}
// c Set the minimum and maximum steps allowed for stp.
if (brackt)
{
stmin = std::min(stx, sty);
stmax = std::max(stx, sty);
}
else
{
stmin = stp + xtrapl * (stp - stx);
stmax = stp + xtrapu * (stp - stx);
}
// c Force the step to be within the bounds stpmax and stpmin.
stp = std::max(stp, stpmin);
stp = std::min(stp, stpmax);
// c If further progress is not possible, let stp be the best
// c point obtained during the search.
if ((brackt && (stp <= stmin || stp >= stmax)) || (brackt && stmax - stmin <= xtol * stmax))
{
stp = stx;
}
// c Obtain another function and derivative.
strcpy(task, "FG");
L10:
// c Save local variables.
if (brackt)
{
isave[1] = 1;
}
else
{
isave[1] = 0;
}
isave[2] = stage;
dsave[1] = ginit;
dsave[2] = gtest;
dsave[3] = gx;
dsave[4] = gy;
dsave[5] = finit;
dsave[6] = fx;
dsave[7] = fy;
dsave[8] = stx;
dsave[9] = sty;
dsave[10] = stmin;
dsave[11] = stmax;
dsave[12] = width;
dsave[13] = width1;
return 0;
}
/* Subroutine */ void dcstep(double& stx,
double& fx,
double& dx,
double& sty,
double& fy,
double& dy,
double& stp,
double& fp,
double& dp,
bool& brackt,
double& stpmin,
double& stpmax)
{
// c **********
// c
// c Subroutine dcstep
// c
// c This subroutine computes a safeguarded step for a search
// c procedure and updates an interval that contains a step that
// c satisfies a sufficient decrease and a curvature condition.
// c
// c The parameter stx contains the step with the least function
// c value. If brackt is set to .true. then a minimizer has
// c been bracketed in an interval with endpoints stx and sty.
// c The parameter stp contains the current step.
// c The subroutine assumes that if brackt is set to .true. then
// c
// c min(stx,sty) < stp < max(stx,sty),
// c
// c and that the derivative at stx is negative in the direction
// c of the step.
// c
// c The subroutine statement is
// c
// c subroutine dcstep(stx,fx,dx,sty,fy,dy,stp,fp,dp,brackt,
// c stpmin,stpmax)
// c
// c where
// c
// c stx is a double precision variable.
// c On entry stx is the best step obtained so far and is an
// c endpoint of the interval that contains the minimizer.
// c On exit stx is the updated best step.
// c
// c fx is a double precision variable.
// c On entry fx is the function at stx.
// c On exit fx is the function at stx.
// c
// c dx is a double precision variable.
// c On entry dx is the derivative of the function at
// c stx. The derivative must be negative in the direction of
// c the step, that is, dx and stp - stx must have opposite
// c signs.
// c On exit dx is the derivative of the function at stx.
// c
// c sty is a double precision variable.
// c On entry sty is the second endpoint of the interval that
// c contains the minimizer.
// c On exit sty is the updated endpoint of the interval that
// c contains the minimizer.
// c
// c fy is a double precision variable.
// c On entry fy is the function at sty.
// c On exit fy is the function at sty.
// c
// c dy is a double precision variable.
// c On entry dy is the derivative of the function at sty.
// c On exit dy is the derivative of the function at the exit sty.
// c
// c stp is a double precision variable.
// c On entry stp is the current step. If brackt is set to .true.
// c then on input stp must be between stx and sty.
// c On exit stp is a new trial step.
// c
// c fp is a double precision variable.
// c On entry fp is the function at stp
// c On exit fp is unchanged.
// c
// c dp is a double precision variable.
// c On entry dp is the the derivative of the function at stp.
// c On exit dp is unchanged.
// c
// c brackt is an logical variable.
// c On entry brackt specifies if a minimizer has been bracketed.
// c Initially brackt must be set to .false.
// c On exit brackt specifies if a minimizer has been bracketed.
// c When a minimizer is bracketed brackt is set to .true.
// c
// c stpmin is a double precision variable.
// c On entry stpmin is a lower bound for the step.
// c On exit stpmin is unchanged.
// c
// c stpmax is a double precision variable.
// c On entry stpmax is an upper bound for the step.
// c On exit stpmax is unchanged.
// c
// c MINPACK-1 Project. June 1983
// c Argonne National Laboratory.
// c Jorge J. More' and David J. Thuente.
// c
// c MINPACK-2 Project. November 1993.
// c Argonne National Laboratory and University of Minnesota.
// c Brett M. Averick and Jorge J. More'.
// c
// c **********
double zero = 0.;
double p66 = 0.66;
double two = 2.;
double three = 3.;
double gamma, p, q, r, s, sgnd, stpc, stpf, stpq, theta;
sgnd = dp * (dx / std::abs(dx));
// c First case: A higher function value. The minimum is bracketed.
// c If the cubic step is closer to stx than the quadratic step, the
// c cubic step is taken, otherwise the average of the cubic and
// c quadratic steps is taken.
if (fp > fx)
{
theta = three * (fx - fp) / (stp - stx) + dx + dp;
double temps = std::max(std::abs(theta), std::abs(dx)); // get max(std::abs(theta),std::abs(dx),std::abs(dp))
s = std::max(temps, std::abs(dp));
gamma = s * sqrt(pow(theta / s, 2) - (dx / s) * (dp / s));
if (stp < stx)
gamma = -gamma;
p = (gamma - dx) + theta;
q = ((gamma - dx) + gamma) + dp;
r = p / q;
stpc = stx + r * (stp - stx);
stpq = stx + ((dx / ((fx - fp) / (stp - stx) + dx)) / two) * (stp - stx);
if (std::abs(stpc - stx) < std::abs(stpq - stx))
{
stpf = stpc;
}
else
{
stpf = stpc + (stpq - stpc) / two;
}
brackt = true;
}
// c Second case: A lower function value and derivatives of opposite
// c sign. The minimum is bracketed. If the cubic step is farther from
// c stp than the secant step, the cubic step is taken, otherwise the
// c secant step is taken.
else if (sgnd < zero)
{
theta = three * (fx - fp) / (stp - stx) + dx + dp;
double temps = std::max(std::abs(theta), std::abs(dx)); // get max(std::abs(theta),std::abs(dx),std::abs(dp))
s = std::max(temps, std::abs(dp));
gamma = s * sqrt(pow(theta / s, 2) - (dx / s) * (dp / s));
if (stp > stx)
gamma = -gamma;
p = (gamma - dp) + theta;
q = ((gamma - dp) + gamma) + dx;
r = p / q;
stpc = stp + r * (stx - stp);
stpq = stp + (dp / (dp - dx)) * (stx - stp);
if (std::abs(stpc - stp) > std::abs(stpq - stp))
{
stpf = stpc;
}
else
{
stpf = stpq;
}
brackt = true;
}
// c Third case: A lower function value, derivatives of the same sign,
// c and the magnitude of the derivative decreases.
else if (std::abs(dp) < std::abs(dx))
{
// c The cubic step is computed only if the cubic tends to infinity
// c in the direction of the step or if the minimum of the cubic
// c is beyond stp. Otherwise the cubic step is defined to be the
// c secant step.
theta = three * (fx - fp) / (stp - stx) + dx + dp;
double temps = std::max(std::abs(theta), std::abs(dx)); // get max(std::abs(theta),std::abs(dx),std::abs(dp))
s = std::max(temps, std::abs(dp));
// c The case gamma = 0 only arises if the cubic does not tend
// c to infinity in the direction of the step.
gamma = s * sqrt(std::max(zero, pow(theta / s, 2) - (dx / s) * (dp / s)));
if (stp > stx)
gamma = -gamma;
p = (gamma - dp) + theta;
q = (gamma + (dx - dp)) + gamma;
r = p / q;
if (r < zero && gamma != zero)
{
stpc = stp + r * (stx - stp);
}
else if (stp > stx)
{
stpc = stpmax;
}
else
{
stpc = stpmin;
}
stpq = stp + (dp / (dp - dx)) * (stx - stp);
if (brackt)
{
// c A minimizer has been bracketed. If the cubic step is
// c closer to stp than the secant step, the cubic step is
// c taken, otherwise the secant step is taken.
if (std::abs(stpc - stp) < std::abs(stpq - stp))
{
stpf = stpc;
}
else
{
stpf = stpq;
}
if (stp > stx)
{
stpf = std::min(stp + p66 * (sty - stp), stpf);
}
else
{
stpf = std::max(stp + p66 * (sty - stp), stpf);
}
}
else
{
// c A minimizer has not been bracketed. If the cubic step is
// c farther from stp than the secant step, the cubic step is
// c taken, otherwise the secant step is taken.
if (std::abs(stpc - stp) > std::abs(stpq - stp))
{
stpf = stpc;
}
else
{
stpf = stpq;
}
stpf = std::min(stpmax, stpf);
stpf = std::max(stpmin, stpf);
}
}
// c Fourth case: A lower function value, derivatives of the same sign,
// c and the magnitude of the derivative does not decrease. If the
// c minimum is not bracketed, the step is either stpmin or stpmax,
// c otherwise the cubic step is taken.
else
{
if (brackt)
{
theta = three * (fp - fy) / (sty - stp) + dy + dp;
double temps
= std::max(std::abs(theta), std::abs(dy)); // get max(std::abs(theta),std::abs(dy),std::abs(dp))
s = std::max(temps, std::abs(dp));
gamma = s * sqrt(pow(theta / s, 2) - (dy / s) * (dp / s));
if (stp > sty)
gamma = -gamma;
p = (gamma - dp) + theta;
q = ((gamma - dp) + gamma) + dy;
r = p / q;
stpc = stp + r * (sty - stp);
stpf = stpc;
}
else if (stp > stx)
{
stpf = stpmax;
}
else
{
stpf = stpmin;
}
}
// c Update the interval which contains a minimizer.
if (fp > fx)
{
sty = stp;
fy = fp;
dy = dp;
}
else
{
if (sgnd < zero)
{
sty = stx;
fy = fx;
dy = dx;
}
stx = stp;
fx = fp;
dx = dp;
}
// c Compute the new step.
stp = stpf;
}
void Opt_DCsrch::dcSrch(double& f, double& g, double& rstp, char* rtask)
{
dcsrch(rstp,
f,
g,
this->ftol_,
this->gtol_,
this->xtol_,
rtask,
this->stpmin_,
this->stpmax_,
this->isave_,
this->dsave_);
}
} // namespace ModuleBase