From be1cfaa57e873da181525ca3529e0575801d4eaf Mon Sep 17 00:00:00 2001 From: Laurent Fainsin Date: Tue, 23 Nov 2021 21:13:27 +0100 Subject: [PATCH] feat: ajout du notebook julia d'exemple --- exemple/solution_ciment.ipynb | 130 ++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 exemple/solution_ciment.ipynb diff --git a/exemple/solution_ciment.ipynb b/exemple/solution_ciment.ipynb new file mode 100644 index 0000000..66a1789 --- /dev/null +++ b/exemple/solution_ciment.ipynb @@ -0,0 +1,130 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Exemple de modèle et résolution du problème de Fabrication du ciment" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "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" + ] + } + ], + "source": [ + "#importer les packages utiles\n", + "import Pkg; Pkg.add(\"Cbc\")\n", + "Pkg.add(\"JuMP\")" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "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" + ] + } + ], + "source": [ + "using Cbc\n", + "using JuMP\n", + "\n", + "# data\n", + "N = 2 # nombre de ciments disponibles\n", + "c = [50,70] # prix de vente par ciment\n", + "b = [360,480] # temps de disponibilité du four et de l'atelier\n", + "A = [40 12;20 30] # temps nécessaire de calcination (four) et de broyage (atelier) par ciment\n", + "\n", + "# set optimizer\n", + "model = Model(Cbc.Optimizer)\n", + "\n", + "# define variables\n", + "@variable(model, ciment[1:N] >= 0)\n", + "\n", + "# define objective function\n", + "@objective(model, Max, sum(c[i]*ciment[i] for i in 1:N))\n", + "\n", + "# define constraints\n", + "for i in 1:length(b)\n", + " @constraint(model, sum(A[i,j]*ciment[j] for j in 1:N) <= b[i])\n", + "end\n", + "\n", + "println(model)\n", + "\n", + "# run optimization\n", + "optimize!(model)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Solution obtenue:\n", + "\t benefice = 1137.5\n", + "\t quantite de ciment 1 = 5.249999999999999\n", + "\t quantite de ciment 2 = 12.5\n" + ] + } + ], + "source": [ + "# print solution\n", + "println(\"Solution obtenue:\")\n", + "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" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 1.5.3", + "language": "julia", + "name": "julia-1.5" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "1.5.3" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}