
    4[gؗ                         d dl Z d dlZd dlmZ ddlmZmZ ddlm	Z	m
Z
 ddlmZmZmZmZmZ ddlmZ ddlmZ  ej*                  e      j.                  Z ej*                  e      j2                  Z G d	 d
      Zy)    N)
lsq_linear   )Models	Quadratic)Options	Constants)cauchy_geometryspider_geometrynormal_byrd_omojokuntangential_byrd_omojokun$constrained_tangential_byrd_omojokun)qr_tangential_byrd_omojokun)get_arrays_tolc                      e Zd ZdZd Zed        Zed        Zed        Zed        Z	ed        Z
ed        Zej                  d	        Zed
        Zej                  d        Zed        Zed        Zed        Zed        Zed        Zed        Zed        Zd Zd Zd Zd Zd Zd Zd Zd Zd+dZd Zd Zd Z d  Z!d! Z"d" Z#d# Z$d$ Z%d,d%Z&d& Z'd' Z(d( Z)d) Z*d* Z+y)-TrustRegionz!
    Trust-region framework.
    c                 :   || _         t        | j                   |      | _        || _        d| _        d| _        | j                          t        j                  | j                        | _
        t        j                  | j                        | _        t        j                  | j                        | _        t        j                  | j                        | _        | j#                  | j$                         |t&        j(                     | _        | j,                  | _        y)a	  
        Initialize the trust-region framework.

        Parameters
        ----------
        pb : `cobyqa.problem.Problem`
            Problem to solve.
        options : dict
            Options of the solver.
        constants : dict
            Constants of the solver.

        Raises
        ------
        `cobyqa.utils.MaxEvalError`
            If the maximum number of evaluations is reached.
        `cobyqa.utils.TargetSuccess`
            If a nearly feasible point has been found with an objective
            function value below the target.
        `cobyqa.utils.FeasibleSuccess`
            If a feasible point has been found for a feasibility problem.
        `numpy.linalg.LinAlgError`
            If the initial interpolation system is ill-defined.
                r   N)_pbr   _models
_constants_penalty_best_indexset_best_indexnpzerosm_linear_ub_lm_linear_ubm_linear_eq_lm_linear_eqm_nonlinear_ub_lm_nonlinear_ubm_nonlinear_eq_lm_nonlinear_eqset_multipliersx_bestr   RHOBEG_resolution
resolution_radius)selfpboptions	constantss       V/var/www/html/bid-api/venv/lib/python3.12/site-packages/scipy/_lib/cobyqa/framework.py__init__zTrustRegion.__init__   s    4 dhh0#    XXd&6&67XXd&6&67 ")<)< = ")<)< =T[[) #7>>2    c                 .    | j                   j                  S )zt
        Number of variables.

        Returns
        -------
        int
            Number of variables.
        )r   nr*   s    r.   r2   zTrustRegion.nL   s     xxzzr0   c                 .    | j                   j                  S )z
        Number of linear inequality constraints.

        Returns
        -------
        int
            Number of linear inequality constraints.
        )r   r   r3   s    r.   r   zTrustRegion.m_linear_ubX        xx###r0   c                 .    | j                   j                  S )z
        Number of linear equality constraints.

        Returns
        -------
        int
            Number of linear equality constraints.
        )r   r   r3   s    r.   r   zTrustRegion.m_linear_eqd   r5   r0   c                 .    | j                   j                  S )z
        Number of nonlinear inequality constraints.

        Returns
        -------
        int
            Number of nonlinear inequality constraints.
        )r   r    r3   s    r.   r    zTrustRegion.m_nonlinear_ubp        xx&&&r0   c                 .    | j                   j                  S )z
        Number of nonlinear equality constraints.

        Returns
        -------
        int
            Number of nonlinear equality constraints.
        )r   r"   r3   s    r.   r"   zTrustRegion.m_nonlinear_eq|   r8   r0   c                     | j                   S )zv
        Trust-region radius.

        Returns
        -------
        float
            Trust-region radius.
        )r)   r3   s    r.   radiuszTrustRegion.radius        ||r0   c                     || _         | j                  | j                  t        j                     | j
                  z  k  r| j
                  | _         yy)z
        Set the trust-region radius.

        Parameters
        ----------
        radius : float
            New trust-region radius.
        N)r)   r;   r   r   DECREASE_RADIUS_THRESHOLDr(   )r*   r;   s     r.   r;   zTrustRegion.radius   sG     KKyBBCoo  ??DL	r0   c                     | j                   S )z
        Resolution of the trust-region framework.

        The resolution is a lower bound on the trust-region radius.

        Returns
        -------
        float
            Resolution of the trust-region framework.
        r'   r3   s    r.   r(   zTrustRegion.resolution   s     r0   c                     || _         y)z
        Set the resolution of the trust-region framework.

        Parameters
        ----------
        resolution : float
            New resolution of the trust-region framework.
        Nr@   )r*   r(   s     r.   r(   zTrustRegion.resolution   s     &r0   c                     | j                   S )zr
        Penalty parameter.

        Returns
        -------
        float
            Penalty parameter.
        )r   r3   s    r.   penaltyzTrustRegion.penalty   s     }}r0   c                     | j                   S )z
        Models of the objective function and constraints.

        Returns
        -------
        `cobyqa.models.Models`
            Models of the objective function and constraints.
        )r   r3   s    r.   modelszTrustRegion.models   r<   r0   c                     | j                   S )z
        Index of the best interpolation point.

        Returns
        -------
        int
            Index of the best interpolation point.
        )r   r3   s    r.   
