
    4[g                         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y)    )suppress)	signatureN)BoundsLinearConstraintNonlinearConstraintOptimizeResult)PreparedConstraint   )PRINT_OPTIONSBARRIER)CallbackSuccessget_arrays_tol)exact_1d_arrayc                   <    e Zd ZdZd Zd Zed        Zed        Zy)ObjectiveFunctionz)
    Real-valued objective function.
    c                     |r3|t        |      sJ t        |t              sJ t        |t              sJ || _        || _        || _        d| _        y)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        T/var/www/html/bid-api/venv/lib/python3.12/site-packages/scipy/_lib/cobyqa/problem.py__init__zObjectiveFunction.__init__   sT    & ;(3-//gt,,,eT***	
    c                    t        j                  |t              }| j                  d}|S t        t        j                   | j                  |g| j
                               }| xj                  dz  c_        | j                  rAt        j                  di t        5  t        | j                   d| d|        ddd       |S |S # 1 sw Y   |S xY w)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     HHQe$99A  bjj1!:tzz!:;<ALLAL}}__5}5TYYKq4s34 6q 6s   C		C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                  }|S |S # t        $ r d}Y |S w xY w)
        Name of the objective function.

        Returns
        -------
        str
            Name of the objective function.
         r   )r   __name__AttributeError)r   r/   s     r   r/   zObjectiveFunction.name[   sM     99 yy)) t " s   * 99N)	r<   
__module____qualname____doc__r    r2   propertyr7   r/   r(   r!   r   r   r      s9    :2 	 	  r!   r   c                   H    e Zd ZdZd Zed        Zed        Zd Zd Z	d Z
y)	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        xrc t        j                  | j                  t         j                  k        xr1 t        j                  | j                  t         j                   kD        | _        t        j                  | j                  t         j                   kD        t        j                  | j                  t         j                  k        z   | _        t        |t        j                   |j                  j"                              | _        y)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   s9    88FIIu-88FIIu- ')ffW!"%'VV!" FF477dgg%& *tww'(*tww"&&() 	
 !!$''RVVG"34r7G7GGGbff8
 
 &fbggfiinn.EFr!   c                     | j                   S )z|
        Lower bound.

        Returns
        -------
        `numpy.ndarray`, shape (n,)
            Lower bound.
        )rF   r6   s    r   rJ   zBoundConstraints.xl        xx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                 Z    t        j                  |t              }| j                  |      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#     JJq&~~a  r!   c                 |    | j                   rt        j                  dg      S | j                  j	                  |      S )Nr   )rN   r)   r*   rS   r[   r\   s     r   r[   zBoundConstraints.violation   s0    88QC= 88%%a((r!   c                 t    | j                   r+t        j                  || 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.
        )rN   r)   cliprJ   rL   r\   s     r   projectzBoundConstraints.project   s,     04/?/?rwwq$''477+FQFr!   N)r<   r>   r?   r@   r    rA   rJ   rL   r]   r[   ra   r(   r!   r   rC   rC   n   sE    G4 	 	 	 	!")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y)LinearConstraintszK
    Linear constraints ``a_ub @ x <= b_ub`` and ``a_eq @ x == b_eq``.
    c           	         |r=t        |t              sJ |D ]  }t        |t              rJ  t        |t              sJ t	        j
                  d|f      | _        t	        j
                  d      | _        t	        j
                  d|f      | _        t	        j
                  d      | _	        |D ]|  }t	        j                  |j                  |j                  z
        t        |j                  |j                        k  }t	        j                  |      ryt	        j                  | j                   |j"                  |   f      | _        t	        j$                  | j&                  d|j                  |   |j                  |   z   z  f      | _	        t	        j(                  |      rt	        j                  | j*                  |j"                  |    |j"                  |     f      | _        t	        j$                  | j,                  |j                  |    |j                  |     f      | _         d| j*                  t	        j.                  | j*                        <   d| j                   t	        j.                  | j                         <   t	        j.                  | j,                        t	        j0                  | j,                        z  }t	        j.                  | j&                        }| j*                  | ddf   | _        | j,                  |    | _        | j                   | ddf   | _        | j&                  |    | _	        |D cg c]8  }|j"                  j2                  st5        |t	        j6                  |            : c}| _        yc c}w )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%   N)r   listr   r   r)   empty_a_ub_b_ub_a_eq_b_eqabsrG   rE   r   anyvstacka_eqAconcatenateb_eqrM   a_ubb_ubrK   isinfrR   r	   rQ   rS   )	r   constraintsnr   
