o
    EbK                    @   s  d Z ddlZddlZddlmZmZmZmZm	Z	m
Z
 ddlmZ ddlmZmZ ddlmZ ddlmZ ddlmZ ddlZddlZd	Zzdd
lmZ W n eyW   dZY nw d	ZzddlZddlm Z! W n eyq   dZY nw dd Z"dd Z#dd Z$dd Z%		d_ddZ&dd Z'dd Z(d`ddZ)dd Z*dad#d$Z+d%d& Z,d'd( Z-d)d* Z.dZ/dZ0dZ1dZ2dZ3G d+d, d,Z4G d-d. d.e4Z5G d/d0 d0e4Z6G d1d2 d2e4Z7G d3d4 d4e4Z8G d5d6 d6e5Z9G d7d8 d8e5Z:G d9d: d:e5Z;G d;d< d<e6Z<erG d=d> d>e6Z=erG d?d@ d@e6Z>G dAdB dBe6Z?G dCdD dDe6Z@G dEdF dFZAG dGdH dHe7ZBG dIdJ dJe7ZCG dKdL dLe8ZDG dMdN dNe8ZEG dOdP dPZFG dQdR dReFZGG dSdT dTeFZHG dUdV dVeFZIG dWdX dXZJG dYdZ dZeJZKG d[d\ d\eJZLG d]d^ d^eJZMdS )bz"
Unit test for Linear Programming
    N)assert_assert_allcloseassert_equalassert_array_lessassert_warnssuppress_warnings)raises)linprogOptimizeWarning)approx_derivative)MatrixRankWarning)LinAlgWarningT)UmfpackWarningF)choleskyc                 C   s0   t | j d t | j|k d t| jdd d S )NIncorrectly reported successz)Incorrectly reported number of iterations   z(Failed to report iteration limit reachedr   successr   status)resmaxiter r   C/usr/lib/python3/dist-packages/scipy/optimize/tests/test_linprog.py_assert_iteration_limit_reached   s   r   c                 C       t | j d t| jdd d S )Nincorrectly reported success   z"failed to report infeasible statusr   r   r   r   r   _assert_infeasible%      r   c                 C   r   )Nr      z!failed to report unbounded statusr   r   r   r   r   _assert_unbounded+   r   r!   c                 C   s"   t | j d t | jdv d d S )Nr   r      z%failed to report optimization failure)r   r   r   r   r   r   r   )_assert_unable_to_find_basic_feasible_sol1   s   r$   :0yE>c                 C   sj   | j sd| j| j}t|t| jd |d ur#t| j|d||d |d ur3t| j|d||d d S d S )Nz linprog status {0}, message: {1}r   z*converged to an unexpected objective valueerr_msgrtolatolz#converged to an unexpected solution)	r   formatr   messageAssertionErrorr   r   funx)r   desired_fun	desired_xr(   r)   msgr   r   r   _assert_success;   s"   
r2   c                 C   s  t jd | | d d  d }t | d | d  d }|| d | | }t | d | | f}g }g }t| d D ]}| }d||ddddf< ||	  |d q:t| D ]"}t| D ]}| }d|dd||f< ||	  |d qbq\t| D ]&}| }|dd|ddf |dd|ddf< ||	  || qt| D ]&}| }|dddd|f |dddd|f< ||	  || q| }|ddt| t| f |ddt| t| f< ||	  || | }|ddt| td|  d df |ddt| td|  d df< ||	  || t j
t |td}	t j
|td}
t j|	jd }|	|
||fS )z
    Generates a linear program for which integer solutions represent an
    n x n magic square; binary decision variables represent the presence
    (or absence) of an integer 1 to n^2 in each position of the square.
    r   r   r   r#   NZdtype)nprandomseedarangeZreshapezerosrangecopyappendZflattenarrayvstackfloatrandshape)nMZnumbersr9   ZA_listZb_listiZA_rowjAbcr   r   r   magic_squareP   sT   ((0
""
rI   c                 C   s   t jd t jj| |fd }t | | | f}t | }t| D ]}|d }d|||| || f< ||  ||< q"t || | f}t |}t|D ]}|d }d|||d|f< d||< qMt ||f}	t ||f}
|	|
| fS )z -> A b c LP test: m*n vars, m+n constraints
        row sums == n/m, col sums == 1
        https://gist.github.com/denis-bz/8647461
    r   sizer   N)	r5   r6   r7   Zexponentialr9   r:   r>   ZhstackZravel)mrB   rH   ZArowZbrowrE   Zj1ZAcolZbcolrF   rG   r   r   r   lpgen_2d   s"   


rM   c                 C   s   t j|  d\}}}t j|d }t j||d }t j|d }t j||d }t j|d }t j| }	t j|}
t j |	|	t j  k < t j|
|
t j k< t |	|
fj}||||||fS )N)
      2         ?)r5   r6   r7   r@   infr>   T)r7   Zm_eqZm_ubrB   rH   A_ubb_ubA_eqb_eqlbubboundsr   r   r   very_random_gen   s   
r[   c                  C   sZ   g d} g dg dg dg dg}g d}g dg}dg}g d	}d
}| ||||||fS )N)r3      r#   )r^      	   )r   r3   r   )rN   r^      )r_   r3   r    r#   )ra   r_   r]   r_   )rb   r   r   )gk椀?gs?r   gkL?gqY:^@r   )rH   rT   rU   rV   rW   x_starf_starr   r   r   nontrivial_problem   s   
rh   r\   r`   d   c                 C   sn  t j|  t jjdd||fd}t j|}t jjdd||fd}|||  t jjdd|d }t || }d|d|< tjd| || f}	d}
t	|D ]<}||dd|f  |	|
d|f< d|	|
|| f< d| |dd|f  |	|
d d|f< d|	|
d || f< |
d7 }
qP|	
 }	t d| }||ddd< | |ddd< dg| d	g|  }||	||fS )
z
    Training data is {(x0, y0), (x1, y2), ..., (xn-1, yn-1)}
        x in R^d
        y in R
    n: number of training samples
    d: dimension of x, i.e. x in R^d
    phi: feature map R^d -> R^m
    m: dimension of feature space
    r   r   rJ   h㈵>Nr   r3   NNr   N)r5   r6   r7   ZnormalrandnonesscipysparseZ
lil_matrixr:   Ztocscr9   )r7   rL   drB   ZphiZw_truer.   yrH   rT   idxiirU   bndsr   r   r   l1_regression_prob   s*   
 (
rv   c                    s   i   fdd}t ddg}ddgddgddgg}g d}t||||| jd	}t|d
ddgd t d |d  t d |d  t d |d  t d |d  d S )Nc                    s   |  d}|  d}t|  ddv  t|  dtdv  tt|  dt tt|t tt|t | d  d< | d	  d	< | d
  d
< | d  d< d S )Nr+   completeZphaser   r   r   r#   nitr.   r-   slackcon)popr   r:   
isinstanceintboolstr)r   r+   rw   Zlast_cbr   r   cb   s   

z!generic_callback_test.<locals>.cbra   r   r   r   rN   r\   r#   rT   rU   callbackmethod      2r_   r/   r0   r-   r.   r{   rz   )r5   r=   r	   r   r2   r   selfr   rH   rT   rU   r   r   r   r   generic_callback_test   s   r   c                  C   sp   t ddg} ddgddgddgg}g d}ttt| ||dd ttt| ||d	d ttt| ||d
did d S )Nra   r   r   r   r   r   ekki-ekki-ekkirT   rU   r   z
highs-ekki	rr_methodrT   rU   options)r5   r=   assert_raises
ValueErrorr	   )rH   rT   rU   r   r   r    test_unknown_solvers_and_options  s   
r   c                  C   sR   t ddg} ddgddgddgg}g d}t| ||dd}t|d	dd
gd d S )Nra   r   r   r   r   r   highsr   r   r_   r   )r5   r=   r	   r2   )rH   rT   rU   r   r   r   r   test_choose_solver  s
   r   c                   @   s  e Zd 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d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Z d<d= Z!d>d? Z"d@dA Z#dBdC Z$dDdE Z%dFdG Z&dHdI Z'dJdK Z(dLdM Z)dNdO Z*dPdQ Z+dRdS Z,dTdU Z-dVdW Z.dXdY Z/dZd[ Z0d\d] Z1d^d_ Z2d`da Z3dbdc Z4ddde Z5dfdg Z6dhdi Z7djdk Z8dldm Z9dndo Z:dpdq Z;drds Z<dtdu Z=dvdw Z>dxdy Z?dzd{ Z@d|d} ZAd~d ZBdd ZCdd ZDdd ZEdd ZFdd ZGdd ZHdd ZIdd ZJdd ZKdd ZLdd ZMdd ZNeOjPjQeRjSdkdddd ZTdS )LinprogCommonTestsa  
    Base class for `linprog` tests. Generally, each test will be performed
    once for every derived class of LinprogCommonTests, each of which will
    typically change self.options and/or self.method. Effectively, these tests
    are run for many combination of method (simplex, revised simplex, and
    interior point) and options (such as pivoting rule or sparse treatment).
    c                 C   s   t |  d S N)r   r   r   r   r   test_callback<  s   z LinprogCommonTests.test_callbackc                 C   8   t dd\}}}t|||| jddid}t|dd d S )NrO   dispTrT   rU   r   r   T*Pr/   rM   r	   r   r2   r   rF   rG   rH   r   r   r   r   	test_disp?  
   zLinprogCommonTests.test_dispc                 C   sT   ddg}ddgddgg}ddg}d}d}t |||||f| j| jd	}t|d
