Coverage for ase/utils/linesearch.py: 83.61%

238 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-27 15:21 +0000

1import numpy as np 

2 

3 

4def standard_gradient_norm(array): 

5 return np.linalg.norm(array.reshape(-1, 3), axis=1).max() 

6 

7 

8class LineSearch: 

9 def __init__(self, xtol=1e-14, get_gradient_norm=standard_gradient_norm): 

10 self.xtol = xtol 

11 self.task = 'START' 

12 self.isave = np.zeros((2,), np.intc) 

13 self.dsave = np.zeros((13,), float) 

14 self.fc = 0 

15 self.gc = 0 

16 self.case = 0 

17 self.old_stp = 0 

18 self.get_gradient_norm = get_gradient_norm 

19 

20 def _line_search( 

21 self, 

22 func, 

23 myfprime, 

24 xk, 

25 pk, 

26 gfk, 

27 old_fval, 

28 old_old_fval, 

29 maxstep=0.2, 

30 c1=0.23, 

31 c2=0.46, 

32 xtrapl=1.1, 

33 xtrapu=4.0, 

34 stpmax=50.0, 

35 stpmin=1e-8, 

36 args=(), 

37 ): 

38 self.stpmin = stpmin 

39 self.pk = pk 

40 # ??? p_size = np.sqrt((pk **2).sum()) 

41 self.stpmax = stpmax 

42 self.xtrapl = xtrapl 

43 self.xtrapu = xtrapu 

44 self.maxstep = maxstep 

45 phi0 = old_fval 

46 derphi0 = np.dot(gfk, pk) 

47 self.dim = len(pk) 

48 self.gms = np.sqrt(self.dim) * maxstep 

49 # alpha1 = pymin(maxstep,1.01*2*(phi0-old_old_fval)/derphi0) 

50 alpha1 = 1.0 

51 self.no_update = False 

52 

53 if isinstance(myfprime, type(())): 

54 # eps = myfprime[1] 

55 fprime = myfprime[0] 

56 # ??? newargs = (f,eps) + args 

57 gradient = False 

58 else: 

59 fprime = myfprime 

60 newargs = args 

61 gradient = True 

62 

63 fval = old_fval 

64 gval = gfk 

65 self.steps = [] 

66 

67 while True: 

68 stp = self.step( 

69 alpha1, phi0, derphi0, c1, c2, self.xtol, self.isave, self.dsave 

70 ) 

71 

72 if self.task[:2] == 'FG': 

73 alpha1 = stp 

74 fval = func(xk + stp * pk, *args) 

75 self.fc += 1 

76 gval = fprime(xk + stp * pk, *newargs) 

77 if gradient: 

78 self.gc += 1 

79 else: 

80 self.fc += len(xk) + 1 

81 phi0 = fval 

82 derphi0 = np.dot(gval, pk) 

83 self.old_stp = alpha1 

84 if self.no_update: 

85 break 

86 else: 

87 break 

88 

89 if self.task[:5] == 'ERROR' or self.task[1:4] == 'WARN': 

90 stp = None # failed 

91 return stp, fval, old_fval, self.no_update 

92 

93 def step(self, stp, f, g, c1, c2, xtol, isave, dsave): 

94 if self.task[:5] == 'START': 

95 # Check the input arguments for errors. 

96 if stp < self.stpmin: 

97 self.task = 'ERROR: STP .LT. minstep' 

98 if stp > self.stpmax: 

99 self.task = 'ERROR: STP .GT. maxstep' 

100 if g >= 0: 

101 self.task = 'ERROR: INITIAL G >= 0' 

102 if c1 < 0: 

103 self.task = 'ERROR: c1 .LT. 0' 

104 if c2 < 0: 

105 self.task = 'ERROR: c2 .LT. 0' 

106 if xtol < 0: 

107 self.task = 'ERROR: XTOL .LT. 0' 

108 if self.stpmin < 0: 

109 self.task = 'ERROR: minstep .LT. 0' 

110 if self.stpmax < self.stpmin: 

111 self.task = 'ERROR: maxstep .LT. minstep' 

112 if self.task[:5] == 'ERROR': 

113 return stp 

114 

115 # Initialize local variables. 

116 self.bracket = False 