constraintis_equalityundef_ubundef_eqcs	            r   r    zLinearConstraints.__init__   s    k4000)
!*.>??? *eT***XXq!f%
XXa[
XXq!f%
XXa[
%J&&
-
z}}=>K vvk"YY		:<<3L'MN
^^		&MM+6(mmK89	
 66+&YY		"k\2#{l33
  ^^		"{l3#|44
1 &B *-		"((499%&),		"((499%&88DII&$)))<<88DII&YYy!|,
YYy)
YYy!|,
YYy)
7B
7B!acchhq"''!*-{
 
s   N
 !N
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.
        )rh   r6   s    r   rs   zLinearConstraints.a_ub       zz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.
        )ri   r6   s    r   rt   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.
        )rj   r6   s    r   ro   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.
        )rk   r6   s    r   rr   zLinearConstraints.b_eq;  r~   r!   c                 .    | j                   j                  S z
        Number of linear inequality constraints.

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

        Returns
        -------
        int
            Number of linear equality constraints.
        )rr   rR   r6   s    r   m_eqzLinearConstraints.m_eqS  r   r!   c                 N    t        j                  | j                  |      d      S )rY   r%   initialr)   maxr[   r\   s     r   r]   zLinearConstraints.maxcv_  s     vvdnnQ'55r!   c                     t        | j                        r<t        j                  | j                  D cg c]  }|j	                  |       c}      S t        j
                  g       S c c}w N)lenrS   r)   rq   r[   r*   )r   r0   pcs      r   r[   zLinearConstraints.violationo  sK    txx=>>TXX"FXr2<<?X"FGGxx| #Gs   A'N)r<   r>   r?   r@   r    rA   rs   rt   ro   rr   r   r   r]   r[   r(   r!   r   rc   rc      s    B
H 	 	 	 	 	 	 	 	 	 	 	 	6 r!   rc   c                   \    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y)NonlinearConstraintszI
    Nonlinear constraints ``c_ub(x) <= 0`` and ``c_eq(x) == b_eq``.
    c                    |rOt        |t              sJ |D ]  }t        |t              rJ  t        |t              sJ t        |t              sJ || _        g | _        || _        d| _        d| _        dx| _	        | _
        y)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   rf   r   r   _constraintsrS   r   _map_ub_map_eq_m_ub_m_eq)r   rv   r   r   rx   s        r   r    zNonlinearConstraints.__init__z  s     k4000)
!*.ABBB *gt,,,eT***' "&&
TZr!   c           
       	   t        | j                        s8dx| _        | _        t	        j
                  g       t	        j
                  g       fS t	        j
                  |t              }t        | j                        sg | _        g | _	        d| _        d| _        | j                  D ]~  }t        |j                        s2t        j                  |      }d |_        d |_        t        ||      }nt        ||      }d|j                  _        | j                  j#                  |       t	        j$                  |j                  j&                        }|j(                  d   |j(                  d   }}t+        ||      }t	        j,                  ||z
        |k  }	| j                  j#                  ||	          | j                  j#                  ||	           | xj                  t	        j.                  |	      z  c_        | xj                  t	        j.                  |	       z  c_         g }
g }t1        | j                        D ]  \  }}|j                  j                  |      }| j2                  rpt	        j4                  di t6        5  t9        t:              5  | j                  |   j                  j<                  }t?        | d| d|        d	d	d	       d	d	d	       | j                  |   }| j                  |   }||   }t        |      r|j(                  d   |   }|j(                  d   |   }|t        j@                   kD  }||   ||   z
  }|
j#                  |       |t        j@                  k  }||   ||   z
  }|
j#                  |       ||   }t        |      r-d
|j(                  d   |   |j(                  d   |   z   z  }||z  }|j#                  |        | j                  rt	        jB                  |      }nt	        j
                  g       }| j                  rt	        jB                  |
      }
