From 9f1515c8b8ce5006359d0fab744435c2c481f598 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laure=CE=B7t?= Date: Tue, 23 Nov 2021 22:56:53 +0100 Subject: [PATCH] feat: ajout de deux notebooks --- exemple/solution_ciment.ipynb | 80 +++++++++------ exo1/voitures.ipynb | 186 ++++++++++++++++++++++++++++++++++ exo3/bourse.ipynb | 168 ++++++++++++++++++++++++++++++ 3 files changed, 403 insertions(+), 31 deletions(-) create mode 100644 exo1/voitures.ipynb create mode 100644 exo3/bourse.ipynb diff --git a/exemple/solution_ciment.ipynb b/exemple/solution_ciment.ipynb index 66a1789..8c5b95f 100644 --- a/exemple/solution_ciment.ipynb +++ b/exemple/solution_ciment.ipynb @@ -9,43 +9,68 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ - "\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n", - "\u001b[32m\u001b[1mNo Changes\u001b[22m\u001b[39m to `~/.julia/environments/v1.5/Project.toml`\n", - "\u001b[32m\u001b[1mNo Changes\u001b[22m\u001b[39m to `~/.julia/environments/v1.5/Manifest.toml`\n", - "\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n", - "\u001b[32m\u001b[1mNo Changes\u001b[22m\u001b[39m to `~/.julia/environments/v1.5/Project.toml`\n", - "\u001b[32m\u001b[1mNo Changes\u001b[22m\u001b[39m to `~/.julia/environments/v1.5/Manifest.toml`\n" + "\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; Pkg.add(\"Cbc\")\n", + "import Pkg;\n", + "Pkg.add(\"Cbc\")\n", "Pkg.add(\"JuMP\")" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 8, "metadata": {}, "outputs": [ { - "ename": "LoadError", - "evalue": "\u001b[91mMethodError: no method matching Model(::Type{Cbc.Optimizer})\u001b[39m\n\u001b[91m\u001b[0mClosest candidates are:\u001b[39m\n\u001b[91m\u001b[0m Model(::Any, \u001b[91m::Any\u001b[39m, \u001b[91m::Any\u001b[39m, \u001b[91m::Any\u001b[39m, \u001b[91m::Any\u001b[39m, \u001b[91m::Any\u001b[39m, \u001b[91m::Any\u001b[39m, \u001b[91m::Any\u001b[39m) at /Users/sungueve/.julia/packages/JuMP/MsUSY/src/JuMP.jl:126\u001b[39m\n\u001b[91m\u001b[0m Model(; caching_mode, solver) at /Users/sungueve/.julia/packages/JuMP/MsUSY/src/JuMP.jl:161\u001b[39m\n\u001b[91m\u001b[0m Model(\u001b[91m::MathOptInterface.AbstractOptimizer\u001b[39m, \u001b[91m::Dict{MathOptInterface.ConstraintIndex,AbstractShape}\u001b[39m, \u001b[91m::Set{Any}\u001b[39m, \u001b[91m::Any\u001b[39m, \u001b[91m::Any\u001b[39m, \u001b[91m::Dict{Symbol,Any}\u001b[39m, \u001b[91m::Int64\u001b[39m, \u001b[91m::Dict{Symbol,Any}\u001b[39m) at /Users/sungueve/.julia/packages/JuMP/MsUSY/src/JuMP.jl:126\u001b[39m\n\u001b[91m\u001b[0m ...\u001b[39m", - "output_type": "error", - "traceback": [ - "\u001b[91mMethodError: no method matching Model(::Type{Cbc.Optimizer})\u001b[39m\n\u001b[91m\u001b[0mClosest candidates are:\u001b[39m\n\u001b[91m\u001b[0m Model(::Any, \u001b[91m::Any\u001b[39m, \u001b[91m::Any\u001b[39m, \u001b[91m::Any\u001b[39m, \u001b[91m::Any\u001b[39m, \u001b[91m::Any\u001b[39m, \u001b[91m::Any\u001b[39m, \u001b[91m::Any\u001b[39m) at /Users/sungueve/.julia/packages/JuMP/MsUSY/src/JuMP.jl:126\u001b[39m\n\u001b[91m\u001b[0m Model(; caching_mode, solver) at /Users/sungueve/.julia/packages/JuMP/MsUSY/src/JuMP.jl:161\u001b[39m\n\u001b[91m\u001b[0m Model(\u001b[91m::MathOptInterface.AbstractOptimizer\u001b[39m, \u001b[91m::Dict{MathOptInterface.ConstraintIndex,AbstractShape}\u001b[39m, \u001b[91m::Set{Any}\u001b[39m, \u001b[91m::Any\u001b[39m, \u001b[91m::Any\u001b[39m, \u001b[91m::Dict{Symbol,Any}\u001b[39m, \u001b[91m::Int64\u001b[39m, \u001b[91m::Dict{Symbol,Any}\u001b[39m) at /Users/sungueve/.julia/packages/JuMP/MsUSY/src/JuMP.jl:126\u001b[39m\n\u001b[91m\u001b[0m ...\u001b[39m", - "", - "Stacktrace:", - " [1] top-level scope at In[3]:11", - " [2] include_string(::Function, ::Module, ::String, ::String) at ./loading.jl:1091" + "name": "stdout", + "output_type": "stream", + "text": [ + "Max 50 ciment[1] + 70 ciment[2]\n", + "Subject to\n", + " 40 ciment[1] + 12 ciment[2] ≤ 360.0\n", + " 20 ciment[1] + 30 ciment[2] ≤ 480.0\n", + " ciment[1] ≥ 0.0\n", + " ciment[2] ≥ 0.0\n", + "\n", + "Presolve 2 (0) rows, 2 (0) columns and 4 (0) elements\n", + "0 Obj -0 Dual inf 120 (2)\n", + "2 Obj 1137.5\n", + "Optimal - objective value 1137.5\n", + "Optimal objective 1137.5 - 2 iterations time 0.002\n" ] } ], @@ -76,12 +101,12 @@ "println(model)\n", "\n", "# run optimization\n", - "optimize!(model)\n" + "optimize!(model)" ] }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -101,28 +126,21 @@ "println(\"\\t benefice = $(objective_value(model))\")\n", "for i in 1:N\n", " println(\"\\t quantite de ciment $i = $(value(ciment[i]))\")\n", - "end\n" + "end" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { "kernelspec": { - "display_name": "Julia 1.5.3", + "display_name": "Julia 1.6.3", "language": "julia", - "name": "julia-1.5" + "name": "julia-1.6" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", - "version": "1.5.3" + "version": "1.6.3" } }, "nbformat": 4, diff --git a/exo1/voitures.ipynb b/exo1/voitures.ipynb new file mode 100644 index 0000000..de20602 --- /dev/null +++ b/exo1/voitures.ipynb @@ -0,0 +1,186 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Exemple de modèle et résolution du problème de Fabrication du voitures" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "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": 7, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Max 10000 voitures[1] + 9000 voitures[2]\n", + "Subject to\n", + " 10 voitures[1] + 20 voitures[2] ≤ 15000.0\n", + " 6 voitures[1] + 5 voitures[2] ≤ 6000.0\n", + " voitures[1] ≤ 800.0\n", + " voitures[1] ≥ 0.0\n", + " voitures[2] ≥ 0.0\n", + " voitures[1] integer\n", + " voitures[2] integer\n", + "\n", + "Welcome to the CBC MILP Solver \n", + "Version: 2.10.5 \n", + "Build Date: Jan 1 1970 \n", + "\n", + "command line - Cbc_C_Interface -solve -quit (default strategy 1)\n", + "Continuous objective value is 1.02857e+07 - 0.00 seconds\n", + "Cgl0004I processed model has 2 rows, 2 columns (2 integer (0 of which binary)) and 4 elements\n", + "Cutoff increment increased from 1e-05 to 1000\n", + "Cbc0012I Integer solution of -10272000 found by DiveCoefficient after 0 iterations and 0 nodes (0.00 seconds)\n", + "Cbc0012I Integer solution of -10284000 found by DiveCoefficient after 3 iterations and 0 nodes (0.01 seconds)\n", + "Cbc0031I 1 added rows had average density of 2\n", + "Cbc0013I At root node, 1 cuts changed objective from -10285714 to -10284000 in 21 passes\n", + "Cbc0014I Cut generator 0 (Probing) - 0 row cuts average 0.0 elements, 47 column cuts (47 active) in 0.001 seconds - new frequency is 1\n", + "Cbc0014I Cut generator 1 (Gomory) - 4 row cuts average 2.0 elements, 0 column cuts (0 active) in 0.001 seconds - new frequency is 1\n", + "Cbc0014I Cut generator 2 (Knapsack) - 0 row cuts average 0.0 elements, 0 column cuts (0 active) in 0.000 seconds - new frequency is -100\n", + "Cbc0014I Cut generator 3 (Clique) - 0 row cuts average 0.0 elements, 0 column cuts (0 active) in 0.000 seconds - new frequency is -100\n", + "Cbc0014I Cut generator 4 (MixedIntegerRounding2) - 0 row cuts average 0.0 elements, 0 column cuts (0 active) in 0.000 seconds - new frequency is -100\n", + "Cbc0014I Cut generator 5 (FlowCover) - 0 row cuts average 0.0 elements, 0 column cuts (0 active) in 0.000 seconds - new frequency is -100\n", + "Cbc0001I Search completed - best objective -10284000, took 3 iterations and 0 nodes (0.01 seconds)\n", + "Cbc0035I Maximum depth 0, 0 variables fixed on reduced cost\n", + "Cuts at root node changed objective from -1.02857e+07 to -1.0284e+07\n", + "Probing was tried 21 times and created 47 cuts of which 0 were active after adding rounds of cuts (0.001 seconds)\n", + "Gomory was tried 21 times and created 4 cuts of which 0 were active after adding rounds of cuts (0.001 seconds)\n", + "Knapsack was tried 21 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)\n", + "Clique was tried 21 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)\n", + "MixedIntegerRounding2 was tried 21 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)\n", + "FlowCover was tried 21 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)\n", + "TwoMirCuts was tried 1 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)\n", + "ZeroHalf was tried 1 times and created 0 cuts of which 0 were active after adding rounds of cuts (0.000 seconds)\n", + "\n", + "Result - Optimal solution found\n", + "\n", + "Objective value: 10284000.00000000\n", + "Enumerated nodes: 0\n", + "Total iterations: 3\n", + "Time (CPU seconds): 0.01\n", + "Time (Wallclock seconds): 0.01\n", + "\n", + "Total time (CPU seconds): 0.01 (Wallclock seconds): 0.01\n", + "\n" + ] + } + ], + "source": [ + "using Cbc\n", + "using JuMP\n", + "\n", + "# data\n", + "N = 2 # nombre de voitures disponibles\n", + "c = [10000, 9000] # prix de vente par voitures\n", + "b = [15000, 6000, 800] \n", + "A = [10 20 ; 6 5 ; 1 0]\n", + "\n", + "# set optimizer\n", + "model = Model(Cbc.Optimizer)\n", + "\n", + "# define variables\n", + "@variable(model, voitures[1:N] >= 0, integer=true)\n", + "\n", + "# define objective function\n", + "@objective(model, Max, sum(c[i] * voitures[i] for i = 1:N))\n", + "\n", + "# define constraints\n", + "for i = 1:length(b)\n", + " @constraint(model, sum(A[i, j] * voitures[j] for j = 1:N) <= b[i])\n", + "end\n", + "\n", + "println(model)\n", + "\n", + "# run optimization\n", + "optimize!(model)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Solution obtenue:\n", + "\t benefice = 1.0284e7\n", + "\t quantite de voitures 1 = 645.0\n", + "\t quantite de voitures 2 = 426.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 voitures $i = $(value(voitures[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 +} diff --git a/exo3/bourse.ipynb b/exo3/bourse.ipynb new file mode 100644 index 0000000..a5f044b --- /dev/null +++ b/exo3/bourse.ipynb @@ -0,0 +1,168 @@ +{ + "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 +}