d d S )Nr3   r#   ra   r   r   r_   rk   ra   N)rT   rU   rZ   r   r   r   )r	   r   r   r2   )r   rH   rF   rG   	x0_bounds	x1_boundsr   r   r   r   test_docstring_exampleF  s   z)LinprogCommonTests.test_docstring_examplec              	   C   s0   dg}dgg}d}t tt|||| j| jd d S )Nr   ZhellorV   rW   r   r   )r   	TypeErrorr	   r   r   )r   rH   rV   rW   r   r   r   test_type_errorQ  s   
z"LinprogCommonTests.test_type_errorc              
   C   sr   t dg}t dgg}t dg}| }dt jf}t|||tt|| j| jd}t	|ddgd t
|| d S )N      ?      @      r   r   re   r   )r5   r=   r;   rR   r	   rV   rW   r   r   r2   r   )r   rH   rT   Z	b_ub_origrU   rZ   r   r   r   r   test_aliasing_b_ub[     
z%LinprogCommonTests.test_aliasing_b_ubc              
   C   sr   t dg}t dgg}t dg}| }dt jf}t|tt|||| j| jd}t	|ddgd t
|| d S )Nr   r   r   r   r    r   )r5   r=   r;   rR   r	   rT   rU   r   r   r2   r   )r   rH   rV   Z	b_eq_origrW   rZ   r   r   r   r   test_aliasing_b_eqh  r   z%LinprogCommonTests.test_aliasing_b_eqc              
   C   sV   dg}dgg}dg}dgg}dg}d}t ||||||| j| jd}t|ddgd d S )Nr   r   g       @)            $@r   r   r   )r	   r   r   r2   r   rH   rT   rU   rV   rW   rZ   r   r   r   r   test_non_ndarray_argsu  s   z(LinprogCommonTests.test_non_ndarray_argsc                    sz   t ddg}ddgddgddgg}g d}d d d d d i f fdd	} fd	d
 jD }d|d< tt|||||d d S )Nra   r   r   r   r   r   c              
      s   t | ||||| j|d d S Nr   r	   r   )rH   rT   rU   rV   rW   rZ   r   r   r   r   f  s   
z2LinprogCommonTests.test_unknown_options.<locals>.fc                       i | ]}| j | qS r   r   .0keyr   r   r   
<dictcomp>      z;LinprogCommonTests.test_unknown_options.<locals>.<dictcomp>*   Zspamr   )r5   r=   r   r   r
   )r   rH   rT   rU   r   or   r   r   test_unknown_options  s   
z'LinprogCommonTests.test_unknown_optionsc                    s  d fdd	}t t|g dddgd t t|g dg dd t t|g dddgd t t|d	d
gd	d
ggd	d
gd t t|d	d
gd	ggd	gd t t|d	d
gd	d
ggd	d
gd t t|d	d
gd	ggd	gd t t|d	d
gd	gd	d d jv r jd rd S t t|d	d
gtdd	d d S )Nc              
          t | ||||| j jd d S r   r	   r   r   rH   rT   rU   rV   rW   rZ   r   r   r   r        
z1LinprogCommonTests.test_invalid_inputs.<locals>.fr   r   r    rx   r    r#   rZ   )rx   r   )r    r#      r   r   r   r   )rT   rU   )rV   rW   _sparse_presolve)r   r   r    )rT   rW   NNNNN)r   r   r   r5   r9   )r   r   r   r   r   test_invalid_inputs  s     
 z&LinprogCommonTests.test_invalid_inputsc              	      s  d fdd	}t jd d}d}tj||d}t j|}t j|}|t j| }|t j| }t ||f}	|| }
 jdv rot	t
d j d	d
 t|||
|	 j jd W d    d S 1 shw   Y  d S i  j} jdv r}d|d< t|||
|	 j|d}|jsJ d S )Nc              
      r   r   r   r   r   r   r   r     r   z5LinprogCommonTests.test_sparse_constraints.<locals>.fr   ri      rQ   >   simplexrevised simplexzMethod 'z.' does not support sparse constraint matrices.match)rH   rV   rW   rZ   r   r   >   interior-pointTrp   r   )r5   r6   r7   ro   rp   r@   rm   Zcolumn_stackr   r   r   r	   r   r   )r   r   rL   rB   rV   Zx_validrH   rY   rX   rZ   rW   r   r   r   r   r   test_sparse_constraints  s0   

"


z*LinprogCommonTests.test_sparse_constraintsc                 C   sh   g d}g dg dg dg}g d}t jd d}t|||| jd|id	}t|| t|j| d S )
Nr#   r\   r    r   r   r   r   r   r    r3   r   r   r          @r\   r   r3   r   r\   rN   r#   r   r   r3         iX  r   r    r   r   )r5   r6   r7   r	   r   r   r   ry   )r   rH   rF   rG   r   r   r   r   r   test_maxiter  s   
zLinprogCommonTests.test_maxiterc                 C   s  | j dd}tdgd| j| j d}t|dd |r t|jd tg dg d| j| j d}t|d	g d
 |r>t|jd tddgddg| j| j d}t|dddg |r\t|jd tg dg dg dgddgg d| j| j d}t|dg d |rt|jd d S d S )NpresolveTr   )r   r   rZ   r   r   r   r   ))r   r   )r3   r3   )r    r       )r   r3   r    )r   r    r   )r   r   r   r   r   r   )r   r   r   rc   ))r   )r   rN   )      @r   )rV   rW   rZ   r   r      )r   rc   r   )r   getr	   r   r2   r   ry   r   do_presolver   r   r   r   test_bounds_fixed  s6   z$LinprogCommonTests.test_bounds_fixedc                 C   s   | j dd}tdgd| j| j d}t| |rt|jd tdgdg| j| j d}t| |r6t|jd tg dg d| j| j d}t| |rRt|jd d S d S )	Nr   Tr   r   r   r   r   ))r   r   rx   r   )r   r   r	   r   r   r   ry   r   r   r   r   test_bounds_infeasible  s   z)LinprogCommonTests.test_bounds_infeasiblec              	      s   j dd}| o jdk}g d}dtjtjfdg}dtj tj fdg}|r fdd}tt# tt	 |||d	 W d    n1 sKw   Y  W d    n1 sZw   Y  tt, tt	 |||d	 W d    n1 s{w   Y  W d    d S W d    d S 1 sw   Y  d S t
|| j j d
}t| |rt|jd t
|| j j d
}t| |rt|jd d S d S )Nr   Tr   r   rx   r   c                    s   t | | j jd}|S )Nr   r   )rH   rZ   r   r   r   r   g#  s   z6LinprogCommonTests.test_bounds_infeasible_2.<locals>.gr   rH   rZ   r   r   r   )r   r   r   r5   rR   pytestwarnsRuntimeWarningr   
IndexErrorr	   r   r   ry   )r   r   Zsimplex_without_presolverH   Zbounds_1Zbounds_2r   r   r   r   r   test_bounds_infeasible_2  s:   	"z+LinprogCommonTests.test_bounds_infeasible_2c                 C   s&   ddg}t || j| jd}t| d S )Nr3   r   r   )r	   r   r   r!   )r   rH   r   r   r   r   test_empty_constraint_18  s   z*LinprogCommonTests.test_empty_constraint_1c                 C   s`   g d}dt jft j dfddg}t||| j| jd}t| | jddr.t|jd d S d S )N)r3   r   r3   r   r   r3   r   r   r   T)	r5   rR   r	   r   r   r!   r   r   ry   r   rH   rZ   r   r   r   r   test_empty_constraint_2=  s   z*LinprogCommonTests.test_empty_constraint_2c                 C   sL   g d}dt jft j dfddg}t||| j| jd}t|g ddd d S )N)r   r3   r   r3   r   r   r   )r   r   r3   r   r   r0   r/   )r5   rR   r	   r   r   r2   r   r   r   r   test_empty_constraint_3G  s   z*LinprogCommonTests.test_empty_constraint_3c              
   C   sb   t ddgd }ddgddgddgg}g d}t|||ttt| j| jd}t|ddd	gd
 d S )Nr    r   r3   r   r   r   r   ir_   r   	r5   r=   r	   rV   rW   rZ   r   r   r2   r   rH   rT   rU   r   r   r   r   test_inequality_constraintsN  s   z.LinprogCommonTests.test_inequality_constraintsc              
   C   sX   ddg}ddgddgddgg}g d}t |||ttt| j| jd}t|d	d
