o
    Ö­j�  ã                   @   sÂ   d dl mZ d dlmZ d dlZd dlZd dlmZm	Z	m
Z
m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 G d
d„ dƒZG dd„ dƒZG dd„ dƒZG dd„ dƒZG dd„ dƒZdS )é    )Úsuppress)Ú	signatureN)ÚBoundsÚLinearConstraintÚNonlinearConstraintÚOptimizeResult)ÚPreparedConstrainté   )ÚPRINT_OPTIONSÚBARRIER)ÚCallbackSuccessÚget_arrays_tol)Úexact_1d_arrayc                   @   s8   e Zd ZdZdd„ Zdd„ Zedd„ ƒZedd	„ ƒZd
S )ÚObjectiveFunctionz)
    Real-valued objective function.
    c                 G   sP   |r|du st |ƒsJ ‚t|tƒsJ ‚t|tƒsJ ‚|| _|| _|| _d| _dS )a  
        Initialize the objective function.

        Parameters
        ----------
        fun : {callable, None}
            Function to evaluate, or None.

                ``fun(x, *args) -> float``

            where ``x`` is an array with shape (n,) and `args` is a tuple.
        verbose : bool
            Whether to print the function evaluations.
        debug : bool
            Whether to make debugging tests during the execution.
        *args : tuple
            Additional arguments to be passed to the function.
        Nr   )ÚcallableÚ
isinstanceÚboolÚ_funÚ_verboseÚ_argsÚ_n_eval)ÚselfÚfunÚverboseÚdebugÚargs© r   úV/var/www/html/CropPilot/venv/lib/python3.10/site-packages/scipy/_lib/cobyqa/problem.pyÚ__init__   s   
zObjectiveFunction.__init__c                 C   s¢   t j|td�}| jdu rd}|S tt  | j|g| j¢R Ž ¡ƒ}|  jd7  _| jrOt jdi t	¤Ž� t
| j› d|› d|› �ƒ W d  ƒ |S 1 sJw   Y  |S )a  
        Evaluate the objective function.

        Parameters
        ----------
        x : array_like, shape (n,)
            Point at which the objective function is evaluated.

        Returns
        -------
        float
            Function value at `x`.
        ©ÚdtypeNç        r	   ú(ú) = r   )ÚnpÚarrayÚfloatr   Úsqueezer   r   r   Úprintoptionsr
   ÚprintÚname)r   ÚxÚfr   r   r   Ú__call__6   s   
û
ÿþzObjectiveFunction.__call__c                 C   ó   | j S ©úŠ
        Number of function evaluations.

        Returns
        -------
        int
            Number of function evaluations.
        )r   ©r   r   r   r   Ún_evalO   ó   
zObjectiveFunction.n_evalc                 C   s:   d}| j durz| j j}W |S  ty   d}Y |S w |S )úŠ
        Name of the objective function.

        Returns
        -------
        str
            Name of the objective function.
        Ú Nr   )r   Ú__name__ÚAttributeError)r   r*   r   r   r   r*   [   s   


þþzObjectiveFunction.nameN)	r6   Ú
__module__Ú__qualname__Ú__doc__r   r-   Úpropertyr2   r*   r   r   r   r   r      s    
r   c                   @   sH   e Zd ZdZdd„ Zedd„ ƒZedd„ ƒZdd	„ Zd
d„ Z	dd„ Z
dS )ÚBoundConstraintsz.
    Bound constraints ``xl <= x <= xu``.
    c                 C   sÆ   t  |jt¡| _t  |jt¡| _t j | jt  	| j¡< t j| j
t  	| j
¡< t  | j| j
k¡o@t  | jt jk ¡o@t  | j
t j k¡| _t  | jt j k¡t  | j
t jk ¡ | _t|t  |jj¡ƒ| _dS )z 
        Initialize the bound constraints.

        Parameters
        ----------
        bounds : scipy.optimize.Bounds
            Bound constraints.
        N)r$   r%   Úlbr&   Ú_xlÚubÚ_xuÚinfÚxlÚisnanÚxuÚallÚis_feasibleÚcount_nonzeroÚmr   ÚonesÚsizeÚpcs)r   Úboundsr   r   r   r   s   s   	ÿý
