
    ^Mh                         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          Z G d d          Z G d d          Z G d d          Z G d d          ZdS )    )suppress)	signatureN)BoundsLinearConstraintNonlinearConstraintOptimizeResult)PreparedConstraint   )PRINT_OPTIONSBARRIER)CallbackSuccessget_arrays_tol)exact_1d_arrayc                   J    e Zd ZdZd Zd Zed             Zed             ZdS )ObjectiveFunctionz)
    Real-valued objective function.
    c                     |rA|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argss        Y/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/scipy/_lib/cobyqa/problem.py__init__zObjectiveFunction.__init__   sk    &  	+;(3--;;;gt,,,,,eT*****	
    c                 v   t          j        |t                    }| j        d}nt          t          j         | j        |g| j        R                      }| xj        dz  c_        | j        rJt          j        di t          5  t          | j         d| d|            ddd           n# 1 swxY w 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
   () =  )nparrayfloatr   squeezer   r   r   printoptionsr   printname)r   xfs      r   __call__zObjectiveFunction.__call__6   s    HQe$$$9AAbj1!:tz!:!:!:;;<<ALLALL} 5_55}55 5 5TY3333334445 5 5 5 5 5 5 5 5 5 5 5 5 5 5s   B..B25B2c                     | j         S 
        Number of function evaluations.

        Returns
        -------
        int
            Number of function evaluations.
        )r   r   s    r   n_evalzObjectiveFunction.n_evalO        |r!   c                 X    d}| j          	 | j         j        }n# t          $ r d}Y nw xY w|S )
        Name of the objective function.

        Returns
        -------
        str
            Name of the objective function.
         Nr   )r   __name__AttributeError)r   r/   s     r   r/   zObjectiveFunction.name[   sK     9 y)!   s    ''N)	r<   
__module____qualname____doc__r    r2   propertyr7   r/   r(   r!   r   r   r      sr           :  2 	 	 X	   X  r!   r   c                   V    e Zd ZdZd Zed             Zed             Zd Zd Z	d Z
dS )	BoundConstraintsz.
    Bound constraints ``xl <= x <= xu``.
    c                 <   t          j        |j        t                    | _        t          j        |j        t                    | _        t           j         | j        t          j	        | j                  <   t           j        | j
        t          j	        | j
                  <   t          j        | j        | j
        k              oNt          j        | j        t           j        k               o't          j        | j
        t           j         k              | _        t          j        | j        t           j         k              t          j        | j
        t           j        k               z   | _        t          |t          j        |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   boundss     r   r    zBoundConstraints.__init__s   s"    8FIu--8FIu-- ')fW!!"%'V!!" F47dg%&& *tw'((*tw"&()) 	
 !$'RVG"344r7GGbf8
 8
 
 &fbgfin.E.EFFr!   c                     | j         S )z|
        Lower bound.

        Returns
        -------
        `numpy.ndarray`, shape (n,)
            Lower bound.
        )rF   r6   s    r   rJ   zBoundConstraints.xl        xr!   c                     | j         S )z|
        Upper bound.

        Returns
        -------
        `numpy.ndarray`, shape (n,)
            Upper bound.
        )rH   r6   s    r   rL   zBoundConstraints.xu   rV   r!   c                 b    t          j        |t                    }|                     |          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   r0   s     r   maxcvzBoundConstraints.maxcv   s*     Jq&&&~~a   r!   c                 n    | j         rt          j        dg          S | j                            |          S )Nr   )rN   r)   r*   rS   r[   r\   s     r   r[   zBoundConstraints.violation   s3     	)8QC== 8%%a(((r!   c                 T    | j         r t          j        || j        | j                  n|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.
        )rN   r)   cliprJ   rL   r\   s     r   projectzBoundConstraints.project   s)     04/?Frwq$'47+++QFr!   N)r<   r>   r?   r@   r    rA   rJ   rL   r]   r[   ra   r(   r!   r   rC   rC   n   s         G G G4 	 	 X	 	 	 X	! ! !") ) )G G G G Gr!   rC   c                       e Zd ZdZd Zed             Zed             Zed             Zed             Z	ed             Z
ed             Zd	 Zd
 ZdS )LinearConstraintszK
    Linear constraints ``a_ub @ x <= b_ub`` and ``a_eq @ x == b_eq``.
    c                    |rJt          |t                    sJ |D ]}t          |t                    sJ t          |t                    sJ t	          j        df          | _        t	          j        d          | _        t	          j        df          | _        t	          j        d          | _	        |D ]C}t	          j
        |j        |j        z
            t          |j        |j                  k    }t	          j        |          rgt	          j        | j        |j        |         f          | _        t	          j        | j        d|j        |         |j        |         z   z  f          | _	        t	          j        |          stt	          j        | j        |j        |          |j        |           f          | _        t	          j        | j        |j        |          |j        |           f          | _        Ed| j        t	          j        | j                  <   d| j        t	          j        | j                  <   t	          j        | j                  t	          j        | j                  z  }t	          j        | j                  }| j        | ddf         | _        | j        |          | _        | j        | ddf         | _        | j        |          | _	        f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                 l    g | ]0}|j         j        t          |t          j                            1S r(   )ArR   r	   r)   rQ   ).0cns     r   