nt	        j
                  g       }
|
jD                  | _        |jD                  | _        |
|fS # 1 sw Y   xY w# 1 sw Y   xY w)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 r   r(   )x0s    r   <lambda>z/NonlinearConstraints.__call__.<locals>.<lambda>  s    rr!   c                      y)Nr%   r(   )r   vs     r   r   z/NonlinearConstraints.__call__.<locals>.<lambda>  s    3r!   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   rl   rO   	enumerater   r-   r   r   r=   r<   r.   rI   rq   rR   )r   r0   rx   r|   r   idxrE   rG   arr_tolry   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88B<"--HHQe$488}DLDLDJDJ"//

/ 		*-A)AE.AF+Aq1B+J:B $( #ii) 1ryy|B(R0 ffR"Wo8##C$45##C$56 

b..{;;


b..|<<
7 0: txx(EAr&&**Q-C}}__5}5!.1#'#4#4Q#7#;#;#D#D
!A3d3%89 2 6 \\!_F\\!_F[F6{YYq\&)YYq\&) "&&L		]VI%66B K	I&I6B [F6{"))A,v"61f9M"MN("KKA )D ::>>$'D88B<D::>>$'D88B<DYY
YY
TzW 21 65s$   	R7Q6R6R ;RR	c                 H    | 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.
        z:The number of nonlinear inequality constraints is unknown.)r   
ValueErrorr6   s    r   r   zNonlinearConstraints.m_ub  s*     ::L  ::r!   c                 H    | 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.
        z8The number of nonlinear equality constraints is unknown.)r   r   r6   s    r   r   zNonlinearConstraints.m_eq  s*     ::J  ::r!   c                 t    t        | j                        r#| j                  d   j                  j                  S y)r5   r   )r   rS   r   nfevr6   s    r   r7   zNonlinearConstraints.n_eval/  s*     txx=88A;??'''r!   Nc                 T    t        j                  | 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>  s)    ( vvNN1gwN?
 	
r!   c                     t        j                  | j                  D cg c]  }|j                  |       c}      S c c}w r   )r)   rq   rS   r[   )r   r0   r   r   r   s        r   r[   zNonlinearConstraints.violationV  s/    ~~B2r||ABCCBs   =NN)r<   r>   r?   r@   r    r2   rA   r   r   r7   r]   r[   r(   r!   r   r   r   u  sZ    ':jX  *  *  
0Dr!   r   c                   8   e Zd 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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y)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kD  sJ t        |t              sJ |dkD  sJ t        |t              sJ || _        || _	        || _
        |t        |      st        d      || _        t        |d      }|j                  }|j                   j                  |k7  rt#        d| d      |j$                  j&                  d   |k7  rt#        d| d	      t)        |j                   |j*                        }|j                   |j*                  k  t-        j.                  |j                   |j*                  z
        |k  z  | _        d
|j                   | j0                     |j*                  | j0                     z   z  | _        t-        j4                  | j2                  |j                   | j0                     |j*                  | j0                           | _        || _        t        t9        |j                   | j0                      |j*                  | j0                                  | _        | j:                  j=                  || j0                            | _        |j@                  |jB                  dd| j0                  f   | j2                  z  z
  }t        tE        |j$                  dd| j0                   f   t,        jF                   |jH                  |j$                  dd| j0                  f   | j2                  z  z
        tE        |jB                  dd| j0                   f   ||      g| jJ                  |      | _	        |xr | j:                  jL                  xrz t-        jN                  t-        jP                  | j:                  j                               xr< t-        jN                  t-        jP                  | j:                  j*                              }|rd
| j:                  j*                  | j:                  j                   z
  z  | _)        d
| j:                  j*                  | j:                  j                   z   z  | _*        t        t9        t-        jV                  | jJ                         t-        jV                  | jJ                                    | _        | j                  j@                  | j                  jB                  | jT                  z  z
  }t        tE        | j                  j$                  t-        jX                  | jR                        z  t,        jF                   | j                  jH                  | j                  j$                  | jT                  z  z
        tE        | j                  jB                  t-        jX                  | jR                        z  ||      g| jJ                  |      | _	        | j>                  | jT                  z
  | jR                  z  | _        nHt-        jV                  | jJ                        | _)        t-        jZ                  | jJ                        | _*        || _.        || _/        g | _0        g | _1        g | _2        |	| _3        |
