--- 1 Mathematical Modeling linear programming assignment problem python

Disclaimer: This article is a blogger original article, follow the CC 4.0 by-sa copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/your_answer/article/details/79160045

Introduction: In recent prepare for digital to analog and see the assignment problem, with interest, on Baidu to find a lot about the solution assignment problem, it is clear that use the Hungarian algorithm, by hand calculation, all kinds of various blog very detailed, probably you are aware of the specific principles (people can not understand their own Baidu), but the basic algorithm can not be found, it is difficult to verify with the right. Possible in order to save time it (do not want to re-create the wheel, lazy), wanted to look for python Is there any library function can be achieved, to find a lot blog or something, still can not find. 0-1 assignment problem is thinking about programming, linear programming is also used scipy.optimize library functions, angrily, direct look through scipy.optimize official website. Or the official website invincible ah. Ado, overhead matrix look at a sample:


Then the naked eye, then it is simple, the first line [413] to a selected second row, a second row [205] on the selected first row 2, a third row [322] to selected third column 2, and then the cost is 5. Then the solution with a python, then use scipy.optimize.linear_sum_assignment (cost_matrix) function of. Directly attached to the code:


  
  
  1. from scipy.optimize import linear_sum_assignment
  2. cost =np.array([[ 4, 1, 3],[ 2, 0, 5],[ 3, 2, 2]])
  3. row_ind,col_ind=linear_sum_assignment(cost)
  4. Print (row_ind) # row index corresponding to the cost matrix
  5. print (col_ind) OptimalAssignment column index # corresponding to the row index
  6. Print (cost [row_ind, col_ind]) # elements of optimal assignment extraction column index where the index of each row, the array is formed
  7. Print (cost [row_ind, col_ind] .sum ()) # summing array

Then the output results:

  
  
  1. [0 1 2]
  2. [1 0 2]
  3. [1 2 2]
  4. 5
Indexes start at zero is not to say, or speak out of turn one, the official website is invincible! Officer posted at the cafe then this function https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.linear_sum_assignment.html#scipy.optimize.linear_sum_assignment

the above.



Published 23 original articles · won praise 20 · views 40000 +
Disclaimer: This article is a blogger original article, follow the CC 4.0 by-sa copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/your_answer/article/details/79160045

Guess you like

Origin blog.csdn.net/GAIYA2050/article/details/100017268