FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
abacus-develop/source/source_relax/line_search.cpp at develop · deepmodeling/abacus-develop · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
deepmodeling
/
abacus-develop
Public
forked from
abacusmodeling/abacus-develop
Notifications
You must be signed in to change notification settings
Fork
244
Star
286
Code
Issues
258
Pull requests
20
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
abacus-develop
/
source
/
source_relax
/
line_search.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
405 lines (365 loc) · 9.6 KB
Breadcrumbs
abacus-develop
/
source
/
source_relax
/
line_search.cpp
Copy path
File metadata and controls
405 lines (365 loc) · 9.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
#
include
"
line_search.h
"
#
include
"
source_base/global_function.h
"
#
include
"
source_base/global_variable.h
"
#
include
<
algorithm
>
#
include
<
cmath
>
#
include
<
ostream
>
bool
Line_Search::line_search
(
const
bool
restart,
const
double
x,
//
current point
const
double
y,
//
function value at current point
const
double
f,
//
derivative at current point
double
& xnew,
//
the next point that we want to try
const
double
conv_thr,
std::ofstream& ofs_running)
{
if
(restart)
{
ls_step =
0
;
}
if
(ls_step ==
0
)
//
first point: make a trial step into trial direction
{
return
this
->
first_order
(x, y, f, xnew);
}
if
(ls_step ==
1
)
//
second point: third order extrapolation/interpolation
{
return
this
->
third_order
(x, y, f, xnew, conv_thr);
}
//
if still not converging after 2 steps: start Brent
if
(ls_step ==
2
)
{
this
->
init_brent
(x, y, f);
}
if
(ls_step >=
3
)
{
this
->
update_brent
(x, y, f);
}
if
(ls_step >=
2
)
{
return
this
->
brent
(x, y, f, xnew, conv_thr, ofs_running);
}
ModuleBase::WARNING_QUIT
(
"
line_search
"
,
"
ls_step <0
"
);
__builtin_unreachable
();
}
bool
Line_Search::first_order
(
const
double
x,
const
double
y,
const
double
f,
double
& xnew)
{
xa = x;
//
set the first endpoint
ya = y;
fa = f;
xnew = x +
1
;
//
move one step towards trial direction
ls_step++;
return
false
;
}
bool
Line_Search::third_order
(
const
double
x,
const
double
y,
const
double
f,
double
& xnew,
const
double
conv_thr)
{
double
dmove =
0.0
;
double
dmoveh =
0.0
;
double
dmove1 =
0.0
;
double
dmove2 =
0.0
;
xb = x;
//
set the second endpoint
yb = y;
fb = f;
double
ffin = yb - ya;
double
fab = (fa + fb) /
2.0
;
double
k3 =
3.0
* (fb + fa -
2.0
* ffin);
double
k2 = fb +
2.0
* fa -
3.0
* ffin;
double
k1 = fa;
double
tmp = k1 * k3 / k2 / k2;
//
cubic extrapolation
if
(
std::abs
(k3 / k1) <
1.0e-2
||
std::abs
(k3) <
2.4e-5
|| tmp >
1.0
)
//
harmonic case
{
dmove = -fa / (fab - fa) /
2.0
;
if
(dmove <
0
)
{
dmove =
4.0
;
}
}
else
//
anharmonic case
{
dmove1 = k2 / k3 * (
1.0
-
std::sqrt
(
1.0
- tmp));
dmove2 = k2 / k3 * (
1.0
+
std::sqrt
(
1.0
- tmp));
double
dy1 = -(k1 - (k2 - k3 * dmove1 /
3.0
) * dmove1) * dmove1;
double
dy2 = -(k1 - (k2 - k3 * dmove2 /
3.0
) * dmove2) * dmove2;
if
(dy1 > dy2)
{
dmove = dmove1;
}
else
{
dmove = dmove2;
}
dmoveh = -fa / (fab - fa) /
2.0
;
if
(dmoveh <
0
)
{
dmoveh =
4.0
;
}
if
(dmove >
2.0
* dmoveh || dmoveh >
2.0
* dmove || (fa * fb >
0
&& dmove <
1.0
)
|| (fa * fb <
0
&& dmove >
1.0
))
{
dmove = dmoveh;
}
}
//
end anharmonic case
if
(dmove >
4.0
)
{
dmove =
4.0
;
}
xnew = dmove + xa;
double
dy = (fb + (fab - fb) / (xa - xb) * (dmove - xb)) * (dmove - xb);
if
(
std::abs
(dy) < conv_thr)
{
return
true
;
}
ls_step++;
return
false
;
}
void
Line_Search::init_brent
(
const
double
x,
const
double
y,
const
double
f)
{
bracked =
true
;
if
(x > xb)
//
x > b, start interval [b,x]
{
xa = xb;
ya = yb;
fa = fb;
xb = x;
yb = y;
fb = f;
if
(fa * fb >
0
)
{
bracked =
false
;
}
fstart = fa;
}
else
//
x < b
{
if
(fa * f <=
0
)
//
minimum between [a,x]
{
xb = x;
yb = y;
fb = f;
}
else
if
(fb * f <=
0
)
//
minimum between [x,b]
{
xa = xb;
ya = yb;
fa = fb;
xb = x;
yb = y;
fb = f;
}
else
//
problematic case, no minimum between [a,b]
{
xa = xb;
ya = yb;
fa = fb;
xb = x;
yb = y;
fb = f;
bracked =
false
;
}
}
xc = xb;
fc = fb;
}
void
Line_Search::update_brent
(
const
double
x,
const
double
y,
const
double
f)
{
xb = x;
yb = y;
fb = f;
if
(!bracked && fstart * f <
0
)
{
bracked =
true
;
xc = xb;
fc = fb;
}
}
bool
Line_Search::brent
(
const
double
x,
const
double
y,
const
double
f,
double
& xnew,
const
double
conv_thr, std::ofstream& ofs_running)
{
ls_step++;
double
xd =
0.0
;
double
xe =
0.0
;
double
xm =
0.0
;
//
if no zero is between xa and xb
if
(!bracked)
{
if
(
std::abs
(fc) <=
std::abs
(fb) || (xa - xc) < (xb - xa))
{
xc = xa;
fc = fa;
xd = xb - xa;
xe = xb - xa;
}
double
tol1 =
2.0
*
e8
*
std::abs
(xb) +
0.5
*
e8
;
xm =
0.5
* (xc - xb);
if
(!(xc <= xa && xa <= xb))
{
ModuleBase::WARNING_QUIT
(
"
Brent
"
,
"
something wrong with Brent line search!
"
);
}
if
(
std::abs
(xm) <= tol1 || fb ==
0.0
)
{
return
true
;
}
if
(
std::abs
(xe) >= tol1 &&
std::abs
(fa) >
std::abs
(fb))
{
double
s = fb / fa;
double
p =
0.0
;
double
qq =
0.0
;
if
(xa == xc)
{
p =
2.0
* xm * s;
qq =
1.0
- s;
}
else
{
qq = fa / fc;
double
r = fb / fc;
p = s * (
2.0
* xm * qq * (qq - r) - (xb - xa) * (r -
1.0
));
qq = (qq -
1.0
) * (r -
1.0
) * (s -
1.0
);
}
if
(p >
0.0
)
{
qq = -qq;
}
p =
std::abs
(p);
if
(p <
std::min
(
2.0
* (xb - xa) * qq -
std::abs
(tol1 * qq),
std::abs
(xe * qq) /
2.0
))
{
xe = xd;
xd = p / qq;
}
else
{
xd =
2.0
* (xb - xa);
xe = xd;
}
}
else
{
xd =
2.0
* (xb - xa);
xe = xd;
}
double
fab = (fa + fb) /
2.0
;
double
dy = (fb + (fab - fb) / (xa - xb) * xd) * xd;
xc = xa;
fc = fa;
xa = xb;
fa = fb;
if
(
std::abs
(xd) > tol1)
{
xb = xb + xd;
}
else
{
xb = xb + tol1;
}
xnew = xb;
if
(
std::abs
(dy) < conv_thr)
{
return
true
;
}
if
(ls_step ==
4
)
//
I'm not sure if this is a good choice, but the idea is there should not be so many line
//
search steps I feel if the line search does not converge, we'd better change the direction
//
and restart line search
{
ofs_running <<
"
Too many Brent steps, let's do next CG step
"
<< std::endl;
return
true
;
//
ModuleBase::WARNING_QUIT("Brent","too many steps in line search, something wrong");
}
return
false
;
}
//
end bracked
else
{
if
(!((xa <= xb && xb <= xc) || (xc <= xb && xb <= xa)))
{
ModuleBase::WARNING_QUIT
(
"
Brent
"
,
"
something wrong with Brent line search!
"
);
}
if
((fb >
0
&& fc >
0
) || (fb <
0
&& fc <
0
))
{
xc = xa;
fc = fa;
xd = xb - xa;
xe = xd;
}
if
(
std::abs
(fc) <
std::abs
(fb))
//
rearrange b,c so that abs(fc)>=abs(fb)
{
xa = xb;
xb = xc;
xc = xa;
fa = fb;
fb = fc;
fc = fa;
}
double
tol1 =
2.0
*
e8
*
std::abs
(xb) +
0.5
*
e8
;
xm =
0.5
* (xc - xb);
if
(
std::abs
(xm) <= tol1 || fb ==
0.0
)
{
return
true
;
}
if
(
std::abs
(xe) >= tol1 &&
std::abs
(fa) >
std::abs
(fb))
{
double
s = fb / fa;
double
p =
0.0
;
double
qq =
0.0
;
if
(xa == xc)
{
p =
2.0
* xm * s;
qq =
1.0
- s;
}
else
{
qq = fa / fc;
double
r = fb / fc;
p = s * (
2.0
* xm * qq * (qq - r) - (xb - xa) * (r -
1.0
));
qq = (qq -
1.0
) * (r -
1.0
) * (s -
1.0
);
}
if
(p >
0.0
)
{
qq = -qq;
}
p =
std::abs
(p);
if
(
2.0
* p <
std::min
(
3.0
* xm * qq -
std::abs
(tol1 * qq),
std::abs
(xe * qq)))
{
xe = xd;
xd = p / qq;
}
else
{
xd = xm;
xe = xd;
}
}
else
{
xd = xm;
xe = xd;
}
double
fab = (fa + fb) /
2.0
;
double
dy = (fb + (fab - fb) / (xa - xb) * xd) * xd;
xa = xb;
fa = fb;
ya = yb;
if
(
std::abs
(xc) > tol1)
{
xb = xb + xd;
}
else
{
if
(xm >
0
)
{
xb = xb + tol1;
}
else
{
xb = xb - tol1;
}
}
xnew = xb;
if
(
std::abs
(dy) < conv_thr)
{
return
true
;
}
if
(ls_step ==
4
)
{
ofs_running <<
"
Too many Brent steps, let's do next CG step
"
<< std::endl;
return
true
;
}
return
false
;
}
//
end ibrack
}
Back
|
FazBrowse Home
|
New Git URL