
    Phah                        d dl Z d dlZd dlmZ ddlmZ ddlmZm	Z	m
Z
mZmZ ddlmZ ddlmZ ddlmZ d	d
lmZ d	dlmZmZ d	dlmZ d	dlmZmZmZ ddddddZ G d d      Z G d d      Zd Zd Z	 	 	 	 	 	 	 	 	 	 ddZ y)    N)LinearOperator   )VectorFunction)NonlinearConstraintLinearConstraintPreparedConstraintBoundsstrict_bounds)BFGS)OptimizeResult)ScalarFunction   )equality_constrained_sqp)CanonicalConstraint initial_constraints_as_canonical)tr_interior_point)BasicReport	SQPReportIPReportz7The maximum number of function evaluations is exceeded.z*`gtol` termination condition is satisfied.z*`xtol` termination condition is satisfied.z*`callback` function requested termination.z#Constraint violation exceeds 'gtol')r   r   r         c                       e Zd ZdZd Zd Zy)HessianLinearOperatorzBuild LinearOperator from hesspc                      || _         || _        y N)hesspn)selfr   r   s      C:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\scipy/optimize/_trustregion_constr/minimize_trustregion_constr.py__init__zHessianLinearOperator.__init__   s    
    c                 \      fd}t         j                   j                  f|      S )Nc                 ,     j                   | g S r   )r   )pargsr   xs    r   matvecz.HessianLinearOperator.__call__.<locals>.matvec!   s    4::a*T**r!   )r'   )r   r   )r   r&   r%   r'   s   ``` r   __call__zHessianLinearOperator.__call__    s$    	+ tvvtvv.v>>r!   N__name__
__module____qualname____doc__r    r(    r!   r   r   r      s    )?r!   r   c                       e Zd ZdZd ZddZy)LagrangianHessianzThe Hessian of the Lagrangian as LinearOperator.

    The Lagrangian is computed as the objective function plus all the
    constraints multiplied with some numbers (Lagrange multipliers).
    c                 .    || _         || _        || _        y r   )r   objective_hessconstraints_hess)r   r   r2   r3   s       r   r    zLagrangianHessian.__init__-   s    , 0r!   Nc                     |t        j                  d      }| j                  |      | j                  |||      fd}t	        | j
                  | j
                  f|      S )Nr   c                 J    j                  |       j                  |       z   S r   )dot)r$   H_constraintsH_objectives    r   r'   z*LagrangianHessian.__call__.<locals>.matvec8   s"    ??1%(9(9!(<<<r!   )npemptyr2   r3   r   r   )r   r&   v_eqv_ineqr'   r7   r8   s        @@r   r(   zLagrangianHessian.__call__2   sZ    >XXa[F))!,--av>	= tvvtvv.77r!   r   r)   r.   r!   r   r0   r0   '   s    
1
	8r!   r0   c	           	         | xj                   dz  c_         |j                  | _        |j                  | _        |j                  | _        |D 	cg c]4  }	t        |	j                  t              r|	j                  j                  nd6 c}	| _        |D 	cg c]4  }	t        |	j                  t              r|	j                  j                  nd6 c}	| _	        |D 	cg c]4  }	t        |	j                  t              r|	j                  j                  nd6 c}	| _
        |s|| _        |j                  | _        |j                  | _        |D 	cg c]  }	|	j                  j                   c}	| _        |D 	cg c]  }	|	j                  j                   c}	| _        |D 	cg c]  }	|	j                  j"                   c}	| _        t'        j(                  | j                        | _        |D ]X  }	| xj*                  |	j                  j"                  j,                  j/                  |	j                  j                        z  c_        Z t&        j0                  j3                  | j*                  t&        j4                        | _        d| _        t;        t=        |            D ]v  }
||
   j>                  \  }}| j                   |
   }	t'        j@                  | j8                  t'        j@                  ||	z
        t'        j@                  |	|z
        g      | _        x tC        jB                         |z
  | _"        || _#        || _$        | xjJ                  |d   z  c_%        |d   | _&        | S c c}	w c c}	w c c}	w c c}	w c c}	w c c}	w )Nr   r   niter	stop_cond)'nitnfevngevnjevnhev
isinstancefunr   constr_nfevconstr_njevconstr_nhevr&   fggradvconstrJjacr9   copylagrangian_gradTr6   linalgnorminf
optimalityconstr_violationrangelenboundsmaxtimeexecution_time	tr_radiusconstr_penaltycg_nitercg_stop_cond)stater&   last_iteration_failed	objectiveprepared_constraints
start_timer_   r`   cg_infocilbubs                r   update_state_sqprm   >   s   	IINIEJEJEJ"68"6Q (2!%%'HaO"68E #78"6Q (2!%%'HaO"68E #78"6Q (2!%%'HaO"68E !KK	[[
$89$8q15577$89)=>)=A)=>&:;&:QUUWW&:;	 "

 3%A!!QUUWWYY]]15577%;;! &99>>%*?*?H!"s/01A)!,33FBQA%'VVU-C-C-/VVBF^-/VVAF^-= &>E" 2  99;3EEO)E	NNgg&&N -ELE888 :>;s$   9M9M$9M)M.1M3M8c                 H    t        | ||||||||	      } |	| _        |