<listcomp>z.LinearConstraints.__init__.<locals>.<listcomp>  sD     
 
 
23ach
q"'!**--
 
 
r!   )r   listr   r   r)   empty_a_ub_b_ub_a_eq_b_eqabsrG   rE   r   anyvstacka_eqrg   concatenateb_eqrM   a_ubb_ubrK   isinfrS   )r   constraintsrj   r   
constraintis_equalityundef_ubundef_eqs     `     r   r    zLinearConstraints.__init__   s     	+k400000) @ @
!*.>??????eT*****Xq!f%%
Xa[[
Xq!f%%
Xa[[
% 	 	J&
- 
z}==>K vk"" Y	:<3L'MNN
^	&M+6(mK89	 	
 6+&& Y	"k\2#{l33 
  ^	"{l3#|44 
 *-	"(49%%&),	"(49%%&8DI&&$))<)<<8DI&&Yy!!!|,
Yy)
Yy!!!|,
Yy)

 
 
 
7B
 
 
r!   c                     | j         S )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.
        )rn   r6   s    r   rx   zLinearConstraints.a_ub       zr!   c                     | j         S )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.
        )ro   r6   s    r   ry   zLinearConstraints.b_ub#  r   r!   c                     | j         S )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.
        )rp   r6   s    r   ru   zLinearConstraints.a_eq/  r   r!   c                     | j         S )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.
        )rq   r6   s    r   rw   zLinearConstraints.b_eq;  r   r!   c                     | j         j        S z
        Number of linear inequality constraints.

        Returns
        -------
        int
            Number of linear inequality constraints.
        )ry   rR   r6   s    r   m_ubzLinearConstraints.m_ubG       y~r!   c                     | j         j        S z
        Number of linear equality constraints.

        Returns
        -------
        int
            Number of linear equality constraints.
        )rw   rR   r6   s    r   m_eqzLinearConstraints.m_eqS  r   r!   c                 T    t          j        |                     |          d          S )rY   r%   initialr)   maxr[   r\   s     r   r]   zLinearConstraints.maxcv_  s%     vdnnQ''5555r!   c                     t          | j                  r%t          j        fd| j        D                       S t          j        g           S )Nc                 :    g | ]}|                               S r(   r[   rh   pcr0   s     r   rk   z/LinearConstraints.violation.<locals>.<listcomp>q  s#    "F"F"Fr2<<??"F"F"Fr!   )lenrS   r)   rv   r*   r\   s    `r   r[   zLinearConstraints.violationo  sI    tx== 	H>"F"F"F"FTX"F"F"FGGGx||r!   N)r<   r>   r?   r@   r    rA   rx   ry   ru   rw   r   r   r]   r[   r(   r!   r   rc   rc      s         B
 B
 B
H 	 	 X	 	 	 X	 	 	 X	 	 	 X	 	 	 X	 	 	 X	6 6 6     r!   rc   c                   p    e Zd ZdZd Zd Zed             Zed             Zed             Z	d
dZ
d
d	ZdS )NonlinearConstraintszI
    Nonlinear constraints ``c_ub(x) <= 0`` and ``c_eq(x) == b_eq``.
    c                 .   |rat          |t                    sJ |D ]}t          |t                    sJ t          |t                    sJ t          |t                    sJ || _        g | _        || _        d| _        d| _        dx| _	        | _
        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   rl   r   r   _constraintsrS   r   _map_ub_map_eq_m_ub_m_eq)r   r{   r   r   r|   s        r   r    zNonlinearConstraints.__init__z  s      	+k400000) C C
