o
    ?Hh                     @   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   `/home/air/sanwanet/gpt-api/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.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   rhoendmaxfunr   -C6?atol)r   r   r   r"   r$   r   r   )r   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                   @      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__r2   r4   r   r   r   r   Callback"   s    r8   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nfevr0   r   r1   )r   r8   r?   conssolr   r   r   test_minimize_simple!   s*   	

zTestCobyla.test_minimize_simplec           	         s   t jd}|dd|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#   )sumrL   )spreadr   r   fG      z8TestCobyla.test_minimize_constraint_violation.<locals>.fc                       dt  |   S )Ni  r   rP   rL   rO   r   r   c1J      z9TestCobyla.test_minimize_constraint_violation.<locals>.c1c                    s   dt  |   S N   rU   rL   rV   r   r   c2M   rX   z9TestCobyla.test_minimize_constraint_violation.<locals>.c2c                    rT   rY   )r   maxrL   rV   r   r   c3P   rX   z9TestCobyla.test_minimize_constraint_violation.<locals>.c3r9   r:   )rJ   r<   catolgư>)r=   r>   r@   )	nprandomRandomStaterandzerosr   r   rE   rC   )	r   rngrR   rW   r[   r]   rG   w0rH   r   )rO   rN   rQ   r   "test_minimize_constraint_violation?   s$   


z-TestCobyla.test_minimize_constraint_violationN)r5   r6   r7   r   r   r"   r$   pytestmarkxslowr-   rI   rf   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 r3   r   rj   r   r   r   fminc   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   )rm      )rn   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>h   s
    &
z:test_vector_constraints.<locals>.cons1.<locals>.<listcomp>)r_   arrayrangelenrj   r   rr   r   cons1f   s   
z&test_vector_constraints.<locals>.cons1c                 S   s   | S r#   r   rj   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 )r9   r:   r   )rp   rG   r   r   r   rt   |   s    z+test_vector_constraints.<locals>.<listcomp>)r>   r   r9   r:   )
r_   ru   r   r   r   r   r   rC   rD   r   )	rl   rx   ry   r   	cons_listxsolfsolrH   r>   r   rk   r   test_vector_constraints]   s(   

r}   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_   rP   rj   r   r   r   rR      rS   z TestBounds.test_basic.<locals>.f)rn   Nr	   N      )r   r   NNr   c                 S   s   g | ]\}}||fqS r   r   )rp   rs   br   r   r   rt      s    z)TestBounds.test_basic.<locals>.<listcomp>)r	   r   r      rZ   r<   r   r=   bounds)r   r   r	   r   r   rB   r*   )zipr   rC   r   r   )r   rR   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   r   r   r   rj   r   r   r   rR      rS   z$TestBounds.test_unbounded.<locals>.fr	   r   r<   r   r   rB   r*   )r   r_   infr   rC   r   r   )r   rR   r   r   r   r   r   test_unbounded   s   

zTestBounds.test_unboundedN)r5   r6   r7   r   r   r   r   r   r   r~      s    r~   )r   numpyr_   numpy.testingr   r   r   rg   scipy.optimizer   r   r   r   r}   r~   r   r   r   r   <module>   s    S*