ÿzBoundConstraints.__init__c                 C   r.   )z|
        Lower bound.

        Returns
        -------
        `numpy.ndarray`, shape (n,)
            Lower bound.
        )r>   r1   r   r   r   rB   �   r3   zBoundConstraints.xlc                 C   r.   )z|
        Upper bound.

        Returns
        -------
        `numpy.ndarray`, shape (n,)
            Upper bound.
        )r@   r1   r   r   r   rD   ™   r3   zBoundConstraints.xuc                 C   s   t j|td�}|  |¡S )á0  
        Evaluate the maximum constraint violation.

        Parameters
        ----------
        x : array_like, shape (n,)
            Point at which the maximum constraint violation is evaluated.

        Returns
        -------
        float
            Maximum constraint violation at `x`.
        r   )r$   Úasarrayr&   Ú	violation©r   r+   r   r   r   Úmaxcv¥   s   
zBoundConstraints.maxcvc                 C   s   | j r	t dg¡S | j |¡S )Nr   )rF   r$   r%   rK   rO   rP   r   r   r   rO   ¶   s   zBoundConstraints.violationc                 C   s   | j rt || j| j¡S |S )a  
        Project a point onto the feasible set.

        Parameters
        ----------
        x : array_like, shape (n,)
            Point to be projected.

        Returns
        -------
        `numpy.ndarray`, shape (n,)
            Projection of `x` onto the feasible set.
        )rF   r$   ÚcliprB   rD   rP   r   r   r   Úproject½   s   zBoundConstraints.projectN)r6   r8   r9   r:   r   r;   rB   rD   rQ   rO   rS   r   r   r   r   r<   n   s    

r<   c                   @   sp   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dd„ Zdd„ ZdS )ÚLinearConstraintszK
    Linear constraints ``a_ub @ x <= b_ub`` and ``a_eq @ x == b_eq``.
    c                    sà  |rt |tƒs	J ‚|D ]	}t |tƒsJ ‚qt |tƒsJ ‚t dˆ f¡| _t d¡| _t dˆ f¡| _t d¡| _	|D ]a}t 
|j|j ¡t|j|jƒk}t |¡rpt | j|j| f¡| _t | jd|j| |j|   f¡| _	t |¡s›t | j|j|  |j|   f¡| _t | j|j|  |j|   f¡| _q:d| jt | j¡< d| jt | j¡< t | j¡t | j¡B }t | j¡}| j| dd…f | _| j|  | _| j| dd…f | _| j|  | _	‡ fdd„|D ƒ| _dS )a2  
        Initialize the linear constraints.

        Parameters
        ----------
        constraints : list of LinearConstraint
            Linear constraints.
        n : int
            Number of variables.
        debug : bool
            Whether to make debugging tests during the execution.
        r   ç      à?r!   Nc                    s$   g | ]}|j jrt|t ˆ ¡ƒ‘qS r   )ÚArJ   r   r$   rI   )Ú.0Úc©Únr   r   Ú
<listcomp>  s
    ÿÿz.LinearConstraints.__init__.<locals>.<listcomp>)r   Úlistr   r   r$   ÚemptyÚ_a_ubÚ_b_ubÚ_a_eqÚ_b_eqÚabsr?   r=   r   ÚanyÚvstackÚa_eqrV   ÚconcatenateÚb_eqrE   Úa_ubÚb_ubrC   ÚisinfrK   )r   ÚconstraintsrZ   r   Ú
constraintÚis_equalityÚundef_ubÚundef_eqr   rY   r   r   Ó   sf   
ÿþ
ÿþþÿ


