
    -iV                        S r SSKJrJrJrJr  SSKJrJrJ	r	J
r
JrJrJrJrJrJrJrJrJrJr  SSKJr  SSKJr  SSKJr  SSKJr  S	S
KrS	SKJr  S	SK J r   \ " S S5      5       r!S
S
S
S
S
S
S
S
S
S
\\
\S
\S
S
\\S
SS
4S jr"SS jr#g
)a
  
This module provides Powell's COBYLA algorithm.

Translated from Zaikun Zhang's modern-Fortran reference implementation in PRIMA.

Dedicated to late Professor M. J. D. Powell FRS (1936--2015).

Python translation by Nickolai Belakovski.

N.B.:

1. The modern-Fortran reference implementation in PRIMA contains bug fixes and improvements over the
original Fortran 77 implementation by Powell. Consequently, the PRIMA implementation behaves differently
from the original Fortran 77 implementation by Powell. Therefore, it is important to point out that
you are using PRIMA rather than the original solvers if you want your results to be reproducible.

2. Compared to Powell's Fortran 77 implementation, the modern-Fortran implementation and hence any
faithful translation like this one generally produce better solutions with fewer function evaluations,
making them preferable for applications with expensive function evaluations. However, if function
evaluations are not the dominant cost in your application, the Fortran 77 solvers are likely to be
faster, as they are more efficient in terms of memory usage and flops thanks to the careful and
ingenious (but unmaintained and unmaintainable) implementation by Powell.

See the PRIMA documentation (www.libprima.net) for more information.
   )evaluate	moderatex	moderatef	moderatec)EPSRHOBEG_DEFAULTRHOEND_DEFAULTCTOL_DEFAULTCWEIGHT_DEFAULTFTARGET_DEFAULTIPRINT_DEFAULTMAXFUN_DIM_DEFAULT	DEBUGGINGBOUNDMAXETA1_DEFAULTETA2_DEFAULTGAMMA1_DEFAULTGAMMA2_DEFAULT)preproc)present)matprod   )cobylb    N)	dataclass)copyc                   
   \ rS rSr% \R
                  \S'   \\S'   \R
                  \S'   \\S'   \\S'   \R
                  S-  \S'   \R
                  S-  \S	'   \R
                  S-  \S
'   \R
                  S-  \S'   \\S'   Sr	g)COBYLAResult*   xfconstrcstrvnfNxhistfhistchistconhistinfo )
