o
    Ö­jô—  ã                   @   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,   úX/var/www/html/CropPilot/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-   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 r­t|| j||||tj fi | j¤Ž}nt|| j|||||||d f	i | j¤Ž}|tj rýt||ƒ}
t ||
 |k ¡sÝt |||
 k ¡rät d	td¡ tj |	| ¡dt d¡ | j	 krýt 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 �r°n 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�r°t $|
||¡}|t j �rét||ƒ}t %|| |k ¡�sÏt %||| k ¡�rÖt& 'dt(d¡ tj |¡dˆj k�rét& '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{®Gáz„?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rÿt | 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  |¡r¶t  | 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    