dgd d S )Nr_   r    r   r3   r   r   )r   r3   r3   r   r   gUUUUUU?gUUUUUU?r   r	   rV   rW   rZ   r   r   r2   r   r   r   r   test_inequality_constraints2Z  s   z/LinprogCommonTests.test_inequality_constraints2c              
   C   sp   ddg}d}t |tttt|| j| jd}t|ddgd ddg}t |tttt|| j| jd}t|ddgd d S )Nr   r   rx   r   r0   )r	   rT   rU   rV   rW   r   r   r2   r   r   r   r   test_bounds_simpleg  s   z%LinprogCommonTests.test_bounds_simplec              
   C   Z   t dg}t dgg}t dg}d}t|tt|||| j| jd}t|ddgd d S )Nr   r   )r   Nr   r    r   r5   r=   r	   rT   rU   r   r   r2   r   rH   rV   rW   rZ   r   r   r   r   test_bounded_below_only_1s     z,LinprogCommonTests.test_bounded_below_only_1c              
   C   s`   t d}t d}t g d}dt jf}t|tt|||| j| j	d}t
||t |d d S )Nr    r   rQ   r   r   r5   rn   Zeyer=   rR   r	   rT   rU   r   r   r2   sumr  r   r   r   test_bounded_below_only_2|  s   


z,LinprogCommonTests.test_bounded_below_only_2c              
   C   r  )Nr   r   )Nr   r   r    r   r  r  r   r   r   test_bounded_above_only_1  r  z,LinprogCommonTests.test_bounded_above_only_1c              
   C   sb   t d}t d}t g d}t j df}t|tt|||| j| j	d}t
||t |d d S )Nr    r   r#   r   r   r  r  r   r   r   test_bounded_above_only_2  s   

z,LinprogCommonTests.test_bounded_above_only_2c              
   C   sd   t d}t d}t g d}t j t jf}t|tt|||| j| j	d}t
||t |d d S )Nr    r   r   r   r  r  r   r   r   test_bounds_infinity  s   

z'LinprogCommonTests.test_bounds_infinityc              
   C   s   t ddgd }t jddgddggt jd}ddg}t j t jf}dt jf}||f}t|||tt|| j| jd}t	|d	d
dgd d S )Nr3   r#   ra   r   r   r4   r_   r   gm۶m&g$I$Ig%I$I@r   )
r5   r=   Zfloat64rR   r	   rV   rW   r   r   r2   )r   rH   rT   rU   r   r   rZ   r   r   r   r   test_bounds_mixed  s   

z$LinprogCommonTests.test_bounds_mixedc              
   C   sV   ddg}ddgddgddgg}g d}dd	g}t |||tt|| j| jd
}t| d S Nre   r   rc   r   r   r   )   r   r    r   r   rl   r   )r	   rV   rW   r   r   r   r   rH   rT   rU   rZ   r   r   r   r    test_bounds_equal_but_infeasible     z3LinprogCommonTests.test_bounds_equal_but_infeasiblec              
   C   sV   ddg}ddgddgddgg}g d}dd	g}t |tt|||| j| jd
}t| d S r  )r	   rT   rU   r   r   r   r  r   r   r   !test_bounds_equal_but_infeasible2  r  z4LinprogCommonTests.test_bounds_equal_but_infeasible2c              
      sj   ddg}ddgddgg}ddg}ddg} fd	d
 j D }d|d< t|||tt| j|d}t| d S )Nr   r   g?g@r#   r\   rx   r  c                    r   r   r   r   r   r   r   r     r   zDLinprogCommonTests.test_bounds_equal_no_presolve.<locals>.<dictcomp>Fr   r   )r   r	   rV   rW   r   r   r   rH   rT   rU   rZ   r   r   r   r   r   test_bounds_equal_no_presolve  s   z0LinprogCommonTests.test_bounds_equal_no_presolvec           
   
   C   s   d\}}t jd t j|}d|d< t j||}d|d d df< t j|}g dg}d}g d}t||||||| j| jd}	t|	dd	 d S )
Nr   r   r   )r   r   r   r   r    )rb   rN   r  )rb   Nrk   r   gyMj#r   )r5   r6   r7   r@   r	   r   r   r2   
r   rL   rB   rH   rV   rW   rT   rU   rZ   r   r   r   r   test_zero_column_1  s   
z%LinprogCommonTests.test_zero_column_1c           
   
   C   s   t jd d\}}t j|}d|d< t j||}d|d d df< t j|}t j||}d|d d df< t j|}d}t||||||| j| jd}	t|	 | jddrat	|	j
d d S d S )	Nr   r"   r3   r   rk   r   r   T)r5   r6   r7   r@   r	   r   r   r!   r   r   ry   r  r   r   r   test_zero_column_2  s$   z%LinprogCommonTests.test_zero_column_2c              
   C   sR   g d}g dg dg dg}g d}t |tt||t| j| jd}t|dd d S )Nr   r   r   r   r   r   r   r   r    r   r   r    r   r	   rT   rU   rZ   r   r   r2   r   rH   rV   rW   r   r   r   r   test_zero_row_1  s   z"LinprogCommonTests.test_zero_row_1c              
   C   sR   g dg dg dg}g d}g d}t |||ttt| j| jd}t|dd d S )Nr  r  r  r   r   r   r   r   )r   rT   rU   rH   r   r   r   r   test_zero_row_2  s   z"LinprogCommonTests.test_zero_row_2c              
   C   s   d\}}t j|}t j||}d|dd d f< t j|}t|tt||t| j| jd}t	| | j
ddr@t|jd d S d S Nr"   r   r   r   T)r5   r6   r@   r	   rT   rU   rZ   r   r   r   r   r   ry   )r   rL   rB   rH   rV   rW   r   r   r   r   test_zero_row_3  s   z"LinprogCommonTests.test_zero_row_3c              
   C   s   d\}}t j|}t j||}d|dd d f< t j| }t|||ttt| j| jd}t	| | j
ddrAt|jd d S d S r!  )r5   r6   r@   r	   rV   rW   rZ   r   r   r   r   r   ry   )r   rL   rB   rH   rT   rU   r   r   r   r   test_zero_row_4  s   z"LinprogCommonTests.test_zero_row_4c              
   C   sr   g d}g dg dg dg dg}g d}t |tt||t| j| jd}t| | jddr7t|j	d	 d S d S )
Nr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r#   r   r   Tr   )
r	   rT   rU   rZ   r   r   r   r   r   ry   r  r   r   r   test_singleton_row_eq_1   s   z*LinprogCommonTests.test_singleton_row_eq_1c              
   C   sX   g d}g dg dg dg dg}g d}t |tt||t| j| jd}t|dd d S )	Nr$  r%  r&  r'  )r   r   r   r#   r   r#   r   r  r  r   r   r   test_singleton_row_eq_2,  s   z*LinprogCommonTests.test_singleton_row_eq_2c              
   C   sz   g d}g dg dg dg dg}g d}g d}t |||tt|| j| jd}t| | jd	d
r;t|jd d S d S )Nr$  r%  r&  r3   r   r   r   r'  )r   r   r   r#   rk   rl   rl   rl   r   r   Tr   	r	   rV   rW   r   r   r   r   r   ry   r  r   r   r   test_singleton_row_ub_14  s   z*LinprogCommonTests.test_singleton_row_ub_1c              
   C   s`   g d}g dg dg dg dg}g d}g d}t |||tt|| j| jd}t|d	d
 d S )Nr$  r%  r&  r*  r'  )r   r         r#   r+  r   rQ   r   )r	   rV   rW   r   r   r2   r  r   r   r   test_singleton_row_ub_2A  s   z*LinprogCommonTests.test_singleton_row_ub_2c              
   C   sN   ddg}ddgddgddgg}g d}t |||ttt| j| jd}t| d S )Nr3   r   r   )r   r   r   r   )r	   rV   rW   rZ   r   r   r   r   r   r   r   test_infeasibleJ  s   z"LinprogCommonTests.test_infeasiblec              
   C   s\   dg}dgg}d}d}t |||tt|| j| jd}t| | jddr,t|jd d S d S )	Nr   r   r#   )r   r_   r   r   Tr   r,  r  r   r   r   !test_infeasible_inequality_boundsU  s   z4LinprogCommonTests.test_infeasible_inequality_boundsc              
   C   sR   t ddgd }ddgddgg}ddg}t|||ttt| j| jd}t| d S )Nr   r3   r   r   )	r5   r=   r	   rV   rW   rZ   r   r   r!   r   r   r   r   test_unboundedb  s   z!LinprogCommonTests.test_unboundedc                    s`   dg}dg} fdd j D }d|d< t|| j|d} jdkr*t|jd	 d S t| d S )
Nr   Nr   c                    r   r   r   r   r   r   r   r   p  r   zQLinprogCommonTests.test_unbounded_below_no_presolve_corrected.<locals>.<dictcomp>Fr   r   r   r   )r   r	   r   r   r   r!   )r   rH   rZ   r   r   r   r   r   *test_unbounded_below_no_presolve_correctedl  s   
z=LinprogCommonTests.test_unbounded_below_no_presolve_correctedc              
   C   s   t g d}t g dg dg dg}t g d}g d}t|||tt|| j| jd}t| | j 	dsOt
|jd	 t j t
|jd
d d d
S d
S )|
        Test whether presolve pathway for detecting unboundedness after
        constraint elimination is working.
        )r   r   r   r   r3   r3   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r3   r   r   r   )rk   rk   rk   r   r   rl   r   r   r3   N$   $The problem is (trivially) unboundedr5   r=   r	   rV   rW   r   r   r!   lower
startswithr   r.   rR   r+   r  r   r   r   *test_unbounded_no_nontrivial_constraints_1|  s"   
z=LinprogCommonTests.test_unbounded_no_nontrivial_constraints_1c              
   C   s   t g d}t g dg dg dg}t g d}g d}t|||tt|| j| jd}t| | j 	dsPt
|jd	 t j  t
|jd
d d d
S d
S )r5  )r   r   r   r   r3   r   r6  r7  )r   r   r   r   r   r   r8  )rk   rk   rk   r   r   )Nr   r   r   r3   Nr9  r:  r;  r  r   r   r   *test_unbounded_no_nontrivial_constraints_2  s"   
z=LinprogCommonTests.test_unbounded_no_nontrivial_constraints_2c              
   C   sd   t g dd }g dg dg dg}g d}t|||ttt| j| jd}t|g dd	d
d d S )N)ri   rN   r   r3   r   )rO   r   r   )   rO   r   )r   ri   '  r   )r   r   rA  gh㈵>Hz>)r0   r)   r(   r   r   r   r   r   test_cyclic_recovery  s   z'LinprogCommonTests.test_cyclic_recoveryc              
   C   s   t g d}t g dg dg dg}g d}d}dd | j D }||d	< t|||ttt| j|d
}| jdkrJ| j	dsJt
||d	  d S t|g dd d S )N)rb   9   r`   g      8@)rQ   g            r`   )rQ   g      r.  r   r%  )r   r   r   ri   c                 S   s   i | ]\}}||qS r   r   )r   r   valr   r   r   r     s    z8LinprogCommonTests.test_cyclic_bland.<locals>.<dictcomp>r   r   r   bland)r   r   r   r   r   )r5   r=   r   itemsr	   rV   rW   rZ   r   r   r   r2   )r   rH   rT   rU   r   r   r   r   r   r   test_cyclic_bland  s   
z$LinprogCommonTests.test_cyclic_blandc                 C   s   d\}}t j|}t j||}t j|}d|dd d f  |dd d f< |d  d9  < t }|td t|tt||t	| j
| jd}W d    n1 sQw   Y  t| d S )N)rN   rN   r   r   r3   A_eq does not appear...r   )r5   r6   r@   r   filterr
   r	   rT   rU   rZ   r   r   r   )r   rL   rB   rH   rV   rW   supr   r   r   r   $test_remove_redundancy_infeasibility  s    z7LinprogCommonTests.test_remove_redundancy_infeasibilityc           	   
   C   sB   t  \}}}}}}}t|||||t| j| jd}t|||d d S )Nr   r   )rh   r	   rZ   r   r   r2   	r   rH   rT   rU   rV   rW   rf   rg   r   r   r   r   test_nontrivial_problem  s
   z*LinprogCommonTests.test_nontrivial_problemc                 C   s   t dd\}}}t '}|td |td |t t|||ttt	| j
| jd}W d    n1 s5w   Y  t|dd d S )NrO   $Solving system with option 'sym_pos'invalid value encounteredr   r   r   )rM   r   rK  r
   r   r   r	   rV   rW   rZ   r   r   r2   )r   rT   rU   rH   rL  r   r   r   r   test_lpgen_problem  s   
z%LinprogCommonTests.test_lpgen_problemc                 C   s2  g d}d\}}|||d|dddd|ddg|dd|d|ddddddgdd||ddddddddgdddddd||dd|dgdddd|||d|dddgddddddd||dd|gddddddddd|||gg}g d}t  }|t t|tt||t| j| jd}W d    n1 sw   Y  t	|dddd	 d S )
N)r   r#   r`      r#   r    r\   rc   r   r         r   r   )r      i!   r   r   ir   i  ư>rB  )r/   r)   r(   )
r   rK  r   r	   rT   rU   rZ   r   r   r2   )r   rH   rB   prV   rW   rL  r   r   r   r   test_network_flow  s$   
z$LinprogCommonTests.test_network_flowc           	      C   s   g d}ddgddgddgddgddgg}d\}}||dddg|d||dgd||d|gddd||gg}g d}t  4}trB|t |td	 |td
 |td |t t|tt	|||| j
