a
    ‡ù»j=  ã                   @   sh   d dl Z d dlZd dlmZmZmZ d dlZd dlm	Z	m
Z
mZ G dd„ dƒZdd„ ZG dd	„ d	ƒZdS )
é    N)Úassert_allcloseÚassert_Úassert_array_equal)Úfmin_cobylaÚminimizeÚBoundsc                   @   sT   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zejj	d	d
d�dd„ ƒZ
dd„ Zdd„ ZdS )Ú
TestCobylac                 C   s.   ddg| _ t d¡dg| _ddddd	œ| _d S )
NgÍÌÌÌÌÌ@g…ëQ¸å?gä8Žã8Ž8@gUUUUUUå?Fé   çñhãˆµøä>éd   )ÚdispÚrhobegÚtolÚmaxiter)Úx0ÚmathÚsqrtÚsolutionÚopts©Úself© r   úFE:\soccer\ml_env\Lib\site-packages\scipy/optimize/tests/test_cobyla.pyÚsetup_method   s
    
ÿzTestCobyla.setup_methodc                 C   s   |d d t |d ƒd  S )Nr   é   r	   é   )Úabs©r   Úxr   r   r   Úfun   s    zTestCobyla.func                 C   s   |d d |d d  d S )Nr   r   r	   é   r   r   r   r   r   Úcon1   s    zTestCobyla.con1c                 C   s   |   |¡ S ©N)r!   r   r   r   r   Úcon2   s    zTestCobyla.con2Tz&not slow, but noisy so only run rarely)Úreasonc              	   C   s6   t | j| j| j| jgddddd�}t|| jdd� d S )Nr	   r
   r   T)r   ÚrhoendZmaxfunr   ç-Cëâ6?©Zatol)r   r   r   r!   r#   r   r   )r   Zcapfdr   r   r   r   Útest_simple   s    ÿzTestCobyla.test_simplec                 C   sÈ   G dd„ dƒ}|ƒ }d| j dœd| jdœf}t| j| jd||| jd�}t|j| jdd� t	|j
|jƒ t	|jd	k |ƒ t	|jd
k |ƒ t	|j|  | j¡d k |ƒ t	|j|jkdƒ t|j|jdƒ d S )Nc                   @   s   e Zd Zdd„ Zdd„ ZdS )z1TestCobyla.test_minimize_simple.<locals>.Callbackc                 S   s   d| _ d | _d S )Nr   ©Ún_callsÚlast_xr   r   r   r   Ú__init__#   s    z:TestCobyla.test_minimize_simple.<locals>.Callback.__init__c                 S   s   |  j d7  _ || _d S ©Nr	   r)   r   r   r   r   Ú__call__'   s    z:TestCobyla.test_minimize_simple.<locals>.Callback.__call__N)Ú__name__Ú
__module__Ú__qualname__r,   r.   r   r   r   r   ÚCallback"   s   r2   Úineq©Útyper   Úcobyla)ÚmethodÚconstraintsÚcallbackÚoptionsr&   r'   r
   éF   çü©ñÒMbP?z<Callback is not called exactly once for every function eval.zGLast design vector sent to the callback is not equal to returned value.)r!   r#   r   r   r   r   r   r   r   r   ÚsuccessÚmessageÚmaxcvZnfevr*   r   r+   )r   r2   r9   ÚconsÚsolr   r   r   Útest_minimize_simple!   s*    	

ÿÿÿýzTestCobyla.test_minimize_simplec                    s¼   t j d¡ t j dd¡‰t j d¡‰‡fdd„‰ ‡fdd„}‡ fdd„}‡ fd	d
„}‡ fdd„}d|dœd|dœd|dœf}t  d¡}t||d|ddid�}t|jdkƒ t|j ƒ d S )NiÒ  é
   c                    s
   ˆ   | ¡S r"   )Údot©Úw)Úpbr   r   ÚpD   s    z8TestCobyla.test_minimize_constraint_violation.<locals>.pc                    s   | ˆ    ¡  S r"   )ÚsumrE   )Úspreadr   r   ÚfG   s    z8TestCobyla.test_minimize_constraint_violation.<locals>.fc                    s   dt ˆ | ƒƒ ¡  S )Niô  ©r   rI   rE   ©rH   r   r   Úc1J   s    z9TestCobyla.test_minimize_constraint_violation.<locals>.c1c                    s   dt ˆ | ƒ ¡ ƒ S ©Né   rL   rE   rM   r   r   Úc2M   s    z9TestCobyla.test_minimize_constraint_violation.<locals>.c2c                    s   dt ˆ | ƒƒ ¡  S rO   )r   ÚmaxrE   rM   r   r   Úc3P   s    z9TestCobyla.test_minimize_constraint_violation.<locals>.c3r3   r4   )rC   r6   Zcatolg�íµ ÷Æ°>)r7   r8   r:   )	ÚnpÚrandomÚseedZrandZzerosr   r   r?   r=   )r   rK   rN   rQ   rS   r@   Zw0rA   r   )rH   rG   rJ   r   Ú"test_minimize_constraint_violation?   s$    þ

