a
    ‡ù»j¿%  ã                   @   s    d dl Z d dlZd dlmZmZ d dlmZ d dl	m
Z
mZmZmZmZmZmZ G dd„ deƒZdd„ Zdd	„ Zd
d„ Zdd„ ZG dd„ dƒZG dd„ dƒZdS )é    N)ÚTestCaseÚassert_array_equal)ÚBoundsÚLinearConstraintÚNonlinearConstraintÚPreparedConstraintÚnew_bounds_to_oldÚold_bound_to_newÚstrict_boundsc                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚTestStrictBoundsc                 C   sŠ   d}d}d}d}t ||||ƒ\}}t|tj tj tj gƒ t|tjtjtjgƒ d}t ||||ƒ\}}t|g d¢ƒ t|g d¢ƒ d S )Né   é   é   FT)r   r   r   )r   r   r   ©r
   r   ÚnpÚinf©ÚselfÚmÚlbÚubÚenforce_feasibilityZ	strict_lbZ	strict_ub© r   úKE:\soccer\ml_env\Lib\site-packages\scipy/optimize/tests/test_constraints.pyÚ+test_scalarvalue_unique_enforce_feasibility   s"    þþz<TestStrictBounds.test_scalarvalue_unique_enforce_feasibilityc                 C   s’   d}g d¢}g d¢}d}t ||||ƒ\}}t|tj tj tj gƒ t|tjtjtjgƒ d}t ||||ƒ\}}t|g d¢ƒ t|g d¢ƒ d S )Nr   ©é   r   r   )r   é   é   FTr   r   r   r   r   Ú+test_vectorvalue_unique_enforce_feasibility   s"    þþz<TestStrictBounds.test_vectorvalue_unique_enforce_feasibilityc                 C   sV   d}d}d}g d¢}t ||||ƒ\}}t|tj dtj gƒ t|tjdtjgƒ d S )Nr   r   r   )FTFr   r   r   r   r   Ú+test_scalarvalue_vector_enforce_feasibility/   s    þz<TestStrictBounds.test_scalarvalue_vector_enforce_feasibilityc                 C   s^   d}g d¢}ddt jg}g d¢}t||||ƒ\}}t|dt j dgƒ t|dt jt jgƒ d S )Nr   r   r   r   )TFTr   )r   r   r
   r   r   r   r   r   Ú+test_vectorvalue_vector_enforce_feasibility:   s    þz<TestStrictBounds.test_vectorvalue_vector_enforce_feasibilityN)Ú__name__Ú
__module__Ú__qualname__r   r   r    r!   r   r   r   r   r   
   s   r   c                     sä  t  g d¢¡} t  dt jdg¡}t  g d¢¡}t jg d¢td�}t| ||ƒ}t tt||¡ tt| |ƒg d¢ƒ}| 	g d¢¡dk 