117 stage = 1 

118 finit = f 

119 ginit = g 

120 gtest = c1 * ginit 

121 width = self.stpmax - self.stpmin 

122 width1 = width / 0.5 

123 # The variables stx, fx, gx contain the values of the step, 

124 # function, and derivative at the best step. 

125 # The variables sty, fy, gy contain the values of the step, 

126 # function, and derivative at sty. 

127 # The variables stp, f, g contain the values of the step, 

128 # function, and derivative at stp. 

129 stx = 0 

130 fx = finit 

131 gx = ginit 

132 sty = 0 

133 fy = finit 

134 gy = ginit 

135 stmin = 0 

136 stmax = stp + self.xtrapu * stp 

137 self.task = 'FG' 

138 self.save( 

139 ( 

140 stage, 

141 ginit, 

142 gtest, 

143 gx, 

144 gy, 

145 finit, 

146 fx, 

147 fy, 

148 stx, 

149 sty, 

150 stmin, 

151 stmax, 

152 width, 

153 width1, 

154 ) 

155 ) 

156 stp = self.determine_step(stp) 

157 # return stp, f, g 

158 return stp 

159 else: 

160 if self.isave[0] == 1: 

161 self.bracket = True 

162 else: 

163 self.bracket = False 

164 stage = self.isave[1] 

165 ( 

166 ginit, 

167 gtest, 

168 gx, 

169 gy, 

170 finit, 

171 fx, 

172 fy, 

173 stx, 

174 sty, 

175 stmin, 

176 stmax, 

177 width, 

178 width1, 

179 ) = self.dsave 

180 

181 # If psi(stp) <= 0 and f'(stp) >= 0 for some step, then the 

182 # algorithm enters the second stage. 

183 ftest = finit + stp * gtest 

184 if stage == 1 and f < ftest and g >= 0.0: 

185 stage = 2 

186 

187 # Test for warnings. 

188 if self.bracket and (stp <= stmin or stp >= stmax): 

189 self.task = 'WARNING: ROUNDING ERRORS PREVENT PROGRESS' 

190 if self.bracket and stmax - stmin <= self.xtol * stmax: 

191 self.task = 'WARNING: XTOL TEST SATISFIED' 

192 if stp == self.stpmax and f <= ftest and g <= gtest: 

193 self.task = 'WARNING: STP = maxstep' 

194 if stp == self.stpmin and (f > ftest or g >= gtest): 

195 self.task = 'WARNING: STP = minstep' 

196 

197 # Test for convergence. 

198 if f <= ftest and abs(g) <= c2 * (-ginit): 

199 self.task = 'CONVERGENCE' 

200 

201 # Test for termination. 

202 if self.task[:4] == 'WARN' or self.task[:4] == 'CONV': 

203 self.save( 

204 ( 

205 stage, 

206 ginit, 

207 gtest, 

208 gx, 

209 gy, 

210 finit, 

211 fx, 

212 fy, 

213 stx, 

214 sty, 

215 stmin, 

216 stmax, 

217 width, 

218 width1, 

219 ) 

220 ) 

221 # return stp, f, g 

222 return stp 

223 

224 # A modified function is used to predict the step during the 

225 # first stage if a lower function value has been obtained but 

226 # the decrease is not sufficient. 

227 # if stage == 1 and f <= fx and f > ftest: 

228 # # Define the modified function and derivative values. 

229 # fm =f - stp * gtest 

230 # fxm = fx - stx * gtest 

231 # fym = fy - sty * gtest 

232 # gm = g - gtest 

233 # gxm = gx - gtest 

234 # gym = gy - gtest 

235 

236 # Call step to update stx, sty, and to compute the new step. 

237 # stx, sty, stp, gxm, fxm, gym, fym = self.update ( 

238 # stx, fxm, gxm, sty, 

239 # fym, gym, stp, fm, gm, 

240 # stmin, stmax) 

241 

242 # # Reset the function and derivative values for f. 

243 

244 # fx = fxm + stx * gtest 

245 # fy = fym + sty * gtest 

246 # gx = gxm + gtest 

247 # gy = gym + gtest 

248 

249 # else: 

250 # Call step to update stx, sty, and to compute the new step. 

251 

