o
    h                     @   s   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ejZeejZG d	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                   @   s  e Zd ZdZdd Zedd Zedd Zedd	 Zed
d Z	edd Z
edd Zejdd Zedd Zejdd Zedd Zedd Zedd Zedd Zedd Zedd Zed d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 ZdQd3d4Zd5d6 Zd7d8 Zd9d: Z d;d< Z!d=d> Z"d?d@ Z#dAdB Z$dCdD Z%dRdEdFZ&dGdH Z'dIdJ Z(dKdL Z)dMdN Z*dOdP Z+d2S )STrustRegionz!
    Trust-region framework.
    c                 C   s   d| _ || _t| j|| j| _|| _d| _|   t	| j
| _t	| j| _t	| j| _t	| 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	constants r,   O/var/www/vscode/kcb/lib/python3.10/site-packages/scipy/_lib/cobyqa/framework.py__init__   s   zTrustRegion.__init__c                 C      | j jS )zt
        Number of variables.

        Returns
        -------
        int
            Number of variables.
        )r   nr(   r,   r,   r-   r0   L      
zTrustRegion.nc                 C   r/   )z
        Number of linear inequality constraints.

        Returns
        -------
        int
            Number of linear inequality constraints.
        )r   r   r1   r,   r,   r-   r   X   r2   zTrustRegion.m_linear_ubc                 C   r/   )z
        Number of linear equality constraints.

        Returns
        -------
        int
            Number of linear equality constraints.
        )r   r   r1   r,   r,   r-   r   d   r2   zTrustRegion.m_linear_eqc                 C   r/   )z
        Number of nonlinear inequality constraints.

        Returns
        -------
        int
            Number of nonlinear inequality constraints.
        )r   r   r1   r,   r,   r-   r   p   r2   zTrustRegion.m_nonlinear_ubc                 C   r/   )z
        Number of nonlinear equality constraints.

        Returns
        -------
        int
            Number of nonlinear equality constraints.
        )r   r    r1   r,   r,   r-   r    |   r2   zTrustRegion.m_nonlinear_eqc                 C      | j S )zv
        Trust-region radius.

        Returns
        -------
        float
            Trust-region radius.
        )r'   r1   r,   r,   r-   radius      
zTrustRegion.radiusc                 C   s.   || _ | j| jtj | j kr| j| _ dS dS )z
        Set the trust-region radius.

        Parameters
        ----------
        radius : float
            New trust-region radius.
        N)r'   r4   r   r   DECREASE_RADIUS_THRESHOLDr&   )r(   r4   r,   r,   r-   r4      s   

c                 C   r3   )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%   r1   r,   r,   r-   r&      s   zTrustRegion.resolutionc                 C   s
   || _ dS )z
        Set the resolution of the trust-region framework.

        Parameters
        ----------
        resolution : float
            New resolution of the trust-region framework.
        Nr7   )r(   r&   r,   r,   r-   r&      s   

c                 C   r3   )zr
        Penalty parameter.

        Returns
        -------
        float
            Penalty parameter.
        )r   r1   r,   r,   r-   r      r5   zTrustRegion.penaltyc                 C   r3   )z
        Models of the objective function and constraints.

        Returns
        -------
        `cobyqa.models.Models`
            Models of the objective function and constraints.
        )r   r1   r,   r,   r-   models   r5   zTrustRegion.modelsc                 C   r3   )z
        Index of the best interpolation point.

        Returns
        -------
        int
            Index of the best interpolation point.
        )r   r1   r,   r,   r-   
best_index   r5   zTrustRegion.best_indexc                 C   s   | 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.
        )r8   interpolationpointr9   r1   r,   r,   r-   r#      s   zTrustRegion.x_bestc                 C   s   | j j| j S )z
        Value of the objective function at `x_best`.

        Returns
        -------
        float
            Value of the objective function at `x_best`.
        )r8   fun_valr9   r1   r,   r,   r-   fun_best   s   
zTrustRegion.fun_bestc                 C      | 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)r8   cub_valr9   r1   r,   r,   r-   cub_best      
zTrustRegion.cub_bestc                 C   r>   )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)r8   ceq_valr9   r1   r,   r,   r-   ceq_best
  rA   zTrustRegion.ceq_bestc                 C   sl   | j || j| jjj| | jjj   | j| jjj| | jjj	   | j