| _4        g | _5        g | _6        g | _7        y)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   rs   shaper   rL   r)   rl   
_fixed_idx
_fixed_valr`   _orig_boundsr   _boundsra   _x0rr   ro   r   rI   rt   rw   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   rw   tolrr   s                   r   r    zProblem.__init___  s   ` c#4555f&6777f&7888i)=>>>ou555eT***mT222lC000#a'''k3///?"?eT***	#H% KLL! B EFGG99>>Q4QCzBCC;;Q1$s)%  VYY		2!99		1FF699vyy()C/
 IIdoo&4??)CC
 ''OOIIdoo&IIdoo&
 #'699doo-.		4??:J0KL

 <<''DOO+;(<= {{V[[DOO);<tNN( KKDOO#3 34VVGKKkk!T__"45GH !Q0@-@!A4N FF
   5((5r{{4<<??345 r{{4<<??34	 	 #&$,,//DLLOO*K#LD "%4<<??)J"KD+'9DL <<$$t||'8'84;N;N'NND,$))BGGD4H4H,II)),,++d.A.AAB %))BGGD4H4H,II DL" 4#6#66$:N:NNDH#%77466?D "$((466"2D !0' ,) r!   c                    t        j                  |t              }| j                  |      }| j	                  |      | j                  |      \  }}| j                  |||      | j                  r| j                  j                         | j                  j                         | j                  j                  |       t        | j                        | j                  kD  rQ| j                  j                  d       | j                  j                  d       | j                  j                  d       t        j                        r.t        j                        rt        | j                         dk(  }nt        j                        r3t#        fdt%        | j                   | j&                        D              }n{t        j                        r3t#        fdt%        | j                   | j&                        D              }n3t#        fdt%        | j                   | j&                        D              }|r | j                   j                         | j&                  j                         | j(                  j                  |       t+        t        | j                         dz
  dd      D ]5  }t        j                        r#t        j                  | j                   |         }nt        j                        r#t        j                  | j&                  |         }nnt        j                  | j                   |         xsJ t        j                  | j&                  |         xs& | j                   |   k  xr | j&                  |   k  }|s| j                   j                  |       | j&                  j                  |       | j(                  j                  |       8 t        | j                         | j,                  kD  rQ| j                   j                  d       | j&                  j                  d       | j(                  j                  d       | j.                  `t1        | j.                        }	 t3        |j4                        d	hk(  r t7        |
      }	| j/                  |	       n| j/                  |       t        j                        rt<        t<        |t        j                  |      <   t<        |t        j                  |      <   t?        tA        t<              t<               t        jB                  t        jD                  |t<              t<               }t        jB                  t        jD                  |t<              t<               }||fS # t8        $ r}
t:        |
d}
~
ww xY w)a  
        Evaluate the objective and nonlinear constraint functions.

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

        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      K   | ]:  \  }}t        j                  |      xr |k  xs t        j                  |       < y wr   r)   rK   ).0
fun_filtermaxcv_filter	maxcv_vals      r   	<genexpr>z#Problem.__call__.<locals>.<genexpr>4  sN       1,J $ -,*88L)*1   A Ac              3      K   | ]:  \  }}t        j                  |      xr |k  xs t        j                  |       < y wr   r   )r   r   r   fun_vals      r   r   z#Problem.__call__.<locals>.<genexpr>>  sN       1,J & )j((88J'(1r   c              3   <   K   | ]  \  }}|k  xs |k    y wr   r(   )r   r   r   r   r   s      r   r   z#Problem.__call__.<locals>.<genexpr>H  s4       1,J *$@	L(@@1s      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   set
parametersr   StopIterationr   r   r   minmaximumminimum)r   r0   x_fullr   r   include_pointkremove_pointsigr   excr   r   s              @@r   r2   zProblem.__call__
  sz   0 JJq&a))F#??62JJq'73	$$W-&&y1OO""1%4$$%(:(::!!%%a(##''*##A& 88G)!4 0 01Q6MXXg   14$$&&1	  M XXi    14$$&&1	  M    03$$&&1  M ##G,%%i0NN!!!$
 3t//014b"=88G$#%88D,<,<Q,?#@LXXi(#%88D,>,>q,A#BL !1!1!!45 ?88D$6$6q$9:?"d&6&6q&99 ?%););A)>>	 !  $$((+&&**1-NN&&q) >$ 4##$t'8'88  $$Q'""&&q)""1% >>%DNN+C/s~~&+@*AA*86w*O'NN7JNKNN6*
 88GG%,!"%,!"c'7+gX6**RZZ9G8D**RZZ9G8D(( ! /%3./s   A
V) )	V>2V99V>c                 .    | j                   j                  S )zt
        Number of variables.

        Returns
        -------
        int
            Number of variables.
        )r   rR   r6   s    r   rw   z	Problem.n  s     ww||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     yy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                 L    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     xx))77r!   c                 L    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_history+  s     xx++599r!   c                     	 | j                   dkD  s| j                  dkD  ry| j                  dkD  s| j                  dkD  ry| j                  dkD  ryy# t
        $ r Y y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.type7  si    	-""Q&$*=*=*A0!!A%)9)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_feasibilityU  s     }}""r!   c                     t        j                  | j                        }| j                  || j                  <   || j
                  z  | j                  z   || 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)   rg   r
  r   r   r   r   r   ra   )r   r0   r  s      r   r   zProblem.build_xa  si     $++&"&//t$%(<(<$<&*&9&9%:   ((00r!   Nc                     | j                  |||      }t        j                  |      rt        j                  |d      S yr   )r[   r)   rO   r   )r   r0   r   r   r[   s        r   r]   zProblem.maxcvu  s:    ( NN1gwNG	I&66)S11r!   c                    g }| j                   j                  s,| j                   j                  |      }|j                  |       t	        | j
                  j                        r,| j
                  j                  |      }|j                  |       t	        | j                  j                        r.| j                  j                  |||      }|j                  |       t	        |      rt        j                  |      S y r   )
rT   rN   r[   r   r   r   rS   r   r)   rq   )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                  ||               sn||t        j                  ||         k  z  }t        j                  |      dkD  r||t        j                  ||         k  z  }t        j                  |      d   }nt        j                  |      rt        j                  |      d   }nt        j                   |t        j"                        }	||   |||   z  z   |	|<   t        j                  t        j                  |	            r2|t        j                  |      k  }
t        j                  |
      d   }n|	t        j                  |	      k  }t        j                  |      dkD  r||t        j                  ||         k  z  }t        j                  |      dkD  r||t        j                  ||         k  z  }t        j                  |      d   }ngt        j                  t        j                  |            s1|t        j                  |      k  }t        j                  |      d   }nt        |      dz
  }| j$                  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   rm   r   rM   rK   nanminrO   r   flatnonzero	full_likenanrT   ra   )r   penaltyr   r   x_filter
finite_idxfeasible_idxfun_min_idxr   merit_filtermin_maxcv_idxmerit_min_idxs               r   	best_evalzProblem.best_eval  s   , t A%M XXd../
xx 2 2388DNN+[[.
66*'4+@+@@Lvvl#BFFL12- +"))J|,D"EE ##K014<266$[14 $ K NN;/3% NN<04  "||J?z*W|J7O-OO Z( 66"((<01 %1BIIl4K$KM}5b9A %1BIIl4K$KM''6:%(7: *  ''6:%rvv&}58 *  }5b9A,-
 %		*(==K{+B/A J!#AKKA/qMO
 	
r!   r   )r<   r>   r?   r@   r    r2   rA   rw   r
  r   r7   r   rT   r   r  r  r  r  r  r  r  r"  r$  r   r]   r[   r8  r(   r!   r   r   r   Z  sj   iVy)v 	 	 	$ 	$ 	 	 	  	  	 	 	 	 	 	 	 	 	  	  	  	  $ $  $ $  	8 	8 	: 	: - -: 	# 	#1(4- 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>r@     so         ; - 2 !W Wt]G ]G@d dNbD bDJm

 m

r!   