
    ЏkhV                     
   d Z ddlmZmZmZmZ ddlmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZmZ ddlmZ ddlmZ ddlmZ ddlmZ d	d
lZd	dlmZ d	dl m Z  e G d d             Z!d
d
d
d
d
d
d
d
d
d
ee
ed
ed
d
eed
dd
fdZ"ddZ#y
)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                      e Zd ZU ej                  ed<   eed<   ej                  ed<   eed<   eed<   ej                  dz  ed<   ej                  dz  ed<   ej                  dz  ed	<   ej                  dz  ed
<   eed<   y)COBYLAResultxfconstrcstrvnfNxhistfhistchistconhistinfo)__name__
__module____qualname__npndarray__annotations__floatint     \/var/www/teggl/fontify/venv/lib/python3.12/site-packages/scipy/_lib/pyprima/cobyla/cobyla.pyr   r   *   se    	zzMHJJLG::::::ZZ$
Ir2   r   i  c                 
   d}d}t        |      rt        |      nd}t        |      rt        |      nd}t        |      rt        |t         kD        nd}t        |      rt        |t        k        nd}||z   d|z  z   |z   |z   }t        |      } t        rG|dk\  s
J | d       | dk\  s
J | d       t        |      t        |      k(  s
J | d       t        |      r|j
                  || fk(  s
J | d       t        |      t        |      k(  s
J | d	       t        |      r|j
                  || fk(  s
J | d
       t        |      rt        |      | k(  s
J | d       t        |      rt        |      | k(  s
J | d       t        |
      rt        |	      s
J | d       t        |	      r*t        j                  |	      st        |
      s
J | d       t        |
      rt        j                  |
      |k(  sJ |:t        |      }t         |t        j                  |      <   t         ||t         k  <   |7t        |      }t        |t        j                  |      <   t        ||t        kD  <   t        ||||||      \  }!}"t        j                  |      }#t        |	      rft        |
      r[t        t        j                  |            r=t        |	      }$|!t        t!        |!|      |"z
        |#d||z
   t        |
      |#||z
  d n4t#        |      }t%        | |||!|"      \  }$}#t        |#d||z
         |#d||z
   t'        t        j(                  d|#            }%t        |      r|}n?t        |      r.t        j                  |      r|dkD  rt'        d|z  t*              }nt*        }t        |      r|}n8|dkD  r-t'        t,        t/        t0        t*        z  |z  t0                    }nt0        }t        |      r|nt2        | z  }t        |      r|}n3t        |      r"d|cxk  rdk  rn nt'        t,        |dz        }nt4        }t        |      r|}nd|cxk  rdk  rn n	|dz   dz  }nt6        }t        |      r|nt'        || dz   t2        | z        }t9        || |||||||||||||||dkD        \  }}}}}}}&}}}}}}}}'t;        | ||||!|"||||||||||#|$|||      \
  }}$}#}%}(})}*}+},}-t=        ||$|#|%|(|)|*|+|,|-
      S )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=   r>   r?   r@   maxhistr:   callbacksolversrnamemineqmeqmxlmxummmnum_varsamatbvecr!   r    r"   npt_x0r#   r$   r%   r&   r'   r(   s.                                                 r3   cobylark   8   sf   N FF "%.CJaEcl#c(C!(#bH9n
!C '#b8m
C
)ae
e
#g
-C1vH !|5x}55|1}000}u~/ 	HhFG	H/EN;;5("33Zx?Y5ZZ3s|ws|+ 	DhBC	D+CL99h/RF8;Q1RR/BKr7h&A6(.(AA&BKr7h&A6(.(AA& 92;W6(*V WW;2;88B<79#5 T(RST5 ywwy!W,,, 
~8b"b"$9bxi	~8b!b""bh CUB;JD$ XXc]F 		*s2;;q>/BbM#,WT1-=-D#E&#-
 !*9!5sW}~aLVQt<	6!*6.3=+A!B~g 		!V$%E v	R[[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Tr2   c           
      0   | | j                   d   }n/||j                   d   }n|t        |      }n|t        |      }nyt        rb||j                   t        |      |fk(  sJ | | j                   t        |      |fk(  sJ |$|"t        |      t        |      cxk(  r|k(  sJ  J | t        j                  |t
         kD        d   nd}|t        j                  |t
        k        d   nd}t        j                  |      }	t        j                  |
|	|ddf    nt        j                  d|f      |	|	|ddf   nt        j                  d|f      | |  nt        j                  d|f      | | nt        j                  d|f      ||nt        j                  d|f      g      }
t        j                  |||    nt        j                  d      |||   nt        j                  d      || nt        j                  d      ||nt        j                  d      ||nt        j                  d      g      }|
j                   d   dkD  r|
nd}
|j                   d   dkD  r|nd}t        r |
||
j                   t        |      |fk(  sJ |
|fS )a  
    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   )
rD   rB   r   r,   wherer   eyevstackemptyhstack)rR   rP   rS   rQ   rT   rU   rf   ixlixuidmatrg   rh   s               r3   rG   rG     sr    99Q<		;;q>	r7	r7 }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/sAvrxxH/Fc1fbhh8}.ERXXq(m%<BHHa]$;"!X(? D 99OC!?3RXXa[BHHQK" D ::a=1$4$D::a=1$4$D $**TH@U2UUU:r2   )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   rk   rG   r1   r2   r3   <module>r}      s   4 H G3 3 3 3
 % $ #   !  
 
 
 &*4TtT$"~Dt tlU^Hr2   