| j |  | j| j |  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`.
        )r8   funr   r   lineara_ubb_ubr   a_eqb_eqr   cubr!   ceqr(   xr,   r,   r-   	lag_model  s   
zTrustRegion.lag_modelc                 C   sP   | j || j| jjj  | j| jjj  | j| j 	|  | j
| j |  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`.
        )r8   fun_gradr   r   rE   rF   r   rH   r   cub_gradr!   ceq_gradrL   r,   r,   r-   lag_model_grad.  s   
zTrustRegion.lag_model_gradc                 C   sJ   | j  }| jdkr|| j| j   7 }| jdkr#|| j| j   7 }|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   )r8   fun_hessr   r   cub_hessr    r!   ceq_hess)r(   hessr,   r,   r-   lag_model_hessD  s   
	

zTrustRegion.lag_model_hessc                 C   0   | j || j| j |  | j| j |  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`.
        )r8   fun_hess_prodr   cub_hess_prodr!   ceq_hess_prodr(   vr,   r,   r-   lag_model_hess_prodT  s   
zTrustRegion.lag_model_hess_prodc                 C   rX   )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`.
        )r8   fun_curvr   cub_curvr!   ceq_curvr\   r,   r,   r-   lag_model_curvk  s   
zTrustRegion.lag_model_curvc                 C   s    || j | jd| |   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      ?)r8   rO   r#   r^   r(   stepr,   r,   r-   sqp_fun  s
   zTrustRegion.sqp_func                 C       | j | j| j | j|  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`.
        )r8   rJ   r#   rP   rc   r,   r,   r-   sqp_cub     zTrustRegion.sqp_cubc                 C   rf   )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`.
        )r8   rK   r#   rQ   rc   r,   r,   r-   sqp_ceq  rh   zTrustRegion.sqp_ceqNc                 C   sp   |du s|du s|du r|  || j\}}}|}| jdkr6| j j|||d}t|r6|| jtj| 7 }|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   )r?   rB   )r   r   r   	violationr   count_nonzerolinalgnorm)r(   rM   r<   r?   rB   m_valc_valr,   r,   r-   merit  s   

zTrustRegion.meritc                 C   s   t | jjjg| j|gg}t | jjj| jjj|  | j| g}t | jjj	g| j
|gg}t | jjj| jj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   rE   rF   r8   rP   rG   rJ   rH   rQ   rI   rK   )r(   rM   aubbubaeqbeqr,   r,   r-   get_constraint_linearizations  s*   

z)TrustRegion.get_constraint_linearizationsc                 C   s  |  | j\}}}}| jjj| j }| jjj| j }| jtj | j	 }t
||||||||tj fi | j}	|tj rjt||}
t|	|
 |k sRt||	|
 k rYtdtd tj|	d| krjtdtd t| j	d |	|	  }||	8 }||	8 }t|||	  d}| j| j| |	 }| jjdv rt|| j||||tj fi | j}nt|| j|||||||d f	i | j}|tj rt||}
t||
 |k st|||
 k rtd	td tj|	| dtd | j	 krt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.)rv   r#   r   boundsxlxur   r   BYRD_OMOJOKUN_FACTORr4   r
   r   DEBUGr   r   anywarningswarnRuntimeWarningrl   rm   sqrtmaximumr8   rO   r^   typer   r   )r(   r*   rr   rs   rt   ru   r}   r~   r4   normal_steptolg_besttangential_stepr,   r,   r-   get_trust_region_step  s   	






z!TrustRegion.get_trust_region_stepc              
      s  |t j r|jksJ dttdjj|}tjj	||t j   
jjj	}jjjj }jjjj }td| fdd||j|t j }jj| |}jj	jjj	jddjtjf  }	|	ddjdgf |	dddjgf< td| fdd|	ddddf ||j|t j }
jj|
 |}t|t|kr|
}|}jjd	v rj\}}}}t||}t|}|| k}||k}||k}t|||||\}}|dd|df |dd|df j|  }tj|}d|  k rjjk rn n|tj krj| | }
  |
jj	dk r/|
 }
t!||
 |