252 stx, sty, stp, gx, fx, gy, fy = self.update( 

253 stx, fx, gx, sty, fy, gy, stp, f, g, stmin, stmax 

254 ) 

255 

256 # Decide if a bisection step is needed. 

257 

258 if self.bracket: 

259 if abs(sty - stx) >= 0.66 * width1: 

260 stp = stx + 0.5 * (sty - stx) 

261 width1 = width 

262 width = abs(sty - stx) 

263 

264 # Set the minimum and maximum steps allowed for stp. 

265 

266 if self.bracket: 

267 stmin = min(stx, sty) 

268 stmax = max(stx, sty) 

269 else: 

270 stmin = stp + self.xtrapl * (stp - stx) 

271 stmax = stp + self.xtrapu * (stp - stx) 

272 

273 # Force the step to be within the bounds maxstep and minstep. 

274 

275 stp = max(stp, self.stpmin) 

276 stp = min(stp, self.stpmax) 

277 

278 if stx == stp and stp == self.stpmax and stmin > self.stpmax: 

279 self.no_update = True 

280 # If further progress is not possible, let stp be the best 

281 # point obtained during the search. 

282 

283 if (self.bracket and stp < stmin or stp >= stmax) or ( 

284 self.bracket and stmax - stmin < self.xtol * stmax 

285 ): 

286 stp = stx 

287 

288 # Obtain another function and derivative. 

289 

290 self.task = 'FG' 

291 self.save( 

292 ( 

293 stage, 

294 ginit, 

295 gtest, 

296 gx, 

297 gy, 

298 finit, 

299 fx, 

300 fy, 

301 stx, 

302 sty, 

303 stmin, 

304 stmax, 

305 width, 

306 width1, 

307 ) 

308 ) 

309 return stp 

310 

311 def update(self, stx, fx, gx, sty, fy, gy, stp, fp, gp, stpmin, stpmax): 

312 sign = gp * (gx / abs(gx)) 

313 

314 # First case: A higher function value. The minimum is bracketed. 

315 # If the cubic step is closer to stx than the quadratic step, the 

316 # cubic step is taken, otherwise the average of the cubic and 

317 # quadratic steps is taken. 

318 if fp > fx: # case1 

319 self.case = 1 

320 theta = 3.0 * (fx - fp) / (stp - stx) + gx + gp 

321 s = max(abs(theta), abs(gx), abs(gp)) 

322 gamma = s * np.sqrt((theta / s) ** 2.0 - (gx / s) * (gp / s)) 

323 if stp < stx: 

324 gamma = -gamma 

325 p = (gamma - gx) + theta 

326 q = ((gamma - gx) + gamma) + gp 

327 r = p / q 

328 stpc = stx + r * (stp - stx) 

329 stpq = stx + ((gx / ((fx - fp) / (stp - stx) + gx)) / 2.0) * ( 

330 stp - stx 

331 ) 

332 if abs(stpc - stx) < abs(stpq - stx): 

333 stpf = stpc 

334 else: 

335 stpf = stpc + (stpq - stpc) / 2.0 

336 

337 self.bracket = True 

338 

339 # Second case: A lower function value and derivatives of opposite 

340 # sign. The minimum is bracketed. If the cubic step is farther from 

341 # stp than the secant step, the cubic step is taken, otherwise the 

342 # secant step is taken. 

343 

344 elif sign < 0: # case2 

345 self.case = 2 

346 theta = 3.0 * (fx - fp) / (stp - stx) + gx + gp 

347 s = max(abs(theta), abs(gx), abs(gp)) 

348 gamma = s * np.sqrt((theta / s) ** 2 - (gx / s) * (gp / s)) 

349 if stp > stx: 

350 gamma = -gamma 

351 p = (gamma - gp) + theta 

352 q = ((gamma - gp) + gamma) + gx 

353 r = p / q 

354 stpc = stp + r * (stx - stp) 

355 stpq = stp + (gp / (gp - gx)) * (stx - stp) 

356 if abs(stpc - stp) > abs(stpq - stp): 

357 stpf = stpc 

358 else: 

359 stpf = stpq 

360 self.bracket = True 

361 

362 # Third case: A lower function value, derivatives of the same sign, 

