
    ^Mh                         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          j        Z G d	 d
          ZdS )    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+dS )-TrustRegionz!
    Trust-region framework.
    c                    d| _         || _        t          | j        || j                  | _        || _        d| _        |                                  t          j	        | j
                  | _        t          j	        | j                  | _        t          j	        | j                  | _        t          j	        | j                  | _        |                     | j                   |t(          j                 | _        | j        | _        dS )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)_penalty_pbr   penalty_models
_constants_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       [/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/scipy/_lib/cobyqa/framework.py__init__zTrustRegion.__init__   s    4  dh>>#   Xd&677Xd&677 ")< = = ")< = =T[))) #7>2    c                     | j         j        S )zt
        Number of variables.

        Returns
        -------
        int
            Number of variables.
        )r   nr+   s    r/   r3   zTrustRegion.nL   s     xzr1   c                     | j         j        S )z
        Number of linear inequality constraints.

        Returns
        -------
        int
            Number of linear inequality constraints.
        )r   r   r4   s    r/   r   zTrustRegion.m_linear_ubX        x##r1   c                     | j         j        S )z
        Number of linear equality constraints.

        Returns
        -------
        int
            Number of linear equality constraints.
        )r   r   r4   s    r/   r   zTrustRegion.m_linear_eqd   r6   r1   c                     | j         j        S )z
        Number of nonlinear inequality constraints.

        Returns
        -------
        int
            Number of nonlinear inequality constraints.
        )r   r!   r4   s    r/   r!   zTrustRegion.m_nonlinear_ubp        x&&r1   c                     | j         j        S )z
        Number of nonlinear equality constraints.

        Returns
        -------
        int
            Number of nonlinear equality constraints.
        )r   r#   r4   s    r/   r#   zTrustRegion.m_nonlinear_eq|   r9   r1   c                     | j         S )zv
        Trust-region radius.

        Returns
        -------
        float
            Trust-region radius.
        )r*   r4   s    r/   radiuszTrustRegion.radius        |r1   c                     || _         | j        | j        t          j                 | j        z  k    r| j        | _         dS dS )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   sJ     KyBCo   ?DLLL	 r1   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(   r4   s    r/   r)   zTrustRegion.resolution   s     r1   c                     || _         dS )z
        Set the resolution of the trust-region framework.

        Parameters
        ----------
        resolution : float
            New resolution of the trust-region framework.
        NrA   )r+   r)   s     r/   r)   zTrustRegion.resolution   s     &r1   c                     | j         S )zr
        Penalty parameter.

        Returns
        -------
        float
            Penalty parameter.
        )r   r4   s    r/   r   zTrustRegion.penalty   s     }r1   c                     | j         S )z
        Models of the objective function and constraints.

        Returns
        -------
        `cobyqa.models.Models`
            Models of the objective function and constraints.
        )r   r4   s    r/   modelszTrustRegion.models   r=   r1   c                     | j         S )z
        Index of the best interpolation point.

        Returns
        -------
        int
            Index of the best interpolation point.
        )r   r4   s    r/   
