o
    i                     @   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npZ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   ZRHOBEG_resolution
resolution_radius)selfZpboptions	constants r)   U/home/kim/smarthome/.venv/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*   r-   L      
zTrustRegion.nc                 C   r,   )z
        Number of linear inequality constraints.

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

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

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

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

        Returns
        -------
        float
            Trust-region radius.
        )r%   r.   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%   r1   r   r   ZDECREASE_RADIUS_THRESHOLDr$   )r&   r1   r)   r)   r*   r1      s   

c                 C   r0   )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#   r.   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.
        Nr3   )r&   r$   r)   r)   r*   r$      s   

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

        Returns
        -------
        float
            Penalty parameter.
        )r   r.   r)   r)   r*   r      r2   zTrustRegion.penaltyc                 C   r0   )z
        Models of the objective function and constraints.

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

        Returns
        -------
        int
            Index of the best interpolation point.
        )r   r.   r)   r)   r*   
best_index   r2   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.
        )r4   interpolationpointr5   r.   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`.
        )r4   fun_valr5   r.   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)r4   cub_valr5   r.   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)r4   ceq_valr5   r.   r)   r)   r*   ceq_best
  r=   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`.
        )r4   Z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`.
        )r4   fun_gradr   r   r@   rA   r   rC   r   cub_gradr    ceq_gradrG   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   )r4   Zfun_hessr   r   Zcub_hessr   r    Zceq_hess)r&   Z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`.
        )r4   Zfun_hess_prodr   Zcub_hess_prodr    Zceq_hess_prodr&   vr)   r)   r*   lag_model_hess_prodT  s   
zTrustRegion.lag_model_hess_prodc                 C   rO   )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`.
        )r4   Zfun_curvr   Zcub_curvr    Zceq_curvrP   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      ?)r4   rJ   r"   rR   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`.
        )r4   rE   r"   rK   rT   r)   r)   r*   sqp_cub     zTrustRegion.sqp_cubc                 C   rW   )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`.
        )r4   rF   r"   rL   rT   r)   r)   r*   sqp_ceq  rY   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;   r>   )r   r   r   Z	violationr   count_nonzerolinalgnorm)r&   rH   r8   r;   r>   m_valZ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   r@   rA   r4   rK   rB   rE   rC   rL   rD   rF   )r&   rH   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   )Zunconstrainedzbound-constraineddebugz;The tangential step does not respect the bound constraints.z<The trial step does not respect the trust-region constraint.)re   r"   r   boundsxlxur   r   ZBYRD_OMOJOKUN_FACTORr1   r
   r   DEBUGr   r   anywarningswarnRuntimeWarningr\   r]   sqrtmaximumr4   rJ   rR   typer   r   )r&   r'   ra   rb   rc   rd   rk   rl   r1   Znormal_steptolg_bestZ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curvr4   r6   rQ   Zlagr&   r)   r*   <lambda>      z/TrustRegion.get_geometry_step.<locals>.<lambda>Nr   c                    rx   ry   rz   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.rf   rg   z?The geometry step does not respect the trust-region constraint.))r   rm   r5   r   Zsqueezeeyer4   nptr   r6   Zgradr"   r   rj   rk   rl   r   r1   determinantsxptnewaxisr	   absrt   re   r   r   Tr\   r]   r-   TINYr{   r`   r   minZcliprn   ro   rp   rq   )r&   Zk_newr'   Z	coord_vecZg_lagrk   rl   rU   sigmar   Zstep_altZ	sigma_altra   rb   rc   rd   Ztol_bdZtol_ubZfree_xlZfree_xuZfree_ubZn_actqZ
g_lag_projZnorm_g_lag_projZcbdrE   rF   Z	maxcv_valru   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.rf   rg   zNThe second-order correction step does not respect the trust-region constraint.)re   r"   r   rj   rk   rl   r   r\   r]   r
   r   rm   r   r   rn   ro   rp   rq   )r&   rU   r'   ra   rb   rc   rd   rk   rl   r1   Zsoc_stepru   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         )r_   r"   r9   r<   r?   r4   rE   rF   rV   rX   rZ   r   r   )	r&   rU   r8   r;   r>   Z	merit_oldZ	merit_newZmerit_model_oldZ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         ?)re   r"   r   r   r\   r]   r`   rs   rV   r   r   r   r    r   r   r5   r   r   r   ZPENALTY_INCREASE_THRESHOLDZPENALTY_INCREASE_FACTORr   )
r&   rU   ra   rb   rc   rd   Z	viol_diffZsqp_val	thresholdZ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   r.   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   )r5   r_   r"   r4   r8   r;   r>   r   ZmaxcvEPSr   r-   r   r   ranger6   r7   r   )	r&   r5   Zm_bestZr_bestru   kZx_valr^   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.
        Nrh   r   Zaxisr   g      @r   )r   sumr4   r6   r   r5   r   r   rs   r   r   r   ZLOW_RADIUS_FACTORr1   r$   Zargmaxr   rr   )r&   Zx_newZdist_sqr   weightsZ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   r\   r]   r   r   Z	LOW_RATIOr1   DECREASE_RADIUS_FACTORZ
HIGH_RATIOr   r   ZINCREASE_RADIUS_FACTORZINCREASE_RADIUS_THRESHOLD)r&   rU   ratioZ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   ZLARGE_RESOLUTION_THRESHOLDr   ZRHOENDr$   ZDECREASE_RESOLUTION_FACTORZMODERATE_RESOLUTION_THRESHOLDr   rr   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)r4   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   NZbvls)rj   method)r   r@   rA   rB   r<   rj   rk   rl   r   r[   r   r   r   r-   Zr_r4   rK   rC   rL   rJ   fullshapeinfr   r   rH   r   r   r   r    )r&   rH   Zincl_linear_ubZincl_nonlinear_ubZincl_xlZincl_xur   r   Zm_xlZm_xuidentityZc_jacrv   Z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   Zc_r4   r6   Zx_baser   r   r   r   r@   rA   rB   r;   rC   rD   r`   r>   ZnanminZnanmaxr   r   ZTHRESHOLD_RATIO_CONSTRAINTSrn   r8   minimumr   r   r   )r&   Zr_val_ubZr_val_eqr   Zc_minZc_maxindicesZf_minZf_maxZ	c_min_negZc_diffr   r)   r)   r*   r     sX   





zTrustRegion._get_low_penalty)NNNry   ),__name__
__module____qualname____doc__r+   propertyr-   r   r   r   r   r1   setterr$   r   r4   r5   r"   r9   r<   r?   rI   rM   rN   rR   rS   rV   rX   rZ   r_   re   rw   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   )ro   numpyr   Zscipy.optimizer   r4   r   r   settingsr   r   Z
subsolversr   r	   r
   r   r   Zsubsolvers.optimr   utilsr   ZfinfofloatZtinyr   epsr   r   r)   r)   r)   r*   <module>   s    