__init__.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. """
  2. =====================================================
  3. Optimization and root finding (:mod:`scipy.optimize`)
  4. =====================================================
  5. .. currentmodule:: scipy.optimize
  6. SciPy ``optimize`` provides functions for minimizing (or maximizing)
  7. objective functions, possibly subject to constraints. It includes
  8. solvers for nonlinear problems (with support for both local and global
  9. optimization algorithms), linear programing, constrained
  10. and nonlinear least-squares, root finding, and curve fitting.
  11. Common functions and objects, shared across different solvers, are:
  12. .. autosummary::
  13. :toctree: generated/
  14. show_options - Show specific options optimization solvers.
  15. OptimizeResult - The optimization result returned by some optimizers.
  16. OptimizeWarning - The optimization encountered problems.
  17. Optimization
  18. ============
  19. Scalar functions optimization
  20. -----------------------------
  21. .. autosummary::
  22. :toctree: generated/
  23. minimize_scalar - Interface for minimizers of univariate functions
  24. The `minimize_scalar` function supports the following methods:
  25. .. toctree::
  26. optimize.minimize_scalar-brent
  27. optimize.minimize_scalar-bounded
  28. optimize.minimize_scalar-golden
  29. Local (multivariate) optimization
  30. ---------------------------------
  31. .. autosummary::
  32. :toctree: generated/
  33. minimize - Interface for minimizers of multivariate functions.
  34. The `minimize` function supports the following methods:
  35. .. toctree::
  36. optimize.minimize-neldermead
  37. optimize.minimize-powell
  38. optimize.minimize-cg
  39. optimize.minimize-bfgs
  40. optimize.minimize-newtoncg
  41. optimize.minimize-lbfgsb
  42. optimize.minimize-tnc
  43. optimize.minimize-cobyla
  44. optimize.minimize-slsqp
  45. optimize.minimize-trustconstr
  46. optimize.minimize-dogleg
  47. optimize.minimize-trustncg
  48. optimize.minimize-trustkrylov
  49. optimize.minimize-trustexact
  50. Constraints are passed to `minimize` function as a single object or
  51. as a list of objects from the following classes:
  52. .. autosummary::
  53. :toctree: generated/
  54. NonlinearConstraint - Class defining general nonlinear constraints.
  55. LinearConstraint - Class defining general linear constraints.
  56. Simple bound constraints are handled separately and there is a special class
  57. for them:
  58. .. autosummary::
  59. :toctree: generated/
  60. Bounds - Bound constraints.
  61. Quasi-Newton strategies implementing `HessianUpdateStrategy`
  62. interface can be used to approximate the Hessian in `minimize`
  63. function (available only for the 'trust-constr' method). Available
  64. quasi-Newton methods implementing this interface are:
  65. .. autosummary::
  66. :toctree: generated/
  67. BFGS - Broyden-Fletcher-Goldfarb-Shanno (BFGS) Hessian update strategy.
  68. SR1 - Symmetric-rank-1 Hessian update strategy.
  69. Global optimization
  70. -------------------
  71. .. autosummary::
  72. :toctree: generated/
  73. basinhopping - Basinhopping stochastic optimizer.
  74. brute - Brute force searching optimizer.
  75. differential_evolution - Stochastic optimizer using differential evolution.
  76. shgo - Simplicial homology global optimizer.
  77. dual_annealing - Dual annealing stochastic optimizer.
  78. direct - DIRECT (Dividing Rectangles) optimizer.
  79. Least-squares and curve fitting
  80. ===============================
  81. Nonlinear least-squares
  82. -----------------------
  83. .. autosummary::
  84. :toctree: generated/
  85. least_squares - Solve a nonlinear least-squares problem with bounds on the variables.
  86. Linear least-squares
  87. --------------------
  88. .. autosummary::
  89. :toctree: generated/
  90. nnls - Linear least-squares problem with non-negativity constraint.
  91. lsq_linear - Linear least-squares problem with bound constraints.
  92. Curve fitting
  93. -------------
  94. .. autosummary::
  95. :toctree: generated/
  96. curve_fit -- Fit curve to a set of points.
  97. Root finding
  98. ============
  99. Scalar functions
  100. ----------------
  101. .. autosummary::
  102. :toctree: generated/
  103. root_scalar - Unified interface for nonlinear solvers of scalar functions.
  104. brentq - quadratic interpolation Brent method.
  105. brenth - Brent method, modified by Harris with hyperbolic extrapolation.
  106. ridder - Ridder's method.
  107. bisect - Bisection method.
  108. newton - Newton's method (also Secant and Halley's methods).
  109. toms748 - Alefeld, Potra & Shi Algorithm 748.
  110. RootResults - The root finding result returned by some root finders.
  111. The `root_scalar` function supports the following methods:
  112. .. toctree::
  113. optimize.root_scalar-brentq
  114. optimize.root_scalar-brenth
  115. optimize.root_scalar-bisect
  116. optimize.root_scalar-ridder
  117. optimize.root_scalar-newton
  118. optimize.root_scalar-toms748
  119. optimize.root_scalar-secant
  120. optimize.root_scalar-halley
  121. The table below lists situations and appropriate methods, along with
  122. *asymptotic* convergence rates per iteration (and per function evaluation)
  123. for successful convergence to a simple root(*).
  124. Bisection is the slowest of them all, adding one bit of accuracy for each
  125. function evaluation, but is guaranteed to converge.
  126. The other bracketing methods all (eventually) increase the number of accurate
  127. bits by about 50% for every function evaluation.
  128. The derivative-based methods, all built on `newton`, can converge quite quickly
  129. if the initial value is close to the root. They can also be applied to
  130. functions defined on (a subset of) the complex plane.
  131. +-------------+----------+----------+-----------+-------------+-------------+----------------+
  132. | Domain of f | Bracket? | Derivatives? | Solvers | Convergence |
  133. + + +----------+-----------+ +-------------+----------------+
  134. | | | `fprime` | `fprime2` | | Guaranteed? | Rate(s)(*) |
  135. +=============+==========+==========+===========+=============+=============+================+
  136. | `R` | Yes | N/A | N/A | - bisection | - Yes | - 1 "Linear" |
  137. | | | | | - brentq | - Yes | - >=1, <= 1.62 |
  138. | | | | | - brenth | - Yes | - >=1, <= 1.62 |
  139. | | | | | - ridder | - Yes | - 2.0 (1.41) |
  140. | | | | | - toms748 | - Yes | - 2.7 (1.65) |
  141. +-------------+----------+----------+-----------+-------------+-------------+----------------+
  142. | `R` or `C` | No | No | No | secant | No | 1.62 (1.62) |
  143. +-------------+----------+----------+-----------+-------------+-------------+----------------+
  144. | `R` or `C` | No | Yes | No | newton | No | 2.00 (1.41) |
  145. +-------------+----------+----------+-----------+-------------+-------------+----------------+
  146. | `R` or `C` | No | Yes | Yes | halley | No | 3.00 (1.44) |
  147. +-------------+----------+----------+-----------+-------------+-------------+----------------+
  148. .. seealso::
  149. `scipy.optimize.cython_optimize` -- Typed Cython versions of zeros functions
  150. Fixed point finding:
  151. .. autosummary::
  152. :toctree: generated/
  153. fixed_point - Single-variable fixed-point solver.
  154. Multidimensional
  155. ----------------
  156. .. autosummary::
  157. :toctree: generated/
  158. root - Unified interface for nonlinear solvers of multivariate functions.
  159. The `root` function supports the following methods:
  160. .. toctree::
  161. optimize.root-hybr
  162. optimize.root-lm
  163. optimize.root-broyden1
  164. optimize.root-broyden2
  165. optimize.root-anderson
  166. optimize.root-linearmixing
  167. optimize.root-diagbroyden
  168. optimize.root-excitingmixing
  169. optimize.root-krylov
  170. optimize.root-dfsane
  171. Linear programming / MILP
  172. =========================
  173. .. autosummary::
  174. :toctree: generated/
  175. milp -- Mixed integer linear programming.
  176. linprog -- Unified interface for minimizers of linear programming problems.
  177. The `linprog` function supports the following methods:
  178. .. toctree::
  179. optimize.linprog-simplex
  180. optimize.linprog-interior-point
  181. optimize.linprog-revised_simplex
  182. optimize.linprog-highs-ipm
  183. optimize.linprog-highs-ds
  184. optimize.linprog-highs
  185. The simplex, interior-point, and revised simplex methods support callback
  186. functions, such as:
  187. .. autosummary::
  188. :toctree: generated/
  189. linprog_verbose_callback -- Sample callback function for linprog (simplex).
  190. Assignment problems
  191. ===================
  192. .. autosummary::
  193. :toctree: generated/
  194. linear_sum_assignment -- Solves the linear-sum assignment problem.
  195. quadratic_assignment -- Solves the quadratic assignment problem.
  196. The `quadratic_assignment` function supports the following methods:
  197. .. toctree::
  198. optimize.qap-faq
  199. optimize.qap-2opt
  200. Utilities
  201. =========
  202. Finite-difference approximation
  203. -------------------------------
  204. .. autosummary::
  205. :toctree: generated/
  206. approx_fprime - Approximate the gradient of a scalar function.
  207. check_grad - Check the supplied derivative using finite differences.
  208. Line search
  209. -----------
  210. .. autosummary::
  211. :toctree: generated/
  212. bracket - Bracket a minimum, given two starting points.
  213. line_search - Return a step that satisfies the strong Wolfe conditions.
  214. Hessian approximation
  215. ---------------------
  216. .. autosummary::
  217. :toctree: generated/
  218. LbfgsInvHessProduct - Linear operator for L-BFGS approximate inverse Hessian.
  219. HessianUpdateStrategy - Interface for implementing Hessian update strategies
  220. Benchmark problems
  221. ------------------
  222. .. autosummary::
  223. :toctree: generated/
  224. rosen - The Rosenbrock function.
  225. rosen_der - The derivative of the Rosenbrock function.
  226. rosen_hess - The Hessian matrix of the Rosenbrock function.
  227. rosen_hess_prod - Product of the Rosenbrock Hessian with a vector.
  228. Legacy functions
  229. ================
  230. The functions below are not recommended for use in new scripts;
  231. all of these methods are accessible via a newer, more consistent
  232. interfaces, provided by the interfaces above.
  233. Optimization
  234. ------------
  235. General-purpose multivariate methods:
  236. .. autosummary::
  237. :toctree: generated/
  238. fmin - Nelder-Mead Simplex algorithm.
  239. fmin_powell - Powell's (modified) level set method.
  240. fmin_cg - Non-linear (Polak-Ribiere) conjugate gradient algorithm.
  241. fmin_bfgs - Quasi-Newton method (Broydon-Fletcher-Goldfarb-Shanno).
  242. fmin_ncg - Line-search Newton Conjugate Gradient.
  243. Constrained multivariate methods:
  244. .. autosummary::
  245. :toctree: generated/
  246. fmin_l_bfgs_b - Zhu, Byrd, and Nocedal's constrained optimizer.
  247. fmin_tnc - Truncated Newton code.
  248. fmin_cobyla - Constrained optimization by linear approximation.
  249. fmin_slsqp - Minimization using sequential least-squares programming.
  250. Univariate (scalar) minimization methods:
  251. .. autosummary::
  252. :toctree: generated/
  253. fminbound - Bounded minimization of a scalar function.
  254. brent - 1-D function minimization using Brent method.
  255. golden - 1-D function minimization using Golden Section method.
  256. Least-squares
  257. -------------
  258. .. autosummary::
  259. :toctree: generated/
  260. leastsq - Minimize the sum of squares of M equations in N unknowns.
  261. Root finding
  262. ------------
  263. General nonlinear solvers:
  264. .. autosummary::
  265. :toctree: generated/
  266. fsolve - Non-linear multivariable equation solver.
  267. broyden1 - Broyden's first method.
  268. broyden2 - Broyden's second method.
  269. Large-scale nonlinear solvers:
  270. .. autosummary::
  271. :toctree: generated/
  272. newton_krylov
  273. anderson
  274. BroydenFirst
  275. InverseJacobian
  276. KrylovJacobian
  277. Simple iteration solvers:
  278. .. autosummary::
  279. :toctree: generated/
  280. excitingmixing
  281. linearmixing
  282. diagbroyden
  283. """
  284. from ._optimize import *
  285. from ._minimize import *
  286. from ._root import *
  287. from ._root_scalar import *
  288. from ._minpack_py import *
  289. from ._zeros_py import *
  290. from ._lbfgsb_py import fmin_l_bfgs_b, LbfgsInvHessProduct
  291. from ._tnc import fmin_tnc
  292. from ._cobyla_py import fmin_cobyla
  293. from ._nonlin import *
  294. from ._slsqp_py import fmin_slsqp
  295. from ._nnls import nnls
  296. from ._basinhopping import basinhopping
  297. from ._linprog import linprog, linprog_verbose_callback
  298. from ._lsap import linear_sum_assignment
  299. from ._differentialevolution import differential_evolution
  300. from ._lsq import least_squares, lsq_linear
  301. from ._constraints import (NonlinearConstraint,
  302. LinearConstraint,
  303. Bounds)
  304. from ._hessian_update_strategy import HessianUpdateStrategy, BFGS, SR1
  305. from ._shgo import shgo
  306. from ._dual_annealing import dual_annealing
  307. from ._qap import quadratic_assignment
  308. from ._direct_py import direct
  309. from ._milp import milp
  310. # Deprecated namespaces, to be removed in v2.0.0
  311. from . import (
  312. cobyla, lbfgsb, linesearch, minpack, minpack2, moduleTNC, nonlin, optimize,
  313. slsqp, tnc, zeros
  314. )
  315. __all__ = [s for s in dir() if not s.startswith('_')]
  316. from scipy._lib._testutils import PytestTester
  317. test = PytestTester(__name__)
  318. del PytestTester