TP-recherche-operationnelle/TP1/exo4/ecommerce.ipynb
2023-04-22 17:22:53 +02:00

254 lines
6.4 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exemple de modèle et résolution du problème de Fabrication du perm"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m registry at `~/.julia/registries/General`\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[32m\u001b[1m No Changes\u001b[22m\u001b[39m to `~/.julia/environments/v1.6/Project.toml`\n",
"\u001b[32m\u001b[1m No Changes\u001b[22m\u001b[39m to `~/.julia/environments/v1.6/Manifest.toml`\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\u001b[32m\u001b[1m No Changes\u001b[22m\u001b[39m to `~/.julia/environments/v1.6/Project.toml`\n",
"\u001b[32m\u001b[1m No Changes\u001b[22m\u001b[39m to `~/.julia/environments/v1.6/Manifest.toml`\n"
]
}
],
"source": [
"#importer les packages utiles\n",
"import Pkg;\n",
"Pkg.add(\"Cbc\")\n",
"Pkg.add(\"JuMP\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Min 2 coeff[1,1,1] + 2 coeff[1,1,2] + 3 coeff[1,1,3] + 2 coeff[1,2,1] + 3 coeff[1,2,2] + 2 coeff[1,2,3] + coeff[2,1,1] + 4 coeff[2,1,2] + 4 coeff[2,1,3] + coeff[2,2,1] + 5 coeff[2,2,2] + 3 coeff[2,2,3]"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Subject to\n",
" "
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"coeff[1,1,1] + coeff[1,1,2] + coeff[1,1,3] = 1.0\n",
" coeff[1,2,1] + coeff[1,2,2] + coeff[1,2,3] = 1.0\n",
" coeff[2,1,1] + coeff[2,1,2] + coeff[2,1,3] = 1.0\n",
" coeff[2,2,1] + coeff[2,2,2] + coeff[2,2,3] = 1.0\n",
" coeff[1,1,1] ≤ 1.0\n",
" coeff[1,1,2] ≤ 1.0\n",
" coeff[1,1,3] ≤ 1.0\n",
" coeff[1,2,1] ≤ 1.0\n",
" coeff[1,2,2] ≤ 1.0\n",
" coeff[1,2,3] ≤ 1.0\n",
" coeff[2,1,1] ≤ 1.0\n",
" coeff[2,1,2] ≤ 1.0\n",
" coeff[2,1,3] ≤ 1.0\n",
" coeff[2,2,1] ≤ 1.0\n",
" coeff[2,2,2] ≤ 1.0\n",
" coeff[2,2,3] ≤ 1.0\n",
" 2 coeff[1,1,1] + coeff[2,1,1] ≤ 2.5\n",
" 2 coeff[1,1,2] + coeff[2,1,2] ≤ 1.0\n",
" 2 coeff[1,1,3] + coeff[2,1,3] ≤ 2.0\n",
" 3 coeff[2,2,1] ≤ 1.0\n",
" 3 coeff[2,2,2] ≤ 2.0\n",
" 3 coeff[2,2,3] ≤ 1.0\n",
" coeff[1,1,1] ≥ 0.0\n",
" coeff[2,1,1] ≥ 0.0\n",
" coeff[1,2,1] ≥ 0.0\n",
" coeff[2,2,1] ≥ 0.0\n",
" coeff[1,1,2] ≥ 0.0\n",
" coeff[2,1,2] ≥ 0.0\n",
" coeff[1,2,2] ≥ 0.0\n",
" coeff[2,2,2] ≥ 0.0\n",
" coeff[1,1,3] ≥ 0.0\n",
" coeff[2,1,3] ≥ 0.0\n",
" coeff[1,2,3] ≥ 0.0\n",
" coeff[2,2,3] ≥ 0.0\n",
"\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Presolve 6 (-16) rows, 9 (-3) columns and 15 (-18) elements\n",
"0 Obj 3.1666666 Primal inf 2.7666637 (3)\n",
"4 Obj 8\n",
"Optimal - objective value 8\n",
"After Postsolve, objective 8, infeasibilities - dual 0 (0), primal 0 (0)\n",
"Optimal objective 8 - 4 iterations time 0.002, Presolve 0.00\n"
]
}
],
"source": [
"using Cbc\n",
"using JuMP\n",
"\n",
"# data\n",
"fluide = [\n",
" 2 0\n",
" 1 3\n",
"]\n",
"stock = [\n",
" 2.5 1\n",
" 1 2\n",
" 2 1\n",
"]\n",
"cout = [\n",
" 1 1\n",
" 2 3\n",
" 3 2\n",
"]\n",
"expe = [\n",
" 1 0 0\n",
" 0 2 1\n",
"]\n",
"\n",
"X = size(fluide)[1]\n",
"Y = size(fluide)[2]\n",
"Z = size(stock)[1]\n",
"\n",
"# set optimizer\n",
"model = Model(Cbc.Optimizer)\n",
"\n",
"# define variables\n",
"@variable(model, coeff[1:X, 1:Y, 1:Z] >= 0)\n",
"\n",
"# define objective function\n",
"@objective(model, Min, sum(cout[k, j] * coeff[i, j, k] + coeff[i, j, k] * expe[i, k] for i = 1:X for j = 1:Y for k in 1:Z))\n",
"\n",
"# define constraints\n",
"for i = 1:X\n",
" for j = 1:Y \n",
" for k = 1:Z\n",
" @constraint(model, coeff[i,j,k] <= 1)\n",
" end\n",
" @constraint(model, sum(coeff[i, j, k] for k = 1:Z) == 1)\n",
" end\n",
"end\n",
"\n",
"for j = 1:Y\n",
" for k = 1:Z \n",
" @constraint(model, sum(coeff[i, j, k] * fluide[i,j] for i = 1:X) <= stock[k,j])\n",
" end\n",
"end\n",
"\n",
"println(model)\n",
"\n",
"# run optimization\n",
"optimize!(model)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Solution obtenue:\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"coeff[1,1,1] 0.75\n",
"coeff[1,1,2] 0.25\n",
"coeff[1,1,3] 0.0\n",
"coeff[1,2,1] 1.0\n",
"coeff[1,2,2] 0.0\n",
"coeff[1,2,3] 0.0\n",
"coeff[2,1,1] 1.0\n",
"coeff[2,1,2] 0.0\n",
"coeff[2,1,3] 0.0\n",
"coeff[2,2,1] 0.3333333333333333\n",
"coeff[2,2,2] 0.33333333333333337\n",
"coeff[2,2,3] 0.3333333333333333\n"
]
}
],
"source": [
"# print solution\n",
"println(\"Solution obtenue:\")\n",
"for i in 1:X\n",
" for j in 1:Y\n",
" for k in 1:Z \n",
" println(\"coeff[$i,$j,$k] $(value(coeff[i,j,k]))\")\n",
" end\n",
" end\n",
"end"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.6.3",
"language": "julia",
"name": "julia-1.6"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}