¡ sŠJ ‚| 	g d¢¡dk ¡ s¤J ‚t  g d	¢¡}t  g d	¢g d
¢g d¢g¡‰ t jg d¢td�}tˆ t j d|ƒ}t tt||¡ ttˆ t j dƒg d	¢ƒ}| 	g d	¢¡dk 
¡ �s8J ‚| 	g d¢¡dk ¡ �sTJ ‚‡ fdd„}‡ fdd„}dd„ }	t|t j d||	|ƒ}
t tt|
|¡ t|
g d¢ƒ}| 	g d	¢¡dk 
¡ �sÄJ ‚| 	g d¢¡dk ¡ �sàJ ‚d S )N)r   é   é   ç      à?éF   r   )FTT)Zdtyper   )g      Ð?é   é   )r   r   r   r   )r   r   r   r   )é   r   é   r   )TTT)éöÿÿÿr   r-   r   c                    s
   ˆ   | ¡S ©N)Údot©Úx©ÚAr   r   Úfun]   s    z2test_prepare_constraint_infeasible_x0.<locals>.func                    s   ˆ S r.   r   r0   r2   r   r   Újac`   s    z2test_prepare_constraint_infeasible_x0.<locals>.jacc                 S   s
   t  d¡S )N)r   r   )ÚspsZ
csr_matrix)r1   Úvr   r   r   Úhessc   s    z3test_prepare_constraint_infeasible_x0.<locals>.hess)r   Úarrayr   Úboolr   ÚpytestÚraisesÚ
ValueErrorr   Ú	violationÚanyÚallr   r   )r   r   Úx0r   ÚboundsÚpcZlinearr4   r5   r8   Z	nonlinearr   r2   r   Ú%test_prepare_constraint_infeasible_x0F   s:    ÿÿrD   c                  C   s¶   dd„ } t | ddgddgƒ}t|ddgƒ}t| ddg¡ddgƒ tj | dd	g¡dd
g¡ tj | d	d	g¡ddg¡ tj | d
dg¡ddg¡ tj | d
dg¡ddg¡ d S )Nc                 S   s.   t  | d d | d  | d d | d  g¡S )Nr   r   r   )r   r9   r0   r   r   r   Úcons_fp   s    ztest_violation.<locals>.cons_féÿÿÿÿg333333ë¿r   r'   r   ç        g333333ó?gš™™™™™¹?g{®Gázä?r   g333333ó¿gR¸…ëQÈ?g{®Gáz„?g=
×£p=ò?)r   r   r   r>   r   ÚtestingZassert_almost_equal)rE   ZnlcrC   r   r   r   Útest_violationo   s    rI   c            	      C   sì   t  t j ddg¡} t  dt jdg¡}g d¢}tt| |dƒ|ƒ g d¢}ttd|dƒ|ƒ g d¢}ttt j |dƒ|ƒ g d¢}tt| d	dƒ|ƒ g d
¢}tt| t jdƒ|ƒ g d¢}ttdddƒ|ƒ g d¢}ttt j t jdƒ|ƒ d S )Nr   r   é
   )©Nr   ©r   N)r   rJ   ))rF   r   ©rF   N)rF   rJ   rF   )rK   ©NN)NrJ   ))Nr%   )r   r%   )r   r%   r%   )rN   rL   )r   N)©r   r   rO   rO   r   )rN   rN   rN   )r   r9   r   r   r   )	r   r   rB   Zbounds_single_lbZbounds_no_lbZbounds_single_ubZbounds_no_ubZbounds_single_bothZbounds_no_bothr   r   r   Útest_new_bounds_to_old�   s     rP   c                  C   s¬   ddgddf} t  dt j dg¡}t  ddt jg¡}t| ƒ\}}t||ƒ t||ƒ t j t jft  dg¡t  dg¡fg} t| ƒ\}}t|t j dgƒ t|t jdgƒ d S )Nr   r   rK   rM   rF   r   )r   r9   r   r	   r   )rB   Zlb_trueZub_truer   r   r   r   r   Útest_old_bounds_to_new›   s    

&rQ   c                   @   s4   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ ZdS )Ú
TestBoundsc              	   C   s¸   ddl m}m} ddtjdft dtj g¡t dtjg¡ft dtj g¡t dtjg¡t ddg¡ffD ]F}t|Ž }ttt|Ž ƒƒ}t|j|jƒ t|j	|j	ƒ t|j
|j
ƒ qld S )	Nr   )r9   r   )ç      ð¿g      @rS   Tç      ð?g       @F)Únumpyr9   r   r   r   ÚevalÚreprr   r   r   Úkeep_feasible)r   r9   r   ÚargsrB   Zbounds2r   r   r   Ú	test_repr¬   s    
 ÿüzTestBounds.test_reprc                 C   s<   t ddgddgd�}t|jtjƒs&J ‚t|jtjƒs8J ‚d S )NrG   rT   )r   r   )r   Ú
isinstancer   r   Zndarrayr   )r   Úbr   r   r   Ú
test_array¼   s    zTestBounds.test_arrayc                 C   sF   t ƒ }t t tj ¡t tj¡ƒ}|j|jks2J ‚|j|jksBJ ‚d S r.   )r   r   Zasarrayr   r   r   )r   Úb1Úb2r   r   r   Útest_defaultsÂ   s    zTestBounds.test_defaultsc                 C   s,  d}t jt|d��( tt ddg¡ddgƒ W d   ƒ n1 s@0    Y  t jt|d��( tddgt ddg¡ƒ W d   ƒ n1 s†0    Y  d}t jt|d��0 tddgddgt ddg¡d� W d   ƒ n1 sØ0    Y  d}t jt|d��" tddgg d	¢ƒ W d   ƒ n1 �s0    Y  d S )
Nz,Lower and upper bounds must be dense arrays.©Úmatchr   r   z&`keep_feasible` must be a dense array.T©rX   z6`lb`, `ub`, and `keep_feasible` must be broadcastable.r   )r;   r<   r=   r   r6   Ú	coo_array)r   Úmessager   r   r   Útest_input_validationÈ   s    66>z TestBounds.test_input_validationc                 C   s6   t ddƒ}ddg}tj | |¡ddgddgf¡ d S )Néþÿÿÿr   rF   r   r   r   )r   r   rH   Úassert_allcloseÚresidual)r   rB   rA   r   r   r   Útest_residual×   s    
zTestBounds.test_residualN)r"   r#   r$   rZ   r]   r`   rf   rj   r   r   r   r   rR   «   s
   rR   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚTestLinearConstraintc                 C   sD   t  d¡}t|ƒ}t|t j t jƒ}t|j|jƒ t|j|jƒ d S )Nr   )r   Úeyer   r   r   r   r   )r   r3   ÚlcZlc2r   r   r   r`   Þ   s
    
z"TestLinearConstraint.test_defaultsc                 C   sˆ  t  d¡}d}tjt|d��$ t|ddgg d¢ƒ W d   ƒ n1 sF0    Y  d}tjt|d��* t|t ddg¡ddgƒ W d   ƒ n1 s’0    Y  tjt|d��* t|ddgt ddg¡ƒ W d   ƒ n1 sÚ0    Y  d	}tjt|d��6 t d
d
g¡}t|ddgddg|d� W d   ƒ n1 �s40    Y  t  d¡}d}tjt|d�� t|ƒ W d   ƒ n1 �sz0    Y  d S )Nr   z5`lb`, `ub`, and `keep_feasible` must be broadcastablera   r   r   r   z&Constraint limits must be dense arraysr   z%`keep_feasible` must be a dense arrayTrc   )r   r   r   z%`A` must have exactly two dimensions.)	r   rl   r;   r<   r=   r   r6   rd   Úempty)r   r3   re   rX   r   r   r   rf   å   s"    
2888
z*TestLinearConstraint.test_input_validationc                 C   sB   t  d¡}t|ddƒ}ddg}t j | |¡ddgddgf¡ d S )Nr   rg   r   rF   r   r   )r   rl   r   rH   rh   ri   )r   r3   rm   rA   r   r   r   rj   û   s    
z"TestLinearConstraint.test_residualN)r"   r#   r$   r`   rf   rj   r   r   r   r   rk   Ý   s   rk   )r;   rU   r   Znumpy.testingr   r   Zscipy.sparseÚsparser6   Zscipy.optimize._constraintsr   r   r   r   r   r	   r
   r   rD   rI   rP   rQ   rR   rk   r   r   r   r   Ú<module>   s   $<)2