best_indexzTrustRegion.best_index   s     r0   c                 `    | j                   j                  j                  | j                        S )z
        Best interpolation point.

        Its value is interpreted as relative to the origin, not the base point.

        Returns
        -------
        `numpy.ndarray`
            Best interpolation point.
        )rE   interpolationpointrG   r3   s    r.   r%   zTrustRegion.x_best   s#     {{((..t??r0   c                 H    | j                   j                  | j                     S )z
        Value of the objective function at `x_best`.

        Returns
        -------
        float
            Value of the objective function at `x_best`.
        )rE   fun_valrG   r3   s    r.   fun_bestzTrustRegion.fun_best   s     {{""4??33r0   c                 P    | j                   j                  | j                  ddf   S )z
        Values of the nonlinear inequality constraints at `x_best`.

        Returns
        -------
        `numpy.ndarray`, shape (m_nonlinear_ub,)
            Values of the nonlinear inequality constraints at `x_best`.
        N)rE   cub_valrG   r3   s    r.   cub_bestzTrustRegion.cub_best   "     {{""4??A#566r0   c                 P    | j                   j                  | j                  ddf   S )z
        Values of the nonlinear equality constraints at `x_best`.

        Returns
        -------
        `numpy.ndarray`, shape (m_nonlinear_eq,)
            Values of the nonlinear equality constraints at `x_best`.
        N)rE   ceq_valrG   r3   s    r.   ceq_bestzTrustRegion.ceq_best
  rQ   r0   c                 $   | j                   j                  |      | j                  | j                  j                  j
                  |z  | j                  j                  j                  z
  z  z   | j                  | j                  j                  j                  |z  | j                  j                  j                  z
  z  z   | j                  | j                   j                  |      z  z   | j                  | j                   j                  |      z  z   S )a/  
        Evaluate the Lagrangian model at a given point.

        Parameters
        ----------
        x : `numpy.ndarray`, shape (n,)
            Point at which the Lagrangian model is evaluated.

        Returns
        -------
        float
            Value of the Lagrangian model at `x`.
        )rE   funr   r   lineara_ubb_ubr   a_eqb_eqr!   cubr#   ceqr*   xs     r.   	lag_modelzTrustRegion.lag_model  s     KKOOA  xx##a'$((//*>*>>@@   xx##a'$((//*>*>>@@
 ##dkkooa&889 ##dkkooa&889	
r0   c                    | j                   j                  |      | j                  | j                  j                  j
                  z  z   | j                  | j                  j                  j                  z  z   | j                  | j                   j                  |      z  z   | j                  | j                   j                  |      z  z   S )ah  
        Evaluate the gradient of the Lagrangian model at a given point.

        Parameters
        ----------
        x : `numpy.ndarray`, shape (n,)
            Point at which the gradient of the Lagrangian model is evaluated.

        Returns
        -------
        `numpy.ndarray`, shape (n,)
            Gradient of the Lagrangian model at `x`.
        )rE   fun_gradr   r   rW   rX   r   rZ   r!   cub_gradr#   ceq_gradr^   s     r.   lag_model_gradzTrustRegion.lag_model_grad.  s     KK  #  488??#7#778  488??#7#778 ##dkk&:&:1&==> ##dkk&:&:1&==	>	
r0   c                    | j                   j                         }| j                  dkD  r*|| j                  | j                   j	                         z  z  }| j
                  dkD  r*|| j                  | j                   j                         z  z  }|S )z
        Evaluate the Hessian matrix of the Lagrangian model at a given point.

        Returns
        -------
        `numpy.ndarray`, shape (n, n)
            Hessian matrix of the Lagrangian model at `x`.
        r   )rE   fun_hessr    r!   cub_hessr"   r#   ceq_hess)r*   hesss     r.   lag_model_hesszTrustRegion.lag_model_hessD  s{     {{##%"D))DKK,@,@,BBBD"D))DKK,@,@,BBBDr0   c                     | j                   j                  |      | j                  | j                   j                  |      z  z   | j                  | j                   j                  |      z  z   S )a  
        Evaluate the right product of the Hessian matrix of the Lagrangian
        model with a given vector.

        Parameters
        ----------
        v : `numpy.ndarray`, shape (n,)
            Vector with which the Hessian matrix of the Lagrangian model is
            multiplied from the right.

        Returns
        -------
        `numpy.ndarray`, shape (n,)
            Right product of the Hessian matrix of the Lagrangian model with
            `v`.
        )rE   fun_hess_prodr!   cub_hess_prodr#   ceq_hess_prodr*   vs     r.   lag_model_hess_prodzTrustRegion.lag_model_hess_prodT  sa    $ KK%%a(##dkk&?&?&BBC##dkk&?&?&BBC	
r0   c                     | j                   j                  |      | j                  | j                   j                  |      z  z   | j                  | j                   j                  |      z  z   S )ar  
        Evaluate the curvature of the Lagrangian model along a given direction.

        Parameters
        ----------
        v : `numpy.ndarray`, shape (n,)
            Direction along which the curvature of the Lagrangian model is
            evaluated.

        Returns
        -------
        float
            Curvature of the Lagrangian model along `v`.
        )rE   fun_curvr!   cub_curvr#   ceq_curvrp   s     r.   lag_model_curvzTrustRegion.lag_model_curvk  s_      KK  ###dkk&:&:1&==>##dkk&:&:1&==>	
r0   c                 |    || j                   j                  | j                        d| j                  |      z  z   z  S )a}  
        Evaluate the objective function of the SQP subproblem.

        Parameters
        ----------
        step : `numpy.ndarray`, shape (n,)
            Step along which the objective function of the SQP subproblem is
            evaluated.

        Returns
        -------
        float
            Value of the objective function of the SQP subproblem along `step`.
        g      ?)rE   rb   r%   rr   r*   steps     r.   sqp_funzTrustRegion.sqp_fun  s?     KK  -D,,T223
 	
r0   c                     | j                   j                  | j                        | j                   j                  | j                        |z  z   S )a  
        Evaluate the linearization of the nonlinear inequality constraints.

        Parameters
        ----------
        step : `numpy.ndarray`, shape (n,)
            Step along which the linearization of the nonlinear inequality
            constraints is evaluated.

        Returns
        -------
        `numpy.ndarray`, shape (m_nonlinear_ub,)
            Value of the linearization of the nonlinear inequality constraints
            along `step`.
        )rE   r\   r%   rc   ry   s     r.   sqp_cubzTrustRegion.sqp_cub  =    " KKOODKK(kk""4;;/$67	
r0   c                     | j                   j                  | j                        | j                   j                  | j                        |z  z   S )a  
        Evaluate the linearization of the nonlinear equality constraints.

        Parameters
        ----------
        step : `numpy.ndarray`, shape (n,)
            Step along which the linearization of the nonlinear equality
            constraints is evaluated.

        Returns
        -------
        `numpy.ndarray`, shape (m_nonlinear_ub,)
            Value of the linearization of the nonlinear equality constraints
            along `step`.
        )rE   r]   r%   rd   ry   s     r.   sqp_ceqzTrustRegion.sqp_ceq  r~   r0   Nc                 "   |||| j                  |      \  }}}|}| j                  dkD  rb| j                   j                  |||      }t        j                  |      r/|| j                  t        j
                  j                  |      z  z  }|S )av  
        Evaluate the merit function at a given point.

        Parameters
        ----------
        x : `numpy.ndarray`, shape (n,)
            Point at which the merit function is evaluated.
        fun_val : float, optional
            Value of the objective function at `x`. If not provided, the
            objective function is evaluated at `x`.
        cub_val : `numpy.ndarray`, shape (m_nonlinear_ub,), optional
            Values of the nonlinear inequality constraints. If not provided,
            the nonlinear inequality constraints are evaluated at `x`.
        ceq_val : `numpy.ndarray`, shape (m_nonlinear_eq,), optional
            Values of the nonlinear equality constraints. If not provided,
            the nonlinear equality constraints are evaluated at `x`.

        Returns
        -------
        float
            Value of the merit function at `x`.
        r   )rO   rS   )r   r   	violationr   count_nonzerolinalgnorm)r*   r_   rL   rO   rS   m_valc_vals          r.   meritzTrustRegion.merit  s    . ?go(,%GWg==3HH&&q'7&KE&)>>>r0   c                    t        j                  | j                  j                  j                  g| j
                  j                  |      gg      }t        j                  | j                  j                  j                  | j                  j                  j                  |z  z
  | j
                  j                  |       g      }t        j                  | j                  j                  j                  g| j
                  j                  |      gg      }t        j                  | j                  j                  j                  | j                  j                  j                  |z  z
  | j
                  j                  |       g      }||||fS )a;  
        Get the linearizations of the constraints at a given point.

        Parameters
        ----------
        x : `numpy.ndarray`, shape (n,)
            Point at which the linearizations of the constraints are evaluated.

        Returns
        -------
        `numpy.ndarray`, shape (m_linear_ub + m_nonlinear_ub, n)
            Left-hand side matrix of the linearized inequality constraints.
        `numpy.ndarray`, shape (m_linear_ub + m_nonlinear_ub,)
            Right-hand side vector of the linearized inequality constraints.
        `numpy.ndarray`, shape (m_linear_eq + m_nonlinear_eq, n)
            Left-hand side matrix of the linearized equality constraints.
        `numpy.ndarray`, shape (m_linear_eq + m_nonlinear_eq,)
            Right-hand side vector of the linearized equality constraints.
        )r   blockr   rW   rX   rE   rc   rY   r\   rZ   rd   r[   r]   )r*   r_   aubbubaeqbeqs         r.   get_constraint_linearizationsz)TrustRegion.get_constraint_linearizations  s1   ( hh%%&%%a()
 hh$$txx';';a'??##
 hh%%&%%a()
 hh$$txx';';a'??##
 Cc!!r0   c                    | j                  | j                        \  }}}}| j                  j                  j                  | j                  z
  }| j                  j                  j
                  | j                  z
  }| j                  t        j                     | j                  z  }t        ||||||||t        j                     fi | j                  }	|t        j                     rt        ||      }
t        j                  |	|
z   |k        st        j                  ||	|
z
  k        rt!        j"                  dt$        d       t        j&                  j)                  |	      d|z  kD  rt!        j"                  dt$        d       t        j*                  | j                  dz  |	|	z  z
        }||	z  }||	z  }t        j,                  |||	z  z
  d      }| j.                  j1                  | j                        | j3                  |	      z   }| j                  j4                  dv r7t7        || j2                  ||||t        j                     fi | j                  }n+t9        || j2                  |||||||d   f	i | j                  }|t        j                     rt        ||      }
t        j                  ||
z   |k        st        j                  |||
z
  k        rt!        j"                  d	t$        d       t        j&                  j)                  |	|z         dt        j*                  d      z  | j                  z  kD  rt!        j"                  d
t$        d       |	|fS )aK  
        Get the trust-region step.

        The trust-region step is computed by solving the derivative-free
        trust-region SQP subproblem using a Byrd-Omojokun composite-step
        approach. For more details, see Section 5.2.3 of [1]_.

        Parameters
        ----------
        options : dict
            Options of the solver.

        Returns
        -------
        `numpy.ndarray`, shape (n,)
            Normal step.
        `numpy.ndarray`, shape (n,)
            Tangential step.

        References
        ----------
        .. [1] T. M. Ragonneau. *Model-Based Derivative-Free Optimization
           Methods and Software*. PhD thesis, Department of Applied
           Mathematics, The Hong Kong Polytechnic University, Hong Kong, China,
           2022. URL: https://theses.lib.polyu.edu.hk/handle/200/12294.
        z6the normal step does not respect the bound constraint.   皙?z=the normal step does not respect the trust-region constraint.       @r   )unconstrainedzbound-constraineddebugz;The tangential step does not respect the bound constraints.z<The trial step does not respect the trust-region constraint.)r   r%   r   boundsxlxur   r   BYRD_OMOJOKUN_FACTORr;   r   r   DEBUGr   r   anywarningswarnRuntimeWarningr   r   sqrtmaximumrE   rb   rr   typer   r   )r*   r,   r   r   r   r   r   r   r;   normal_steptolg_besttangential_steps                r.   get_trust_region_stepz!TrustRegion.get_trust_region_step  s   8 "??LS#sXX__$++-XX__$++- !?!?@4;;N*GMM"

 oo

 7==! R(C{S(2-.vvb;#445L"
 yy~~k*S6\9""	 c)K+,EEF
k
kjjs[00#6%%dkk2T5M5M6
 
 88==BB6((& //O C((  //O 7==! R(Cvvo+b01RVV_s**6 #"	 		{_<=$t{{23 ""	 O++r0   c                     |t         j                     r| j                  k7  sJ d       t        j                  t        j
                  d j                  j                  |            }t         j                  j                  ||t         j                           j                   j                   j                  j                        } j                  j                  j                   j                  z
  } j                  j                  j                   j                  z
  }t!        d| fd|| j"                  |t         j                           } j                  j%                   j                  |z   |      } j                  j                  j&                   j                  j                  j&                  dd j                  t        j(                  f   z
  }	|	dd j                  dgf   |	ddd j                  gf<   t+        d| fd|	ddddf   || j"                  |t         j                           }
 j                  j%                   j                  |
z   |      }t-        |      t-        |      kD  r|
}|} j                  j.                  dv r j1                   j                        \  }}}}t3        ||      }t3        |      }|| k  }||k\  }||k\  }t5        |||||      \  }}|dd|df   |dd|df   j6                  |z  z  }t        j8                  j;                  |      }d|cxk  r j                  j<                  k  rn n|t>         j"                  z  kD  r j"                  |z  |z  }
jA                  |
 j                  j                        dk  r|
 }
t        jB                  ||
z
  |
|z
  g      }||
z  |z
  }||
z  |z
  }tE        d	 ||t        j,                  |      fD              }t        jD                  t        j,                  |
|          d
      }t        jD                  t        j,                  |
|          |
      }t        jD                  t        j,                  || ddf   |
z        |
      }tG        d|z  dt        j8                  j;                  |
      z        }||k  rZ j                  j%                   j                  |
z   |      }t-        |      dt-        |      z  k\  rt        jH                  |
||      }|t         j                     rt3        ||      }t        jJ                  ||z   |k        st        jJ                  |||z
  k        rtM        jN                  dtP        d       t        j8                  j;                  |      d j"                  z  kD  rtM        jN                  dtP        d       |S )a  
        Get the geometry-improving step.

        Three different geometry-improving steps are computed and the best one
        is returned. For more details, see Section 5.2.7 of [1]_.

        Parameters
        ----------
        k_new : int
            Index of the interpolation point to be modified.
        options : dict
            Options of the solver.

        Returns
        -------
        `numpy.ndarray`, shape (n,)
            Geometry-improving step.

        Raises
        ------
        `numpy.linalg.LinAlgError`
            If the computation of a determinant fails.

        References
        ----------
        .. [1] T. M. Ragonneau. *Model-Based Derivative-Free Optimization
           Methods and Software*. PhD thesis, Department of Applied
           Mathematics, The Hong Kong Polytechnic University, Hong Kong, China,
           2022. URL: https://theses.lib.polyu.edu.hk/handle/200/12294.
        z8The index `k_new` must be different from the best index.r   r   c                 P    j                  | j                  j                        S NcurvrE   rI   rq   lagr*   s    r.   <lambda>z/TrustRegion.get_geometry_step.<locals>.<lambda>      chhq$++";";<r0   Nr   c                 P    j                  | j                  j                        S r   r   r   s    r.   r   z/TrustRegion.get_geometry_step.<locals>.<lambda>  r   r0   )zlinearly constrainedznonlinearly constrainedc              3   J   K   | ]  }t        j                  |d         yw)r   initialN)r   max).0arrays     r.   	<genexpr>z0TrustRegion.get_geometry_step.<locals>.<genexpr>  s&       !8 FF5#..!8s   !#r         $@g{Gz?g?z9The geometry step does not respect the bound constraints.r   r   z?The geometry step does not respect the trust-region constraint.))r   r   rG   r   squeezeeyerE   nptr   rI   gradr%   r   r   r   r   r	   r;   determinantsxptnewaxisr
   absr   r   r   r   Tr   r   r2   TINYr   r   r   minclipr   r   r   r   )r*   k_newr,   	coord_vecg_lagr   r   rz   sigmar   step_alt	sigma_altr   r   r   r   tol_bdtol_ubfree_xlfree_xufree_ubn_actq
g_lag_projnorm_g_lag_projcbdr\   r]   	maxcv_valr   r   s   `                             @r.   get_geometry_stepzTrustRegion.get_geometry_step  s   > 7==!(JIJ( JJrvva%@A	KK%%GMM"

 dkk&?&?@ XX__$++-XX__$++-<KKGMM"
 ((t);UC KK%%))kk''++At

,JKL 	 (+1t.B+B'CA4??##$"<12JKKGMM"	
 KK,,T[[8-CUK	y>CJ&DE 88== 
 

 224;;? Cc3#B+F#C(FVGmGFlGVmG 3HE1 1ef91ef9%)?@J iinnZ8O5%488::%/D4;;<N*N KK/9ZG88Hdkk&?&?@3F (yH hhXx"}=>Hns*Hns*  "%sBFF3K!8  	 ffRVVHgX$67EffRVVHgX$67EffRVVC!$4x$?@#N$*dRYY^^H-E&EF# $ 8 8h.!I 9~s5z)99!wwxR87==! R(CvvdSj2o&"&&dSj*A#"	 yy~~d#cDKK&77/"	 r0   c                    | j                  | j                        \  }}}}| j                  j                  j                  | j                  z
  }| j                  j                  j
                  | j                  z
  }t        j                  j                  |      }	t        |||||||	|t        j                     fi | j                  }
