Last active
November 7, 2020 04:15
-
-
Save chkwon/61bb18641162475a01d7585d8f918438 to your computer and use it in GitHub Desktop.
The Transportation Problem (simple example)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# The Transportation Problem \n", | |
"\n", | |
"Data Table Representation \n", | |
"\n", | |
"| | Warehouse 1 | Warehouse 2 | Warehouse 3 | Supply |\n", | |
"| -------- | ----------: : ----------: | ----------: | -----: |\n", | |
"|Factory 1 | 30 | 20 | 10 | 100 |\n", | |
"|Factory 2 | 25 | 15 | 5 | 200 |\n", | |
"| Demand | 100 | 120 | 80 | |\n", | |
"\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2×3 Array{Int64,2}:\n", | |
" 30 20 10\n", | |
" 25 15 5" | |
] | |
}, | |
"execution_count": 1, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"cost = [ 30 20 10 ; \n", | |
" 25 15 5 ]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"2-element Array{Int64,1}:\n", | |
" 100\n", | |
" 200" | |
] | |
}, | |
"execution_count": 2, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"supply = [100, 200]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"3-element Array{Int64,1}:\n", | |
" 100\n", | |
" 120\n", | |
" 80" | |
] | |
}, | |
"execution_count": 3, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"demand = [100, 120, 80]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 16, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Min 30 x[1,1] + 20 x[1,2] + 10 x[1,3] + 25 x[2,1] + 15 x[2,2] + 5 x[2,3]\n", | |
"Subject to\n", | |
" factory[1] : x[1,1] + x[1,2] + x[1,3] = 100.0\n", | |
" factory[2] : x[2,1] + x[2,2] + x[2,3] = 200.0\n", | |
" warehouse[1] : x[1,1] + x[2,1] = 100.0\n", | |
" warehouse[2] : x[1,2] + x[2,2] = 120.0\n", | |
" warehouse[3] : x[1,3] + x[2,3] = 80.0\n", | |
" x[1,1] ≥ 0.0\n", | |
" x[2,1] ≥ 0.0\n", | |
" x[1,2] ≥ 0.0\n", | |
" x[2,2] ≥ 0.0\n", | |
" x[1,3] ≥ 0.0\n", | |
" x[2,3] ≥ 0.0\n" | |
] | |
} | |
], | |
"source": [ | |
"m = 2 # number of factories (suppliers)\n", | |
"n = 3 # number of warehouses (demanders)\n", | |
"\n", | |
"using JuMP, GLPK\n", | |
"tp = Model(GLPK.Optimizer)\n", | |
"@variable(tp, x[1:m, 1:n] >= 0)\n", | |
"@objective(tp, Min, sum(cost[i,j] * x[i,j] for i in 1:m, j in 1:n))\n", | |
"@constraint(tp, factory[i in 1:2], sum(x[i,j] for j in 1:n) == supply[i])\n", | |
"@constraint(tp, warehouse[j in 1:3], sum(x[i,j] for i in 1:m) == demand[j])\n", | |
"print(tp)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 17, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"raw_status(tp) = \"Solution is optimal\"\n", | |
"objective_value(tp) = 5200.0\n", | |
"value.(x) = [100.0 0.0 0.0; 0.0 120.0 80.0]\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"2×3 Array{Float64,2}:\n", | |
" 100.0 0.0 0.0\n", | |
" 0.0 120.0 80.0" | |
] | |
}, | |
"execution_count": 17, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"optimize!(tp)\n", | |
"@show raw_status(tp)\n", | |
"@show objective_value(tp)\n", | |
"@show value.(x)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Julia 1.5.2", | |
"language": "julia", | |
"name": "julia-1.5" | |
}, | |
"language_info": { | |
"file_extension": ".jl", | |
"mimetype": "application/julia", | |
"name": "julia", | |
"version": "1.5.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 4 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment