Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/scipy/optimize/__init__.py: 100%

29 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-12-12 06:31 +0000

1""" 

2===================================================== 

3Optimization and root finding (:mod:`scipy.optimize`) 

4===================================================== 

5 

6.. currentmodule:: scipy.optimize 

7 

8SciPy ``optimize`` provides functions for minimizing (or maximizing) 

9objective functions, possibly subject to constraints. It includes 

10solvers for nonlinear problems (with support for both local and global 

11optimization algorithms), linear programing, constrained 

12and nonlinear least-squares, root finding, and curve fitting. 

13 

14Common functions and objects, shared across different solvers, are: 

15 

16.. autosummary:: 

17 :toctree: generated/ 

18 

19 show_options - Show specific options optimization solvers. 

20 OptimizeResult - The optimization result returned by some optimizers. 

21 OptimizeWarning - The optimization encountered problems. 

22 

23 

24Optimization 

25============ 

26 

27Scalar functions optimization 

28----------------------------- 

29 

30.. autosummary:: 

31 :toctree: generated/ 

32 

33 minimize_scalar - Interface for minimizers of univariate functions 

34 

35The `minimize_scalar` function supports the following methods: 

36 

37.. toctree:: 

38 

39 optimize.minimize_scalar-brent 

40 optimize.minimize_scalar-bounded 

41 optimize.minimize_scalar-golden 

42 

43Local (multivariate) optimization 

44--------------------------------- 

45 

46.. autosummary:: 

47 :toctree: generated/ 

48 

49 minimize - Interface for minimizers of multivariate functions. 

50 

51The `minimize` function supports the following methods: 

52 

53.. toctree:: 

54 

55 optimize.minimize-neldermead 

56 optimize.minimize-powell 

57 optimize.minimize-cg 

58 optimize.minimize-bfgs 

59 optimize.minimize-newtoncg 

60 optimize.minimize-lbfgsb 

61 optimize.minimize-tnc 

62 optimize.minimize-cobyla 

63 optimize.minimize-slsqp 

64 optimize.minimize-trustconstr 

65 optimize.minimize-dogleg 

66 optimize.minimize-trustncg 

67 optimize.minimize-trustkrylov 

68 optimize.minimize-trustexact 

69 

70Constraints are passed to `minimize` function as a single object or 

71as a list of objects from the following classes: 

72 

73.. autosummary:: 

74 :toctree: generated/ 

75 

76 NonlinearConstraint - Class defining general nonlinear constraints. 

77 LinearConstraint - Class defining general linear constraints. 

78 

79Simple bound constraints are handled separately and there is a special class 

80for them: 

81 

82.. autosummary:: 

83 :toctree: generated/ 

84 

85 Bounds - Bound constraints. 

86 

87Quasi-Newton strategies implementing `HessianUpdateStrategy` 

88interface can be used to approximate the Hessian in `minimize` 

89function (available only for the 'trust-constr' method). Available 

90quasi-Newton methods implementing this interface are: 

91 

92.. autosummary:: 

93 :toctree: generated/ 

94 

95 BFGS - Broyden-Fletcher-Goldfarb-Shanno (BFGS) Hessian update strategy. 

96 SR1 - Symmetric-rank-1 Hessian update strategy. 

97 

98Global optimization 

99------------------- 

100 

101.. autosummary:: 

102 :toctree: generated/ 

103 

104 basinhopping - Basinhopping stochastic optimizer. 

105 brute - Brute force searching optimizer. 

106 differential_evolution - Stochastic optimizer using differential evolution. 

107 

108 shgo - Simplicial homology global optimizer. 

109 dual_annealing - Dual annealing stochastic optimizer. 

110 direct - DIRECT (Dividing Rectangles) optimizer. 

111 

112Least-squares and curve fitting 

113=============================== 

114 

115Nonlinear least-squares 

116----------------------- 

117 

118.. autosummary:: 

119 :toctree: generated/ 

120 

121 least_squares - Solve a nonlinear least-squares problem with bounds on the variables. 

122 

123Linear least-squares 

124-------------------- 

125 

126.. autosummary:: 

127 :toctree: generated/ 

128 

129 nnls - Linear least-squares problem with non-negativity constraint. 

130 lsq_linear - Linear least-squares problem with bound constraints. 

131 

132Curve fitting 

133------------- 

134 

135.. autosummary:: 

136 :toctree: generated/ 

137 

138 curve_fit -- Fit curve to a set of points. 

139 

140Root finding 

141============ 

142 

143Scalar functions 

144---------------- 

145.. autosummary:: 

146 :toctree: generated/ 

147 

148 root_scalar - Unified interface for nonlinear solvers of scalar functions. 

149 brentq - quadratic interpolation Brent method. 

150 brenth - Brent method, modified by Harris with hyperbolic extrapolation. 

151 ridder - Ridder's method. 

152 bisect - Bisection method. 

153 newton - Newton's method (also Secant and Halley's methods). 

154 toms748 - Alefeld, Potra & Shi Algorithm 748. 

155 RootResults - The root finding result returned by some root finders. 

156 

157The `root_scalar` function supports the following methods: 

158 

159.. toctree:: 

160 

161 optimize.root_scalar-brentq 

162 optimize.root_scalar-brenth 

163 optimize.root_scalar-bisect 

164 optimize.root_scalar-ridder 

165 optimize.root_scalar-newton 

166 optimize.root_scalar-toms748 

167 optimize.root_scalar-secant 

168 optimize.root_scalar-halley 

169 

170 

171 

172The table below lists situations and appropriate methods, along with 

173*asymptotic* convergence rates per iteration (and per function evaluation) 

174for successful convergence to a simple root(*). 

175Bisection is the slowest of them all, adding one bit of accuracy for each 

176function evaluation, but is guaranteed to converge. 

177The other bracketing methods all (eventually) increase the number of accurate 

178bits by about 50% for every function evaluation. 

179The derivative-based methods, all built on `newton`, can converge quite quickly 

180if the initial value is close to the root. They can also be applied to 

181functions defined on (a subset of) the complex plane. 

182 

183+-------------+----------+----------+-----------+-------------+-------------+----------------+ 

184| Domain of f | Bracket? | Derivatives? | Solvers | Convergence | 

185+ + +----------+-----------+ +-------------+----------------+ 

186| | | `fprime` | `fprime2` | | Guaranteed? | Rate(s)(*) | 

187+=============+==========+==========+===========+=============+=============+================+ 

188| `R` | Yes | N/A | N/A | - bisection | - Yes | - 1 "Linear" | 

189| | | | | - brentq | - Yes | - >=1, <= 1.62 | 

190| | | | | - brenth | - Yes | - >=1, <= 1.62 | 

191| | | | | - ridder | - Yes | - 2.0 (1.41) | 

192| | | | | - toms748 | - Yes | - 2.7 (1.65) | 

193+-------------+----------+----------+-----------+-------------+-------------+----------------+ 

194| `R` or `C` | No | No | No | secant | No | 1.62 (1.62) | 

195+-------------+----------+----------+-----------+-------------+-------------+----------------+ 

196| `R` or `C` | No | Yes | No | newton | No | 2.00 (1.41) | 

197+-------------+----------+----------+-----------+-------------+-------------+----------------+ 

198| `R` or `C` | No | Yes | Yes | halley | No | 3.00 (1.44) | 

199+-------------+----------+----------+-----------+-------------+-------------+----------------+ 

200 

201.. seealso:: 

202 

203 `scipy.optimize.cython_optimize` -- Typed Cython versions of zeros functions 

204 

205Fixed point finding: 

206 

207.. autosummary:: 

208 :toctree: generated/ 

209 

210 fixed_point - Single-variable fixed-point solver. 

211 

212Multidimensional 

213---------------- 

214 

215.. autosummary:: 

216 :toctree: generated/ 

217 

218 root - Unified interface for nonlinear solvers of multivariate functions. 

219 

220The `root` function supports the following methods: 

221 

222.. toctree:: 

223 

224 optimize.root-hybr 

225 optimize.root-lm 

226 optimize.root-broyden1 

227 optimize.root-broyden2 

228 optimize.root-anderson 

229 optimize.root-linearmixing 

230 optimize.root-diagbroyden 

231 optimize.root-excitingmixing 

232 optimize.root-krylov 

233 optimize.root-dfsane 

234 

235Linear programming / MILP 

236========================= 

237 

238.. autosummary:: 

239 :toctree: generated/ 

240 

241 milp -- Mixed integer linear programming. 

242 linprog -- Unified interface for minimizers of linear programming problems. 

243 

244The `linprog` function supports the following methods: 

245 

246.. toctree:: 

247 

248 optimize.linprog-simplex 

249 optimize.linprog-interior-point 

250 optimize.linprog-revised_simplex 

251 optimize.linprog-highs-ipm 

252 optimize.linprog-highs-ds 

253 optimize.linprog-highs 

254 

255The simplex, interior-point, and revised simplex methods support callback 

256functions, such as: 

257 

258.. autosummary:: 

259 :toctree: generated/ 

260 

261 linprog_verbose_callback -- Sample callback function for linprog (simplex). 

262 

263Assignment problems 

264=================== 

265 

266.. autosummary:: 

267 :toctree: generated/ 

268 

269 linear_sum_assignment -- Solves the linear-sum assignment problem. 

270 quadratic_assignment -- Solves the quadratic assignment problem. 

271 

272The `quadratic_assignment` function supports the following methods: 

273 

274.. toctree:: 

275 

276 optimize.qap-faq 

277 optimize.qap-2opt 

278 

279Utilities 

280========= 

281 

282Finite-difference approximation 

283------------------------------- 

284 

285.. autosummary:: 

286 :toctree: generated/ 

287 

288 approx_fprime - Approximate the gradient of a scalar function. 

289 check_grad - Check the supplied derivative using finite differences. 

290 

291 

292Line search 

293----------- 

294 

295.. autosummary:: 

296 :toctree: generated/ 

297 

298 bracket - Bracket a minimum, given two starting points. 

299 line_search - Return a step that satisfies the strong Wolfe conditions. 

300 

301Hessian approximation 

302--------------------- 

303 

304.. autosummary:: 

305 :toctree: generated/ 

306 

307 LbfgsInvHessProduct - Linear operator for L-BFGS approximate inverse Hessian. 

308 HessianUpdateStrategy - Interface for implementing Hessian update strategies 

309 

310Benchmark problems 

311------------------ 

312 

313.. autosummary:: 

314 :toctree: generated/ 

315 

316 rosen - The Rosenbrock function. 

317 rosen_der - The derivative of the Rosenbrock function. 

318 rosen_hess - The Hessian matrix of the Rosenbrock function. 

319 rosen_hess_prod - Product of the Rosenbrock Hessian with a vector. 

320 

321Legacy functions 

322================ 

323 

324The functions below are not recommended for use in new scripts; 

325all of these methods are accessible via a newer, more consistent 

326interfaces, provided by the interfaces above. 

327 

328Optimization 

329------------ 

330 

331General-purpose multivariate methods: 

332 

333.. autosummary:: 

334 :toctree: generated/ 

335 

336 fmin - Nelder-Mead Simplex algorithm. 

337 fmin_powell - Powell's (modified) level set method. 

338 fmin_cg - Non-linear (Polak-Ribiere) conjugate gradient algorithm. 

339 fmin_bfgs - Quasi-Newton method (Broydon-Fletcher-Goldfarb-Shanno). 

340 fmin_ncg - Line-search Newton Conjugate Gradient. 

341 

342Constrained multivariate methods: 

343 

344.. autosummary:: 

345 :toctree: generated/ 

346 

347 fmin_l_bfgs_b - Zhu, Byrd, and Nocedal's constrained optimizer. 

348 fmin_tnc - Truncated Newton code. 

349 fmin_cobyla - Constrained optimization by linear approximation. 

350 fmin_slsqp - Minimization using sequential least-squares programming. 

351 

352Univariate (scalar) minimization methods: 

353 

354.. autosummary:: 

355 :toctree: generated/ 

356 

357 fminbound - Bounded minimization of a scalar function. 

358 brent - 1-D function minimization using Brent method. 

359 golden - 1-D function minimization using Golden Section method. 

360 

361Least-squares 

362------------- 

363 

364.. autosummary:: 

365 :toctree: generated/ 

366 

367 leastsq - Minimize the sum of squares of M equations in N unknowns. 

368 

369Root finding 

370------------ 

371 

372General nonlinear solvers: 

373 

374.. autosummary:: 

375 :toctree: generated/ 

376 

377 fsolve - Non-linear multivariable equation solver. 

378 broyden1 - Broyden's first method. 

379 broyden2 - Broyden's second method. 

380 

381Large-scale nonlinear solvers: 

382 

383.. autosummary:: 

384 :toctree: generated/ 

385 

386 newton_krylov 

387 anderson 

388 

389 BroydenFirst 

390 InverseJacobian 

391 KrylovJacobian 

392 

393Simple iteration solvers: 

394 

395.. autosummary:: 

396 :toctree: generated/ 

397 

398 excitingmixing 

399 linearmixing 

400 diagbroyden 

401 

402""" 

403 

404from ._optimize import * 

405from ._minimize import * 

406from ._root import * 

407from ._root_scalar import * 

408from ._minpack_py import * 

409from ._zeros_py import * 

410from ._lbfgsb_py import fmin_l_bfgs_b, LbfgsInvHessProduct 

411from ._tnc import fmin_tnc 

412from ._cobyla_py import fmin_cobyla 

413from ._nonlin import * 

414from ._slsqp_py import fmin_slsqp 

415from ._nnls import nnls 

416from ._basinhopping import basinhopping 

417from ._linprog import linprog, linprog_verbose_callback 

418from ._lsap import linear_sum_assignment 

419from ._differentialevolution import differential_evolution 

420from ._lsq import least_squares, lsq_linear 

421from ._constraints import (NonlinearConstraint, 

422 LinearConstraint, 

423 Bounds) 

424from ._hessian_update_strategy import HessianUpdateStrategy, BFGS, SR1 

425from ._shgo import shgo 

426from ._dual_annealing import dual_annealing 

427from ._qap import quadratic_assignment 

428from ._direct_py import direct 

429from ._milp import milp 

430 

431# Deprecated namespaces, to be removed in v2.0.0 

432from . import ( 

433 cobyla, lbfgsb, linesearch, minpack, minpack2, moduleTNC, nonlin, optimize, 

434 slsqp, tnc, zeros 

435) 

436 

437__all__ = [s for s in dir() if not s.startswith('_')] 

438 

439from scipy._lib._testutils import PytestTester 

440test = PytestTester(__name__) 

441del PytestTester