| jd}W d    n1 sqw   Y  t|dd d S )N)r   r   r   r    r   r   r#   r   r    r   r   )re   r   r   r#   scipy.linalg.solve
Ill...rJ  Solving system with option...r   r  r   )r   has_umfpackrK  r   r   r
   r   r	   rT   rU   r   r   r2   )	r   rH   rZ   rB   rY  rV   rW   rL  r   r   r   r   "test_network_flow_limited_capacity  s4   


z5LinprogCommonTests.test_network_flow_limited_capacityc              
   C   sL   g d}g dg dg}ddg}t |||ttt| j| jd}t|dd d S )	N)r   ra   re   )r    r   r   )r   r   r    rN   r   r   ir   r   r   r   r   r   (test_simplex_algorithm_wikipedia_example%  s   z;LinprogCommonTests.test_simplex_algorithm_wikipedia_examplec              
   C   s\   g d}g dg dg dg}g d}t |tt||t| j| jd}t|dg dd	d
d d S )Nr   r   r   r   r   r   g     s@)g     P@r   g     1@r   g     f@r   gTqs*>rB  )r/   r0   r)   r(   r  r  r   r   r   test_enzo_example0  s   
z$LinprogCommonTests.test_enzo_examplec                 C   s   g d}g dg dg dg dg dg}g d}t  }|td t|tt||t| j| jd	}W d    n1 s;w   Y  t	|d
g dd d S )N)gffffff@g333333@g%@gffffffg333333g%)r3   r3   r3   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r.  皙?333333?rb  rb  rJ  r   gRQ)rb  g?        rc  g?rb  r   )
r   rK  r
   r	   rT   rU   rZ   r   r   r2   )r   rH   rV   rW   rL  r   r   r   r   test_enzo_example_bD  s"   
z&LinprogCommonTests.test_enzo_example_bc              
   C   s   d}t | }dt j t d|d  |d  }t t |d t |f}ddg}t|tt	||t
| j| jd}t|dt |d d S )NrO   r   r   r   r   r   )r5   rn   pir8   r>   cossinr	   rT   rU   rZ   r   r   r2   r9   r   rL   rH   tmprV   rW   r   r   r   r   #test_enzo_example_c_with_degeneracyU  s   "z6LinprogCommonTests.test_enzo_example_c_with_degeneracyc              
   C   sz   d}t | }dt j t | |d  }t t |d t |f}ddg}t|tt	||t
