diff --git a/linear_programming.ipynb b/linear_programming.ipynb index 6eca79a..740b7fe 100644 --- a/linear_programming.ipynb +++ b/linear_programming.ipynb @@ -649,7 +649,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.5" + "version": "3.7.2" } }, "nbformat": 4, diff --git a/transportation_problem.ipynb b/transportation_problem.ipynb index 3062d90..f291f8f 100644 --- a/transportation_problem.ipynb +++ b/transportation_problem.ipynb @@ -16,7 +16,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": 1, "metadata": {}, "outputs": [], "source": [ @@ -25,7 +25,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 2, "metadata": {}, "outputs": [], "source": [ @@ -55,7 +55,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 3, "metadata": {}, "outputs": [ { @@ -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" } @@ -89,7 +89,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -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" } @@ -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" ] } @@ -170,7 +182,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 7, "metadata": {}, "outputs": [], "source": [ @@ -197,7 +209,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 8, "metadata": {}, "outputs": [], "source": [ @@ -214,7 +226,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 9, "metadata": {}, "outputs": [], "source": [ @@ -226,7 +238,7 @@ }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 10, "metadata": {}, "outputs": [], "source": [ @@ -238,7 +250,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 11, "metadata": {}, "outputs": [], "source": [ @@ -257,7 +269,7 @@ }, { "cell_type": "code", - "execution_count": 28, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ @@ -278,7 +290,7 @@ }, { "cell_type": "code", - "execution_count": 29, + "execution_count": 13, "metadata": {}, "outputs": [], "source": [ @@ -302,7 +314,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 14, "metadata": {}, "outputs": [], "source": [ @@ -329,7 +341,7 @@ }, { "cell_type": "code", - "execution_count": 31, + "execution_count": 15, "metadata": {}, "outputs": [], "source": [ @@ -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", @@ -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": [ @@ -384,7 +416,7 @@ " [ 0., 0., 40.]])" ] }, - "execution_count": 33, + "execution_count": 17, "metadata": {}, "output_type": "execute_result" } @@ -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": { @@ -419,7 +503,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.6.5" + "version": "3.7.2" } }, "nbformat": 4,