best_indexzTrustRegion.best_index   s     r1   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   r4   s    r/   r&   zTrustRegion.x_best   s     {(..t???r1   c                 0    | 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   r4   s    r/   fun_bestzTrustRegion.fun_best   s     {"4?33r1   c                 8    | 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   r4   s    r/   cub_bestzTrustRegion.cub_best        {"4?AAA#566r1   c                 8    | 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   r4   s    r/   ceq_bestzTrustRegion.ceq_best
  rQ   r1   c                 ~   | 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                             |          z  z   | 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     KOOA x#a'$(/*>>@@  x#a'$(/*>>@@
 #dkooa&8&889 #dkooa&8&889	
r1   c                 *   | j                             |          | j        | j        j        j        z  z   | j        | j        j        j        z  z   | j        | j         	                    |          z  z   | 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     K  ## 48?#778 48?#778 #dk&:&:1&=&==> #dk&:&:1&=&==	>	
r1   c                     | j                                         }| j        dk    r$|| j        | j                                         z  z  }| j        dk    r$|| 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  sx     {##%%""D)DK,@,@,B,BBBD""D)DK,@,@,B,BBBDr1   c                     | j                             |          | j        | j                             |          z  z   | 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  s^    $ K%%a((#dk&?&?&B&BBC#dk&?&?&B&BBC	
r1   c                     | j                             |          | j        | j                             |          z  z   | 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\      K  ###dk&:&:1&=&==>#dk&:&:1&=&==>	
r1   c                 x    || j                             | j                  d|                     |          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  sA     K  --D,,T2223
 	
r1   c                     | 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  ;    " KOODK((k""4;//$67	
r1   c                     | 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~   r1   Nc                     ||||                      || j                  \  }}}|}| j        dk    r[| j                             |||          }t	          j        |          r*|| j        t          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`.
        Nr   )rO   rS   )r   r   r   	violationr   count_nonzerolinalgnorm)r+   r_   rL   rO   rS   m_valc_vals          r/   meritzTrustRegion.merit  s    . ?go(,DL(A(A%GWg=3H&&q'7&KKE&& ?)>)>>>r1   c                 V   t          j        | j        j        j        g| j                            |          gg          }t          j        | j        j        j        | j        j        j        |z  z
  | j                            |           g          }t          j        | j        j        j	        g| j        
                    |          gg          }t          j        | j        j        j        | j        j        j	        |z  z
  | 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  s   ( h%&%%a(()
 
 h$tx';a'??###
 
 h%&%%a(()
 
 h$tx';a'??###
 
 Cc!!r1   c                    |                      | 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                            |	          d|z  k    rt!          j        dt$          d           t          j        | j	        dz  |	|	z  z
            }||	z  }||	z  }t          j        |||	z  z
  d          }| j                            | j                  |                     |	          z   }| j        j        dv r-t7          || j        ||||t          j                 fi | j        }n%t9          || j        |||||||d         f	i | j        }|t          j                 rt          ||          }
t          j        ||
z   |k               st          j        |||
z
  k               rt!          j        d	t$          d           t          j                            |	|z             dt          j        d          z  | j	        z  k    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  sN   8 "??LLS#sX_$+-X_$+- !?@4;N*GM"

 

 o

 

 7=! 	 R((C{S(2-.. vb;#4455L"  
 y~~k**S6\99""	   c)K+,EEFF
k
kjs[00#66%%dk22T5M5M6
 6
 
 8=BBB6(&  / OO C(   / O 7=! 	 R((Cvo+b011 RV_s**6 6  #"	   	{_<==$t{23 3 ""	   O++r1   c                 
    |t           j                 r| j        k    s
J d            t          j        t          j        d j        j        |                    }t           j        j	        ||t           j                           
                     j         j        j	                  } j        j        j         j        z
  } j        j        j         j        z
  }t!          d| fd|| j        |t           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        |
z   |          }t-          |          t-          |          k    r|
}|} j        j        dv r                      j                  \  }}}}t3          ||          }t3          |          }|| k    }||k    }||k    }t5          |||||          \  }}|dd|df         |dd|df         j        |z  z  }t          j                            |          }d|cxk     r j        j        k     rn n|t>           j        z  k    r j        |z  |z  }
                     |
 j        j	                  dk     r|
 }
t          j!        ||
z
  |
|z
  g          }||
z  |z
  }||
z  |z
  }tE          d	 ||t          j        |          fD                       }t          j"        t          j        |
|                    d
          }t          j"        t          j        |
|                    |
          }t          j"        t          j        || ddf         |
z            |
          }tG          d|z  dt          j                            |
          z            }||k    r\ j                             j        |
z   |          }t-          |          dt-          |          z  k    rt          j$        |
||          }|t           j                 rt3          ||          }t          j%        ||z   |k               st          j%        |||z
  k               rtM          j'        dtP          d           t          j                            |          d j        z  k    rtM          j'        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                 D                         | j        j                  S NcurvrE   rI   rq   lagr+   s    r/   <lambda>z/TrustRegion.get_geometry_step.<locals>.<lambda>      chhq$+";<< r1   Nr   c                 D                         | j        j                  S r   r   r   s    r/   r   z/TrustRegion.get_geometry_step.<locals>.<lambda>  r   r1   )zlinearly constrainedznonlinearly constrainedc              3   B   K   | ]}t          j        |d           V  dS )r   initialN)r   max).0arrays     r/   	<genexpr>z0TrustRegion.get_geometry_step.<locals>.<genexpr>  sE           F5#...           r1   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   r3   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 )(( Jrva%@@AA	K%GM"
 

 dk&?@@ X_$+-X_$+-<<<<<KGM"
 
 ((t);UCC K%)k'+AAAt
,JKL 	 (+111t.B+B'CAAA4?##$"<<<<<122JKGM"	
 	
 K,,T[8-CUKK	y>>CJJ&&DE 8= 
 
 

 224;?? Cc3#B++F#C((FVGmGFlGVmG 3 HE1 111eff9111eff9%)?@J innZ88O5%%%%48:%%%%%/D4;<N*N*N K/9ZG88Hdk&?@@3FF (yH hXx"}=>>Hns*Hns*    "%sBF3KK!8       	 fRVHgX$677EEEfRVHgX$677EEEfRVC!!!$4x$?@@#NNN$*dRY^^H-E-E&EFF## $ 8 8h.! !I 9~~s5zz)999!wxR887=! 	 R((CvdSj2o&& "&dSj*A*A #"	   y~~d##cDK&777/"	   r1   c                    |                      | j                  \  }}}}| j        j        j        | j        z
  }| j        j        j        | j        z
  }t          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                            |
          d|	z  k    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  sS   " "??LLS#sX_$+-X_$+-%%'GM"

 

 o

 

 7=! 	 R((Cvhnr)** bfR(S.5H.I.I 5"	   y~~h''#,66;"	   r1   c                    |                      | j        | j        | j        | j                  }|                      | j        |z   |||          }|                      | j        d| j                            | j                  | j                            | j                            }|                      | j        |z   |                     |          | 	                    |          | 
                    |                    }t          ||z
            t          t          ||z
            z  k    r||z
  t          ||z
            z  S dS )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KMMM	
 
	 JJt{T17GWMM	**KKOODK((KOODK((	
 
 **K$LLLLLL	
 
 011D3	!<
 <
 5
 
 
 	)S/1. .   4r1   c                    |                      | j                  \  }}}}t          t          j                            t          j        t          j        d|           |g                    t          j                            t          j        t          j        d||z  |z
            ||z  |z
  g                    z
  d          }|                     |          }t          j                            t          j        | j	        | j
        | j        | j        g                    }t          |          t          t          |          z  k    rt          |||z            }| j        }	| j        | j        t$          j                 |z  k    rAt          | j        t$          j                 |z  d          | _        |                                  |	| 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LS#sINN
3--   inn
3d
S(899d
S(    #
 
	& ,,t$$INNH&&))	 	
 	
	 y>>D3w<<///Iw':;;I/MyCD   	 ABYN DM !!!$/11r1   c                     t          | j        |                                           | _        |                                  dS )z1
        Decrease the penalty parameter.
        N)r   r   _get_low_penaltyr   r4   s    r/   decrease_penaltyzTrustRegion.decrease_penalty  s;     DM4+@+@+B+BCCr1   c           
         | 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                   }dt          z  t          | j        j        | j        j                  z  t          t          |          d          z  }t          | j        j                  D ]}|| j         k    r| j        j                            |          }|                     || j        j        |         | j        j        |ddf         | j        j        |ddf                   }| j                            || j        j        |ddf         | j        j        |ddf                   }||k     s|||z   k     r||k     r|}|}|}|| _        dS )z2
        Set the index of the best point.
        Nr   r   )rG   r   r&   rE   rL   rO   rS   r   maxcvEPSr   r3   r   r   rangerI   rJ   r   )	r+   rG   m_bestr_bestr   kx_valr   r_vals	            r/   r   zTrustRegion.set_best_index  s    _
KK
+K
AAA.K
AAA.	
 
 KK
AAA.K
AAA.
 
 $+-112 #f++s##$ 	 t{'' 	# 	#ADO##177::

K'*K'111-K'111-	  K'111-K'111- 
 6>>efsl&:&:uv~~!"J"F"F%r1   c                 &   t          j        | j        j        j        | j        j        j        dd| j        t           j        f         z
  dz  d          }|d}|}nr| 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?BJ0NOP 	
 
 
 
 =EGGK,,U33E
	(CD+& 
 	 	 
  (,GDO$	'BF5MM122bggen----r1   c                 P   t           j                            |          }|| j        t          j                 k    r'| xj        | j        t          j                 z  c_        dS || j        t          j                 k    r4t          | j        t          j                 | j        z  |          | _        dS t          | j        t          j                 | j        z  t          | j        t          j                 | j        z  | j        t          j                 |z                      | _        dS )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     %%DOI$7888KK4?9+KLLKKKKdoi&:;;;	 @A+ DKKK 	 @A+OI$DEk"OI$GH 	 	DKKKr1   c                    | j         t          j                 |t          j                 z  | j        k     r&| xj        | j         t          j                 z  c_        n|| j         t          j                 |t          j                 z  | j        k     r2t          j	        | j        |t          j                 z            | _        n|t          j                 | _        t          | j         t          j                 | j        z  | j                  | _        dS )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     OI@Agn%&o  OOt4  OOO OICDgn%&o  !gdo(/(?'@ A ADOO &gn5DO OI<=LO
 
r1   c                 j    | j                             t          j        | j                  |           dS )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.     	  !5!5w?????r1   c           	      D   | 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k    rt          j        | j         j                  }
t          j        |
|ddf          |
|ddf         | j         j        j        |ddf         | j                            ||          | j         j        j        | j                            |          f         }| j                            |          }t          j        |j        d         t          j                   }d|d||	z   |z   |z   <   t/          |j        | |t          j        fd          }|j        ||	z   ||	z   |z            | j        |<   d| j        | <   |j        ||	z   |z   ||	z   |z   |z            | j        |<   d| j        | <   |j        ||	z   |z   |z   ||	z   |z   |z   | j
        z            | j        dd<   |j        ||	z   |z   |z   | j
        z   d         | j        dd<   dS dS )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   r3   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    -1TX_5II MS0(/$)(/$)&~66)*;<<(((( .(4+;;%&()* * vdhj))HE'111*%%!!!$$^QQQ%67$$Q(9::$$$Q'')E [))!,,FGEKNRVG44EBEE>D4K+->>?rv	  C 25tD4K+552D~. 36D/7:u" !!!8D!"34 9<D!#4"45$'E !! "&"" !"! "	"##	%Dqqq! (+utk)N:T=MMNN(D!!!!$$$a* *r1   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          j        d||                   }	t          j        ||         |	z
            }
|
t4          ||z
  z  k    r	||z
  |
z  }nt           j        }n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_diffr   s               r/   r   zTrustRegion._get_low_penalty  s   5)0QQQ?++/12 ho"$	%
 ho"2:qqq=12 K!
 K%,RZ];k'+-.HO "# &*X_%9"*aaa-%HI 8	#$$	
 
 (H-..	%a(((	%a(((oiCDuLM 	 6'?? 
	Idk122EIdk122E
3g77IVE'NY677F... 5=F2&Gr1   )NNNr   ),__name__
__module____qualname____doc__r0   propertyr3   r   r   r!   r#   r<   setterr)   r   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    r1   r/   r   r      sb        .' .' .'` 	 	 X	 	$ 	$ X	$ 	$ 	$ X	$ 	' 	' X	' 	' 	' X	' 	 	 X	 ]+ + ]+"     X  	& 	& 	& 	 	 X	 	 	 X	 	  	  X	  @ @ X@ 	4 	4 X	4 	7 	7 X	7 	7 	7 X	7
 
 
0
 
 
,   
 
 
.
 
 
*
 
 
(
 
 
*
 
 
*   @," ," ,"\r, r, r,hU U Un0 0 0d. . .`62 62 62p  (& (& (&T5. 5. 5. 5.n  @
 
 
B	@ 	@ 	@I I IV( ( ( ( (r1   r   )r   numpyr   scipy.optimizer   rE   r   r   settingsr   r   
subsolversr	   r
   r   r   r   subsolvers.optimr   utilsr   finfofloattinyr   epsr   r   r;  r1   r/   <module>rF     s*        % % % % % % % % % % % % % % ( ( ( ( ( ( ( (              : 9 9 9 9 9 ! ! ! ! ! ! rxbhuooA A A A A A A A A Ar1   