| _        | S r   )rm   barrier_parameterbarrier_tolerance)rc   r&   rd   re   rf   rg   r_   r`   rh   ro   rp   s              r   update_state_iprq   i   s:     UA'<i1:y+W6E 0E/ELr!   c                   	
)*+, t        j                  |      j                  t              }t        j                  |      }|"t        |      rt        ||      }n
t               }|rdk(  rd|Yt        j                  |j                  t         j                   |j                  t         j                   kD        }t        j                  |j                  t         j                  |j                  t         j                  k        }t        j                  t        j                  |j                        ||j                        }t        j                  t        j                  |j                        ||j                        }t        |||j                        }t!        |j                  |j                  |j                  |      }n!t         j                   t         j                  f}t#        | ||||||      *t%        |t&        t(        z        r|g}|D cg c]  }t+        ||||       c}+t-        d +D              }d|cxk  rt/        +      k  rt1        d       +r|dkD  }| |d}+j3                  t+        |||             t5        |+|      \  }}}}+D cg c]  }t7        j8                  |       } }t/        |       dk(  rt7        j:                  |      )n*t/        |       dk(  r| d   )nt7        j<                  | |      )t?        |*j@                  )j@                        }!)jB                  dk(  rd}"nd	}"tE        d+i d
dddddddddddd*jF                  d*jH                  dt        jJ                  *jH                        d+D cg c]  }|jL                  jF                   c}d+D cg c]  }|jL                  jN                   c}d+D cg c]  }d c}d+D cg c]  }d c}d+D cg c]  }d c}d+D cg c]  }|jL                  jP                   c}d|"}#tS        jR                         ,|"dk(  r	*+,fd}$n|"d	k(  r
	*+,f	d}$dk(  rtU        jV                          n8dkD  r3|"dk(  rtY        jV                          n|"d	k(  rt[        jV                          |"dk(  r;)*fd}%)*fd}&t]        |%|&|!|*jF                  *jH                  ||$|#|||      \  }'}(n|"d	k(  rt_        *jL                  *j`                  |!|)jB                  )jb                  )jL                  )jd                  |*jF                  *jH                  ||||$)j                  |#||||||      \  }'}((jf                  dv r|#jh                  	kD  rd |(_3        |(jf                  dv rdnd!|(_5        tl        |(jf                     |(_7        |(jp                  |(_9        dk(  rtU        jt                          n8dkD  r3|"dk(  rtY        jt                          n|"d	k(  rt[        jt                          dk\  rrtw        |(jn                         tw        d"|(jp                   d#|(jx                   d$|(jz                   d%|(j|                  d&d'|(jh                  d&d(|(j~                  d)d*       |(S c c}w c c}w c c}w c c}w c c}w c c}w c c}w c c}w ),a&  Minimize a scalar function subject to constraints.

    Parameters
    ----------
    gtol : float, optional
        Tolerance for termination by the norm of the Lagrangian gradient.
        The algorithm will terminate when both the infinity norm (i.e., max
        abs value) of the Lagrangian gradient and the constraint violation
        are smaller than ``gtol``. Default is 1e-8.
    xtol : float, optional
        Tolerance for termination by the change of the independent variable.
        The algorithm will terminate when ``tr_radius < xtol``, where
        ``tr_radius`` is the radius of the trust region used in the algorithm.
        Default is 1e-8.
    barrier_tol : float, optional
        Threshold on the barrier parameter for the algorithm termination.
        When inequality constraints are present, the algorithm will terminate
        only when the barrier parameter is less than `barrier_tol`.
        Default is 1e-8.
    sparse_jacobian : {bool, None}, optional
        Determines how to represent Jacobians of the constraints. If bool,
        then Jacobians of all the constraints will be converted to the
        corresponding format. If None (default), then Jacobians won't be
        converted, but the algorithm can proceed only if they all have the
        same format.
    initial_tr_radius: float, optional
        Initial trust radius. The trust radius gives the maximum distance
        between solution points in consecutive iterations. It reflects the
        trust the algorithm puts in the local approximation of the optimization
        problem. For an accurate local approximation the trust-region should be
        large and for an  approximation valid only close to the current point it
        should be a small one. The trust radius is automatically updated throughout
        the optimization process, with ``initial_tr_radius`` being its initial value.
        Default is 1 (recommended in [1]_, p. 19).
    initial_constr_penalty : float, optional
        Initial constraints penalty parameter. The penalty parameter is used for
        balancing the requirements of decreasing the objective function
        and satisfying the constraints. It is used for defining the merit function:
        ``merit_function(x) = fun(x) + constr_penalty * constr_norm_l2(x)``,
        where ``constr_norm_l2(x)`` is the l2 norm of a vector containing all
        the constraints. The merit function is used for accepting or rejecting
        trial points and ``constr_penalty`` weights the two conflicting goals
        of reducing objective function and constraints. The penalty is automatically
        updated throughout the optimization  process, with
        ``initial_constr_penalty`` being its  initial value. Default is 1
        (recommended in [1]_, p 19).
    initial_barrier_parameter, initial_barrier_tolerance: float, optional
        Initial barrier parameter and initial tolerance for the barrier subproblem.
        Both are used only when inequality constraints are present. For dealing with
        optimization problems ``min_x f(x)`` subject to inequality constraints
        ``c(x) <= 0`` the algorithm introduces slack variables, solving the problem
        ``min_(x,s) f(x) + barrier_parameter*sum(ln(s))`` subject to the equality
        constraints  ``c(x) + s = 0`` instead of the original problem. This subproblem
        is solved for decreasing values of ``barrier_parameter`` and with decreasing
        tolerances for the termination, starting with ``initial_barrier_parameter``
        for the barrier parameter and ``initial_barrier_tolerance`` for the
        barrier tolerance. Default is 0.1 for both values (recommended in [1]_ p. 19).
        Also note that ``barrier_parameter`` and ``barrier_tolerance`` are updated
        with the same prefactor.
    factorization_method : string or None, optional
        Method to factorize the Jacobian of the constraints. Use None (default)
        for the auto selection or one of:

        - 'NormalEquation' (requires scikit-sparse)
        - 'AugmentedSystem'
        - 'QRFactorization'
        - 'SVDFactorization'

        The methods 'NormalEquation' and 'AugmentedSystem' can be used only
        with sparse constraints. The projections required by the algorithm
        will be computed using, respectively, the normal equation  and the
        augmented system approaches explained in [1]_. 'NormalEquation'
        computes the Cholesky factorization of ``A A.T`` and 'AugmentedSystem'
        performs the LU factorization of an augmented system. They usually
        provide similar results. 'AugmentedSystem' is used by default for
        sparse matrices.

        The methods 'QRFactorization' and 'SVDFactorization' can be used
        only with dense constraints. They compute the required projections
        using, respectively, QR and SVD factorizations. The 'SVDFactorization'
        method can cope with Jacobian matrices with deficient row rank and will
        be used whenever other factorization methods fail (which may imply the
        conversion of sparse matrices to a dense format when required).
        By default, 'QRFactorization' is used for dense matrices.
    finite_diff_rel_step : None or array_like, optional
        Relative step size for the finite difference approximation.
    maxiter : int, optional
        Maximum number of algorithm iterations. Default is 1000.
    verbose : {0, 1, 2, 3}, optional
        Level of algorithm's verbosity:

        * 0 (default) : work silently.
        * 1 : display a termination report.
        * 2 : display progress during iterations.
        * 3 : display progress during iterations (more complete report).

    disp : bool, optional
        If True (default), then `verbose` will be set to 1 if it was 0.

    Returns
    -------
    `OptimizeResult` with the fields documented below. Note the following:

        1. All values corresponding to the constraints are ordered as they
           were passed to the solver. And values corresponding to `bounds`
           constraints are put *after* other constraints.
        2. All numbers of function, Jacobian or Hessian evaluations correspond
           to numbers of actual Python function calls. It means, for example,
           that if a Jacobian is estimated by finite differences, then the
           number of Jacobian evaluations will be zero and the number of
           function evaluations will be incremented by all calls during the
           finite difference estimation.

    x : ndarray, shape (n,)
        Solution found.
    optimality : float
        Infinity norm of the Lagrangian gradient at the solution.
    constr_violation : float
        Maximum constraint violation at the solution.
    fun : float
        Objective function at the solution.
    grad : ndarray, shape (n,)
        Gradient of the objective function at the solution.
    lagrangian_grad : ndarray, shape (n,)
        Gradient of the Lagrangian function at the solution.
    nit : int
        Total number of iterations.
    nfev : integer
        Number of the objective function evaluations.
    njev : integer
        Number of the objective function gradient evaluations.
    nhev : integer
        Number of the objective function Hessian evaluations.
    cg_niter : int
        Total number of the conjugate gradient method iterations.
    method : {'equality_constrained_sqp', 'tr_interior_point'}
        Optimization method used.
    constr : list of ndarray
        List of constraint values at the solution.
    jac : list of {ndarray, sparse matrix}
        List of the Jacobian matrices of the constraints at the solution.
    v : list of ndarray
        List of the Lagrange multipliers for the constraints at the solution.
        For an inequality constraint a positive multiplier means that the upper
        bound is active, a negative multiplier means that the lower bound is
        active and if a multiplier is zero it means the constraint is not
        active.
    constr_nfev : list of int
        Number of constraint evaluations for each of the constraints.
    constr_njev : list of int
        Number of Jacobian matrix evaluations for each of the constraints.
    constr_nhev : list of int
        Number of Hessian evaluations for each of the constraints.
    tr_radius : float
        Radius of the trust region at the last iteration.
    constr_penalty : float
        Penalty parameter at the last iteration, see `initial_constr_penalty`.
    barrier_tolerance : float
        Tolerance for the barrier subproblem at the last iteration.
        Only for problems with inequality constraints.
    barrier_parameter : float
        Barrier parameter at the last iteration. Only for problems
        with inequality constraints.
    execution_time : float
        Total execution time.
    message : str
        Termination message.
    status : {0, 1, 2, 3, 4}
        Termination status:

        * 0 : The maximum number of function evaluations is exceeded.
        * 1 : `gtol` termination condition is satisfied.
        * 2 : `xtol` termination condition is satisfied.
        * 3 : `callback` function requested termination.
        * 4 : Constraint violation exceeds 'gtol'.

        .. versionchanged:: 1.15.0
            If the constraint violation exceeds `gtol`, then ``result.success``
            will now be False.

    cg_stop_cond : int
        Reason for CG subproblem termination at the last iteration:

        * 0 : CG subproblem not evaluated.
        * 1 : Iteration limit was reached.
        * 2 : Reached the trust-region boundary.
        * 3 : Negative curvature detected.
        * 4 : Tolerance was satisfied.

    References
    ----------
    .. [1] Conn, A. R., Gould, N. I., & Toint, P. L.
           Trust region methods. 2000. Siam. pp. 19.
    r   r   )where)keep_feasiblec              3   H   K   | ]  }|j                   j                    y wr   )rF   sparse_jacobian).0ri   s     r   	<genexpr>z/_minimize_trustregion_constr.<locals>.<genexpr>f  s     G2FQ155((2Fs    "zAll constraints must have the same kind of the Jacobian --- either all sparse or all dense. You can set the sparsity globally by setting `sparse_jacobian` to either True of False.Tr   r   r@   rA   rC   rD   ra   rb   rF   rL   rR   rN   rP   rG   rH   rI   rM   methodc                 2   t        | |||||	      } dk(  rbt        j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                         n|dkD  rwt        j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  | j                  	       d | _        | j                  | _        	d}	  	|       }|rd| _        y| j                  
k  r| j                  
k  rd| _        n-| j                  k  rd| _        n| j                  k\  rd| _        | j                  dv S # t        $ r d}Y xw xY wNr   FTr   r   r   )r   r   r   r   )rm   r   print_iterationr@   rA   ra   rF   r_   rW   rX   r   r`   rb   statusr>   StopIteration)rc   r&   rd   rW   rX   r_   r`   rh   callback_stopcallbackgtolmaxiterre   rf   rg   verbosextols            r   stop_criteriaz3_minimize_trustregion_constr.<locals>.stop_criteria  sx    %UA/D%.0D%/N%,.E !|++EII,1JJ,1NN,1II,1OO,1,<,<,1,B,BD 1))%))*/***/..*/))*///*/*:*:*/*@*@*/*>*>*/*<*<>  EL))EK# %)$,UOM !#$EL$&5+A+AD+H 4' g% <<<// % )$(M)s   F FFc                 j  	 t        | |||||||      } dk(  rbt        j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                         ndkD  rt        j                  | j                  | j                  | j
                  | j                  | j                  | j                  | j                  | j                  | j                  | j                  
       d | _        | j                  | _        
d}	  
|       }|rd| _        y| j                  k  r| j                  k  rd| _        n<| j                  k  r| j                  	k  rd| _        n| j                  k\  rd| _        | j                  dv S # t         $ r d}Y w xY wr{   )rq   r   r|   r@   rA   ra   rF   r_   rW   rX   r   r`   ro   rb   r}   r>   r~   )rc   r&   rd   r_   r`   rh   ro   rp   r   barrier_tolr   r   r   re   rf   rg   r   r   s            r   r   z3_minimize_trustregion_constr.<locals>.stop_criteria  s    $E1.C$-/C$.	>$+->@QSE !|++EII,1JJ,1NN,1II,1OO,1,<,<,1,B,BD 1(().).).).).)9)9).)?)?).)=)=).)@)@).););	=  EL))EK# %)$,UOM !#$EL$&5+A+AD+H //D(--; g% <<<// % )$(M)s   "F$ $F21F2r   c                 V    j                  |       }j                  |       \  }}||fS r   )rF   )r&   rJ   c_eq_	canonicalre   s       r   fun_and_constrz4_minimize_trustregion_constr.<locals>.fun_and_constr  s,    a AmmA&GD!d7Nr!   c                 V    j                  |       }j                  |       \  }}||fS r   )rL   rP   )r&   rK   J_eqr   r   re   s       r   grad_and_jacz2_minimize_trustregion_constr.<locals>.grad_and_jac
  s,    q!AmmA&GD!d7Nr!   )r   r   r   FzNumber of iterations: z, function evaluations: z, CG iterations: z, optimality: z.2ez, constraint violation: z, execution time: z4.2z s.r.   )@r9   