ýÿ
ýÿ€	
ÿzLinearConstraints.__init__c                 C   r.   )zÜ
        Left-hand side matrix of the linear inequality constraints.

        Returns
        -------
        `numpy.ndarray`, shape (m, n)
            Left-hand side matrix of the linear inequality constraints.
        )r^   r1   r   r   r   rh     r3   zLinearConstraints.a_ubc                 C   r.   )zÞ
        Right-hand side vector of the linear inequality constraints.

        Returns
        -------
        `numpy.ndarray`, shape (m, n)
            Right-hand side vector of the linear inequality constraints.
        )r_   r1   r   r   r   ri   #  r3   zLinearConstraints.b_ubc                 C   r.   )zØ
        Left-hand side matrix of the linear equality constraints.

        Returns
        -------
        `numpy.ndarray`, shape (m, n)
            Left-hand side matrix of the linear equality constraints.
        )r`   r1   r   r   r   re   /  r3   zLinearConstraints.a_eqc                 C   r.   )zÚ
        Right-hand side vector of the linear equality constraints.

        Returns
        -------
        `numpy.ndarray`, shape (m, n)
            Right-hand side vector of the linear equality constraints.
        )ra   r1   r   r   r   rg   ;  r3   zLinearConstraints.b_eqc                 C   ó   | j jS ©zœ
        Number of linear inequality constraints.

        Returns
        -------
        int
            Number of linear inequality constraints.
        )ri   rJ   r1   r   r   r   Úm_ubG  ó   
zLinearConstraints.m_ubc                 C   rp   ©z˜
        Number of linear equality constraints.

        Returns
        -------
        int
            Number of linear equality constraints.
        )rg   rJ   r1   r   r   r   Úm_eqS  rs   zLinearConstraints.m_eqc                 C   s   t j|  |¡dd�S )rM   r!   ©Úinitial©r$   ÚmaxrO   rP   r   r   r   rQ   _  s   zLinearConstraints.maxcvc                    s.   t | jƒrt ‡ fdd„| jD ƒ¡S t g ¡S )Nc                    ó   g | ]}|  ˆ ¡‘qS r   ©rO   ©rW   Úpc©r+   r   r   r[   q  ó    z/LinearConstraints.violation.<locals>.<listcomp>)ÚlenrK   r$   rf   r%   rP   r   r~   r   rO   o  s   

zLinearConstraints.violationN)r6   r8   r9   r:   r   r;   rh   ri   re   rg   rr   ru   rQ   rO   r   r   r   r   rT   Î   s"    D





rT   c                   @   sX   e Zd ZdZdd„ Zdd„ Zedd„ ƒZedd	„ ƒZed
d„ ƒZ	ddd„Z
ddd„ZdS )ÚNonlinearConstraintszI
    Nonlinear constraints ``c_ub(x) <= 0`` and ``c_eq(x) == b_eq``.
    c                 C   st   |r#t |tƒs	J ‚|D ]	}t |tƒsJ ‚qt |tƒsJ ‚t |tƒs#J ‚|| _g | _|| _d| _d| _d | _	| _
dS )aA  
        Initialize the nonlinear constraints.

        Parameters
        ----------
        constraints : list
            Nonlinear constraints.
        verbose : bool
            Whether to print the function evaluations.
        debug : bool
            Whether to make debugging tests during the execution.
        N)r   r\   r   r   Ú_constraintsrK   r   Ú_map_ubÚ_map_eqÚ_m_ubÚ_m_eq)r   rk   r   r   rl   r   r   r   r   z  s   zNonlinearConstraints.__init__c              
   C   s  t | jƒsd | _| _t g ¡t g ¡fS tj|td�}t | jƒs¢g | _g | _	d| _d| _| jD ]q}t
|jƒsLt |¡}dd„ |_dd„ |_t||ƒ}nt||ƒ}d|j_| j |¡ t |jj¡}|jd |jd }}t||ƒ}t || ¡|k}	| j	 ||	 ¡ | j ||	  ¡ |  jt |	¡7  _|  jt |	 ¡7  _q0g }
g }t| jƒD ]´\}}|j |¡}| jr÷tjdi t¤Ž�/ ttƒ� | j| jj}t|› d|› d	|› �ƒ W d
  ƒ n1 sãw   Y  W d
  ƒ n1 sòw   Y  | j	| }| j| }|| }t |ƒ�r=|jd | }|jd | }|tj  k}|| ||  }|
 |¡ |tj k }|| ||  }|
 |¡ || }t |ƒ�rZd|jd | |jd |   }||8 }| |¡ q«| j�rjt !|¡}nt g ¡}| j�ryt !|
¡}
nt g ¡}
|
j"| _|j"| _|
|fS )a¿  
        Calculates the residual (slack) for the constraints.

        Parameters
        ----------
        x : array_like, shape (n,)
            Point at which the constraints are evaluated.

        Returns
        -------
        `numpy.ndarray`, shape (m_nonlinear_ub,)
            Nonlinear inequality constraint slack values.
        `numpy.ndarray`, shape (m_nonlinear_eq,)
            Nonlinear equality constraint slack values.
        r   r   c                 S   s   | S ©Nr   )Úx0r   r   r   Ú<lambda>¹  ó    z/NonlinearConstraints.__call__.<locals>.<lambda>c                 S   s   dS )Nr!   r   )rˆ   Úvr   r   r   r‰   º  rŠ   Tr	   r"   r#   NrU   r   )#r€   r‚   r†   r…   r$   r%   r&   rK   rƒ   r„   r   ÚjacÚcopyÚhessr   r   Ú	f_updatedÚappendÚarangerH   rL   r   rb   rG   Ú	enumerater   r(   r
   r   r7   r6   r)   rA   rf   rJ   )r   r+   rl   rX   r}   Úidxr=   r?   Úarr_tolrm   Úc_ubÚc_eqÚiÚvalÚfun_nameÚeq_idxÚub_idxÚub_valrB   rD   Ú	finite_xlÚ_vÚ	finite_xuÚeq_valÚmidpointr   r   r   r-   —  s€   









þ€ÿ






 

zNonlinearConstraints.__call__c                 C   ó   | j du r	tdƒ‚| j S )a  
        Number of nonlinear inequality constraints.

        Returns
        -------
        int
            Number of nonlinear inequality constraints.

        Raises
        ------
        ValueError
            If the number of nonlinear inequality constraints is unknown.
        Nz:The number of nonlinear inequality constraints is unknown.)r…   Ú
ValueErrorr1   r   r   r   rr     ó
   
ÿzNonlinearConstraints.m_ubc                 C   r¢   )a  
        Number of nonlinear equality constraints.

        Returns
        -------
        int
            Number of nonlinear equality constraints.

        Raises
        ------
        ValueError
            If the number of nonlinear equality constraints is unknown.
        Nz8The number of nonlinear equality constraints is unknown.)r†   r£   r1   r   r   r   ru     r¤   zNonlinearConstraints.m_eqc                 C   s   t | jƒr| jd jjS dS )r0   r   )r€   rK   r   Únfevr1   r   r   r   r2   /  s   

zNonlinearConstraints.n_evalNc                 C   s   t j| j|||d�dd�S ©aÔ  
        Evaluate the maximum constraint violation.

        Parameters
        ----------
        x : array_like, shape (n,)
            Point at which the maximum constraint violation is evaluated.
        cub_val : array_like, shape (m_nonlinear_ub,), optional
            Values of the nonlinear inequality constraints. If not provided,
            the nonlinear inequality constraints are evaluated at `x`.
        ceq_val : array_like, shape (m_nonlinear_eq,), optional
            Values of the nonlinear equality constraints. If not provided,
            the nonlinear equality constraints are evaluated at `x`.

        Returns
        -------
        float
            Maximum constraint violation at `x`.
        )Úcub_valÚceq_valr!   rv   rx   ©r   r+   r§   r¨   r   r   r   rQ   >  s   ÿzNonlinearConstraints.maxcvc                    s   t  ‡ fdd„| jD ƒ¡S )Nc                    rz   r   r{   r|   r~   r   r   r[   W  r   z2NonlinearConstraints.violation.<locals>.<listcomp>)r$   rf   rK   r©   r   r~   r   rO   V  s   zNonlinearConstraints.violation©NN)r6   r8   r9   r:   r   r-   r;   rr   ru   r2   rQ   rO   r   r   r   r   r�   u  s    l



r�   c                   @   s  e Zd ZdZdd„ Zd0d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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ed#d$„ ƒZed%d&„ ƒZd'd(„ Zd1d*d+„Zd1d,d-„Zd.d/„ Zd)S )2ÚProblemz
    Optimization problem.
    c                 C   sÒ  |rVt |tƒs	J ‚t |tƒsJ ‚t |tƒsJ ‚t |tƒsJ ‚t |tƒs%J ‚t |tƒs,J ‚t |	tƒs3J ‚t |
tƒs:J ‚|	rB|
dksBJ ‚t |tƒsIJ ‚|dksOJ ‚t |tƒsVJ ‚|| _|| _	|| _
|durkt|ƒsktdƒ‚|| _t|dƒ}|j}|jj|kr„td|› d�ƒ‚|jjd |kr”td|› d	�ƒ‚t|j|jƒ}|j|jkt |j|j ¡|k @ | _d
|j| j |j| j   | _t | j|j| j |j| j ¡| _|| _tt|j| j  |j| j  ƒƒ| _| j || j  ¡| _|j |j!dd…| jf | j  }tt"|jdd…| j f tj# |j$|jdd…| jf | j  ƒt"|j!dd…| j f ||ƒg| j%|ƒ| _	|�oM| jj&�oMt 't (| jj¡¡�oMt 't (| jj¡¡}|�r»d
| jj| jj  | _)d
| jj| jj  | _*ttt +| j%¡ t +| j%¡ƒƒ| _| j	j | j	j!| j*  }tt"| j	jt ,| j)¡ tj# | j	j$| j	j| j*  ƒt"| j	j!t ,| j)¡ ||ƒg| j%|ƒ| _	| j| j* | j) | _nt +| j%¡| _)t -| j%¡| _*|| _.|| _/g | _0g | _1g | _2|	| _3|
| _4g | _5g | _6g | _7dS )aY  
        Initialize the nonlinear problem.

        The problem is preprocessed to remove all the variables that are fixed
        by the bound constraints.

        Parameters
        ----------
        obj : ObjectiveFunction
            Objective function.
        x0 : array_like, shape (n,)
            Initial guess.
        bounds : BoundConstraints
            Bound constraints.
        linear : LinearConstraints
            Linear constraints.
        nonlinear : NonlinearConstraints
            Nonlinear constraints.
        callback : {callable, None}
            Callback function.
        feasibility_tol : float
            Tolerance on the constraint violation.
        scale : bool
            Whether to scale the problem according to the bounds.
        store_history : bool
            Whether to store the function evaluations.
        history_size : int
            Maximum number of function evaluations to store.
        filter_size : int
            Maximum number of points in the filter.
        debug : bool
            Whether to make debugging tests during the execution.
        r   Nz)The callback must be a callable function.z#The initial guess must be a vector.zThe bounds must have z
 elements.r	   z@The left-hand side matrices of the linear constraints must have z	 columns.rU   )8r   r   r<   rT   r�   r&   r   ÚintÚ_objÚ_linearÚ
_nonlinearr   Ú	TypeErrorÚ	_callbackr   rJ   rB   r£   rh   Úshaper   rD   r$   rb   Ú
_fixed_idxÚ
_fixed_valrR   Ú_orig_boundsr   Ú_boundsrS   Ú_x0rg   re   r   rA   ri   rZ   rF   rE   ÚisfiniteÚ_scaling_factorÚ_scaling_shiftrI   ÚdiagÚzerosÚ_feasibility_tolÚ_filter_sizeÚ_fun_filterÚ_maxcv_filterÚ	_x_filterÚ_store_historyÚ_history_sizeÚ_fun_historyÚ_maxcv_historyÚ
_x_history)r   Úobjrˆ   rL   ÚlinearÚ	nonlinearÚcallbackÚfeasibility_tolÚscaleÚstore_historyÚhistory_sizeÚfilter_sizer   rZ   Útolrg   r   r   r   r   _  sÔ   0
ÿÿ
ÿÿ

ýÿ ÿýù	õÿþüÿÿýýùñ
zProblem.__init__r!   c              
      sT  t j|td�}|  |¡}|  |¡‰ |  |¡\}}|  |||¡‰| jrN| j 	ˆ ¡ | j
 	ˆ¡ | j 	|¡ t| jƒ| jkrN| j d¡ | j
 d¡ | j d¡ t  ˆ ¡r`t  ˆ¡r`t| jƒdk}n=t  ˆ ¡rvt‡fdd„t| j| jƒD ƒƒ}n't  ˆ¡rŒt‡ fdd„t| j| jƒD ƒƒ}nt‡ ‡fdd„t| j| jƒD ƒƒ}|�r*| j 	ˆ ¡ | j 	ˆ¡ | j 	|¡ tt| jƒd ddƒD ]Q}t  ˆ ¡rÍt  | j| ¡}n,t  ˆ¡rÛt  | j| ¡}nt  | j| ¡pøt  | j| ¡pøˆ | j| koøˆ| j| k}|�r| j |¡ | j |¡ | j |¡ q½t| jƒ| jk�r*| j d¡ | j d¡ | j d¡ | jd	u�rpt| jƒ}	z*|  |¡\}
}}|  |
¡}
t|	jƒd
hk�rYt|
|d�}| j|d� n|  |
¡ W n t�yo } zt|‚d	}~ww t  ˆ ¡�rxt‰ t|t  |¡< t|t  |¡< t t!ˆ tƒt ƒ‰ t  "t  #|t¡t ¡}t  "t  #|t¡t ¡}ˆ ||fS )a  
        Evaluate the objective and nonlinear constraint functions.

        Parameters
        ----------
        x : array_like, shape (n,)
            Point at which the functions are evaluated.
        penalty : float, optional
            Penalty parameter used to select the point in the filter to forward
            to the callback function.

        Returns
        -------
        float
            Objective function value.
        `numpy.ndarray`, shape (m_nonlinear_ub,)
            Nonlinear inequality constraint function values.
        `numpy.ndarray`, shape (m_nonlinear_eq,)
            Nonlinear equality constraint function values.

        Raises
        ------
        `cobyqa.utils.CallbackSuccess`
            If the callback function raises a ``StopIteration``.
        r   r   c                 3   s0   � | ]\}}t  |¡rˆ |k pt  |¡V  qd S r‡   ©r$   rC   ©rW   Ú
fun_filterÚmaxcv_filter)Ú	maxcv_valr   r   Ú	<genexpr>7  ó   € 
ýÿ
ýz#Problem.__call__.<locals>.<genexpr>c                 3   s0   � | ]\}}t  |¡rˆ |k pt  |¡V  qd S r‡   rÑ   rÒ   )Úfun_valr   r   rÖ   A  r×   c                 3   s$   � | ]\}}ˆ |k pˆ|k V  qd S r‡   r   rÒ   ©rØ   rÕ   r   r   rÖ   K  s
   € ÿ
ÿé   éÿÿÿÿNÚintermediate_result)r+   r   )rÜ   )$r$   rN   r&   Úbuild_xr­   r¯   rQ   rÂ   rÄ   r�   rÅ   rÆ   r€   rÃ   ÚpoprC   r¿   rE   ÚziprÀ   rÁ   Úranger¾   r±   r   Ú	best_evalÚsetÚ
parametersr   ÚStopIterationr   r   ry   ÚminÚmaximumÚminimum)r   r+   ÚpenaltyÚx_fullr§   r¨   Úinclude_pointÚkÚremove_pointÚsigÚx_bestÚfun_bestÚ_rÜ   Úexcr   rÙ   r   r-   
  s¨   


þ
ü
	þ
ü
þþ

ÿü€

þ
€€ÿ
zProblem.__call__c                 C   rp   )zt
        Number of variables.

        Returns
        -------
        int
            Number of variables.
        )rˆ   rJ   r1   r   r   r   rZ   Ž  rs   z	Problem.nc                 C   rp   )zÒ
        Number of variables in the original problem (with fixed variables).

        Returns
        -------
        int
            Number of variables in the original problem (with fixed variables).
        )r³   rJ   r1   r   r   r   Ún_origš  rs   zProblem.n_origc                 C   r.   )z€
        Initial guess.

        Returns
        -------
        `numpy.ndarray`, shape (n,)
            Initial guess.
        )r·   r1   r   r   r   rˆ   ¦  r3   z
Problem.x0c                 C   rp   r/   )r­   r2   r1   r   r   r   r2   ²  rs   zProblem.n_evalc                 C   rp   )r4   )r­   r*   r1   r   r   r   r™   ¾  rs   zProblem.fun_namec                 C   r.   )z}
        Bound constraints.

        Returns
        -------
        BoundConstraints
            Bound constraints.
        )r¶   r1   r   r   r   rL   Ê  r3   zProblem.boundsc                 C   r.   )z€
        Linear constraints.

        Returns
        -------
        LinearConstraints
            Linear constraints.
        )r®   r1   r   r   r   rÈ   Ö  r3   zProblem.linearc                 C   rp   )z„
        Number of bound constraints.

        Returns
        -------
        int
            Number of bound constraints.
        )rL   rH   r1   r   r   r   Úm_boundsâ  rs   zProblem.m_boundsc                 C   rp   rq   )rÈ   rr   r1   r   r   r   Úm_linear_ubî  rs   zProblem.m_linear_ubc                 C   rp   rt   )rÈ   ru   r1   r   r   r   Úm_linear_eqú  rs   zProblem.m_linear_eqc                 C   rp   )a   
        Number of nonlinear inequality constraints.

        Returns
        -------
        int
            Number of nonlinear inequality constraints.

        Raises
        ------
        ValueError
            If the number of nonlinear inequality constraints is not known.
        )r¯   rr   r1   r   r   r   Úm_nonlinear_ub  ó   zProblem.m_nonlinear_ubc                 C   rp   )a  
        Number of nonlinear equality constraints.

        Returns
        -------
        int
            Number of nonlinear equality constraints.

        Raises
        ------
        ValueError
            If the number of nonlinear equality constraints is not known.
        )r¯   ru   r1   r   r   r   Úm_nonlinear_eq  r÷   zProblem.m_nonlinear_eqc                 C   ó   t j| jtd�S )z½
        History of objective function evaluations.

        Returns
        -------
        `numpy.ndarray`, shape (n_eval,)
            History of objective function evaluations.
        r   )r$   r%   rÄ   r&   r1   r   r   r   Úfun_history(  ó   
zProblem.fun_historyc                 C   rù   )z»
        History of maximum constraint violations.

        Returns
        -------
        `numpy.ndarray`, shape (n_eval,)
            History of maximum constraint violations.
        r   )r$   r%   rÅ   r&   r1   r   r   r   Úmaxcv_history4  rû   zProblem.maxcv_historyc                 C   s`   z%| j dks| jdkrW dS | jdks| jdkrW dS | jdkr#W dS W dS  ty/   Y dS w )zû
        Type of the problem.

        The problem can be either 'unconstrained', 'bound-constrained',
        'linearly constrained', or 'nonlinearly constrained'.

        Returns
        -------
        str
            Type of the problem.
        r   znonlinearly constrainedzlinearly constrainedzbound-constrainedÚunconstrained)rö   rø   rô   rõ   ró   r£   r1   r   r   r   Útype@  s   