|t        j                     rt        ||      }t        j                  |
|z   |k        st        j                  ||
|z
  k        rt        j                   dt"        d       t        j                  j                  |
      d|	z  kD  rt        j                   dt"        d       |
S )aQ  
        Get the second-order correction step.

        Parameters
        ----------
        step : `numpy.ndarray`, shape (n,)
            Trust-region step.
        options : dict
            Options of the solver.

        Returns
        -------
        `numpy.ndarray`, shape (n,)
            Second-order correction step.
        zHThe second-order correction step does not respect the bound constraints.r   r   zNThe second-order correction step does not respect the trust-region constraint.)r   r%   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r*   rz   r,   r   r   r   r   r   r   r;   soc_stepr   s               r.    get_second_order_correction_stepz,TrustRegion.get_second_order_correction_step  s7   " "??LS#sXX__$++-XX__$++-%'GMM"

 oo

 7==! R(Cvvhnr)*bffR(S.5H.I5"	 yy~~h'#,6;"	 r0   c                    | j                  | j                  | j                  | j                  | j                        }| j                  | j                  |z   |||      }| j                  | j                  d| j
                  j                  | j                        | j
                  j                  | j                              }| j                  | j                  |z   | j                  |      | j                  |      | j                  |            }t        ||z
        t        t        ||z
        z  kD  r||z
  t        ||z
        z  S y)a:  
        Get the reduction ratio.

        Parameters
        ----------
        step : `numpy.ndarray`, shape (n,)
            Trust-region step.
        fun_val : float
            Objective function value at the trial point.
        cub_val : `numpy.ndarray`, shape (m_nonlinear_ub,)
            Nonlinear inequality constraint values at the trial point.
        ceq_val : `numpy.ndarray`, shape (m_nonlinear_eq,)
            Nonlinear equality constraint values at the trial point.

        Returns
        -------
        float
            Reduction ratio.
        r         )r   r%   rM   rP   rT   rE   r\   r]   r{   r}   r   r   r   )	r*   rz   rL   rO   rS   	merit_old	merit_newmerit_model_oldmerit_model_news	            r.   get_reduction_ratiozTrustRegion.get_reduction_ratioI  s   ( JJKKMMMMMM	
	 JJt{{T17GWM	**KKKKOODKK(KKOODKK(	
 **KK$LLLLLL	
 01D3	!<
 5
 
 	)S/1.   r0   c                    | j                  | j                        \  }}}}t        t        j                  j                  t        j                  t        j                  d|       |g            t        j                  j                  t        j                  t        j                  d||z  |z
        ||z  |z
  g            z
  d      }| j                  |      }t        j                  j                  t        j                  | j                  | j                  | j                  | j                  g            }t        |      t        t        |      z  kD  rt        |||z        }| j                  }	| j                   | j"                  t$        j&                     |z  k  r?t        | j"                  t$        j(                     |z  d      | _        | j+                          |	| j                  k(  S )z
        Increase the penalty parameter.

        Parameters
        ----------
        step : `numpy.ndarray`, shape (n,)
            Trust-region step.
        r         ?)r   r%   r   r   r   r   r   r   r{   r   r   r!   r#   r   r   rG   r   r   r   PENALTY_INCREASE_THRESHOLDPENALTY_INCREASE_FACTORr   )
r*   rz   r   r   r   r   	viol_diffsqp_val	thresholdbest_index_saves
             r.   increase_penaltyzTrustRegion.increase_penaltyy  s    "??LS#sIINN

3- iinn

3d
S(89d
S(  #
	& ,,t$IINNHH&&&&))))		
	 y>D3w<//Iw':;I//MMyCCD  	 A ABYNDM !$//11r0   c                 v    t        | j                  | j                               | _        | j                          y)z1
        Decrease the penalty parameter.
        N)r   r   _get_low_penaltyr   r3   s    r.   decrease_penaltyzTrustRegion.decrease_penalty  s+     DMM4+@+@+BCr0   c           
         | j                   }| j                  | j                  | j                  j                  |   | j                  j
                  |ddf   | j                  j                  |ddf         }| j                  j                  | j                  | j                  j
                  |ddf   | j                  j                  |ddf         }dt        z  t        | j                  j                  | j                  j                        z  t        t        |      d      z  }t        | j                  j                        D ]  }|| j                   k7  s| j                  j                  j!                  |      }| j                  || j                  j                  |   | j                  j
                  |ddf   | j                  j                  |ddf         }| j                  j                  || j                  j
                  |ddf   | j                  j                  |ddf         }||k  s|||z   k  s||k  s|}|}|}
 || _        y)z2
        Set the index of the best point.
        Nr   r   )rG   r   r%   rE   rL   rO   rS   r   maxcvEPSr   r2   r   r   rangerI   rJ   r   )	r*   rG   m_bestr_bestr   kx_valr   r_vals	            r.   r   zTrustRegion.set_best_index  s    __
