Submit
Path:
~
/
/
proc
/
thread-self
/
root
/
opt
/
alt
/
python35
/
lib64
/
python3.5
/
site-packages
/
scipy
/
optimize
/
__pycache__
/
File Content:
_hungarian.cpython-35.pyc
DB�W�$ � @ sn d d l Z d d � Z Gd d � d e � Z d d � Z d d � Z d d � Z d d � Z d d � Z d S)� Nc C s� t j | � } t | j � d k r: t d | j f � � | j d | j d k rf | j } d } n d } t | � } d | j k r� d n t } x | d k r� | | � } q� W| r� | j j } n | j } t j | d k � S)a� 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 � z-expected a matrix (2-d array), got a %r array� r TFN) �np�asarray�len�shape� ValueError�T�_Hungary�_step1�marked�where)�cost_matrixZ transposed�state�stepr � r �/_hungarian.py�linear_sum_assignment s E r c @ s. e Z d Z d Z d d � Z d d � Z d S)r z�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)N�dtyper r )�copy�Cr r Zones�bool� row_uncovered� col_uncovered�Z0_r�Z0_cZzeros�int�pathr )�selfr �n�mr r r �__init__w s "z_Hungary.__init__c C s* d | j d d � <d | j d d � <d S)zClear all covered matrix cellsTN)r r )r r r r � _clear_covers� s z_Hungary._clear_coversN)�__name__� __module__�__qualname__�__doc__r! r"