
    9i*              
           S r SSKJr  SSKrSSKJrJrJr  SSK	J
r
  S/r  SSS.S jjrSSS	S
SSS\R                  * SS4
S jrg)z
Interface to Constrained Optimization By Linear Approximation

Functions
---------
.. autosummary::
   :toctree: generated/

    fmin_cobyla

    )	signatureN   )OptimizeResult_check_unknown_options_prepare_scalar_function)NonlinearConstraintfmin_cobyla       ?-C6?  )callbackc
                  ^ Sn [        U5        U H  n[        U5      (       a  M  [        U5      e   Tc  Um/ nU H6  nU4U4S jjnUR                  [	        US[
        R                  5      5        M8     UUUUU	U
S.n[        XU4SU0UD6nU(       a"  US   (       d  [        SUR                   35        US	   $ ! [         a)  n[        U5      (       a  U/n SnAN[        U5      UeSnAff = f)
a  
Minimize a function using the Constrained Optimization By Linear
Approximation (COBYLA) method. This method uses the pure-python implementation
of the algorithm from PRIMA.

Parameters
----------
func : callable
    Function to minimize. In the form func(x, \*args).
x0 : ndarray
    Initial guess.
cons : sequence
    Constraint functions; must all be ``>=0`` (a single function
    if only 1 constraint). Each function takes the parameters `x`
    as its first argument, and it can return either a single number or
    an array or list of numbers.
args : tuple, optional
    Extra arguments to pass to function.
consargs : tuple, optional
    Extra arguments to pass to constraint functions (default of None means
    use same extra arguments as those passed to func).
    Use ``()`` for no extra arguments.
rhobeg : float, optional
    Reasonable initial changes to the variables.
rhoend : float, optional
    Final accuracy in the optimization (not precisely guaranteed). This
    is a lower bound on the size of the trust region.
disp : {0, 1, 2, 3}, optional
    Controls the frequency of output; 0 implies no output.
maxfun : int, optional
    Maximum number of function evaluations.
catol : float, optional
    Absolute tolerance for constraint violations.
callback : callable, optional
    Called after each iteration, as ``callback(x)``, where ``x`` is the
    current parameter vector.

Returns
-------
x : ndarray
    The argument that minimises `f`.

See also
--------
minimize: Interface to minimization algorithms for multivariate
    functions. See the 'COBYLA' `method` in particular.

Notes
-----
This algorithm is based on linear approximations to the objective
function and each constraint. We briefly describe the algorithm.

Suppose the function is being minimized over k variables. At the
jth iteration the algorithm has k+1 points v_1, ..., v_(k+1),
an approximate solution x_j, and a radius RHO_j.
(i.e., linear plus a constant) approximations to the objective
function and constraint functions such that their function values
agree with the linear approximation on the k+1 points v_1,.., v_(k+1).
This gives a linear program to solve (where the linear approximations
of the constraint functions are constrained to be non-negative).

However, the linear approximations are likely only good
approximations near the current simplex, so the linear program is
given the further requirement that the solution, which
will become x_(j+1), must be within RHO_j from x_j. RHO_j only
decreases, never increases. The initial RHO_j is rhobeg and the
final RHO_j is rhoend. In this way COBYLA's iterations behave
like a trust region algorithm.

Additionally, the linear program may be inconsistent, or the
approximation may give poor improvement. For details about
how these issues are resolved, as well as how the points v_i are
updated, refer to the source code or the references below.

    .. versionchanged:: 1.16.0
        The original Powell implementation was replaced by a pure
        Python version from the PRIMA package, with bug fixes and
        improvements being made.


References
----------
Powell M.J.D. (1994), "A direct search optimization method that models
the objective and constraint functions by linear interpolation.", in
Advances in Optimization and Numerical Analysis, eds. S. Gomez and
J-P Hennart, Kluwer Academic (Dordrecht), pp. 51-67

Powell M.J.D. (1998), "Direct search algorithms for optimization
calculations", Acta Numerica 7, 287-336

Powell M.J.D. (2007), "A view of algorithms for optimization without
derivatives", Cambridge University Technical Report DAMTP 2007/NA03

Zhang Z. (2023), "PRIMA: Reference Implementation for Powell's Methods with
Modernization and Amelioration", https://www.libprima.net,
:doi:`10.5281/zenodo.8052654`

Examples
--------
Minimize the objective function f(x,y) = x*y subject
to the constraints x**2 + y**2 < 1 and y > 0::

    >>> def objective(x):
    ...     return x[0]*x[1]
    ...
    >>> def constr1(x):
    ...     return 1 - (x[0]**2 + x[1]**2)
    ...
    >>> def constr2(x):
    ...     return x[1]
    ...
    >>> from scipy.optimize import fmin_cobyla
    >>> fmin_cobyla(objective, [0.0, 0.1], [constr1, constr2], rhoend=1e-7)
    array([-0.70710685,  0.70710671])

The exact solution is (-sqrt(2)/2, sqrt(2)/2).



zLcons must be a sequence of callable functions or a single callable function.Nc                    > U" U /TQ76 $ Nr
   )xconfuncconsargss     Y/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/scipy/optimize/_cobyla_py.pywrapped_con fmin_cobyla.<locals>.wrapped_con   s    1(x((    r   )rhobegtoldispmaxitercatolr   constraintssuccessz"COBYLA failed to find a solution: r   )
lencallable	TypeErrorappendr   npinf_minimize_cobylaprintmessage)funcx0consargsr   r   rhoendmaxfunr   r   r   errthisfuncenlcsconr   optssols       `              r   r	   r	      s   v C
%D	 HH%%n$   D#& 	)'Q?@	   "D 4T #t #!#CC	N23;;-@As8OC  (D>>6DC.a'	(s   B= =
C0C+C++C0c           
        ^
^ SSK Jn  SSKJnJn  SSKJn  [        U5        UnUb  UOSnUS:w  a   US:w  a  US:w  a  US:w  a  [        S	U 35      eS
 n[        XUUS9nT
b9  [        T
5      n[        UR                  5      S1:X  a  U
4S jmOU
4S jmU4S jnOSnUb  UO7[        R                  " [        R                  " [         5      R"                  5      nUUUUUU	S.nU" UR$                  USUUUUS9nUR&                  U:  a  SnSnO8UR(                  U:H  =(       d    UR(                  U:H  nU" SUR(                  5      n[+        UR,                  UR(                  UUUR.                  UR0                  UR&                  S9$ )a  
Minimize a scalar function of one or more variables using the
Constrained Optimization BY Linear Approximation (COBYLA) algorithm.
This method uses the pure-python implementation of the algorithm from PRIMA.

Options
-------
rhobeg : float
    Reasonable initial changes to the variables.
tol : float
    Final accuracy in the optimization (not precisely guaranteed).
    This is a lower bound on the size of the trust region.
disp : int
    Controls the frequency of output:
        0. (default) There will be no printing
        1. A message will be printed to the screen at the end of iteration, showing
           the best vector of variables found and its objective function value
        2. in addition to 1, each new value of RHO is printed to the screen,
           with the best vector of variables so far and its objective function
           value.
        3. in addition to 2, each function evaluation with its variables will
           be printed to the screen.
maxiter : int
    Maximum number of function evaluations.
catol : float
    Tolerance (absolute) for constraint violations
f_target : float
    Stop if the objective function is less than `f_target`.

    .. versionchanged:: 1.16.0
        The original Powell implementation was replaced by a pure
        Python version from the PRIMA package, with bug fixes and
        improvements being made.


References
----------
Zhang Z. (2023), "PRIMA: Reference Implementation for Powell's Methods with
Modernization and Amelioration", https://www.libprima.net,
:doi:`10.5281/zenodo.8052654`
   )minimize)SMALL_TR_RADIUSFTARGET_ACHIEVED)get_info_stringNr   r      zSdisp argument to minimize must be 0, 1, 2, or 3,                          received c                     g r   r
   )r   r,   s     r   _jac_minimize_cobyla.<locals>._jac   s    r   )r,   jacintermediate_resultc                 P   > [        [        R                  " U 5      XX4S9nT" US9  g )N)r   funnfevnitmaxcv)rA   )r   r$   copy)r   fnftrcstrvnlconstrlistrA   r   s          r   wrapped_callback_intermediate7_minimize_cobyla.<locals>.wrapped_callback_intermediate   s'    &4rwwqzq9;'J#-@Ar   c                 >   > T" [         R                  " U 5      5        g r   )r$   rG   )r   rH   rI   rJ   rK   rL   r   s         r   rM   rN      s    $r   c                 <   >  T" XX#XE5        g! [          a     gf = f)NFT)StopIteration)r   rH   rI   rJ   rK   rL   rM   s         r   wrapped_callback*_minimize_cobyla.<locals>.wrapped_callback  s)    -aBEP  s   
 
)r   r-   maxfeviprintctolftargetcobyla)methodboundsr   r   optionsFzjDid not converge to a solution satisfying the constraints. See `maxcv` for the magnitude of the violation.COBYLA)r   statusr   r(   rD   rC   rF   )_lib.pyprimar8   _lib.pyprima.common.infosr9   r:   _lib.pyprima.common.messager;   r   
ValueErrorr   r   set
parametersr$   sqrtfinfofloatepsrC   rK   infor   r   rI   rH   )rC   r*   r,   r   r   r   r   r   r   f_targetr   rZ   unknown_optionsr8   r9   r:   r;   r-   rU   r>   sfsigrR   rV   r[   resultr   r(   rM   s             `                 @r   r&   r&      s   Z (M=?+F%T1F{v{v{v{ $$*8- . 	. 
"#$	?B!s~~#8"99B
%	   %5277288E?3F3F+GDG bffb&"-8H%'F
 ||d@ ++0SFKKCS4S!(FKK8FHH!'")")%yy$hh &. .r   )r
   Nr   r   r   Ng-C6*?)__doc__inspectr   numpyr$   	_optimizer   r   r   _constraintsr   __all__r	   r%   r&   r
   r   r   <module>rt      sh   
    - / @C;?` `F $&2T44266'"4n.r   