KKKK
+KK
A.KK
A.	
 KKKK
A.KK
A.
 $++--12 #f+s#$ 	 t{{'ADOO#1177:

KK''*KK''1-KK''1-	 KK''1-KK''1-
 6>efsl&:uv~!"J"F"F# ($ &r0   c                    t        j                  | j                  j                  j                  | j                  j                  j                  dd| j
                  t         j                  f   z
  dz  d      }|d}|}n| j                  j                  |      }t        j                  d|t        | j                  t        j                     | j                  z  | j                        dz  z        dz  }d|| j
                  <   t        j                  |t        j                   |      z        }|t        j"                  ||         fS )a  
        Get the index of the interpolation point to remove.

        If `x_new` is not provided, the index returned should be used during
        the geometry-improvement phase. Otherwise, the index returned is the
        best index for included `x_new` in the interpolation set.

        Parameters
        ----------
        x_new : `numpy.ndarray`, shape (n,), optional
            New point to be included in the interpolation set.

        Returns
        -------
        int
            Index of the interpolation point to remove.
        float
            Distance between `x_best` and the removed point.

        Raises
        ------
        `numpy.linalg.LinAlgError`
            If the computation of a determinant fails.
        Nr   r   axisr   g      @r   )r   sumrE   rI   r   rG   r   r   r   r   r   r   LOW_RADIUS_FACTORr;   r(   argmaxr   r   )r*   x_newdist_sqr   weightsk_maxs         r.   get_index_to_removezTrustRegion.get_index_to_remove  s&   2 &&))--++++//4??BJJ0NOP 	
 
 =EGKK,,U3E

	(C(CD++&
 	 
  (,GDOO$		'BFF5M12bgggen---r0   c                    t         j                  j                  |      }|| j                  t        j
                     k  r1| xj                  | j                  t        j                     z  c_        y|| j                  t        j                     k  r:t        | j                  t        j                     | j                  z  |      | _        yt        | j                  t        j                     | j                  z  t        | j                  t        j                     | j                  z  | j                  t        j                     |z              | _        y)z
        Update the trust-region radius.

        Parameters
        ----------
        step : `numpy.ndarray`, shape (n,)
            Trust-region step.
        ratio : float
            Reduction ratio.
        N)r   r   r   r   r   	LOW_RATIOr;   DECREASE_RADIUS_FACTOR
HIGH_RATIOr   r   INCREASE_RADIUS_FACTORINCREASE_RADIUS_THRESHOLD)r*   rz   ratios_norms       r.   update_radiuszTrustRegion.update_radius  s     %DOOI$7$788KK4??9+K+KLLKdooi&:&:;;	 @ @A++DK 	 @ @A++OOI$D$DEkk"OOI$G$GH	DKr0   c                    | j                   t        j                     |t        j                     z  | j
                  k  r1| xj
                  | j                   t        j                     z  c_        n| j                   t        j                     |t        j                     z  | j
                  k  r9t        j                  | j
                  |t        j                     z        | _        n|t        j                     | _        t        | j                   t        j                     | j                  z  | j
                        | _        y)z
        Enhance the resolution of the trust-region framework.

        Parameters
        ----------
        options : dict
            Options of the solver.
        N)r   r   LARGE_RESOLUTION_THRESHOLDr   RHOENDr(   DECREASE_RESOLUTION_FACTORMODERATE_RESOLUTION_THRESHOLDr   r   r   r  r)   r*   r,   s     r.   enhance_resolutionzTrustRegion.enhance_resolution9  s     OOI@@Agnn%&oo OOt44  O OOICCDgnn%&oo !ggdoo(/(?'@ ADO &gnn5DO OOI<<=LOO
r0   c                 v    | j                   j                  t        j                  | j                        |       y)z
        Shift the base point to `x_best`.

        Parameters
        ----------
        options : dict
            Options of the solver.
        N)rE   shift_x_baser   copyr%   r  s     r.   r  zTrustRegion.shift_x_baseZ  s%     	  !5w?r0   c           	      x   | j                   j                  j                  |z  | j                   j                  j                  k\  }| j                  dk\  }| j                   j
                  j                  |k\  }| j                   j
                  j                  |k  }t        j                  |      }t        j                  |      }t        j                  |      }t        j                  |      }	||z   | j                  z   | j                  z   dkD  r*t        j                  | j                   j                        }
