o
    Eb                     @   sN   d dl Z d dlZd dlmZmZmZ d dlmZm	Z	 G dd dZ
dd ZdS )    N)assert_allcloseassert_assert_array_equal)fmin_cobylaminimizec                   @   sD   e Zd Zdd Zdd Zdd Zdd Z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?g888@gUUUUUU?F   h㈵>d   )disprhobegtolmaxiter)x0mathZsqrtsolutionoptsself r   B/usr/lib/python3/dist-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      zTestCobyla.func                 C   s   |d d |d d  d S )Nr   r   r      r   r   r   r   r   con1   r   zTestCobyla.con1c                 C   s   |  | S N)r    r   r   r   r   con2      zTestCobyla.con2c              	   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   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    r1   ineqtyper   cobyla)methodconstraintscallbackoptionsr%   r&   r	   F   gMbP?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   r1   r8   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   p?   s   
z8TestCobyla.test_minimize_constraint_violation.<locals>.pc                    s   |      S r!   )sumrC   )spreadr   r   fB   s   z8TestCobyla.test_minimize_constraint_violation.<locals>.fc                       dt  |   S )Ni  r   rG   rC   rF   r   r   c1E      z9TestCobyla.test_minimize_constraint_violation.<locals>.c1c                    s   dt  |   S N   rK   rC   rL   r   r   c2H   rN   z9TestCobyla.test_minimize_constraint_violation.<locals>.c2c                    rJ   rO   )r   maxrC   rL   r   r   c3K   rN   z9TestCobyla.test_minimize_constraint_violation.<locals>.c3r2   r3   )rA   r   r5   Zcatolgư>)r6   r7   r9   )	npZrandomZseedZrandZzerosr   r   r=   r;   )r   rI   rM   rQ   rS   r>   Zw0r?   r   )rF   rE   rH   r   "test_minimize_constraint_violation:   s$   

z-TestCobyla.test_minimize_constraint_violationN)
r.   r/   r0   r   r   r    r"   r'   r@   rU   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   rV   r   r   r   fmin^   r#   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   )rY      )rZ   r   r   c                    s@   g | ]} |d f d    |df d    |df  qS )r   r   r   r   ).0iar   r   r   
<listcomp>c   s
    &
z:test_vector_constraints.<locals>.cons1.<locals>.<listcomp>)rT   arrayrangelenrV   r   r^   r   cons1a   s   
z&test_vector_constraints.<locals>.cons1c                 S   s   | S r!   r   rV   r   r   r   cons2f   s   z&test_vector_constraints.<locals>.cons2r   r   gffffff?g333333?g?r	   )r$   r%   r&   r   c                 S   s   g | ]}d |dqS )r2   r3   r   )r\   r>   r   r   r   r`   w   s    z+test_vector_constraints.<locals>.<listcomp>)r7   r   r2   r3   )
rT   ra   r   r   r   r   r   r;   r<   r   )	rX   rd   re   r   Z	cons_listZxsolZfsolr?   r7   r   rW   r   test_vector_constraintsX   s(   

rf   )r   ZnumpyrT   Znumpy.testingr   r   r   Zscipy.optimizer   r   r   rf   r   r   r   r   <module>   s    O