__name__
__module____qualname____firstlineno__npndarray__annotations__floatint__static_attributes__r*       S/var/www/html/venv/lib/python3.13/site-packages/scipy/_lib/pyprima/cobyla/cobyla.pyr   r   *   se    	zzMHJJLG::::::ZZ$
Ir5   r   i  c                 B   SnSn[        U5      (       a  [        U5      OSn[        U5      (       a  [        U5      OSn[        U5      (       a  [        U[        * :  5      OSn[        U5      (       a  [        U[        :  5      OSnUU-   SU-  -   U-   U-   n[        U5      n [        (       Ga}  US:  d
   U S35       eU S:  d
   U S35       e[        U5      [        U5      :X  d
   U S35       e[        U5      (       a  UR
                  UU 4:X  d
   U S35       e[        U5      [        U5      :X  d
   U S	35       e[        U5      (       a  UR
                  UU 4:X  d
   U S
35       e[        U5      (       a  [        U5      U :X  d
   U S35       e[        U5      (       a  [        U5      U :X  d
   U S35       e[        U
5      (       a  [        U	5      (       d
   U S35       e[        U	5      (       a5  [        R                  " U	5      (       d  [        U
5      (       d
   U S35       e[        U
5      (       a  [        R                  " U
5      U:X  d   eUb:  [        U5      n[        * U[        R                  " U5      '   [        * Xw[        * :  '   Ub7  [        U5      n[        U[        R                  " U5      '   [        X[        :  '   [        XSXdXx5      u  n!n"[        R                  " U5      n#[        U	5      (       ar  [        U
5      (       ab  [        [        R                  " U5      5      (       a>  [        U	5      n$U!b  [        [!        U!U5      U"-
  5      U#SUU-
  & [        U
5      U#UU-
  S& O3[#        U5      n[%        XUU!U"5      u  n$n#[        U#SUU-
   5      U#SUU-
  & ['        [        R(                  " SU#5      5      n%[        U5      (       a  UnOK[        U5      (       a5  [        R                  " U5      (       a  US:  a  ['        SU-  [*        5      nO[*        n[        U5      (       a  UnO9US:  a-  ['        [,        [/        [0        [*        -  U-  [0        5      5      nO[0        n[        U5      (       a  UO[2        U -  n[        U5      (       a  UnO:[        U5      (       a$  SUs=:  a  S:  a  O  O['        [,        US-  5      nO[4        n[        U5      (       a  UnOSUs=:  a  S:  a  O  O	US-   S-  nO[6        n[        U5      (       a  UO['        UU S-   [2        U -  5      n[9        UU UUUUUUUUUUUUUUUS:  S9u  nnnnnnn&nnnnnnnn'[;        U UUUU!U"UUUUUUUUUU#U$UUU5      u
  nn$n#n%n(n)n*n+n,n-[=        UU$U#U%U(U)U*U+U,U-5
      $ )a&  
Among all the arguments, only CALCFC, M_NLCON, and X are obligatory. The others are
OPTIONAL and you can neglect them unless you are familiar with the algorithm. Any
unspecified optional input will take the default value detailed below. For
instance, we may invoke the solver as follows.

# First define CALCFC, M_NLCON, and X, and then do the following.
result = cobyla(calcfc, m_nlcon, x)

or

# First define CALCFC, M_NLCON, X, Aineq, and Bineq, and then do the following.
result = cobyla(calcfc, m_nlcon, x, Aineq=Aineq, bineq=bineq, rhobeg=1.0e0,
    rhoend=1.0e-6)

####################################################################################
# IMPORTANT NOTICE: The user must set M_NLCON correctly to the number of nonlinear
# constraints, namely the size of NLCONSTR introduced below. Set it to 0 if there
# is no nonlinear constraint.
####################################################################################

See examples/cobyla/cobyla_example.py for a concrete example.

A detailed introduction to the arguments is as follows.

####################################################################################
# INPUTS
####################################################################################

CALCFC
  Input, function.
  f, nlconstr = CALCFC(X) should evaluate the objective function and nonlinear
  constraints at the given vector X; it should return a tuple consisting of the
  objective function value and the nonlinear constraint value. It must be provided
  by the user, and its definition must conform to the following interface:
  #-------------------------------------------------------------------------#
   def calcfc(x):
       f = 0.0
       nlconstr = np.zeros(m_nlcon)
       return f, nlconstr
  #-------------------------------------------------------------------------#

M_NLCON
  Input, scalar.
  M_NLCON must be set to the number of nonlinear constraints, namely the size of
  NLCONSTR(X).
  N.B.:
  1. Why don't we define M_NLCON as optional and default it to 0 when it is absent?
  This is because we need to allocate memory for CONSTR_LOC using M_NLCON. To
  ensure that the size of CONSTR_LOC is correct, we require the user to specify
  M_NLCON explicitly.

X
  Input, vector.
  As an input, X should be an N-dimensional vector that contains the starting
  point, N being the dimension of the problem.

Aineq, Bineq
  Input, matrix of size [Mineq, N] and vector of size Mineq unless they are both
  empty, default: None and None.
  Aineq and Bineq represent the linear inequality constraints: Aineq*X <= Bineq.

Aeq, Beq
  Input, matrix of size [Meq, N] and vector of size Meq unless they are both
  empty, default: None and None.
  Aeq and Beq represent the linear equality constraints: Aeq*X = Beq.

XL, XU
  Input, vectors of size N unless they are both None, default: None and None.
  XL is the lower bound for X. If XL is None, X has no
  lower bound. Any entry of XL that is NaN or below -BOUNDMAX will be taken as
  -BOUNDMAX, which effectively means there is no lower bound for the corresponding
  entry of X. The value of BOUNDMAX is 0.25*HUGE(X), which is about 8.6E37 for
  single precision and 4.5E307 for double precision. XU is similar.

F0
  Input, scalar.
  F0, if present, should be set to the objective function value of the starting X.

NLCONSTR0
  Input, vector.
  NLCONSTR0, if present, should be set to the nonlinear constraint value at the
  starting X; in addition, SIZE(NLCONSTR0) must be M_NLCON, or the solver will
  abort.

RHOBEG, RHOEND
  Inputs, scalars, default: RHOBEG = 1, RHOEND = 10^-6. RHOBEG and RHOEND must be
  set to the initial and final values of a trust-region radius, both being positive
  and RHOEND <= RHOBEG. Typically RHOBEG should be about one tenth of the greatest
  expected change to a variable, and RHOEND should indicate the accuracy that is
  required in the final values of the variables.

FTARGET
  Input, scalar, default: -Inf.
  FTARGET is the target function value. The algorithm will terminate when a
  feasible point with a function value <= FTARGET is found.

CTOL
  Input, scalar, default: sqrt(machine epsilon).
  CTOL is the tolerance of constraint violation. X is considered feasible if
  CSTRV(X) <= CTOL.
  N.B.:
    1. CTOL is absolute, not relative.
    2. CTOL is used only when selecting the returned X. It does not affect the
       iterations of the algorithm.

CWEIGHT
  Input, scalar, default: CWEIGHT_DFT defined in common/consts.py.
  CWEIGHT is the weight that the constraint violation takes in the selection of the
  returned X.

MAXFUN
  Input, integer scalar, default: MAXFUN_DIM_DFT*N with MAXFUN_DIM_DFT defined in
  common/consts.py. MAXFUN is the maximal number of calls of CALCFC.

IPRINT
  Input, integer scalar, default: 0.
  The value of IPRINT should be set to 0, 1, -1, 2, -2, 3, or -3, which controls
  how much information will be printed during the computation:
  0: there will be no printing;
  1: a message will be printed to the screen at the return, 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; each new
     value of CPEN is also printed;
  3: in addition to 2, each function evaluation with its variables will be printed
     to the screen; -1, -2, -3: the same information as 1, 2, 3 will be printed,
     not to the screen but to a file named COBYLA_output.txt; the file will be
     created if it does not exist; the new output will be appended to the end of
     this file if it already exists.
  Note that IPRINT = +/-3 can be costly in terms of time and/or space.

ETA1, ETA2, GAMMA1, GAMMA2
  Input, scalars, default: ETA1 = 0.1, ETA2 = 0.7, GAMMA1 = 0.5, and GAMMA2 = 2.
  ETA1, ETA2, GAMMA1, and GAMMA2 are parameters in the updating scheme of the
  trust-region radius detailed in the subroutine TRRAD in trustregion.py. Roughly
  speaking, the trust-region radius is contracted by a factor of GAMMA1 when the
  reduction ratio is below ETA1, and enlarged by a factor of GAMMA2 when the
  reduction ratio is above ETA2. It is required that 0 < ETA1 <= ETA2 < 1 and
  0 < GAMMA1 < 1 < GAMMA2. Normally, ETA1 <= 0.25. It is NOT advised to set
  ETA1 >= 0.5.

MAXFILT
  Input, scalar.
  MAXFILT is a nonnegative integer indicating the maximal length of the filter used
  for selecting the returned solution; default: MAXFILT_DFT (a value lower than
  MIN_MAXFILT is not recommended);
  see common/consts.py for the definitions of MAXFILT_DFT and MIN_MAXFILT.

CALLBACK
  Input, function to report progress and optionally request termination.


####################################################################################
# OUTPUTS
####################################################################################

The output is a single data structure, COBYLAResult, with the following fields:

X
  Output, vector.
  As an output, X will be set to an approximate minimizer.

F
  Output, scalar.
  F will be set to the objective function value of X at exit.

CONSTR
  Output, vector.
  CONSTR will be set to the constraint value of X at exit.

CSTRV
  Output, scalar.
  CSTRV will be set to the constraint violation of X at exit, i.e.,
  max([0, XL - X, X - XU, Aineq*X - Bineq, ABS(Aeq*X -Beq), NLCONSTR(X)]).

NF
  Output, scalar.
  NF will be set to the number of calls of CALCFC at exit.

XHIST, FHIST, CHIST, CONHIST, MAXHIST
  XHIST: Output, rank 2 array;
  FHIST: Output, rank 1 array;
  CHIST: Output, rank 1 array;
  CONHIST: Output, rank 2 array;
  MAXHIST: Input, scalar, default: MAXFUN
  XHIST, if present, will output the history of iterates; FHIST, if present, will
  output the history function values; CHIST, if present, will output the history of
  constraint violations; CONHIST, if present, will output the history of constraint
  values; MAXHIST should be a nonnegative integer, and XHIST/FHIST/CHIST/CONHIST
  will output only the history of the last MAXHIST iterations.
  Therefore, MAXHIST= 0 means XHIST/FHIST/CONHIST/CHIST will output
  nothing, while setting MAXHIST = MAXFUN requests XHIST/FHIST/CHIST/CONHIST to
  output all the history. If XHIST is present, its size at exit will be
  (N, min(NF, MAXHIST)); if FHIST is present, its size at exit will be
  min(NF, MAXHIST); if CHIST is present, its size at exit will be min(NF, MAXHIST);
  if CONHIST is present, its size at exit will be (M, min(NF, MAXHIST)).

  IMPORTANT NOTICE:
  Setting MAXHIST to a large value can be costly in terms of memory for large
  problems.
  MAXHIST will be reset to a smaller value if the memory needed exceeds MAXHISTMEM
  defined in common/consts.py
  Use *HIST with caution!!! (N.B.: the algorithm is NOT designed for large
  problems).

INFO
  Output, scalar.
  INFO is the exit flag. It will be set to one of the following values defined in
  common/infos.py:
  SMALL_TR_RADIUS: the lower bound for the trust region radius is reached;
  FTARGET_ACHIEVED: the target function value is reached;
  MAXFUN_REACHED: the objective function has been evaluated MAXFUN times;
  MAXTR_REACHED: the trust region iteration has been performed MAXTR times (MAXTR = 2*MAXFUN);
  NAN_INF_X: NaN or Inf occurs in X;
  DAMAGING_ROUNDING: rounding errors are becoming damaging.
  #--------------------------------------------------------------------------#
  The following case(s) should NEVER occur unless there is a bug.
  NAN_INF_F: the objective function returns NaN or +Inf;
  NAN_INF_MODEL: NaN or Inf occurs in the model;
  TRSUBP_FAILED: a trust region step failed to reduce the model
  #--------------------------------------------------------------------------#
COBYLAr   r   z M_NLCON >= 0r   z N >= 1z0 Aineq and Bineq are both present or both absentz SIZE(Aineq) == [Mineq, N]z, Aeq and Beq are both present or both absentz SIZE(Aeq) == [Meq, N]z SIZE(XL) == Nz SIZE(XU) == Nz, If NLCONSTR0 is present, then F0 is presentz8 If F0 is present and not NaN, then NLCONSTR0 is presentN
         )	num_constraintsmaxfiltctolcweighteta1eta2gamma1gamma2is_constrained)r   lensumr   r   shaper/   isnansizer   
get_linconzerosallisfiniter   r   r   r   r   maxappendr   r   minr	   r   r   r   r   r   r   ).calcfcm_nlconr    AineqbineqAeqbeqxlxuf0	nlconstr0rhobegrhoendftargetr>   r?   maxfuniprintr@   rA   rB   rC   maxhistr=   callbacksolversrnamemineqmeqmxlmxummmnum_varsamatbvecr"   r!   r#   npt_x0r$   r%   r&   r'   r(   r)   s.                                                 r6   cobylarn   8   s   N FF "%..CJaEcll#c(C!(#bH9n
!C '#b8m
C
)ae
e
#g
-C1vH y!|5x}55|1}000}u~/ 	HhFG	H/ENN;;5("33Zx?Y5ZZ3s|ws|+ 	DhBC	D+CLL99h/RF8;Q1RR/BKKr7h&A6(.(AA&BKKr7h&A6(.(AA& 92;;W6(*V WW;2;;88B<<79#5#5 T(RST5 ywwy!W,,, 
~8b"b"$9bxi	~8b!b""bh CB;JD$ XXc]F 		**s2;;q>/B/BbM#,WT1-=-D#E&#-
 !*9!5sW}~aLVt<	6!*6.3=+A!B~g 		!V$%E v	R[[00VaZR&[.1v	!S#n^;fDnUVvV,>,IFt}}	1t<a<3q!t}}	
TAqA~ 7 	A'9H'DE 0 	a#	!: CI		)C?Aq&%UE5'4. 1feRugtTTr5   c           
         U b  U R                   S   nO2Ub  UR                   S   nOUb  [        U5      nOUb  [        U5      nOg[        (       aj  Ub  UR                   [        U5      U4:X  d   eU b  U R                   [        U5      U4:X  d   eUb'  Ub$  [        U5      [        U5      s=:X  a  U:X  d   e   eUb!  [        R                  " U[
        * :  5      S   OSnUb   [        R                  " U[
        :  5      S   OSn[        R                  " U5      n	[        R                  " Ub	  XSS24   * O[        R                  " SU45      Ub  XSS24   O[        R                  " SU45      U b  U * O[        R                  " SU45      U b  U O[        R                  " SU45      Ub  UO[        R                  " SU45      /5      n
[        R                  " Ub  XG   * O[        R                  " S5      Ub  XX   O[        R                  " S5      Ub  U* O[        R                  " S5      Ub  UO[        R                  " S5      Ub  UO[        R                  " S5      /5      nU
R                   S   S:  a  U
OSn
UR                   S   S:  a  UOSn[        (       a#  U
c  Ub  U
R                   [        U5      U4:X  d   eX4$ )z
This subroutine wraps the linear and bound constraints into a single constraint:
    AMAT*X <= BVEC.

N.B.:

LINCOA normalizes the linear constraints so that each constraint has a gradient
of norm 1. However, COBYLA does not do this.
Nr   )NNr   )
rG   rE   r   r/   wherer   eyevstackemptyhstack)rU   rS   rV   rT   rW   rX   ri   ixlixuidmatrj   rk   s               r6   rJ   rJ     sl    99Q<		;;q>	r7	r7 y}E
H/E EEE{ciiCHh+????
bjSWB-K8-KKK-KKK *,"((2	>
"1
%TC(*"((2=
!!
$DC FF8E99/AvrxxH/F1fbhh8}.ERXXq(m%<BHHa]$;"!X(? D 99O!?RXXa[BHHQK" D ::a=1$4$D::a=1$4$D y$**TH@U2UUU:r5   )NNNNNN)$__doc__common.evaluater   r   r   r   common.constsr   r   r	   r
   r   r   r   r   r   r   r   r   r   r   common.preprocr   common.presentr   common.linalgr   r   numpyr/   dataclassesr   r   r   rn   rJ   r*   r5   r6   <module>r      s   4 H G3 3 3 3
 % $ #   !  
 
 
 &*4TtT$"~Dt tlU^Hr5   