!*.ABBBBBBgt,,,,,eT*****' "&&
TZZZr!   c           
         t          | j                  s6dx| _        | _        t	          j        g           t	          j        g           fS t	          j        |t                    }t          | j                  sg | _        g | _	        d| _        d| _        | j        D ]k}t          |j                  s5t          j        |          }d |_        d |_        t          ||          }nt          ||          }d|j        _        | j                            |           t	          j        |j        j                  }|j        d         |j        d         }}t+          ||          }t	          j        ||z
            |k    }	| j	                            ||	                    | j                            ||	                     | xj        t	          j        |	          z  c_        | xj        t	          j        |	           z  c_        mg }
g }t1          | j                  D ]\  }}|j                            |          }| j        rt	          j        di t6          5  t9          t:                    5  | j        |         j        j        }t?          | d| d|            d	d	d	           n# 1 swxY w Y   d	d	d	           n# 1 swxY w Y   | j	        |         }| j        |         }||         }t          |          r|j        d         |         }|j        d         |         }|t          j          k    }||         ||         z
  }|
                    |           |t          j         k     }||         ||         z
  }|
                    |           ||         }t          |          r/d
|j        d         |         |j        d         |         z   z  }||z  }|                    |           | j        rt	          j!        |          }nt	          j        g           }| j        rt	          j!        |
          }
nt	          j        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 Nr(   )x0s    r   <lambda>z/NonlinearConstraints.__call__.<locals>.<lambda>  s    r r!   c                     dS )Nr%   r(   )r   vs     r   r   z/NonlinearConstraints.__call__.<locals>.<lambda>  s    3 r!   Tr
   r&   r'   Nre   r(   )#r   r   r   r   r)   r*   r+   rS   r   r   r   jaccopyhessr	   r   	f_updatedappendarangerP   rT   r   rr   rO   	enumerater   r-   r   r   r=   r<   r.   rI   rv   rR   )r   r0   r|   ri   r   idxrE   rG   arr_tolr}   c_ubc_eqivalfun_nameeq_idxub_idxub_valrJ   rL   	finite_xl_v	finite_xueq_valmidpoints                            r   r2   zNonlinearConstraints.__call__  s     4$%% 	.&''DJ8B<<"--HQe$$$48}} !	=DLDLDJDJ"/ = =

// 	; 	*--A)MAE..AF+Aq11BB+J::B $( ###i)) 1ry|B(R00 fR"Woo8##C$4555##C$5666 

