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

169 lines
4.8 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Exemple de modèle et résolution du problème de Fabrication du produits"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"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": [
"Max 0.07 produits[1] + 0.1 produits[2] + 0.19 produits[3] + 0.12 produits[4] + 0.8 produits[5] + 0.14 produits[6]\n",
"Subject to\n",
" produits[1] + produits[2] + produits[3] + produits[4] + produits[5] + produits[6] = 1.0\n",
" produits[1] + produits[2] ≥ 0.45\n",
" produits[3] + produits[4] ≥ 0.3\n",
" 1.7 produits[1] + 1.2 produits[2] + 3.7 produits[3] + 2.4 produits[4] + 2 produits[5] + 2.9 produits[6] ≤ 2.0\n",
" produits[1] ≤ 0.25\n",
" produits[2] ≤ 0.25\n",
" produits[3] ≤ 0.25\n",
" produits[4] ≤ 0.25\n",
" produits[5] ≤ 0.25\n",
" produits[6] ≤ 0.25\n",
" produits[1] ≥ 0.0\n",
" produits[2] ≥ 0.0\n",
" produits[3] ≥ 0.0\n",
" produits[4] ≥ 0.0\n",
" produits[5] ≥ 0.0\n",
" produits[6] ≥ 0.0\n",
"\n",
"Presolve 4 (-6) rows, 6 (0) columns and 16 (-6) elements\n",
"0 Obj 0.016999999 Primal inf 1.349997 (3) Dual inf 1.419994 (6)\n",
"5 Obj 0.28253846\n",
"Optimal - objective value 0.28253846\n",
"After Postsolve, objective 0.28253846, infeasibilities - dual 0 (0), primal 0 (0)\n",
"Optimal objective 0.2825384615 - 5 iterations time 0.002, Presolve 0.00\n"
]
}
],
"source": [
"using Cbc\n",
"using JuMP\n",
"\n",
"# data\n",
"N = 6 # nombre de produits disponibles\n",
"risques = [1.7 1.2 3.7 2.4 2.0 2.9]\n",
"interets = [ 0.07 0.10 0.19 0.12 0.8 0.14]\n",
"\n",
"# set optimizer\n",
"model = Model(Cbc.Optimizer)\n",
"\n",
"# define variables\n",
"@variable(model, produits[1:N] >= 0)\n",
"\n",
"# define objective function\n",
"@objective(model, Max, sum(interets[i] * produits[i] for i = 1:N))\n",
"\n",
"# define constraints\n",
"@constraint(model, sum(produits[i] for i = 1:N) == 1.0) # somme à 100%\n",
"@constraint(model, sum(produits[i]*risques[i] for i = 1:N) <= 2.0) # risque total\n",
"@constraint(model, produits[1] + produits[2] >= 0.45)\n",
"@constraint(model, produits[3] + produits[4] >= 0.3)\n",
"for i = 1:N\n",
" @constraint(model, produits[i] <= 0.25) # limite de 25%\n",
"end\n",
"\n",
"println(model)\n",
"\n",
"# run optimization\n",
"optimize!(model)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Solution obtenue:\n",
"\t benefice = 0.2825384615384615\n",
"\t quantite de produits 1 = 0.2\n",
"\t quantite de produits 2 = 0.25\n",
"\t quantite de produits 3 = 0.10769230769230764\n",
"\t quantite de produits 4 = 0.19230769230769232\n",
"\t quantite de produits 5 = 0.25\n",
"\t quantite de produits 6 = 0.0\n"
]
}
],
"source": [
"# print solution\n",
"println(\"Solution obtenue:\")\n",
"println(\"\\t benefice = $(objective_value(model))\")\n",
"for i = 1:N\n",
" println(\"\\t quantite de produits $i = $(value(produits[i]))\")\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
}