ÿz-TestCobyla.test_minimize_constraint_violationN)r/   r0   r1   r   r   r!   r#   ÚpytestÚmarkZxslowr(   rB   rW   r   r   r   r   r   
   s   
r   c            	         s  dd„ ‰ ‡ fdd„} dd„ }dd„ }t  d	d
g¡}ˆ ||g}ddg}d}tˆ ||dd�}t||dd� tˆ || dd�}tˆ |ƒddd� dd„ |D ƒ}tˆ ||dd�}t|j|dd� t|j|jƒ t|j	|dd� d| dœ}tˆ ||dd�}t|j	ddd� d S )Nc                 S   s    | d d d | d d d  S )Nr   r	   r   g      @r   ©r   r   r   r   r   `   s    z$test_vector_constraints.<locals>.func                    s   ˆ | ƒd S r-   r   rZ   ©r   r   r   Úfminc   s    z%test_vector_constraints.<locals>.fminc                    s>   t  g d¢g d¢g d¢g¡‰ t  ‡ ‡fdd„ttˆ ƒƒD ƒ¡S )N)r	   éþÿÿÿr   )éÿÿÿÿr]   é   )r^   r   r   c                    s@   g | ]8}ˆ |d f ˆd   ˆ |df ˆd   ˆ |df  ‘qS )r   r	   r   r   )Ú.0Úi©Úar   r   r   Ú
<listcomp>h   s   ÿ&
ÿz:test_vector_constraints.<locals>.cons1.<locals>.<listcomp>)rT   ÚarrayÚrangeÚlenrZ   r   rb   r   Úcons1f   s    
ÿz&test_vector_constraints.<locals>.cons1c                 S   s   | S r"   r   rZ   r   r   r   Úcons2k   s    z&test_vector_constraints.<locals>.cons2r   r   gffffffö?g333333û?gš™™™™™é?r
   )r%   r&   r'   r	   c                 S   s   g | ]}d |dœ‘qS )r3   r4   r   )r`   r@   r   r   r   rd   |   ó    z+test_vector_constraints.<locals>.<listcomp>)r8   r   r3   r4   )
rT   re   r   r   r   r   r   r=   r>   r   )	r\   rh   ri   r   Z	cons_listZxsolZfsolrA   r8   r   r[   r   Útest_vector_constraints]   s(    

rk   c                   @   s   e Zd Zdd„ Zdd„ ZdS )Ú
TestBoundsc                 C   sf   dd„ }g d¢}g d¢}dd„ t ||ƒD ƒ}t|g d¢d|d	�}g d
¢}|jsRJ ‚t|j|dd� d S )Nc                 S   s   t  | d ¡S ©Nr   ©rT   rI   rZ   r   r   r   rK   �   s    z TestBounds.test_basic.<locals>.f)r^   Nr	   Nç      à¿)ro   ro   NNro   c                 S   s   g | ]\}}||f‘qS r   r   )r`   rc   Úbr   r   r   rd   ’   rj   z)TestBounds.test_basic.<locals>.<listcomp>)r	   r   r   é   rP   r6   ©r   r7   Úbounds)ro   ro   r	   r   ro   r<   r'   )Úzipr   r=   r   r   )r   rK   ZlbZubrs   ÚresÚrefr   r   r   Ú
test_basicŒ   s    
zTestBounds.test_basicc                 C   s¤   dd„ }t tj tj gtjtjgƒ}t|ddgd|d�}|jsDJ ‚t|jddd	� t dtj gtjtjgƒ}t|ddgd|d�}|jsŒJ ‚t|jddgdd	� d S )
Nc                 S   s   t  | d ¡S rm   rn   rZ   r   r   r   rK   ›   s    z$TestBounds.test_unbounded.<locals>.fr	   r   r6   rr   r   r<   r'   )r   rT   Úinfr   r=   r   r   )r   rK   rs   ru   r   r   r   Útest_unboundedš   s    

zTestBounds.test_unboundedN)r/   r0   r1   rw   ry   r   r   r   r   rl   ‡   s   rl   )r   ÚnumpyrT   Znumpy.testingr   r   r   rX   Zscipy.optimizer   r   r   r   rk   rl   r   r   r   r   Ú<module>   s   S*