b.{;;;



b.|<<<


tx((  	   	 EAr&**Q--C} :_55}55 : :!.11 : :#'#4Q#7#;#D88A88388999: : : : : : : : : : : : : : :: : : : : : : : : : : : : : : \!_F\!_F[F6{{  Yq\&)Yq\&) "&L		]VI%66B K	I&I6B [F6{{ #")A,v"61f9M"MN("KK: 	 >$''DD8B<<D: 	 >$''DD8B<<DY
Y
Tzs6    K5/J0$K0J44K7J48KK	K	c                 <    | j         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   
ValueErrorr6   s    r   r   zNonlinearConstraints.m_ub  s+     :L   :r!   c                 <    | j         t          d          | j         S )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   r6   s    r   r   zNonlinearConstraints.m_eq  s+     :J   :r!   c                 \    t          | j                  r| j        d         j        j        S dS )r5   r   )r   rS   r   nfevr6   s    r   r7   zNonlinearConstraints.n_eval/  s*     tx== 	8A;?''1r!   Nc                 Z    t          j        |                     |||          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%   r   r   r   r0   r   r   s       r   r]   zNonlinearConstraints.maxcv>  s4    ( vNN1gwN??
 
 
 	
r!   c                 N    t          j        fd| j        D                       S )Nc                 :    g | ]}|                               S r(   r   r   s     r   rk   z2NonlinearConstraints.violation.<locals>.<listcomp>W  s#    BBB2r||ABBBr!   )r)   rv   rS   r   s    `  r   r[   zNonlinearConstraints.violationV  s*    ~BBBBBBBCCCr!   NN)r<   r>   r?   r@   r    r2   rA   r   r   r7   r]   r[   r(   r!   r   r   r   u  s         ' ' ':j j jX   X*   X*   X
 
 
 
0D D D D D Dr!   r   c                      e Zd ZdZd ZddZed             Zed             Zed             Z	ed             Z
ed	             Zed
             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zed             Zd ZddZddZd ZdS )Problemz
    Optimization problem.
    c           
         |rt          |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                    sJ t          |
t                    sJ |	r|
dk    sJ t          |t                    sJ |dk    sJ t          |t                    sJ || _        || _	        || _
        |t          |          st          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        |j        z
            |k     z  | _        d
|j        | j                 |j        | j                 z   z  | _        t-          j        | j        |j        | j                 |j        | j                           | _        || _        t          t9          |j        | j                  |j        | j                                      | _        | j                            || j                            | _        |j         |j!        dd| j        f         | j        z  z
  }t          tE          |j        dd| j         f         t,          j#         |j$        |j        dd| j        f         | j        z  z
            tE          |j!        dd| j         f         ||          g| j%        |          | _	        |ok| j        j&        o_t-          j'        t-          j(        | j        j                            o/t-          j'        t-          j(        | j        j                            }|r~d
| j        j        | j        j        z
  z  | _)        d
| j        j        | j        j        z   z  | _*        t          t9          t-          j+        | j%                   t-          j+        | j%                                      | _        | j	        j         | j	        j!        | j*        z  z
  }t          tE          | j	        j        t-          j,        | j)                  z  t,          j#         | j	        j$        | j	        j        | j*        z  z
            tE          | j	        j!        t-          j,        | j)                  z  ||          g| j%        |          | _	        | j        | j*        z
  | j)        z  | _        n<t-          j+        | j%                  | _)        t-          j-        | j%                  | _*        || _.        || _/        g | _0        g | _1        g | _2        |	| _3        |