| g}||
 | }||
 | }t"d
d ||t|fD }tj"t|
|  dd}tj"t|
|  |d}tj"t|| ddf |
 |d}t#d| dtj|
 }||krjj|
 |}t|dt| krt$|
||}|t j rt||}t%|| |k st%||| k rt&'dt(d tj|dj krt&'dt(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                         | jjS Ncurvr8   r:   r]   lagr(   r,   r-   <lambda>      z/TrustRegion.get_geometry_step.<locals>.<lambda>Nr   c                    r   r   r   r   r   r,   r-   r     r   )zlinearly constrainedznonlinearly constrainedc                 s   s    | ]
}t j|d dV  qdS )r   initialN)r   max).0arrayr,   r,   r-   	<genexpr>  s
    
z0TrustRegion.get_geometry_step.<locals>.<genexpr>r         $@g{Gz?g?z9The geometry step does not respect the bound constraints.rw   rx   z?The geometry step does not respect the trust-region constraint.))r   r   r9   r   squeezeeyer8   nptr   r:   gradr#   r   r|   r}   r~   r   r4   determinantsxptnewaxisr	   absr   rv   r   r   Trl   rm   r0   TINYr   rq   r   minclipr   r   r   r   )r(   k_newr*   	coord_vecg_lagr}   r~   rd   sigmar   step_alt	sigma_altrr   rs   rt   ru   tol_bdtol_ubfree_xlfree_xufree_ubn_actq
g_lag_projnorm_g_lag_projcbdrJ   rK   	maxcv_valr   r,   r   r-   get_geometry_step  s   
	(




.0&


(zTrustRegion.get_geometry_stepc              
   C   s   |  | j\}}}}| jjj| j }| jjj| j }tj|}	t	|||||||	|t
j fi | j}
|t
j rgt||}t|
| |k sOt||
| k rVtdtd tj|
d|	 krgt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.rw   rx   zNThe second-order correction step does not respect the trust-region constraint.)rv   r#   r   r|   r}   r~   r   rl   rm   r
   r   r   r   r   r   r   r   r   )r(   rd   r*   rr   rs   rt   ru   r}   r~   r4   soc_stepr   r,   r,   r-    get_second_order_correction_step  s>   	

$z,TrustRegion.get_second_order_correction_stepc           	      C   s   |  | j| j| j| j}|  | j| |||}|  | jd| j| j| j| j}|  | j| | || 	|| 
|}t|| tt||  krV|| t||  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         )rp   r#   r=   r@   rC   r8   rJ   rK   re   rg   ri   r   r   )	r(   rd   r<   r?   rB   	merit_old	merit_newmerit_model_oldmerit_model_newr,   r,   r-   get_reduction_ratioI  s4   zTrustRegion.get_reduction_ratioc           
      C   s   |  | j\}}}}ttjttd| |gtjttd|| | || | g d}| |}tjt| j	| j
| j| jg}t|tt| kr[t||| }| j}	| j| jtj | kryt| jtj | d| _|   |	| jkS )z
        Increase the penalty parameter.

        Parameters
        ----------
        step : `numpy.ndarray`, shape (n,)
            Trust-region step.
        r         ?)rv   r#   r   r   rl   rm   rq   r   re   r   r   r   r!   r   r   r9   r   r   r   PENALTY_INCREASE_THRESHOLDPENALTY_INCREASE_FACTORr   )
r(   rd   rr   rs   rt   ru   	viol_diffsqp_val	thresholdbest_index_saver,   r,   r-   increase_penaltyy  sV   	




zTrustRegion.increase_penaltyc                 C   s   t | j|  | _|   dS )z1
        Decrease the penalty parameter.
        N)r   r   _get_low_penaltyr   r1   r,   r,   r-   decrease_penalty  s   zTrustRegion.decrease_penaltyc           	   
   C   s^  | 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	 t
| jj| jj t
t|d }t| jjD ]V}|| 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||| k r||k r|}|}|}qS|| _dS )z2
        Set the index of the best point.
        Nr   r   )r9   rp   r#   r8   r<   r?   rB   r   maxcvEPSr   r0   r   r   ranger:   r;   r   )	r(   r9   m_bestr_bestr   kx_valrn   r_valr,   r,   r-   r     sP   



