{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from scipy.optimize import linear_sum_assignment"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(8, array([0, 1, 2, 3]), array([3, 2, 0, 1]))"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cost = np.array([\n",
" [2, 5, 1, 1],\n",
" [6, 8, 4, 6],\n",
" [3, 7, 3, 2],\n",
" [0, 0, 0, 0]\n",
"])\n",
"row_ind, col_ind = linear_sum_assignment(cost)\n",
"cost[row_ind, col_ind].sum(), row_ind, col_ind"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(15.0, array([0, 1, 2]), array([0, 3, 1]))"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cost = np.array([\n",
" [8.0, 5.0, 9.0, 9.0],\n",
" [4.0, 2.0, 6.0, 4.0],\n",
" [7.0, 3.0, 7.0, 8.0],\n",
"])\n",
"\n",
"row_ind, col_ind = linear_sum_assignment(cost)\n",
"cost[row_ind, col_ind].sum(), row_ind, col_ind"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.15"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "88db7227d3cc9a138557c8e6d2b17faeee1265d333fb084011d74bc0d566fe18"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}