a
    h                     @   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?g888@gUUUUUU?F   h㈵>d   )disprhobegtolmaxiter)x0mathsqrtsolutionoptsself r   \/var/www/html/assistant/venv/lib/python3.9/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sJ 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*