| j| jd}t| d S )NrP   r   r   r   r   )r5   rn   re  r8   r>   rf  rg  r	   rT   rU   rZ   r   r   r!   rh  r   r   r   &test_enzo_example_c_with_unboundedness`  s   z9LinprogCommonTests.test_enzo_example_c_with_unboundednessc              
      s   d}t | }dt j t | |d  }t t |d t |f}ddg} fdd jD }d|d< t|t	t
||t j|d}t| d S )	NrP   r   r   c                    r   r   r   r   r   r   r   r   s  r   zMLinprogCommonTests.test_enzo_example_c_with_infeasibility.<locals>.<dictcomp>Fr   r   )r5   rn   re  r8   r>   rf  rg  r   r	   rT   rU   rZ   r   r   )r   rL   rH   ri  rV   rW   r   r   r   r   r   &test_enzo_example_c_with_infeasibilityk  s   z9LinprogCommonTests.test_enzo_example_c_with_infeasibilityc              
   C   s   t g d}t g dg dg dg dg dg}t g d}t g dg d	g}t d
d
g}t|||||t| j| jd}t|d
t |dd d S )N)ggQ뱿Mbp?rm  rm  rm  )r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   g      4@)r   r   r3   r   r3   r   )r   r   r3   r   r3   r   r   r   g>)r/   r0   r)   )r5   r=   r	   rZ   r   r   r2   Z
zeros_like)r   rH   rT   rU   rV   rW   r   r   r   r   test_basic_artificial_varsz  s   
z-LinprogCommonTests.test_basic_artificial_varsc           	   
   C   s   t d\}}}}}}t||||||| j| jd}t|j t|j t|j  t|jdk t	||j
 |j t	|||j
  |jdd t	|||j
  |jdd dD ] }|| v rtt|| d tjshJ t|| d tjstJ qTd S )	Nr   rT   rU   rV   rW   rZ   r   r   z%Optimization terminated successfully.gdy=r)   )eqlinineqlinr<  upper	marginalsZresidual)r[   r	   r   r   r   r   ry   r   r+   r   r.   r-   r{   rz   keysr}   r5   Zndarray)	r   rH   rT   rU   rV   rW   rZ   r   r   r   r   r   test_optimize_result  s"   


z'LinprogCommonTests.test_optimize_resultc           
   /   C   sb  g d}d}d}d}t g dddd|dd	dd
ddd
dd
ddddddgddd|ddd	d
d
dd
d
dddddddgg dg dg dg dg dg dg dg dg dg dg dg dg dg dg dg dg dg dddd
d
dddd
||ddd|dddddgdddddd
d
dddd
||d|ddddgdd
dd ddd|d
d
ddddd|dddgddddd
dd
ddd|d
|ddd|ddgdd
d
d ddd||d
ddddddd|dgddddd
d
dddd||d
ddddd|gg}t g d!}t g d"}t '}|td# |td$ |t t|||tt	|| j
| jd%}	W d    n	1 s$w   Y  t|	d&d' d S )(N)rl   r   ri   rw  rw  rw  rw  rw  r     rx  rx  rx  rx  rx  rl   rl   rl   rl   rl   rl   gqq?     g)r   gQr   r   ra   r   r   r   r3   r3   r   r3   r3   r   r   r   r   r   r   r   r   g333333ra   r3   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r3   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r3   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r3   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r3   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r3   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r3   gףp=
?r   gffffff?g @)rc  r   r   ri   ri   ri   ri   ri   ri   ry  ry  ry  ry  ry  ry  r   r   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rP  rQ  r   g^Zr   )r5   r=   r   rK  r
   r   r   r	   rV   rW   r   r   r2   )
r   rZ   r   r   hrT   rU   rH   rL  r   r   r   r   test_bug_5400  s\   ((((((((
z LinprogCommonTests.test_bug_5400c              
   C   s   t g d}t g dg dg}t ddg}t g dg }t dg }d}t||||||| j| jd	}t|d
t g dd d S )Nr  )r   rc  rc  )     @rc  r}  g      @rz  )rc  g    .Ag    .Ag    cArk   r   gfffff-@)r   g@r   r   r5   r=   r	   r   r   r2   )r   rH   rV   rW   rT   rU   rZ   r   r   r   r   test_bug_6139  s   	
z LinprogCommonTests.test_bug_6139c                 C   sV  t g dg}t dg}t g dg dg dg dg dg}t g d}t g d	g d
gj}t g d}t 4}trG|t |td |td |td |t	 t
||||||| j| jd}W d    n1 svw   Y  d}	t g d}
t||	|
d d}t|d d df | |j t|j|d d df |  d S )N)
r   r   r   g(\?r   ?r   r   g(\?r   g%䃞?)
r   r   r   zG?r   r   r   gɿr   g)\(̿)
r   r   r   r   r   r   r   r   r   r   )
r   r   r   gQ?r   r   r   r   r   r   )
r   gQ      пr   r   r   g{Gz r   r   gQ?)
r   r   r   r   r   r   r   r  r   r   )gGz?r   gjt?g+gI+)
gzG
ףp=
(\?ra  gQտgGzGz?
ףp=
?g333333\(\)
Gz?g{Gz?gzG@gQ?r  rQ   g)\(?g(\?{Gz?g333333ÿ)
g=
ףp=gffffff?g?g(\g(\gp=
ף?g
ףp=
@g{Gz?gQ?gQ?z%Solving system with option 'cholesky'rP  rQ  r   gV)
r  r  r  ra  r  rQ   r  r  r  r  r   rX  r   r   )r5   r=   rS   r   r]  rK  r   r
   r   r   r	   r   r   r2   r   r.   )r   rV   rW   rT   rU   rZ   rH   rL  r   r/   r0   r)   r   r   r   test_bug_6690  sH   

 z LinprogCommonTests.test_bug_6690c                 C   s   t d\}}}}t '}|td |td |t t|tt||t	| j
| jd}W d    n1 s5w   Y  d}t||d t||j| tt|jjd |j d S )Nr    rJ  rQ  r   -U?r   rj   )rI   r   rK  r
   r   r   r	   rT   rU   rZ   r   r   r2   r   dotr.   r   r5   r9   rK   )r   rV   rW   rH   NrL  r   r/   r   r   r   test_bug_7044  s   
z LinprogCommonTests.test_bug_7044c                 C   s   t g d}t g dg dg dg dg dg dg dg d	g d
g dg dg dg dg dg dg dg dg dg dg dg dg dg}t g d}t g dg}t dgg}dgd }t||||||| j| jd}t|ddd  d S )!N)	r3   r   r   r   r   r   r   r   r   )	r   g     g     x@g     8g     Xg      g     }@g      Tg     Pr)	r   g     @g      E@g     H@g      m@g     @g     x@g     hg     L@)	r   g      jg     g     ؀@g     t@g     @g     g     p@g     K@)	r   g     Ȋ@g     ؊g     T@g     |@g     pg     Xg     Љ@g      ~@)	r   g     s@     zg     n@r  g     @h@g     {g      Zg     z)	r   g     @g     8@g     v@g     b@g     ؉g     `@g     ȃ@g     r@)	rc  r          r  r  r  r  r  r  )	rc  r  r   r  r  r  r  r  r  )	rc  r  r  r   r  r  r  r  r  )	rc  r  r  r  r   r  r  r  r  )	rc  r  r  r  r  r   r  r  r  )	rc  r  r  r  r  r  r   r  r  )	rc  r  r  r  r  r  r  r   r  )	rc  r  r  r  r  r  r  r  r   )	rc  r   rc  rc  rc  rc  rc  rc  rc  )	rc  rc  r   rc  rc  rc  rc  rc  rc  )	rc  rc  rc  r   rc  rc  rc  rc  rc  )	rc  rc  rc  rc  r   rc  rc  rc  rc  )	rc  rc  rc  rc  rc  r   rc  rc  rc  )	rc  rc  rc  rc  rc  rc  r   rc  rc  )	rc  rc  rc  rc  rc  rc  rc  r   rc  )	rc  rc  rc  rc  rc  rc  rc  rc  r   )rc  rc  rc  rc  rc  rc  rc  rc  rc  rc  rc  rc  rc  rc  r   r   r   r   r   r   r   r   )	rc  r   r   r   r   r   r   r   r   r   rk   r`   r   ghb$[@rX  r/   r)   r~  r   r   r   r   test_bug_7237+  sB   
z LinprogCommonTests.test_bug_7237c                 C   s   t g dg dg dg dg}t |jd }t |jd  }dg|jd  }t !}|td |t t	|||t
t|| j| jd	}W d    n1 sQw   Y  | jd
ddk rj| jdkrjt| d S t|ddd d S )N)X  i  iD4       3i)ii    @i  )r  r   r   r  r   )r  r   r   r  r   r   r   r   r   rQ  r   tolg&.>绽|=r   g rX  r  )r5   r=   r9   rA   rn   r   rK  r   r   r	   rV   rW   r   r   r   r$   r2   )r   rT   rU   rH   rZ   rL  r   r   r   r   test_bug_8174T  s&   
z LinprogCommonTests.test_bug_8174c                 C   s   t g d}t d }t dgdgdgdgdgdgdgg}t g dg dg dg dg dg}t d	gd
gd
gd
gd
gg}t #}trN|t |td t|||||t	| j
| jd}W d    n1 slw   Y  t|dd d S )N)r   r   r   r   r   r   r   rc   r   )r   r   r   r   r   r   r   )rb  g?g?r   r   r   r3   )rb  r   r   r   r   r   gUUUUUU)r   r  r   r   r   r   )r   r   rb  r   r   r   r  ri   r   rJ  r   g?E@r   )r5   r=   identityr   r]  rK  r   r
   r	   rZ   r   r   r2   r   rH   rT   rU   rV   rW   rL  r   r   r   r   test_bug_8174_2k  s(   &
z"LinprogCommonTests.test_bug_8174_2c                 C   sn   t g d}t g dg dg dg dg dg}t g d}t|||| j| jd}t|g d	d
 d S )N)rc   r   re         ?r  )r#         @r  r         )r   rE  r   r   rQ   )r    r.  r#   g      )r^   )r3   g      @r   r  r   )r  r   g      r3   g      #@)r   r   r   r   r   )rT   rU   r   r   )r   r   rV  gUUUUUU@gUUUUUU#@r   )r5   r=   r	   r   r   r2   r   r   r   r   test_bug_8561  s   z LinprogCommonTests.test_bug_8561c           
      C   sH  g d}g dg dg dg dg}g d}g d}d}t  (}tr'|t |td	 |t t|||tt|| j	| j
d
}W d    n1 sJw   Y  |g d |d d|d< t  (}trj|t |td	 |t t|||tt|| j	| j
d
}W d    n1 sw   Y  d}	t|||	d t|||	d d S )N)rb   rN   r_   r    )r\   rd   re   r_   )rd   r\   r#   r]   )re   r#   r\   re   )r    ra   ra   rb   )r`   r  re   )rl   rl   rl   rl   g      B@rQ  r   )r   r   r3   r   r   rk   r   rj   )r/   r(   )r   r]  rK  r   r   r   r	   rV   rW   r   r   r<   r2   )
r   rH   rT   rU   rZ   r/   rL  res1res2r(   r   r   r   test_bug_8662  sB   

	


z LinprogCommonTests.test_bug_8662c              
   C   sR   ddg}ddgg}dg}ddg}t |tt|||| j| jd}t|dd	gd