úzProblem.typec                 C   s
   | j dkS )z§
        Whether the problem is a feasibility problem.

        Returns
        -------
        bool
            Whether the problem is a feasibility problem.
        r5   )r™   r1   r   r   r   Úis_feasibility^  s   

zProblem.is_feasibilityc                 C   s<   t  | j¡}| j|| j< || j | j || j < | j |¡S )a0  
        Build the full vector of variables from the reduced vector.

        Parameters
        ----------
        x : array_like, shape (n,)
            Reduced vector of variables.

        Returns
        -------
        `numpy.ndarray`, shape (n_orig,)
            Full vector of variables.
        )	r$   r]   rò   r´   r³   r¹   rº   rµ   rS   )r   r+   ré   r   r   r   rÝ   j  s   ÿzProblem.build_xNc                 C   s,   | j |||d�}t |¡rtj|dd�S dS r¦   )rO   r$   rG   ry   )r   r+   r§   r¨   rO   r   r   r   rQ   ~  s   
zProblem.maxcvc                 C   s€   g }| j js| j  |¡}| |¡ t| jjƒr"| j |¡}| |¡ t| jjƒr5| j |||¡}| |¡ t|ƒr>t 	|¡S d S r‡   )
rL   rF   rO   r�   r€   rÈ   rK   r¯   r$   rf   )r   r+   r§   r¨   rO   ÚbÚlcÚnlcr   r   r   rO   ˜  s   



