ó
ÚÆ÷Xc           @   s\   d  d l  Z d „  Z d e f d „  ƒ  YZ d „  Z d „  Z d „  Z d „  Z d	 „  Z	 d S(
   iÿÿÿÿNc         C   sæ   t  j |  ƒ }  t |  j ƒ d k r= t d |  j f ƒ ‚ n  |  j d |  j d k  ri |  j }  t } n t } t |  ƒ } d |  j k r d n t
 } x | d k	 r´ | | ƒ } q™ W| rÊ | j j } n	 | j } t  j | d k ƒ S(   s’	  Solve the linear sum assignment problem.

    The linear sum assignment problem is also known as minimum weight matching
    in bipartite graphs. A problem instance is described by a matrix C, where
    each C[i,j] is the cost of matching vertex i of the first partite set
    (a "worker") and vertex j of the second set (a "job"). The goal is to find
    a complete assignment of workers to jobs of minimal cost.

    Formally, let X be a boolean matrix where :math:`X[i,j] = 1` iff row i is
    assigned to column j. Then the optimal assignment has cost

    .. math::
        \min \sum_i \sum_j C_{i,j} X_{i,j}

    s.t. each row is assignment to at most one column, and each column to at
    most one row.

    This function can also solve a generalization of the classic assignment
    problem where the cost matrix is rectangular. If it has more rows than
    columns, then not every row needs to be assigned to a column, and vice
    versa.

    The method used is the Hungarian algorithm, also known as the Munkres or
    Kuhn-Munkres algorithm.

    Parameters
    ----------
    cost_matrix : array
        The cost matrix of the bipartite graph.

    Returns
    -------
    row_ind, col_ind : array
        An array of row indices and one of corresponding column indices giving
        the optimal assignment. The cost of the assignment can be computed
        as ``cost_matrix[row_ind, col_ind].sum()``. The row indices will be
        sorted; in the case of a square cost matrix they will be equal to
        ``numpy.arange(cost_matrix.shape[0])``.

    Notes
    -----
    .. versionadded:: 0.17.0

    Examples
    --------
    >>> cost = np.array([[4, 1, 3], [2, 0, 5], [3, 2, 2]])
    >>> from scipy.optimize import linear_sum_assignment
    >>> row_ind, col_ind = linear_sum_assignment(cost)
    >>> col_ind
    array([1, 0, 2])
    >>> cost[row_ind, col_ind].sum()
    5

    References
    ----------
    1. http://csclab.murraystate.edu/bob.pilgrim/445/munkres.html

    2. Harold W. Kuhn. The Hungarian Method for the assignment problem.
       *Naval Research Logistics Quarterly*, 2:83-97, 1955.

    3. Harold W. Kuhn. Variants of the Hungarian method for assignment
       problems. *Naval Research Logistics Quarterly*, 3: 253-258, 1956.

    4. Munkres, J. Algorithms for the Assignment and Transportation Problems.
       *J. SIAM*, 5(1):32-38, March, 1957.

    5. https://en.wikipedia.org/wiki/Hungarian_algorithm
    i   s-   expected a matrix (2-d array), got a %r arrayi   i    N(   t   npt   asarrayt   lent   shapet
   ValueErrort   Tt   Truet   Falset   _Hungaryt   Nonet   _step1t   markedt   where(   t   cost_matrixt
   transposedt   statet   stepR   (    (    s8   /tmp/pip-build-X4mzal/scipy/scipy/optimize/_hungarian.pyt   linear_sum_assignment   s     E			R   c           B   s    e  Z d  Z d „  Z d „  Z RS(   sš   State of the Hungarian algorithm.

    Parameters
    ----------
    cost_matrix : 2D matrix
        The cost matrix. Must have shape[1] >= shape[0].
    c         C   s§   | j  ƒ  |  _ |  j j \ } } t j | d t ƒ|  _ t j | d t ƒ|  _ d |  _ d |  _	 t j
 | | d f d t ƒ|  _ t j
 | | f d t ƒ|  _ d  S(   Nt   dtypei    i   (   t   copyt   CR   R    t   onest   boolt   row_uncoveredt   col_uncoveredt   Z0_rt   Z0_ct   zerost   intt   pathR   (   t   selfR   t   nt   m(    (    s8   /tmp/pip-build-X4mzal/scipy/scipy/optimize/_hungarian.pyt   __init__w   s    		"c         C   s   t  |  j (t  |  j (d S(   s   Clear all covered matrix cellsN(   R   R   R   (   R   (    (    s8   /tmp/pip-build-X4mzal/scipy/scipy/optimize/_hungarian.pyt   _clear_covers‚   s    
(   t   __name__t
   __module__t   __doc__R!   R"   (    (    (    s8   /tmp/pip-build-X4mzal/scipy/scipy/optimize/_hungarian.pyR   n   s   	c         C   s¸   |  j  |  j  j d d ƒ d d … t j f 8_  xv t t j |  j  d k ƒ Œ  D]V \ } } |  j | rP |  j | rP d |  j | | f <t	 |  j | <t	 |  j | <qP qP W|  j
 ƒ  t S(   s$   Steps 1 and 2 in the Wikipedia page.t   axisi   Ni    (   R   t   minR    t   newaxist   zipR   R   R   R   R   R"   t   _step3(   R   t   it   j(    (    s8   /tmp/pip-build-X4mzal/scipy/scipy/optimize/_hungarian.pyR
   ‹   s    1+
c         C   sO   |  j  d k } t |  j t j | d d ƒ<| j ƒ  |  j j d k  rK t Sd S(   sÌ   
    Cover each column containing a starred zero. If n columns are covered,
    the starred zeros describe a complete set of unique assignments.
    In this case, Go to DONE, otherwise, Go to Step 4.
    i   R&   i    N(	   R   R   R   R    t   anyt   sumR   R   t   _step4(   R   R   (    (    s8   /tmp/pip-build-X4mzal/scipy/scipy/optimize/_hungarian.pyR*   ž   s    c         C   sŠ  |  j  d k j t ƒ } | |  j d d … t j f } | t j |  j d t ƒ9} |  j  j d } |  j  j d } xt	 r…t j
 t j | ƒ | | f ƒ \ } } | | | f d k r¾ t Sd |  j | | f <t j |  j | d k ƒ } |  j | | f d k r| |  _ | |  _ t S| } t |  j | <t	 |  j | <| d d … | f t j |  j d t ƒ| d d … | f <d | | <qw Wd S(   sS  
    Find a noncovered zero and prime it. If there is no starred zero
    in the row containing this primed zero, Go to Step 5. Otherwise,
    cover this row and uncover the column containing the starred
    zero. Continue in this manner until there are no uncovered zeros
    left. Save the smallest uncovered value and Go to Step 6.
    i    NR   i   i   (   R   t   astypeR   R   R    R(   R   R   R   R   t   unravel_indext   argmaxt   _step6R   R   R   t   _step5R   (   R   R   t	   covered_CR   R    t   rowt   colt   star_col(    (    s8   /tmp/pip-build-X4mzal/scipy/scipy/optimize/_hungarian.pyR/   «   s*    	 	'		)c         C   s  d } |  j  } |  j | | d f <|  j | | d f <xt rRt j |  j d d … | | d f f d k ƒ } |  j | | | d f f d k r— Pn8 | d 7} | | | d f <| | d d f | | d f <t j |  j | | d f d k ƒ } |  j | | f d k rd } n  | d 7} | | d d f | | d f <| | | d f <q8 Wx– t | d ƒ D]„ } |  j | | d f | | d f f d k rÁd |  j | | d f | | d f f <qdd |  j | | d f | | d f f <qdW|  j ƒ  d |  j |  j d k <t	 S(   s#  
    Construct a series of alternating primed and starred zeros as follows.
    Let Z0 represent the uncovered primed zero found in Step 4.
    Let Z1 denote the starred zero in the column of Z0 (if any).
    Let Z2 denote the primed zero in the row of Z1 (there will always be one).
    Continue until the series terminates at a primed zero that has no starred
    zero in its column. Unstar each starred zero of the series, star each
    primed zero of the series, erase all primes and uncover every line in the
    matrix. Return to Step 3
    i    i   Ni   iÿÿÿÿ(
   R   R   R   R   R    R2   R   t   rangeR"   R*   (   R   t   countR   R6   R7   R+   (    (    s8   /tmp/pip-build-X4mzal/scipy/scipy/optimize/_hungarian.pyR4   Ñ   s0    		2#
&	
-*+
c         C   s™   t  j |  j ƒ r• t  j |  j ƒ r• t  j |  j |  j d d ƒ} t  j | |  j ƒ } |  j |  j c | 7<|  j d d … |  j f c | 8<n  t S(   sÙ   
    Add the value found in Step 4 to every element of each covered row,
    and subtract it from every element of each uncovered column.
    Return to Step 4 without altering any stars, primes, or covered lines.
    R&   i    N(   R    R-   R   R   R'   R   R/   (   R   t   minval(    (    s8   /tmp/pip-build-X4mzal/scipy/scipy/optimize/_hungarian.pyR3     s    $%(
   t   numpyR    R   t   objectR   R
   R*   R/   R4   R3   (    (    (    s8   /tmp/pip-build-X4mzal/scipy/scipy/optimize/_hungarian.pyt   <module>
   s   	a			&	2