d d S )Nr   r   r   r^   r]   rl   rk   r   g۶m۶m?gI$I$@r   )r	   rT   rU   r   r   r2   r  r   r   r   test_bug_8663  s   
z LinprogCommonTests.test_bug_8663c                 C   s   dg}dgdgg}ddg}dgdgdgg}g d}t  !}|t |td t|||||t| j| jd	}W d    n1 s@w   Y  t| d S )
Nr#   r   r   r   rd   r`   r    r   rN   r\  r   )	r   rK  r   r
   r	   rZ   r   r   r   r  r   r   r   test_bug_8664  s   
z LinprogCommonTests.test_bug_8664c              
   C   sx   t g d}t g dg dg}t ddg}g d}t|||tt|| j| jd}t|dd t||j	 |j
 d	S )
zv
        Test whether bug described at:
        https://github.com/scipy/scipy/issues/8973
        was fixed.
        )r   r   r   r   r3   )r   r   r   r   r   )r   r   r   r   r   r   r   )rk   rk   rk   r   r   r   r   N)r5   r=   r	   rV   rW   r   r   r2   r   r.   r-   r  r   r   r   test_bug_8973  s   z LinprogCommonTests.test_bug_8973c              
   C   sX   t d}t dgg}t dg}d}t|||tt|| j| jd}t|dgdd dS )z
        Additional test for:
        https://github.com/scipy/scipy/issues/8973
        suggested in
        https://github.com/scipy/scipy/pull/8985
        review by @antonior92
        r   r   rk   r   r   r   N)	r5   r9   r=   r	   rV   rW   r   r   r2   r  r   r   r   test_bug_8973_2  s   
z"LinprogCommonTests.test_bug_8973_2c              
   C   s   t d}t dgg}t dg}d}ddg}ddgddgg}ddg}dd	g}d
di}|| j t|||tt|| j|d}t	|ddgdd dS )zg
        Test for linprog docstring problem
        'disp'=True caused revised simplex failure
        r   r   rk   r3   r#   ra   r   r_   r   r   Tr   rN   r   r   N)
r5   r9   r=   updater   r	   rV   rW   r   r2   r  r   r   r   test_bug_10124  s   
z!LinprogCommonTests.test_bug_10124c                 C   s   t g dg dg dg dg dg dg}t g d}t jdd	t d
fdd}t }|td t|tt	||t
| j| jd}W d   n1 sMw   Y  t|g ddd dS )zq
        Test for redundancy removal tolerance issue
        https://github.com/scipy/scipy/issues/10349
        )r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )r   r   r   r   r   r   )      rN         f   r   r   r#   N)ZaxisrJ  r   )   \   r   r  r   rN   r  r   )r5   r=   Zconcatenater9   r   rK  r
   r	   rT   rU   rZ   r   r   r2   )r   rV   rW   rH   rL  r   r   r   r   test_bug_10349  s    
z!LinprogCommonTests.test_bug_10349darwinz0Failing on some local macOS builds, see gh-13846reasonc                 C   s  g d}g dg dg dg dg dg dg dg d	g d
g dg
}g d}i }| j ds4ddi}|| j t @}|td trK|t |t	d |t	d |t	d |t	d |t
d t|tt||t| j |d}W d   n1 sw   Y  t|jd dS )zA
        Test that autoscale fixes poorly-scaled problem
        )       r  r  r  r  r  r  r  r  r  r  r  r  )r   r   rc  rc  rc  rc  rc  rc  rc  rc  rc  rc  rc  )rc  rc  r   r   rc  rc  rc  rc  rc  rc  rc  rc  rc  )rc  rc  rc  rc  r   r   rc  rc  rc  rc  rc  rc  rc  )r   rc  r   rc  r   rc  r   rc  rc  rc  rc  rc  rc  )r   rc  r   rc  r   rc  rc  r   rc  rc  rc  rc  rc  )r   rc  rc  rc  rc  rc  rc  rc  r   rc  rc  rc  rc  )r   rc  rc  rc  rc  rc  rc  rc  rc  r   rc  rc  rc  )r   rc  r   rc  r   rc  rc  rc  rc  rc  r   rc  rc  )rc  rc  r   rc  r   rc  rc  rc  rc  rc  rc  r   rc  )rc  rc  r   rc  r   rc  rc  rc  rc  rc  rc  rc  r   )
g     Ag      Ag     @Ag      A   Ar  r  r  r  r  r   	autoscaleTr\  r[  zdivide by zero encountered...zoverflow encountered...zinvalid value encountered...Ill-conditioned matrix...r   Nl )r   r=  r  r   r   rK  r
   r]  r   r   r   r	   rT   rU   rZ   r   r-   )r   rH   rV   rW   r   rL  r   r   r   r   test_bug_10466  s>   
z!LinprogCommonTests.test_bug_10466N)U__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r	  r
  r  r  r  r  r  r  r  r  r   r"  r#  r(  r)  r-  r/  r0  r1  r2  r4  r>  r?  rC  rI  rM  rO  rR  rZ  r^  r_  r`  rd  rj  rk  rl  rn  rv  r|  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r   markZskipifsysplatformr  r   r   r   r   r   /  s    
! &
								
7,)$r   c                   @      e Zd ZdZdS )LinprogSimplexTestsr   Nr  r  r  r   r   r   r   r   r  J      r  c                   @   r  )LinprogIPTestsr   Nr  r   r   r   r   r  N  r  r  c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	LinprogRSTestsr   c                 C      t d d S Nz Intermittent failure acceptable.r   skipr   r   r   r   r|  e     zLinprogRSTests.test_bug_5400c                 C   r  r  r  r   r   r   r   r  h  r  zLinprogRSTests.test_bug_8662c                 C   r  r  r  r   r   r   r   rZ  k  r  z LinprogRSTests.test_network_flowN)r  r  r  r   r|  r  rZ  r   r   r   r   r  R  s
    r  c                   @   sz   e Zd Zdd Zejdddiddiddiddid	did
diddiddigdd Zdd Zdd Z	dd Z
dd ZdS )LinprogHiGHSTestsc              	   C   st   dd }t ddg}ddgddgddgg}g d}ttt||||| jd	 t|||| jd
}t|dddgd d S )Nc                 S   s   d S r   r   r   r   r   r   <lambda>r  s    z1LinprogHiGHSTests.test_callback.<locals>.<lambda>ra   r   r   r   r   r   r   r   r   r_   r   )r5   r=   r   NotImplementedErrorr	   r   r2   r   r   r   r   r   p  s   zLinprogHiGHSTests.test_callbackr   r   r3   r   r   Z
time_limitZdual_feasibility_toleranceZprimal_feasibility_toleranceZipm_optimality_toleranceZ!simplex_dual_edge_weight_strategyZekkic                    s*    fdd}|  j tt||d d S )Nc                    s   t d j| d d S )Nr   r   r   r   r   r   r   r     s   z7LinprogHiGHSTests.test_invalid_option_values.<locals>.fr   )r  r   r   r
   )r   r   r   r   r   r   test_invalid_option_values{  s   z,LinprogHiGHSTests.test_invalid_option_valuesc              
   C   s^   t ddgd }t ddgg}dg}t|||ttt| j| jd}t|j	dk| jdk d S )Nr   r3   ro  r   	highs-ipm)
r5   r=   r	   rV   rW   rZ   r   r   r   Zcrossover_nitr   r   r   r   test_crossover  s   
z LinprogHiGHSTests.test_crossoverc           
   
      s  t dd\ t jjd}j\ fdd}t|d|jd}t|jj	|  fdd	}t|d|jd}t|j