| _4        g | _5        g | _6        g | _7        d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.re   )8r   r   rC   rc   r   r+   r   int_obj_linear
_nonlinearr   	TypeError	_callbackr   rR   rJ   r   rx   shaper   rL   r)   rr   
_fixed_idx
_fixed_valr`   _orig_boundsr   _boundsra   _x0rw   ru   r   rI   ry   rj   rN   rM   isfinite_scaling_factor_scaling_shiftrQ   diagzeros_feasibility_tol_filter_size_fun_filter_maxcv_filter	_x_filter_store_history_history_size_fun_history_maxcv_history
_x_history)r   objr   rT   linear	nonlinearcallbackfeasibility_tolscalestore_historyhistory_sizefilter_sizer   rj   tolrw   s                   r   r    zProblem.__init___  s   `  	+c#455555f&677777f&788888i)=>>>>>ou55555eT*****mT22222lC00000 (#a''''k3/////????eT*****	#H%% M KLLL! B EFFG9>QBQBBBCCC;Q1$$%% % %   VY	22!9	1F69vy())C/
 Ido&4?)CC
 'OIdo&Ido&
 
 #'69do-.	4?:J0KLL
 

 <''DO+;(<== {V[DO);<tNN( KDO#3 34VGKk!!!T_"45GH  !QQQ0@-@!A4NN F
 
   5(5r{4<?33445 r{4<?3344	 	  	3#&$,/DLO*K#LD "%4<?)J"KD+'99 DL <$t|'84;N'NND,$)BGD4H,I,II),+d.AAB  %)BGD4H,I,II   DL" 4#66$:NNDHH#%746??D "$(46"2"2D !0' ,) r!   r%   c                    t          j        |t                    }|                     |          }|                     |          |                     |          \  }}|                     |||          | j        r| j        	                               | j
        	                               | j        	                    |           t          | j                  | j        k    rN| j                            d           | j
                            d           | j                            d           t          j                  r-t          j                  rt          | j                  dk    }nt          j                  r4t#          fdt%          | j        | j                  D                       }n|t          j                  r4t#          fdt%          | j        | j                  D                       }n4t#          fdt%          | j        | j                  D                       }|r| j        	                               | j        	                               | j        	                    |           t+          t          | j                  dz
  dd          D ]}t          j                  r t          j        | j        |                   }nt          j                  r t          j        | j        |                   }n`t          j        | j        |                   p@t          j        | j        |                   p!| j        |         k    o| j        |         k    }|rN| j                            |           | j                            |           | j                            |           t          | j                  | j        k    rN| j                            d           | j                            d           | j                            d           | j        t1          | j                  }		 |                     |          \  }
}}|                     |
          }
t5          |	j                  d	hk    r(t9          |
|
          }|                     |           n|                     |
           n# t:          $ r}t<          |d}~ww xY wt          j                  rt>          t>          |t          j        |          <   t>          |t          j        |          <   tA          tC          t>                    t>                     t          j"        t          j#        |t>                    t>                     }t          j"        t          j#        |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   z   K   | ]5\  }}t          j        |          r|k     pt          j        |          V  6d S r   r)   rK   )rh   
fun_filtermaxcv_filter	maxcv_vals      r   	<genexpr>z#Problem.__call__.<locals>.<genexpr>7  sf           -J $$ -,*8L))           r!   c              3   z   K   | ]5\  }}t          j        |          r|k     pt          j        |          V  6d S r   r   )rh   r   r   fun_vals      r   r   z#Problem.__call__.<locals>.<genexpr>A  sf           -J && )j((8J''           r!   c              3   6   K   | ]\  }}|k     p|k     V  d S r   r(   )rh   r   r   r   r   s      r   r   z#Problem.__call__.<locals>.<genexpr>K  sJ          ,J *$@	L(@           r!      Nintermediate_result)r0   r   )r   )$r)   rZ   r+   build_xr   r   r]   r   r   r   r   r   r   r   poprK   r   rM   zipr   r   ranger   r   r   	best_evalset
parametersr   StopIterationr   r   r   minmaximumminimum)r   r0   penaltyx_fullr   r   include_pointkremove_pointsigx_bestfun_best_r   excr   r   s                  @@r   r2   zProblem.__call__
  s   6 Jq&&&a))F##??622JJq'733	 	'$$W---&&y111O""1%%%4$%%(:::!%%a(((#''***##A&&& 8G 	)!4!4 	 011Q6MMXg 	         14$&1 1	       MM Xi   	         14$&1 1	       MM            03$&1 1       M  	&##G,,,%%i000N!!!$$$
 3t/0014b"== * *8G$$ 
#%8D,<Q,?#@#@LLXi(( #%8D,>q,A#B#BLL !1!!455 ?8D$6q$9::?"d&6q&99 ?%);A)>>	 !   *$((+++&**1---N&&q))) 4#$$t'888 $$Q'''"&&q)))""1%%% >%DN++C/&*nnW&=&=#!f--s~&&+@*AAA*8 $+ + +'
 NN7JNKKKKNN6***  / / /%3./ 8G 	G%,!!"%,!!"c'7++gX66*RZ99G8DD*RZ99G8DD((s   BS 
S3&S..S3c                     | j         j        S )zt
        Number of variables.

        Returns
        -------
        int
            Number of variables.
        )r   rR   r6   s    r   rj   z	Problem.n  s     w|r!   c                     | j         j        S )z
        Number of variables in the original problem (with fixed variables).

        Returns
        -------
        int
            Number of variables in the original problem (with fixed variables).
        )r   rR   r6   s    r   n_origzProblem.n_orig  s     ##r!   c                     | j         S )z
        Initial guess.

        Returns
        -------
        `numpy.ndarray`, shape (n,)
            Initial guess.
        )r   r6   s    r   r   z
Problem.x0  rV   r!   c                     | j         j        S r4   )r   r7   r6   s    r   r7   zProblem.n_eval  s     yr!   c                     | j         j        S )r:   )r   r/   r6   s    r   r   zProblem.fun_name  r   r!   c                     | j         S )z}
        Bound constraints.

        Returns
        -------
        BoundConstraints
            Bound constraints.
        )r   r6   s    r   rT   zProblem.bounds  r8   r!   c                     | j         S )z
        Linear constraints.

        Returns
        -------
        LinearConstraints
            Linear constraints.
        )r   r6   s    r   r   zProblem.linear  r8   r!   c                     | j         j        S )z
        Number of bound constraints.

        Returns
        -------
        int
            Number of bound constraints.
        )rT   rP   r6   s    r   m_boundszProblem.m_bounds  s     {}r!   c                     | j         j        S r   )r   r   r6   s    r   m_linear_ubzProblem.m_linear_ub       {r!   c                     | j         j        S r   )r   r   r6   s    r   m_linear_eqzProblem.m_linear_eq  r  r!   c                     | j         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 not known.
        )r   r   r6   s    r   m_nonlinear_ubzProblem.m_nonlinear_ub       ##r!   c                     | j         j        S )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   r   r6   s    r   m_nonlinear_eqzProblem.m_nonlinear_eq  r$  r!   c                 B    t          j        | j        t                    S )z
        History of objective function evaluations.

        Returns
        -------
        `numpy.ndarray`, shape (n_eval,)
            History of objective function evaluations.
        r#   )r)   r*   r   r+   r6   s    r   fun_historyzProblem.fun_history(  s     x)7777r!   c                 B    t          j        | j        t                    S )z
        History of maximum constraint violations.

        Returns
        -------
        `numpy.ndarray`, shape (n_eval,)
            History of maximum constraint violations.
        r#   )r)   r*   r   r+   r6   s    r   maxcv_historyzProblem.maxcv_history4  s     x+59999r!   c                     	 | j         dk    s| j        dk    rdS | j        dk    s| j        dk    rdS | j        dk    rdS dS # t
          $ r Y dS w xY 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   r6   s    r   typezProblem.type@  s    	-"Q&&$*=*A*A00!A%%)9A)=)=--""**& 	- 	- 	- -,,	-s   A A A 
AAc                     | j         dk    S )z
        Whether the problem is a feasibility problem.

        Returns
        -------
        bool
            Whether the problem is a feasibility problem.
        r;   )r   r6   s    r   is_feasibilityzProblem.is_feasibility^  s     }""r!   c                     t          j        | j                  }| j        || j        <   || j        z  | j        z   || 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)   rm   r  r   r   r   r   r   ra   )r   r0   r
  s      r   r   zProblem.build_xj  s\     $+&&"&/t$%(<$<&*&9%:  ((000r!   Nc                     |                      |||          }t          j        |          rt          j        |d          S dS r   )r[   r)   rO   r   )r   r0   r   r   r[   s        r   r]   zProblem.maxcv~  sH    ( NN1gwNGG	I&& 	6)S11113r!   c                    g }| j         j        s/| j                             |          }|                    |           t	          | j        j                  r/| j                            |          }|                    |           t	          | j        j                  r1| j                            |||          }|                    |           t	          |          rt          j	        |          S d S r   )
rT   rN   r[   r   r   r   rS   r   r)   rv   )r   r0   r   r   r[   blcnlcs           r   r[   zProblem.violation  s    	{& 	 %%a((AQt{ 	!&&q))BR   t"## 	"/++Aw@@CS!!!y>> 	->),,,	- 	-r!   c                    t          | j                  dk    r | | j                   t          j        | j                  }t          j        | j                  }t          j        | j                  }t          j        |          }t          j        |          r&|| j	        k    }t          j        |          rt          j
        t          j        ||                             sv||t          j        ||                   k    z  }t          j        |          dk    r!||t          j        ||                   k    z  }t          j        |          d         }nt          j        |          rt          j        |          d         }nt          j        |t          j                  }	||         |||         z  z   |	|<   t          j
        t          j        |	                    r4|t          j        |          k    }
t          j        |
          d         }n|	t          j        |	          k    }t          j        |          dk    r!||t          j        ||                   k    z  }t          j        |          dk    r!||t          j        ||                   k    z  }t          j        |          d         }nkt          j
        t          j        |                    s3|t          j        |          k    }t          j        |          d         }nt          |          dz
  }| 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   rs   r   rM   rK   nanminrO   r  flatnonzero	full_likenanrT   ra   )r   r	  r   r   x_filter
finite_idxfeasible_idxfun_min_idxr   merit_filtermin_maxcv_idxmerit_min_idxs               r   r  zProblem.best_eval  s%   , t  A%%DMMM Xd.//
x 2338DN++[..
6* E	$'4+@@Lvl## 7:BFL122- - 7: +")J|,D"E"EE #K00144<26$[14 4 $ K N;//3%% ': N<004  "|J??z*W|J7O-OO Z( 6"(<0011 : %1BIl4K4K$KM}55b9AA %1BIl4K4K$KM'66::%(7: : *  '66::%rv&}58 8 *  }55b9AA,,-- 
	$
 %	*(=(==K{++B/AA J!#AKAAA//qMO
 	
r!   )r%   r   )r<   r>   r?   r@   r    r2   rA   rj   r  r   r7   r   rT   r   r  r  r!  r#  r&  r(  r*  r-  r/  r   r]   r[   r  r(   r!   r   r   r   Z  sE        i i iVB) B) B) B)H 	 	 X	 	$ 	$ X	$ 	 	 X	 	  	  X	  	 	 X	 	 	 X	 	 	 X	 	 	 X	 	  	  X	  	  	  X	  $ $ X$  $ $ X$  	8 	8 X	8 	: 	: X	: - - X-: 	# 	# X	#1 1 1(   4- - - - h
 h
 h
 h
 h
r!   r   )
contextlibr   inspectr   r   numpyr)   scipy.optimizer   r   r   r   scipy.optimize._constraintsr	   settingsr   r   utilsr   r   r   r   rC   rc   r   r   r(   r!   r   <module>rI     s                               ; : : : : : - , , , , , , , 2 2 2 2 2 2 2 2 ! ! ! ! ! !W W W W W W W Wt]G ]G ]G ]G ]G ]G ]G ]G@d d d d d d d dNbD bD bD bD bD bD bD bDJv

 v

 v

 v

 v

 v

 v

 v

 v

 v

r!   