zTrustRegion.set_best_indexc                 C   s   t j| jjj| jjjdd| jt jf  d dd}|du r#d}|}n"| j|}t d|t	| j
tj | j | jd  d }d|| j< t |t | }|t || 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.
        Nry   r   axisr   g      @r   )r   sumr8   r:   r   r9   r   r   r   r   r   r   LOW_RADIUS_FACTORr4   r&   argmaxr   r   )r(   x_newdist_sqr   weightsk_maxr,   r,   r-   get_index_to_remove  s>   


zTrustRegion.get_index_to_removec                 C   s   t j|}|| jtj kr|  j| jtj 9  _dS || jtj kr2t	| jtj | j || _dS t
| jtj | j t	| jtj | j | jtj | | _dS )z
        Update the trust-region radius.

        Parameters
        ----------
        step : `numpy.ndarray`, shape (n,)
            Trust-region step.
        ratio : float
            Reduction ratio.
        N)r   rl   rm   r   r   	LOW_RATIOr4   DECREASE_RADIUS_FACTOR
HIGH_RATIOr   r   INCREASE_RADIUS_FACTORINCREASE_RADIUS_THRESHOLD)r(   rd   ratios_normr,   r,   r-   update_radius  s.   





zTrustRegion.update_radiusc                 C   s   | j tj |tj  | jk r|  j| j tj 9  _n!| j tj |tj  | jk r5t	| j|tj  | _n|tj | _t
| j tj | j | 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*   r,   r,   r-   enhance_resolution9  s*   






zTrustRegion.enhance_resolutionc                 C   s   | j t| j| dS )z
        Shift the base point to `x_best`.

        Parameters
        ----------
        options : dict
            Options of the solver.
        N)r8   shift_x_baser   copyr#   r   r,   r,   r-   r   Z  s   	zTrustRegion.shift_x_basec              	   C   s  | j jj| | j jjk}| jdk}| j jj|k}| j jj|k}t	|}t	|}t	|}t	|}	|| | j
 | j dkrt| 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d tj }d|d||	 | | < t|j| |tjfdd}|j||	 ||	 |  | j|< d| j| < |j||	 | ||	 | |  | j|< d| j| < |j||	 | | ||	 | | | j
  | jdd< |j||	 | | | j
 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   rE   rF   rG   r@   r|   r}   r~   r   rk   r   r    r   r0   r_r8   rP   rH   rQ   rO   fullshapeinfr   r   rM   r   r   r   r!   )r(   rM   incl_linear_ubincl_nonlinear_ubincl_xlincl_xur   r   m_xlm_xuidentityc_jacr   xl_lmresr,   r,   r-   r"   e  s   









zTrustRegion.set_multipliersc                 C   st  t j| jjjt jd d f | jjjj | jj	j
j | jj	jt jd d f  | jjf }| jjjt jd d f | jjjj | jj	jj | jj	jt jd d f  }t || | jj| jj g}t ||g}t j|dd}t j|dd}|| jtj | k }t |rt | jj}t | jj}t d|| }	t || |	 }
|
t||  kr|| |
 }|S t j}|S d}|S )Nr   r   r   )r   c_r8   r:   x_baser   r   r   r   rE   rF   rG   r?   rH   rI   rq   rB   nanminnanmaxr   r   THRESHOLD_RATIO_CONSTRAINTSr   r<   minimumr   r   r   )r(   r_val_ubr_val_eqr   c_minc_maxindicesf_minf_max	c_min_negc_diffr   r,   r,   r-   r     sX   





zTrustRegion._get_low_penalty)NNNr   ),__name__
__module____qualname____doc__r.   propertyr0   r   r   r   r    r4   setterr&   r   r8   r9   r#   r=   r@   rC   rN   rR   rW   r^   rb   re   rg   ri   rp   rv   r   r   r   r   r   r   r   r   r   r   r   r"   r   r,   r,   r,   r-   r      sv    0
















 .t 208
*7 !Kr   )r   numpyr   scipy.optimizer   r8   r   r   settingsr   r   
subsolversr   r	   r
   r   r   subsolvers.optimr   utilsr   finfofloattinyr   epsr   r   r,   r,   r,   r-   <module>   s    