j	|  fd
d}tjdd t|d|jd}d|t < W d    n1 sw   Y  t|jj	|  fdd}tjdd t|d|jd}	d|	t < W d    n1 sw   Y  t|jj	|	 d S )Nr   r7   ro  c              	      s   t |  jdjS Nr   r	   r   r-   r.   )rV   rT   rW   rZ   rH   r   r   r   f_bub  
   z/LinprogHiGHSTests.test_marginals.<locals>.f_bubz3-point)r   Zf0c              	      s   t  | jdjS r  r  r  )rV   rT   rU   rZ   rH   r   r   r   f_beq  r  z/LinprogHiGHSTests.test_marginals.<locals>.f_beqc              	      s*   t | gj}t |jdjS r  r5   r=   rS   r	   r   r-   r.   rZ   )rV   rT   rW   rU   rH   r   rY   r   r   f_lb     z.LinprogHiGHSTests.test_marginals.<locals>.f_lbignore)Zinvalidc              	      s*   t | gj}t |jdjS r  r  r  )rV   rT   rW   rU   rH   rX   r   r   r   f_ub  r  z.LinprogHiGHSTests.test_marginals.<locals>.f_ub)r[   r	   r   r   rS   r   r-   r   rr  rt  rq  r5   ZerrstateZisfiniter<  rs  )
r   r   r  Zdfdbubr  Zdfdbeqr  Zdfdlbr  Zdfdubr   )	rV   rT   rW   rU   rZ   rH   rX   r   rY   r   test_marginals  s.   

z LinprogHiGHSTests.test_marginalsc           	   
   C   sv   t dd\}}}}}}t||||||| j| jd}| |j|jj  |j|jj  |jj |j	j }t
|ddd d S )Nr   r  ro  r   -q=rp  )r[   r	   r   r   rS   rr  rt  rq  rs  r<  r   )	r   rH   rT   rU   rV   rW   rZ   r   Zresidr   r   r   test_dual_feasibility  s   
z'LinprogHiGHSTests.test_dual_feasibilityc              
   C   sX   t dd\}}}}}}t||||||| j| jd}t|jj|||j   ds*J d S )Nr   r  ro  r   )	r[   r	   r   r   r5   Zallcloserr  rt  r.   r   r   r   r   test_complementary_slackness  s
   
&z.LinprogHiGHSTests.test_complementary_slacknessN)r  r  r  r   r   r  Zparametrizer  r  r  r  r  r   r   r   r   r  o  s$    

	1r  c                       s4   e Zd Zdd Zdd Zdd Z fddZ  ZS )	TestLinprogSimplexDefaultc                 C   s
   i | _ d S r   r   r   r   r   r   setup_method  s   
z&TestLinprogSimplexDefault.setup_methodc                 C   r  NzSimplex fails on this problem.r  r   r   r   r   r|    r  z'TestLinprogSimplexDefault.test_bug_5400c                 C   r  r  r  r   r   r   r   test_bug_7237_low_tol     z/TestLinprogSimplexDefault.test_bug_7237_low_tolc                    J   | j ddi tt t   W d    d S 1 sw   Y  d S Nr  r  r   r  r   r   r
   superr  r   	__class__r   r   test_bug_8174_low_tol     "z/TestLinprogSimplexDefault.test_bug_8174_low_tol)r  r  r  r  r|  r  r  __classcell__r   r   r  r   r    s
    r  c                       s,   e Zd Zdd Zdd Z fddZ  ZS )TestLinprogSimplexBlandc                 C      ddi| _ d S )NrG  Tr   r   r   r   r   r    r  z$TestLinprogSimplexBland.setup_methodc                 C   r  r  r  r   r   r   r   r|    r  z%TestLinprogSimplexBland.test_bug_5400c              	      s   | j ddi tt+ tt t   W d    n1 s#w   Y  W d    d S W d    d S 1 s;w   Y  d S r  )	r   r  r   r   r,   r   r
   r  r  r   r  r   r   r    s   "z-TestLinprogSimplexBland.test_bug_8174_low_tol)r  r  r  r  r|  r  r  r   r   r  r   r    s    r  c                       s   e Zd Zdd Zedjdk Zej	
dZejjeoedd fdd	Z fd
dZdd Z fddZdd Zdd Z  ZS )TestLinprogSimplexNoPresolvec                 C   r  )Nr   Fr   r   r   r   r   r    r  z)TestLinprogSimplexNoPresolve.setup_methodr   r\   Zlinuxz"Fails with warning on 32-bit linux)Z	conditionr  c                       t    d S r   )r  r|  r   r  r   r   r|       z*TestLinprogSimplexNoPresolve.test_bug_5400c                    sL   | j ddi tjtdd t  W  d    S 1 sw   Y  d S )Nr  r  zlinprog status 4r   )r   r  r   r   r,   r  r  r   r  r   r   test_bug_6139_low_tol  s   $z2TestLinprogSimplexNoPresolve.test_bug_6139_low_tolc                 C   r  r  r  r   r   r   r   r  !  r  z2TestLinprogSimplexNoPresolve.test_bug_7237_low_tolc                    r  r  r  r   r  r   r   r  $  r  z2TestLinprogSimplexNoPresolve.test_bug_8174_low_tolc                 C   r  Nz#Tests behavior specific to presolver  r   r   r   r   r>  +  r  zGTestLinprogSimplexNoPresolve.test_unbounded_no_nontrivial_constraints_1c                 C   r  r  r  r   r   r   r   r?  .  r  zGTestLinprogSimplexNoPresolve.test_unbounded_no_nontrivial_constraints_2)r  r  r  r  r5   ZintpitemsizeZ	is_32_bitr  r  r=  Zis_linuxr   r  xfailr|  r  r  r  r>  r?  r  r   r   r  r   r  
  s    	r  c                   @      e Zd ZddiZdS )TestLinprogIPDenserp   FNr  r  r  r   r   r   r   r   r  7      r  c                   @   s   e Zd ZdddZdS )TestLinprogIPSparseCholmodTrp   r   Nr   r   r   r   r   r  <  s    r  c                   @   s&   e Zd ZdddZdd Zdd ZdS )	TestLinprogIPSparseUmfpackTFr  c                 C   r  )Nz0Autoscale doesn't fix everything, and that's OK.r  r   r   r   r   r  D  r  z)TestLinprogIPSparseUmfpack.test_bug_10466c                 C   r  )Nz2Failing due to numerical issues on some platforms.r  r   r   r   r   r^  G  r  z=TestLinprogIPSparseUmfpack.test_network_flow_limited_capacityN)r  r  r  r   r  r^  r   r   r   r   r  A  s    
r  c                       s^   e Zd ZddddZejd fddZejjdd fd	d
Z	dd Z
dd Z  ZS )TestLinprogIPSparseTFrp   r   Zsym_posrThis test is sensitive to machine epsilon level perturbations in linear system solution in _linprog_ip._sym_solve.c                    r  r   r  r  r   r  r   r   r  N  r  z!TestLinprogIPSparse.test_bug_6139Fails with ATLAS, see gh-7877r  c                    r  r   r  r  r   r  r   r   r  T  r  z!TestLinprogIPSparse.test_bug_6690c           	         s   t d\}}}}d}t 6}tr|t |td |td  fdd jD }d|d< t|t	t
||| j|d	}W d    n1 sFw   Y  t|d
d d S )Nr    r  zMatrix is exactly singularr\  c                    r   r   r   r   r   r   r   r   d  r   zLTestLinprogIPSparse.test_magic_square_sparse_no_presolve.<locals>.<dictcomp>Fr   r   r  r   )rI   r   r]  rK  r   r   r
   r   r	   rT   rU   r   r2   )	r   rV   rW   rH   r  rZ   rL  r   r   r   r   r   $test_magic_square_sparse_no_presolveY  s   
z8TestLinprogIPSparse.test_magic_square_sparse_no_presolvec           
         s   t d\}}}}t =}|td |td  fdd jD }d}|D ]}||d< t|tt||t j	|d}	t
|	d	d
 q&W d    d S 1 sKw   Y  d S )Nr    rJ  zInvalid permc_spec optionc                    r   r   r   r   r   r   r   r   q  r   zATestLinprogIPSparse.test_sparse_solve_options.<locals>.<dictcomp>)ZNATURALZMMD_ATAZMMD_AT_PLUS_AZCOLAMDr   
permc_specr   r  r   )rI   r   rK  r
   r   r	   rT   rU   rZ   r   r2   )
r   rV   rW   rH   r  rL  r   Zpermc_specsr  r   r   r   r   test_sparse_solve_optionsk  s   "z-TestLinprogIPSparse.test_sparse_solve_options)r  r  r  r   r   r  xfail_on_32bitr  r  r  r  r  r  r   r   r  r   r  K  s    
r  c                       sT   e Zd ZdddZejd fddZdd Zejj	dd	 fd
dZ
  ZS )TestLinprogIPSparsePresolveT)rp   r   r  c                    r  r   r  r   r  r   r   r    r  z)TestLinprogIPSparsePresolve.test_bug_6139c                 C   r  )Nz6_sparse_presolve=True incompatible with presolve=Falser  r   r   r   r   rl    r  zBTestLinprogIPSparsePresolve.test_enzo_example_c_with_infeasibilityr	  r  c                    r  r   r
  r   r  r   r   r    r  z)TestLinprogIPSparsePresolve.test_bug_6690)r  r  r  r   r   r  r  r  rl  r  r  r  r   r   r  r   r  }  s    

