
    (phs*                         S r SSKrSSKJr  SSKrSSKJr  SSK	J
r
JrJr   SSKJr  S/r\" 5       rS	 r\  SSS
.S jj5       r\   SS j5       rg! \ a    \r N5f = f)z
Interface to Constrained Optimization By Linear Approximation

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

    fmin_cobyla

    N)RLock)_cobyla   )OptimizeResult_check_unknown_options_prepare_scalar_function)izipfmin_cobylac                 F   ^  [         R                  " T 5      U 4S j5       nU$ )Nc                  V   > [            T" U 0 UD6sS S S 5        $ ! , (       d  f       g = fN)_module_lock)argskwargsfuncs     L/var/www/html/venv/lib/python3.13/site-packages/scipy/optimize/_cobyla_py.pywrappersynchronized.<locals>.wrapper    s    (( \\s   
()	functoolswraps)r   r   s   ` r   synchronizedr      s%    __T) ) N    )callbackc
                  ^ Sn [        U5        U H  n[        U5      (       a  M  [        U5      e   Tc  Um[        U4S jU 5       5      n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)	ad  
Minimize a function using the Constrained Optimization By Linear
Approximation (COBYLA) method. This method wraps a FORTRAN
implementation of the algorithm.

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.


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


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              3   0   >#    U  H  nS UTS.v   M     g7f)ineq)typefunr   N ).0cconsargss     r   	<genexpr>fmin_cobyla.<locals>.<genexpr>   s     Kd8<ds   )rhobegtoldispmaxitercatolr   constraintssuccessz"COBYLA failed to find a solution: x)lencallable	TypeErrortuple_minimize_cobylaprintmessage)r   x0consr   r"   r%   rhoendmaxfunr'   r)   r   errthisfunceconoptssols       `            r   r
   r
   &   s    h C
%D	 HH%%n$   KdK
KC  "D 4T #s #!#CC	N23;;-@As8O9  (D>>6DC.a'	(s   B 
CC6CCc                   ^^	^
^^^ ^! [        U5        UnUn[        [        U5      5      n[        T[        5      (       a  T4mT
(       a  [
        R                  " T
R                  5      m[
        R                  " T5      (       a  U
U4S jnTR                  SUS.5        [
        R                  " T
R                  5      m [
        R                  " T 5      (       a  U
U 4S jnTR                  SUS.5        [        T5       HT  u  nn US   R                  5       nUS:w  a  [        SUS    S35      e SU;  a  [        SU-  5      eSU;  d  MO  SUS'   MV     / mT H/  nUS   " U/US   Q76 n [#        U5      nTR                  U5        M1     [%        T5      nS n['        XUUS9m!UUU!4S jnU	4S jn[
        R(                  " S[
        R*                  5      n[,        R.                  " UU[
        R0                  " U5      UXUUUS9	u  nnUS   U:  a  SUS'   [3        U[        US   5      US   S:H  SSSSSS.R5                  US   S5      [        US   5      US    US   S!9$ ! [         a  n[        SU-  5      UeS	nAf[         a  n[        S
5      UeS	nAf[          a  n[        S5      UeS	nAff = f! [         a    Sn GNOf = f)"aH  
Minimize a scalar function of one or more variables using the
Constrained Optimization BY Linear Approximation (COBYLA) algorithm.

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 : bool
    Set to True to print convergence messages. If False,
    `verbosity` is ignored as set to 0.
maxiter : int
    Maximum number of function evaluations.
catol : float
    Tolerance (absolute) for constraint violations

c                 .   > U T   TR                   T   -
  $ r   )lb)r,   r   r   boundsi_lbs      r   lb_constraint'_minimize_cobyla.<locals>.lb_constraint   s    w400r   r   )r   r   c                 .   > TR                   T   U T   -
  $ r   )ub)r,   rA   i_ubs    r   ub_constraint'_minimize_cobyla.<locals>.ub_constraint   s    yy400r   r   zConstraints of type 'z' not handled by COBYLA.z"Constraint %d has no type defined.Nz/Constraints must be defined using a dictionary.z#Constraint's type must be a string.r   z&Constraint %d has no function defined.r   r   r   c                     g r   r   )r,   r   s     r   _jac_minimize_cobyla.<locals>._jac  s    r   )r   jacc                    > TR                  U 5      nSn[        TT5       H  u  pEUS   " U /US   Q76 XX4-   & X4-  nM     U$ )Nr   r   r   )r   r	   )	r,   r;   fisizer!   cons_lengthsr*   sfs	         r   calcfc _minimize_cobyla.<locals>.calcfc  sT    FF1IL+6GD x6AfI6C18IA 7 r   c                 F   > Tb  T" [         R                  " U 5      5        g g r   )npcopy)r,   r   s    r   wrapped_callback*_minimize_cobyla.<locals>.wrapped_callback   s    RWWQZ   r      )mr,   r%   r6   iprintr7   dinfor      r   z%Optimization terminated successfully.z9Maximum number of function evaluations has been exceeded.z;Rounding errors are becoming damaging in COBYLA subroutine.zbDid not converge to a solution satisfying the constraints. See `maxcv` for magnitude of violation.zNaN result encountered.)r      r_   r[      zUnknown exit status.r`   )r,   statusr+   r3   nfevr   maxcv)r   intbool
isinstancedictrW   isfiniter@   anyappendrF   	enumeratelower
ValueErrorKeyErrorr/   AttributeErrorr-   sumr   zerosfloat64cobylaminimizerX   r   get)"r   r4   r   r*   r%   r&   r(   r'   r)   r   rA   unknown_optionsr7   r6   r]   rC   rH   icr;   ctyper:   r!   rO   cons_lengthr\   rK   rT   rY   infoxoptrR   rB   rG   rS   s"      `     ``                   @@@@r   r1   r1      s   2 ?+FFd_F +t$$"o{{699%66$<<1 }EF{{699%66$<<1 }EF[)C	,K%%'E  #8V F+ "+ , , 
 CbHII CK- *4 LeHR$!F)$	a&K 	K(  	LA 
"#$	?B! 88Arzz"D1F)/v(,7GIJD$ AwQD!$T!W"&q'Q,&M';'>'L '@	#% &)Sa2H%I"47|"1g $Q) )w  	M?"DE1L 	2 * +012 	JABI	J,  	K	sB   I(0J;(
J82JJ8JJ8'J33J8;K
K)r   N      ?-C6?  N-C6*?)	r   r   r}   r~   r   Fr   NN)__doc__r   	threadingr   numpyrW   scipy.optimizer   rt   	_optimizer   r   r   	itertoolsr	   ImportErrorzip__all__r   r   r
   r1   r   r   r   <module>r      s   
    ,  /
 w ?B;?S S Sl 3537CG~) ~)Q  Ds   A AA