Skip to content

Commit

Permalink
fix: north west
Browse files Browse the repository at this point in the history
  • Loading branch information
rodion committed Apr 8, 2019
1 parent d0d6006 commit b1ffa7a
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 30 deletions.
2 changes: 1 addition & 1 deletion linear_programming.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -649,7 +649,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
"version": "3.7.2"
}
},
"nbformat": 4,
Expand Down
142 changes: 113 additions & 29 deletions transportation_problem.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -25,7 +25,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -55,7 +55,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 3,
"metadata": {},
"outputs": [
{
Expand All @@ -64,7 +64,7 @@
"([40, 30, 10], [30, 50], [[3, 4], [2, 4], [3, 1]])"
]
},
"execution_count": 19,
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -89,7 +89,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 4,
"metadata": {},
"outputs": [
{
Expand All @@ -98,7 +98,7 @@
"([40, 30], [30, 30, 10], [[3, 4], [2, 4], [0, 0]])"
]
},
"execution_count": 20,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -122,34 +122,46 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def north_west_corner(supply, demand):\n",
" supply_copy = supply.copy()\n",
" demand_copy = demand.copy()\n",
" i = 0\n",
" j = 0\n",
" bfs = []\n",
" while supply_copy[-1] != 0 and demand_copy[-1] != 0:\n",
" i = next(i for i, v in enumerate(supply_copy) if v > 0)\n",
" j = next(j for j, v in enumerate(demand_copy) if v > 0)\n",
" value = min(supply_copy[i], demand_copy[j])\n",
" supply_copy[i] -= value\n",
" demand_copy[j] -= value\n",
" bfs.append(((i, j), value))\n",
"\n",
" while len(bfs) < len(supply) + len(demand) - 1:\n",
" s = supply_copy[i]\n",
" d = demand_copy[j]\n",
" v = min(s, d)\n",
" supply_copy[i] -= v\n",
" demand_copy[j] -= v\n",
" bfs.append(((i, j), v))\n",
" if supply_copy[i] == 0 and i < len(supply_copy) - 1:\n",
" i += 1\n",
" elif demand_copy[j] == 0 and j < len(demand_copy) - 1:\n",
" j += 1\n",
" return bfs"
]
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 0\n",
"1 1\n",
"1 2\n",
"2 2\n",
"2 3\n",
"2 3\n",
"[((0, 0), 30), ((1, 0), 10), ((1, 1), 30), ((1, 2), 30), ((2, 2), 10), ((2, 3), 40)]\n",
"[((0, 0), 30), ((1, 0), 10), ((1, 1), 30), ((1, 2), 30), ((2, 2), 10), ((2, 3), 40)]\n"
]
}
Expand All @@ -170,7 +182,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -197,7 +209,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -214,7 +226,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -226,7 +238,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -238,7 +250,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -257,7 +269,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -278,7 +290,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -302,7 +314,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -329,7 +341,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -343,13 +355,20 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 0\n",
"1 1\n",
"1 2\n",
"2 2\n",
"2 3\n",
"2 3\n",
"[((0, 0), 30), ((1, 0), 10), ((1, 1), 30), ((1, 2), 30), ((2, 2), 10), ((2, 3), 40)]\n",
"[[30. 0. 0. 0.]\n",
" [ 0. 0. 30. 40.]\n",
" [10. 30. 10. 0.]]\n",
Expand All @@ -372,9 +391,22 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 0\n",
"1 1\n",
"2 1\n",
"2 2\n",
"3 2\n",
"3 2\n",
"[((0, 0), 10), ((1, 0), 65), ((1, 1), 15), ((2, 1), 5), ((2, 2), 10), ((3, 2), 40)]\n"
]
},
{
"data": {
"text/plain": [
Expand All @@ -384,7 +416,7 @@
" [ 0., 0., 40.]])"
]
},
"execution_count": 33,
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -401,6 +433,58 @@
" ]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 0\n",
"2 0\n",
"3 0\n",
"3 1\n",
"3 2\n",
"3 2\n",
"[((0, 0), 0), ((1, 0), 0), ((2, 0), 0), ((3, 0), 0), ((3, 1), 0), ((3, 2), 0)]\n"
]
},
{
"data": {
"text/plain": [
"array([[0., 0., 0.],\n",
" [0., 0., 0.],\n",
" [0., 0., 0.],\n",
" [0., 0., 0.]])"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"transportation_simplex_method(\n",
" [0, 0, 0, 0],\n",
" [0, 0, 0],\n",
" [\n",
" [0, 0, 0],\n",
" [0, 0, 0],\n",
" [0, 0, 0],\n",
" [0, 0, 0]\n",
" ]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -419,7 +503,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
"version": "3.7.2"
}
},
"nbformat": 4,
Expand Down

0 comments on commit b1ffa7a

Please sign in to comment.