r  c                   @   s8   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d ZdS )TestLinprogIPSpecificr   c                 C   s|   t rddd}ntrddd}ndddd}tdd\}}}t|||| j|d}t|||| jd}t|j|jdd	d	d
 d S )NTr  Fr  rO   r   r   z(linprog default solver unexpected resultgV瞯<r&   )has_cholmodr]  rM   r	   r   r   r-   )r   r   rF   rG   rH   r  r  r   r   r   test_solver_select  s   

z(TestLinprogIPSpecific.test_solver_selectc                 C   s4   dg}dg}t ||| jdddd}t|dd d S )Nr3   r3  FT)r   r   r   r   )r	   r   r2   r   r   r   r   )test_unbounded_below_no_presolve_original  s   z?TestLinprogIPSpecific.test_unbounded_below_no_presolve_originalc                 C   r   )NrO   r   Tr   r   r   r   r   r   r   r   test_cholesky  r   z#TestLinprogIPSpecific.test_choleskyc              	   C   s   t dd\}}}t '}|td |td |td t|||| jdddd}W d    n1 s5w   Y  t|dd	 d S )
NrO   r[  r\  r  T)Zipr   r   r   r   )	rM   r   rK  r   r
   r   r	   r   r2   )r   rF   rG   rH   rL  r   r   r   r   test_alternate_initial_point  s   z2TestLinprogIPSpecific.test_alternate_initial_pointc                 C   s   dg}dgdgg}ddg}dgdgdgg}g d}t  "}|t |td t|||||t| jd	d
id}W d    n1 sAw   Y  t|j d d S )Nr#   r   r   r   rd   r`   r  r\  r   Fr   r   )	r   rK  r   r
   r	   rZ   r   r   r   r  r   r   r   r    s   

z#TestLinprogIPSpecific.test_bug_8664N)	r  r  r  r   r  r  r  r  r  r   r   r   r   r    s    	r  c                   @   sH   e Zd Zi Zdd Zdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dS )TestLinprogRSCommonc                 C   r  r  r  r   r   r   r   rI    r  z%TestLinprogRSCommon.test_cyclic_blandc           	      C   P   t  \}}}}}}}t|||||t| j| j|d	}t|||d t|jd d S Nr   r   x0r   r   rh   r	   rZ   r   r   r2   r   ry   rN  r   r   r   "test_nontrivial_problem_with_guess     
z6TestLinprogRSCommon.test_nontrivial_problem_with_guessc           
      C   X   t  \}}}}}}}g d}t||||||| j| j|d	}	t|	||d t|	jd d S )N)rk   rk   rl   rk   r  r   r   rh   r	   r   r   r2   r   ry   
r   rH   rT   rU   rV   rW   rf   rg   rZ   r   r   r   r   0test_nontrivial_problem_with_unbounded_variables     
zDTestLinprogRSCommon.test_nontrivial_problem_with_unbounded_variablesc           
      C   r  )N)r3  r   Nrl   ra  g333333?r  r   r   r  r   r   r   r   .test_nontrivial_problem_with_bounded_variables  r"  zBTestLinprogRSCommon.test_nontrivial_problem_with_bounded_variablesc           
      C   sp   t  \}}}}}}}dg}tg d}d}g d}t||||||| j| j|d	}	t|	||d t|	jd d S )Nr#   )gibl3g%G/?r   ra  g@Q$@)rk   r#  rl   r$  r  r   r   )	rh   r5   r=   r	   r   r   r2   r   ry   r   r   r   r   8test_nontrivial_problem_with_negative_unbounded_variable  s   
zLTestLinprogRSCommon.test_nontrivial_problem_with_negative_unbounded_variablec           
      C   J   t  \}}}}}}}g d}t|||||t| j| j|d	}	t|	jd d S N)r   r   r    rQ   r  r_   rh   r	   rZ   r   r   r   r   
r   rH   rT   rU   rV   rW   rf   rg   Z	bad_guessr   r   r   r   &test_nontrivial_problem_with_bad_guess     
z:TestLinprogRSCommon.test_nontrivial_problem_with_bad_guessc           
      C   s   t d\}}}}tjj|j }t :}|td |td |t	 t
|||| jd}t
|||| j|jd}t
|| ||| j|jd}	W d    n1 sOw   Y  t|dd t|jd t|	 t|	j|jk  d S )	Nr    rJ  rQ  )rV   rW   r   )rV   rW   r   r  r  r   r   )rI   r5   r6   r@   rA   r   rK  r
   r   r   r	   r   r.   r2   r   ry   r   )
r   rF   rG   rH   r  rY  rL  r   r  Zres3r   r   r   %test_redundant_constraints_with_guess  s   
z9TestLinprogRSCommon.test_redundant_constraints_with_guessN)r  r  r  r   rI  r  r!  r%  r&  r+  r-  r   r   r   r   r    s    r  c                   @   r  )TestLinprogRSBlandpivotrG  Nr   r   r   r   r   r.    r  r.  c                   @   s   e Zd ZdZi Zdd ZdS )TestLinprogHiGHSSimplexDualzhighs-dsc                 C   s   t  \}}}}t||||| j| jd}t|jd td|jv  t|jdu tt	
|jdk tt	
|jdd |D k tt	
|jdd |D k dS )	zAThe scaled model should be optimal but unscaled model infeasible.)rT   rU   rZ   r   r   r#   z5An optimal solution to the scaled model was found butNgưc                 S   s"   g | ]\}}|d u rt jn|qS r   r5   rR   r   lur   r   r   
<listcomp>)  s   " zCTestLinprogHiGHSSimplexDual.test_lad_regression.<locals>.<listcomp>c                 S   s$   g | ]\}}|d u rt j n|qS r   r1  r2  r   r   r   r5  *  s   $ )rv   r	   r   r   r   r   r   r+   r.   r5   allrz   )r   rH   rT   rU   ru   r   r   r   r   test_lad_regression  s   
"z/TestLinprogHiGHSSimplexDual.test_lad_regressionN)r  r  r  r   r   r7  r   r   r   r   r0    s    r0  c                   @   s   e Zd ZdZi ZdS )TestLinprogHiGHSIPMr  N)r  r  r  r   r   r   r   r   r   r8  2  s    r8  c                   @   s&   e Zd ZddiZejZejZejZdS )AutoscaleTestsr  TN)r  r  r  r   r   r  r  r  r   r   r   r   r9  <  s
    
r9  c                   @   s   e Zd ZdZdd ZdS )TestAutoscaleIPr   c                 C   s   d| j d< t| S )Nr  r  )r   r9  r  r   r   r   r   r  G  s   

zTestAutoscaleIP.test_bug_6139N)r  r  r  r   r  r   r   r   r   r:  D  s    r:  c                   @   r  )TestAutoscaleSimplexr   Nr  r   r   r   r   r;  L  r  r;  c                   @   s    e Zd ZdZdd Zdd ZdS )TestAutoscaleRSr   c           	      C   r  r  r  rN  r   r   r   r  S  r  z2TestAutoscaleRS.test_nontrivial_problem_with_guessc           
      C   r'  r(  r)  r*  r   r   r   r+  Z  r,  z6TestAutoscaleRS.test_nontrivial_problem_with_bad_guessN)r  r  r  r   r  r+  r   r   r   r   r<  P  s    r<  c                   @   s2   e Zd ZdZeZejZejZej	Z	ej
ZejZdS )RRTestsr   N)r  r  r  r   r   ZLCTrM  Ztest_RR_infeasibilityr  r  r^  Z	test_NFLCrd  r   r   r   r   r=  g  s    
r=  c                   @   r  )	TestRRSVDr   ZSVDNr   r   r   r   r   r>  r  r  r>  c                   @   r  )TestRRPivotr   r/  Nr   r   r   r   r   r?  v  r  r?  c                   @   r  )TestRRIDr   ZIDNr   r   r   r   r   r@  z  r  r@  )NNr%   r%   )r   )r   r\   r`   ri   )Nr  r  Znumpyr5   Znumpy.testingr   r   r   r   r   r   r   r   r   Zscipy.optimizer	   r
   Zscipy.optimize._numdiffr   Zscipy.sparse.linalgr   Zscipy.linalgr   Zscipy.sparsero   r]  Zscikits.umfpackr   ImportErrorr  ZsksparseZsksparse.cholmodr   Zcholmodr   r   r!   r$   r2   rI   rM   r[   rh   rv   r   r   r   rT   rU   rV   rW   rZ   r   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r.  r0  r8  r9  r:  r;  r<  r=  r>  r?  r@  r   r   r   r   <module>   s     

B

#
          %u-
2D?	
