_cobyqa_py.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import numpy as np
  2. from threading import Lock
  3. from ._optimize import _check_unknown_options
  4. COBYQA_LOCK = Lock()
  5. def _minimize_cobyqa(fun, x0, args=(), bounds=None, constraints=(),
  6. callback=None, disp=False, maxfev=None, maxiter=None,
  7. f_target=-np.inf, feasibility_tol=1e-8,
  8. initial_tr_radius=1.0, final_tr_radius=1e-6, scale=False,
  9. **unknown_options):
  10. """
  11. Minimize a scalar function of one or more variables using the
  12. Constrained Optimization BY Quadratic Approximations (COBYQA) algorithm [1]_.
  13. .. versionadded:: 1.14.0
  14. Options
  15. -------
  16. disp : bool
  17. Set to True to print information about the optimization procedure.
  18. Default is ``False``.
  19. maxfev : int
  20. Maximum number of function evaluations. Default is ``500 * n``, where
  21. ``n`` is the number of variables.
  22. maxiter : int
  23. Maximum number of iterations. Default is ``1000 * n``, where ``n`` is
  24. the number of variables.
  25. f_target : float
  26. Target value for the objective function. The optimization procedure is
  27. terminated when the objective function value of a feasible point (see
  28. `feasibility_tol` below) is less than or equal to this target. Default
  29. is ``-numpy.inf``.
  30. feasibility_tol : float
  31. Absolute tolerance for the constraint violation. Default is ``1e-8``.
  32. initial_tr_radius : float
  33. Initial trust-region radius. Typically, this value should be in the
  34. order of one tenth of the greatest expected change to the variables.
  35. Default is ``1.0``.
  36. final_tr_radius : float
  37. Final trust-region radius. It should indicate the accuracy required in
  38. the final values of the variables. If provided, this option overrides
  39. the value of `tol` in the `minimize` function. Default is ``1e-6``.
  40. scale : bool
  41. Set to True to scale the variables according to the bounds. If True and
  42. if all the lower and upper bounds are finite, the variables are scaled
  43. to be within the range :math:`[-1, 1]`. If any of the lower or upper
  44. bounds is infinite, the variables are not scaled. Default is ``False``.
  45. References
  46. ----------
  47. .. [1] COBYQA
  48. https://www.cobyqa.com/stable/
  49. """
  50. from .._lib.cobyqa import minimize # import here to avoid circular imports
  51. _check_unknown_options(unknown_options)
  52. options = {
  53. 'disp': bool(disp),
  54. 'maxfev': int(maxfev) if maxfev is not None else 500 * len(x0),
  55. 'maxiter': int(maxiter) if maxiter is not None else 1000 * len(x0),
  56. 'target': float(f_target),
  57. 'feasibility_tol': float(feasibility_tol),
  58. 'radius_init': float(initial_tr_radius),
  59. 'radius_final': float(final_tr_radius),
  60. 'scale': bool(scale),
  61. }
  62. with COBYQA_LOCK:
  63. return minimize(fun, x0, args, bounds, constraints, callback, options)