t        j                  |
|ddf    |
|ddf   | j                   j                  j                  |ddf   | j                  j!                  ||      | j                   j                  j"                  | j                  j%                  |      f   }| j                  j'                  |      }t        j(                  |j*                  d   t        j,                         }d|d||	z   |z   |z    t/        |j0                  | |t        j,                  fd      }|j2                  ||	z   ||	z   |z    | j4                  |<   d| j4                  | <   |j2                  ||	z   |z   ||	z   |z   |z    | j6                  |<   d| j6                  | <   |j2                  ||	z   |z   |z   ||	z   |z   |z   | j                  z    | j8                  dd |j2                  ||	z   |z   |z   | j                  z   d | j:                  dd yy)aI  
        Set the Lagrange multipliers.

        This method computes and set the Lagrange multipliers of the linear and
        nonlinear constraints to be the QP multipliers.

        Parameters
        ----------
        x : `numpy.ndarray`, shape (n,)
            Point at which the Lagrange multipliers are computed.
        r   r   Nbvls)r   method)r   rW   rX   rY   rP   r   r   r   r   r   r   r"   r   r2   r_rE   rc   rZ   rd   rb   fullshapeinfr   r   r_   r   r!   r   r#   )r*   r_   incl_linear_ubincl_nonlinear_ubincl_xlincl_xur   r    m_xlm_xuidentityc_jacr   xl_lmress                  r.   r$   zTrustRegion.set_multiplierse  s,    --1TXX__5I5II MMS0((//$$)((//$$)&&~6))*;<(( .(4+;+;;%%&()* vvdhhjj)HEE'1*%%!$$$^Q%67$$Q(9:$$$$Q')E [[))!,FGGEKKNRVVG4EBEE>D4K+->?rvv	C 25tD4K+52D~. 36D/7:uu" !!8D!!"34 9<D!!#4"45$'EE !! "&"" !"! ""	"#		%Dq! (+uutk)N:T=M=MMN(D!!!$a*r0   c                 >   t         j                  | j                  j                  j                  t         j
                  d d f   | j                  j                  j                  j                  z   | j                  j                  j                  j                  z  | j                  j                  j                  t         j
                  d d f   z
  | j                  j                  f   }| j                  j                  j                  t         j
                  d d f   | j                  j                  j                  j                  z   | j                  j                  j                  j                  z  | j                  j                  j                  t         j
                  d d f   z
  }t        j                  || | j                  j                   | j                  j                    g      }t        j                  ||g      }t        j"                  |d      }t        j$                  |d      }|| j&                  t(        j*                     |z  k  }t        j,                  |      rt        j"                  | j                  j.                        }t        j$                  | j                  j.                        }t        j0                  d||         }	t        j2                  ||   |	z
        }