363 # and the magnitude of the derivative decreases. 

364 

365 elif abs(gp) < abs(gx): # case3 

366 self.case = 3 

367 # The cubic step is computed only if the cubic tends to infinity 

368 # in the direction of the step or if the minimum of the cubic 

369 # is beyond stp. Otherwise the cubic step is defined to be the 

370 # secant step. 

371 

372 theta = 3.0 * (fx - fp) / (stp - stx) + gx + gp 

373 s = max(abs(theta), abs(gx), abs(gp)) 

374 

375 # The case gamma = 0 only arises if the cubic does not tend 

376 # to infinity in the direction of the step. 

377 

378 gamma = s * np.sqrt( 

379 max(0.0, (theta / s) ** 2 - (gx / s) * (gp / s)) 

380 ) 

381 if stp > stx: 

382 gamma = -gamma 

383 p = (gamma - gp) + theta 

384 q = (gamma + (gx - gp)) + gamma 

385 r = p / q 

386 if r < 0.0 and gamma != 0: 

387 stpc = stp + r * (stx - stp) 

388 elif stp > stx: 

389 stpc = stpmax 

390 else: 

391 stpc = stpmin 

392 stpq = stp + (gp / (gp - gx)) * (stx - stp) 

393 

394 if self.bracket: 

395 # A minimizer has been bracketed. If the cubic step is 

396 # closer to stp than the secant step, the cubic step is 

397 # taken, otherwise the secant step is taken. 

398 

399 if abs(stpc - stp) < abs(stpq - stp): 

400 stpf = stpc 

401 else: 

402 stpf = stpq 

403 if stp > stx: 

404 stpf = min(stp + 0.66 * (sty - stp), stpf) 

405 else: 

406 stpf = max(stp + 0.66 * (sty - stp), stpf) 

407 else: 

408 # A minimizer has not been bracketed. If the cubic step is 

409 # farther from stp than the secant step, the cubic step is 

410 # taken, otherwise the secant step is taken. 

411 

412 if abs(stpc - stp) > abs(stpq - stp): 

413 stpf = stpc 

414 else: 

415 stpf = stpq 

416 stpf = min(stpmax, stpf) 

417 stpf = max(stpmin, stpf) 

418 

419 # Fourth case: A lower function value, derivatives of the same sign, 

420 # and the magnitude of the derivative does not decrease. If the 

421 # minimum is not bracketed, the step is either minstep or maxstep, 

422 # otherwise the cubic step is taken. 

423 

424 else: # case4 

425 self.case = 4 

426 if self.bracket: 

427 theta = 3.0 * (fp - fy) / (sty - stp) + gy + gp 

428 s = max(abs(theta), abs(gy), abs(gp)) 

429 gamma = s * np.sqrt((theta / s) ** 2 - (gy / s) * (gp / s)) 

430 if stp > sty: 

431 gamma = -gamma 

432 p = (gamma - gp) + theta 

433 q = ((gamma - gp) + gamma) + gy 

434 r = p / q 

435 stpc = stp + r * (sty - stp) 

436 stpf = stpc 

437 elif stp > stx: 

438 stpf = stpmax 

439 else: 

440 stpf = stpmin 

441 

442 # Update the interval which contains a minimizer. 

443 

444 if fp > fx: 

445 sty = stp 

446 fy = fp 

447 gy = gp 

448 else: 

449 if sign < 0: 

450 sty = stx 

451 fy = fx 

452 gy = gx 

453 stx = stp 

454 fx = fp 

455 gx = gp 

456 # Compute the new step. 

457 

458 stp = self.determine_step(stpf) 

459 

460 return stx, sty, stp, gx, fx, gy, fy 

461 

462 def determine_step(self, stp): 

463 dr = stp - self.old_stp 

464 maxsteplength = self.get_gradient_norm(self.pk * dr) 

465 if maxsteplength >= self.maxstep: 

466 dr *= self.maxstep / maxsteplength 

467 stp = self.old_stp + dr 

468 return stp 

469 

470 def save(self, data): 

471 if self.bracket: 

472 self.isave[0] = 1 

473 else: 

474 self.isave[0] = 0 

475 self.isave[1] = data[0] 

476 self.dsave = data[1:]