atleast_1dastypefloatsizecallabler   r   	nextafterrk   rV   rl   rs   isfiniter	   rt   r
   r   rE   r   r   r   sumrZ   
ValueErrorappendr   r   from_PreparedConstraintr:   concatenater0   hessn_ineqr   rJ   rK   rQ   rF   rO   rM   r]   r   print_headerr   r   r   r   rL   n_eqrP   r}   rX   successTERMINATION_MESSAGESmessager@   r>   print_footerprintrA   ra   rW   r^   )-rF   x0r%   rL   r   r   r[   constraintsr   r   r   rv   r   r   r   finite_diff_rel_stepinitial_constr_penaltyinitial_tr_radiusinitial_barrier_parameterinitial_barrier_tolerancefactorization_methoddispn_varsmodified_lbmodified_ubfinite_diff_boundsri   n_sparsec_eq0c_ineq0J_eq0J_ineq0canonical_alllagrangian_hessry   rc   r   r   r   r   resultr   re   rf   rg   s-           ``` ```                          @@@@r   _minimize_trustregion_constrr   u   s   \ 
r		!	!%	(BWWR[F|E?(7D6D1ll699rvvgVYY"&&=PQll699bffFII<NOhhr{{6995{FIINhhr{{6995{FIIN[@T@TU*699fii+1+?+?I  "vvgrvv. sBdD35GII + 36F FH"m
 A 	1b/3EF
 G2FGGH8/c.// F G 	G 0
 "Q,""O##$6vr7F%H 	I &F$o&7"E7E7
 342 )@@C2  4 =Q'--f5		]	q	 !!$	'33M4CE	 (		OO 1+$  $%!" KK (kk 	,	
 "66!5A!56 332QUUWW23 !55 41Q 45 !55 41Q 45 !55 41Q 45 1
10q155770
1 E J ++*	0 *	0V 
&	&,	0 ,	0\ !|  "	1//""$**!!# ++	
	
 -L/	Y[[55"$5 "	6 
&	&%MM9>>?I$$innMM9==	Y[[WeU##%2%"$5 "46	6 }}5#9#9D#@ $]]f4T%FN)&--8FN ::FL!|  "	1//""$**!!#!|fnn&vzzl 3''-{{m 4  &0 1#..s3 4''-'>'>s&C D!!'!6!6s ;3@ 	A M244 73555
1s0   ![*[#[[-	[>	[	[! [&):0yE>r   r   NNi  r   N      ?r   皙?r   NF)!r]   numpyr9   scipy.sparse.linalgr   _differentiable_functionsr   _constraintsr   r   r   r	   r
   _hessian_update_strategyr   	_optimizer   r   r   canonical_constraintr   r   r   reportr   r   r   r   r   r0   rm   rq   r   r.   r!   r   <module>r      s      . 6V V + & 6 >E 0 4 4 A333, 
? 
?8 8.(V	 26-1158<AEOR;>;>6:&+Kr!   