o
    h                      @   s`   d dl Z d dlZd dlmZmZm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                   @   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mathsqrtsolutionoptsself r   [/var/www/html/scripts/venv/lib/python3.10/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      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   rhoendmaxfunr   -C6?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                   @      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__r0   r2   r   r   r   r   Callback    s    r6   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maxcvnfevr.   r   r/   )r   r6   r=   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#   )sumrJ   )spreadr   r   fB      z8TestCobyla.test_minimize_constraint_violation.<locals>.fc                       dt  |   S )Ni  r   rN   rJ   rM   r   r   c1E      z9TestCobyla.test_minimize_constraint_violation.<locals>.c1c                    s   dt  |   S N   rS   rJ   rT   r   r   c2H   rV   z9TestCobyla.test_minimize_constraint_violation.<locals>.c2c                    rR   rW   )r   maxrJ   rT   r   r   c3K   rV   z9TestCobyla.test_minimize_constraint_violation.<locals>.c3r7   r8   )rH   r:   catolgư>)r;   r<   r>   )	nprandomseedrandzerosr   r   rC   rA   )r   rP   rU   rY   r[   rE   w0rF   r   )rM   rL   rO   r   "test_minimize_constraint_violation:   s$   

z-TestCobyla.test_minimize_constraint_violationN)
r3   r4   r5   r   r   r"   r$   r+   rG   rc   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 r1   r   rd   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   )rg      )rh   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>)r]   arrayrangelenrd   r   rl   r   cons1a   s   
z&test_vector_constraints.<locals>.cons1c                 S   s   | S r#   r   rd   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 )r7   r8   r   )rj   rE   r   r   r   rn   w   s    z+test_vector_constraints.<locals>.<listcomp>)r<   r   r7   r8   )
r]   ro   r   r   r   r   r   rA   rB   r   )	rf   rr   rs   r   	cons_listxsolfsolrF   r<   r   re   r   test_vector_constraintsX   s(   

rw   c                   @   r,   )
TestBoundsc                 C   sf   dd }g d}g d}dd t ||D }t|g dd|d	}g d
}|js)J t|j|dd d S )Nc                 S      t | d S Nr   r]   rN   rd   r   r   r   rP      rQ   z TestBounds.test_basic.<locals>.f)rh   Nr	   N      )r|   r|   NNr|   c                 S   s   g | ]\}}||fqS r   r   )rj   rm   br   r   r   rn      s    z)TestBounds.test_basic.<locals>.<listcomp>)r	   r   r      rX   r:   r   r;   bounds)r|   r|   r	   r   r|   r@   r)   )zipr   rA   r   r   )r   rP   lbubr   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s"J t|jddd	 t dtj gtjtjg}t|ddgd|d}|jsFJ t|jddgdd	 d S )
Nc                 S   ry   rz   r{   rd   r   r   r   rP      rQ   z$TestBounds.test_unbounded.<locals>.fr	   r   r:   r   r   r@   r)   )r   r]   infr   rA   r   r   )r   rP   r   r   r   r   r   test_unbounded   s   

zTestBounds.test_unboundedN)r3   r4   r5   r   r   r   r   r   r   rx      s    rx   )r   numpyr]   numpy.testingr   r   r   scipy.optimizer   r   r   r   rw   rx   r   r   r   r   <module>   s    O*