ÿzProblem.violationc                 C   sö  t | jƒdkr| | jƒ t | j¡}t | j¡}t | j¡}t |¡}t |¡rË|| j	k}t |¡rat 
t || ¡¡sa||t || ¡k@ }t |¡dkrY||t || ¡kM }t |¡d }n‡t |¡rnt |¡d }nzt |tj¡}	|| |||   |	|< t 
t |	¡¡r˜|t |¡k}
t |
¡d }nP|	t |	¡k}t |¡dkr±||t || ¡kM }t |¡dkrÃ||t || ¡kM }t |¡d }nt 
t |¡¡sâ|t |¡k}t |¡d }nt |ƒd }| j ||dd…f ¡|| || fS )aÎ  
        Return the best point in the filter and the corresponding objective and
        nonlinear constraint function evaluations.

        Parameters
        ----------
        penalty : float
            Penalty parameter

        Returns
        -------
        `numpy.ndarray`, shape (n,)
            Best point.
        float
            Corresponding objective function value.
        float
            Corresponding maximum constraint violation.
        r   r	   rÛ   N)r€   r¿   rˆ   r$   r%   rÀ   rÁ   r¸   rc   r½   rE   rC   ÚnanminrG   rå   ÚflatnonzeroÚ	full_likeÚnanrL   rS   )r   rè   rÓ   rÔ   Úx_filterÚ
finite_idxÚfeasible_idxÚfun_min_idxr—   Úmerit_filterÚmin_maxcv_idxÚmerit_min_idxr   r   r   rá   ¨  sZ   



ÿÿÿ
ÿ	ÿÿýzProblem.best_eval)r!   rª   )r6   r8   r9   r:   r   r-   r;   rZ   rò   rˆ   r2   r™   rL   rÈ   ró   rô   rõ   rö   rø   rú   rü   rþ   rÿ   rÝ   rQ   rO   rá   r   r   r   r   r«   Z  sT     
, 

















r«   )Ú
contextlibr   Úinspectr   r�   Únumpyr$   Úscipy.optimizer   r   r   r   Úscipy.optimize._constraintsr   Úsettingsr
   r   Úutilsr   r   r   r   r<   rT   r�   r«   r   r   r   r   Ú<module>   s     Z` ( f