|
t4        ||z
  z  kD  r
||z
  |
z  }|S t         j6                  }|S d}|S )Nr   r   r   )r   c_rE   rI   x_baser   r   r   r   rW   rX   rY   rO   rZ   r[   r   rS   nanminnanmaxr   r   THRESHOLD_RATIO_CONSTRAINTSr   rL   minimumr   r   r  )r*   r_val_ubr_val_eqr   c_minc_maxindicesf_minf_max	c_min_negc_diffrC   s               r.   r   zTrustRegion._get_low_penalty  si   55))00Q?++++//112 hhoo""$$	%
 hhoo""2::q=12 KK!
 KK%%,,RZZ];kk''++--.HHOO  ""# &*XX__%9%9"**a-%HI 88	##$$$	
 (H-.		%a(		%a(ooiCCDuLM 	 66'?IIdkk112EIIdkk112E

3g7IVVE'NY67F.. 5=F2
  &&  Gr0   )NNNr   ),__name__
__module____qualname____doc__r/   propertyr2   r   r   r    r"   r;   setterr(   rC   rE   rG   r%   rM   rP   rT   r`   re   rk   rr   rw   r{   r}   r   r   r   r   r   r   r   r   r   r   r   r	  r  r  r$   r    r0   r.   r   r      s   .'` 	 	 	$ 	$ 	$ 	$ 	' 	' 	' 	' 	 	 ]]+ +"     	& 	& 	 	 	 	 	  	  @ @ 	4 	4 	7 	7 	7 	7
0
, 
.
*
(
*
*@,"\r,hUn0d.`62p(&T5.n@
B	@IV(r0   r   )r   numpyr   scipy.optimizer   rE   r   r   settingsr   r   
subsolversr	   r
   r   r   r   subsolvers.optimr   utilsr   finfofloattinyr   epsr   r   r;  r0   r.   <module>rF     s^      % % (  : ! rxxbhhuoA Ar0   