TP-recherche-operationnelle/notebook.ipynb

563 lines
58 KiB
Plaintext
Raw Normal View History

2021-11-26 13:51:01 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# TP 2-3 : Branch-and-bound applied to a knapsack problem"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2021-12-05 15:21:15 +00:00
"## Initialisation (à faire une seule fois)"
2021-11-26 13:51:01 +00:00
]
},
{
"cell_type": "code",
2021-12-05 15:21:15 +00:00
"execution_count": 1,
2021-11-26 13:51:01 +00:00
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
2021-12-05 15:21:15 +00:00
"\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m registry at `~/.julia/registries/General`\n",
2021-11-26 13:51:01 +00:00
"\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n",
"\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",
2021-11-26 16:57:38 +00:00
"\u001b[32m\u001b[1mPrecompiling\u001b[22m\u001b[39m project...\n",
"\u001b[32m ✓ \u001b[39mTestOptinum\n",
2021-12-05 15:21:15 +00:00
" 1 dependency successfully precompiled in 7 seconds (270 already precompiled)\n",
2021-11-26 13:51:01 +00:00
"\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n",
"\u001b[32m\u001b[1m No Changes\u001b[22m\u001b[39m to `~/.julia/environments/v1.6/Project.toml`\n",
2021-11-26 16:57:38 +00:00
"\u001b[32m\u001b[1m No Changes\u001b[22m\u001b[39m to `~/.julia/environments/v1.6/Manifest.toml`\n",
"\u001b[32m\u001b[1mPrecompiling\u001b[22m\u001b[39m project...\n",
"\u001b[32m ✓ \u001b[39mTestOptinum\n",
2021-12-05 15:21:15 +00:00
" 1 dependency successfully precompiled in 4 seconds (270 already precompiled)\n"
2021-11-26 13:51:01 +00:00
]
}
],
"source": [
2021-12-05 15:21:15 +00:00
"import Pkg;\n",
2021-12-05 14:25:55 +00:00
"Pkg.add(\"GraphRecipes\");\n",
2021-12-05 15:21:15 +00:00
"Pkg.add(\"Plots\");\n",
2021-11-26 13:51:01 +00:00
"using GraphRecipes, Plots #only used to visualize the search tree at the end of the branch-and-bound"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2021-12-05 15:21:15 +00:00
"## Récupération des données"
2021-11-26 13:51:01 +00:00
]
},
{
"cell_type": "code",
2021-12-05 15:21:15 +00:00
"execution_count": 2,
2021-11-26 13:51:01 +00:00
"metadata": {},
2021-12-05 15:21:15 +00:00
"outputs": [
{
"data": {
"text/plain": [
"readKnapInstance"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
2021-11-26 13:51:01 +00:00
"source": [
2021-12-05 14:25:55 +00:00
"\"\"\"Open and read a KnapFile.\n",
"\n",
2021-12-05 15:21:15 +00:00
"Args: \\\\\n",
" - filename (String): the name of the file to read.\n",
2021-12-05 14:25:55 +00:00
"\n",
2021-12-05 15:21:15 +00:00
"Returns: \\\\\n",
" - price (Vector{Integer}): prices of items to put in the KnapSack. \\\\\n",
" - weight (Vector{Integer}): weights of items to put in the KnapSack. \\\\\n",
" - capacity (Integer): the maximum capacity of the KnapSack.\n",
2021-12-05 14:25:55 +00:00
"\"\"\"\n",
"function readKnapInstance(filename)\n",
2021-12-05 15:21:15 +00:00
" price = []\n",
" weight = []\n",
" capacity = -1\n",
2021-11-26 13:51:01 +00:00
" open(filename) do f\n",
2021-12-05 15:21:15 +00:00
" for i = 1:3\n",
2021-11-26 13:51:01 +00:00
" tok = split(readline(f))\n",
" if (tok[1] == \"ListPrices=\")\n",
2021-12-05 15:21:15 +00:00
" for i = 2:(length(tok)-1)\n",
" push!(price, parse(Int64, tok[i]))\n",
2021-11-26 13:51:01 +00:00
" end\n",
" elseif (tok[1] == \"ListWeights=\")\n",
2021-12-05 15:21:15 +00:00
" for i = 2:(length(tok)-1)\n",
" push!(weight, parse(Int64, tok[i]))\n",
2021-11-26 13:51:01 +00:00
" end\n",
" elseif (tok[1] == \"Capacity=\")\n",
" capacity = parse(Int64, tok[2])\n",
2021-11-26 13:51:01 +00:00
" else\n",
" println(\"Unknown read :\", tok)\n",
2021-12-05 15:21:15 +00:00
" end\n",
2021-11-26 13:51:01 +00:00
" end\n",
" end\n",
" return price, weight, capacity\n",
2021-12-05 14:25:55 +00:00
"end"
]
},
{
"cell_type": "code",
2021-12-05 15:21:15 +00:00
"execution_count": 3,
2021-12-05 14:25:55 +00:00
"metadata": {},
"outputs": [],
"source": [
"# on teste de lire tous les fichiers .opb\n",
"for (root, dirs, files) in walkdir(\"data\")\n",
" for file in files\n",
" if endswith(file, \".opb\")\n",
" readKnapInstance(root * \"/\" * file)\n",
" end\n",
" end\n",
"end"
2021-11-26 13:51:01 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2021-12-05 15:21:15 +00:00
"## Procédure d'application des tests de sondabilités TA, TO et TR pour le cas de la relaxation linéaire"
2021-11-26 13:51:01 +00:00
]
},
{
"cell_type": "code",
2021-12-05 15:21:15 +00:00
"execution_count": 4,
2021-11-26 13:51:01 +00:00
"metadata": {},
2021-12-05 15:21:15 +00:00
"outputs": [
{
"data": {
"text/plain": [
"TestsSondabilite_relaxlin"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
2021-11-26 13:51:01 +00:00
"source": [
2021-12-05 15:21:15 +00:00
"\"\"\"Test if a node should be pruned.\n",
"\n",
"Args: \\\\\n",
" - x (Vector{Integer}): the node to be tested. \\\\\n",
" - price (Vector{Integer}): prices of items to put in the KnapSack. \\\\\n",
" - weight (Vector{Integer}): weights of items to put in the KnapSack. \\\\\n",
" - capacity (Integer): the maximum capacity of the KnapSack. \\\\\n",
" - BestProfit (Integer): the current BestProfit value. \\\\\n",
" - Bestsol (Integer): the current BestSol values. \\\\\n",
" - affich (Bool): determine if the function should print to stdout.\n",
"\n",
"Returns: \\\\\n",
" - TA (Bool): true if the node is feasible. \\\\\n",
" - TO (Bool): true if the node is optimal. \\\\\n",
" - TR (Bool): true if the node is resolvable. \\\\\n",
" - BestProfit (Integer): the updated value of BestProfit. \\\\\n",
" - Bestsol (Vector{Integer}): the updated values of BestSol.\n",
"\"\"\"\n",
2021-11-27 10:20:11 +00:00
"function TestsSondabilite_relaxlin(x, price, weight, capacity, BestProfit, Bestsol, affich)\n",
2021-11-26 13:51:01 +00:00
" TA, TO, TR = false, false, false\n",
2021-12-05 15:21:15 +00:00
"\n",
" if (!Constraints(x, weight, capacity)) # Test de faisabilite\n",
" TA = true\n",
2021-11-27 10:20:11 +00:00
" if affich\n",
2021-12-05 15:21:15 +00:00
" println(\"TA\\n\")\n",
2021-11-27 10:20:11 +00:00
" end\n",
2021-12-05 15:21:15 +00:00
"\n",
" elseif (Objective(x, price) <= BestProfit) # Test d'optimalite\n",
" TO = true\n",
2021-11-27 10:20:11 +00:00
" if affich\n",
2021-12-05 15:21:15 +00:00
" println(\"TO\\n\")\n",
2021-11-27 10:20:11 +00:00
" end\n",
2021-12-05 15:21:15 +00:00
"\n",
" elseif (AllDef(x)) # Test de resolution\n",
" TR = true\n",
2021-11-27 10:20:11 +00:00
" if affich\n",
2021-12-05 15:21:15 +00:00
" println(\"TR : solution \", \" de profit \", Objective(x, price), \"\\n\")\n",
2021-11-27 10:20:11 +00:00
" end\n",
2021-11-27 10:44:55 +00:00
" if (Objective(x, price) >= BestProfit) # Le profit de la solution trouvée est meilleur que les autres\n",
2021-11-27 10:20:11 +00:00
" if affich\n",
2021-12-05 15:21:15 +00:00
" println(\"\\t-> Cette solution a un meilleur profit.\\n\")\n",
2021-11-27 10:20:11 +00:00
" end\n",
2021-11-27 10:44:55 +00:00
" # On remplace la solution et le profit par les nouvelles valeurs\n",
" Bestsol = x\n",
" BestProfit = Objective(x, price)\n",
2021-11-27 10:20:11 +00:00
" else\n",
" if affich\n",
2021-12-05 15:21:15 +00:00
" println(\"\\t-> Cette solution est moins bonne.\\n\")\n",
2021-11-27 10:20:11 +00:00
" end\n",
2021-11-26 13:51:01 +00:00
" end\n",
2021-12-05 15:21:15 +00:00
"\n",
" elseif affich\n",
" println(\"non sondable\\n\")\n",
2021-11-26 13:51:01 +00:00
" end\n",
2021-12-05 15:21:15 +00:00
"\n",
" return TA, TO, TR, Bestsol, BestProfit\n",
2021-11-26 13:51:01 +00:00
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2021-12-05 15:21:15 +00:00
"## Procédure de séparation et stratégie d'exploration permettant de se placer au prochain noeud à traiter"
2021-11-26 13:51:01 +00:00
]
},
{
"cell_type": "code",
2021-12-05 15:21:15 +00:00
"execution_count": 5,
2021-11-26 13:51:01 +00:00
"metadata": {},
2021-12-05 15:21:15 +00:00
"outputs": [
{
"data": {
"text/plain": [
"SeparerNoeud_relaxlin"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
2021-11-26 13:51:01 +00:00
"source": [
2021-12-05 15:21:15 +00:00
"\"\"\"Split a node in two.\n",
"\n",
"Args: \\\\\n",
" - price (Vector{Integer}): prices of items to put in the KnapSack. \\\\\n",
" - listvars (Vector{Vector{Integer}}): the current values of listvars. \\\\\n",
" - listvals (Vector{Integer}): the current values of listvals.\n",
"\n",
"Returns: \\\\\n",
" - listvars (Vector{Vector{Integer}}): the updated values of listvars. \\\\\n",
" - listvals (Vector{Integer}): the updated values of listvals.\n",
"\"\"\"\n",
2021-11-26 16:57:38 +00:00
"function SeparerNoeud_relaxlin(price, listvars, listvals)\n",
2021-11-27 10:44:55 +00:00
" # Le noeud est non-sondable. Appliquer le critère de séparation pour le séparer en sous-noeuds \n",
2021-11-26 16:57:38 +00:00
"\n",
" # Cas du noeud le plus à gauche\n",
2021-11-27 10:44:55 +00:00
"\n",
" # On sépare le noeud actuel en 2 sous-noeuds\n",
2021-11-27 10:20:11 +00:00
" predX = pop!(listvars)\n",
" nextX0 = copy(predX)\n",
" nextX1 = copy(predX)\n",
2021-11-27 10:44:55 +00:00
"\n",
" # On initialise leurs valeurs à zéro\n",
2021-11-26 16:57:38 +00:00
" val0 = 0\n",
" val1 = 0\n",
2021-11-27 10:44:55 +00:00
"\n",
" # On fixe la nouvelle variable des deux sous-noeuds\n",
" n = length(predX)\n",
2021-12-05 15:21:15 +00:00
" for i = 1:n\n",
2021-11-26 16:57:38 +00:00
" if predX[i] == -1\n",
2021-11-27 10:44:55 +00:00
" # L'un a zéro\n",
2021-11-26 16:57:38 +00:00
" nextX0[i] = 0\n",
2021-11-27 10:44:55 +00:00
" # L'autre a un\n",
2021-11-26 16:57:38 +00:00
" nextX1[i] = 1\n",
2021-11-26 13:51:01 +00:00
"\n",
2021-11-27 10:44:55 +00:00
" # On calcule leurs valeurs\n",
2021-11-26 16:57:38 +00:00
" val0 = Objective(nextX0, price)\n",
" val1 = Objective(nextX1, price)\n",
" break\n",
" end\n",
" end\n",
2021-12-05 15:21:15 +00:00
"\n",
2021-11-27 10:44:55 +00:00
" # On ajoute les sous-noeuds a la pile des noeuds a explorer\n",
2021-11-27 10:20:11 +00:00
" push!(listvars, nextX0)\n",
2021-11-27 15:33:20 +00:00
" push!(listvars, nextX1)\n",
2021-11-27 10:44:55 +00:00
"\n",
" # On ajoute aussi leurs valeurs\n",
2021-11-27 10:20:11 +00:00
" push!(listvals, val0)\n",
2021-11-27 15:33:20 +00:00
" push!(listvals, val1)\n",
2021-11-27 10:44:55 +00:00
"\n",
2021-12-05 15:21:15 +00:00
" return listvars, listvals\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ExplorerAutreNoeud_relaxlin"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\"\"\"Pop node fom the list to explore another node.\n",
2021-11-26 13:51:01 +00:00
"\n",
2021-12-05 15:21:15 +00:00
"Args: \\\\\n",
" - price (Vector{Integer}): prices of items to put in the KnapSack. \\\\\n",
" - listvars (Vector{Vector{Integer}}): the current values of listvars. \\\\\n",
" - listvals (Vector{Integer}): the current values of listvals.\n",
2021-11-26 13:51:01 +00:00
"\n",
2021-12-05 15:21:15 +00:00
"Returns: \\\\\n",
" - listvars (Vector{Vector{Integer}}): the updated values of listvars. \\\\\n",
" - listvals (Vector{Integer}): the updated values of listvals. \\\\\n",
" - stop (Bool): true if the tree search is finished.\n",
"\"\"\"\n",
2021-11-26 16:57:38 +00:00
"function ExplorerAutreNoeud_relaxlin(listvars, listvals)\n",
2021-11-27 10:44:55 +00:00
" # Le noeud est sondable, on l'enlève de la pile des noeuds à sonder\n",
2021-12-05 15:21:15 +00:00
"\n",
2021-11-26 16:57:38 +00:00
" stop = false\n",
" if (length(listvars) > 1)\n",
2021-11-27 10:44:55 +00:00
" # On passe au noeud suivant\n",
2021-11-26 16:57:38 +00:00
" var = pop!(listvars)\n",
" val = pop!(listvals)\n",
2021-11-26 13:51:01 +00:00
" else\n",
2021-11-27 10:44:55 +00:00
" # Il n'y a pas d'autre noeud\n",
2021-11-26 16:57:38 +00:00
" stop = true\n",
2021-11-26 13:51:01 +00:00
" end\n",
2021-11-27 10:44:55 +00:00
"\n",
2021-12-05 15:21:15 +00:00
" return listvars, listvals, stop\n",
2021-11-26 13:51:01 +00:00
"end"
]
},
2021-12-05 15:21:15 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Fonctions décrivant l'objectif et les contraintes"
]
},
2021-11-26 13:51:01 +00:00
{
"cell_type": "code",
2021-12-05 15:21:15 +00:00
"execution_count": 14,
2021-11-26 13:51:01 +00:00
"metadata": {},
2021-12-05 15:21:15 +00:00
"outputs": [
{
"data": {
"text/plain": [
"AllDef (generic function with 1 method)"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
2021-11-26 13:51:01 +00:00
"source": [
2021-11-27 10:44:55 +00:00
"# Fonction objectif que l'on souhaite maximiser/minimiser (évalué dans le meilleur des cas)\n",
2021-12-05 15:21:15 +00:00
"Objective(x, price) =\n",
" sum(\n",
" if x[i] < 0\n",
2021-11-27 10:20:11 +00:00
" price[i]\n",
" else\n",
2021-12-05 15:21:15 +00:00
" price[i] * x[i]\n",
" end\n",
2021-12-05 15:21:15 +00:00
" for i = 1:length(x)\n",
" )\n",
2021-11-26 13:51:01 +00:00
"\n",
2021-11-27 10:44:55 +00:00
"# Fonction permettant de vérfier toutes les contraintes du modèle (dans le meilleur des cas)\n",
"Constraints(x, weight, capacity) =\n",
" sum(\n",
" if x[i] < 0\n",
" 0\n",
2021-12-05 15:21:15 +00:00
" else\n",
" weight[i] * x[i]\n",
2021-11-26 13:51:01 +00:00
" end\n",
2021-12-05 15:21:15 +00:00
" for i = 1:length(x)\n",
" ) <= capacity\n",
2021-11-26 13:51:01 +00:00
"\n",
2021-11-27 10:44:55 +00:00
"# Fonction qui nous dis si toutes les variables de x sont fixées\n",
2021-11-27 10:20:11 +00:00
"function AllDef(x)\n",
2021-12-05 15:21:15 +00:00
" for i = 1:length(x)\n",
2021-11-27 10:20:11 +00:00
" if x[i] < 0\n",
" return false\n",
" end\n",
2021-11-26 13:51:01 +00:00
" end\n",
2021-11-27 10:20:11 +00:00
" return true\n",
"end"
2021-11-26 13:51:01 +00:00
]
2021-11-26 16:57:38 +00:00
},
2021-12-05 15:21:15 +00:00
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Résolution du problème KnapSack"
]
},
2021-11-26 16:57:38 +00:00
{
"cell_type": "code",
2021-12-05 15:21:15 +00:00
"execution_count": 15,
2021-11-26 16:57:38 +00:00
"metadata": {},
2021-12-05 15:21:15 +00:00
"outputs": [
{
"data": {
"text/plain": [
"SolveKnapInstance"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
2021-11-26 16:57:38 +00:00
"source": [
2021-12-05 15:21:15 +00:00
"\"\"\"Solve the KnapSack problem for the data contained in `filename`.\n",
"\n",
"Args: \\\\\n",
" - filename (String): the name of the file to read.\n",
"\n",
"Returns: \\\\\n",
" - trParentnodes (Vector{Integer}): the parents nodes, to plot the tree.\n",
" - trChildnodes (Vector{Integer}): the child nodes, to plot the tree.\n",
" - trNamenodes (Vector{Integer}): the name of the nodes, to plot the tree.\n",
"\"\"\"\n",
2021-11-26 16:57:38 +00:00
"function SolveKnapInstance(filename)\n",
"\n",
2021-12-05 15:21:15 +00:00
" stop = false\n",
" affich = false\n",
"\n",
2021-11-27 15:33:20 +00:00
" # Extraction des données\n",
2021-11-26 16:57:38 +00:00
" price, weight, capacity = readKnapInstance(filename)\n",
"\n",
2021-12-05 15:21:15 +00:00
" if affich\n",
" println(\"Capacity : \", capacity, \" | Number of objects : \", length(price), \"\\n\")\n",
" end\n",
2021-11-27 10:20:11 +00:00
"\n",
2021-11-27 15:33:20 +00:00
" # Pour dessiner le graph\n",
" trParentnodes = Int64[]\n",
" trChildnodes = Int64[]\n",
" trNamenodes = []\n",
"\n",
2021-11-27 10:44:55 +00:00
" # Liste des variable pour naviguer de noeuds en noeuds\n",
2021-11-27 15:33:20 +00:00
" listvars = []\n",
" listvals = []\n",
" listnodes = []\n",
2021-11-26 16:57:38 +00:00
"\n",
2021-11-27 10:44:55 +00:00
" # La meilleur solution et sa valeur\n",
2021-11-27 15:33:20 +00:00
" BestProfit = -1\n",
" Bestsol = []\n",
2021-11-26 16:57:38 +00:00
"\n",
2021-11-27 10:44:55 +00:00
" # Compter le nombre de noeud explorés\n",
" current_node_number = 0\n",
"\n",
" # On ajoute le premier noeud à explorer (la racine)\n",
2021-11-26 16:57:38 +00:00
" push!(listvars, [-1 for p in price])\n",
2021-11-27 10:20:11 +00:00
" push!(listvals, Objective(last(listvars), price))\n",
2021-11-27 15:33:20 +00:00
" push!(listnodes, 1)\n",
" push!(trNamenodes, 0)\n",
" newnodeid = 2\n",
2021-11-26 16:57:38 +00:00
"\n",
" while (!stop)\n",
2021-11-27 15:33:20 +00:00
"\n",
2021-11-27 10:44:55 +00:00
" # Le noeud actuel\n",
2021-11-27 10:20:11 +00:00
" x = last(listvars)\n",
2021-11-26 16:57:38 +00:00
"\n",
2021-12-05 15:21:15 +00:00
" if affich && current_node_number % 10000 == 0\n",
" println(\"----------\\nNode n°\", current_node_number, \" :\\n\")\n",
" println(\"Previous Solution memorized \", \" with bestprofit \", BestProfit, \"\\n\")\n",
2021-11-26 16:57:38 +00:00
" end\n",
"\n",
2021-11-27 10:44:55 +00:00
" # Test de sondabilité du noeud actuel\n",
" # -> On mets a jour la solution et sa valeur si besoin\n",
2021-11-27 10:20:11 +00:00
" TA, TO, TR, Bestsol, BestProfit = TestsSondabilite_relaxlin(x, price, weight, capacity, BestProfit, Bestsol, affich)\n",
2021-12-05 15:21:15 +00:00
"\n",
2021-11-26 16:57:38 +00:00
" is_node_sondable = TA || TO || TR\n",
" if (!is_node_sondable)\n",
2021-11-27 10:44:55 +00:00
" # Le noeud n'est pas sondable, on le sépare en 2 sous-noeuds\n",
2021-11-26 16:57:38 +00:00
" listvars, listvals = SeparerNoeud_relaxlin(price, listvars, listvals)\n",
2021-11-27 15:33:20 +00:00
"\n",
" curnode = pop!(listnodes)\n",
"\n",
" push!(trParentnodes, curnode)\n",
" push!(trParentnodes, curnode)\n",
"\n",
2021-12-05 15:21:15 +00:00
" push!(listnodes, newnodeid + 1)\n",
2021-11-27 15:33:20 +00:00
" push!(listnodes, newnodeid)\n",
"\n",
" push!(trChildnodes, newnodeid)\n",
2021-12-05 15:21:15 +00:00
" push!(trChildnodes, newnodeid + 1)\n",
2021-11-27 15:33:20 +00:00
"\n",
2021-12-05 15:21:15 +00:00
" push!(trNamenodes, newnodeid - 1)\n",
2021-11-27 15:33:20 +00:00
" push!(trNamenodes, newnodeid)\n",
"\n",
" newnodeid += 2\n",
"\n",
2021-11-26 16:57:38 +00:00
" else\n",
2021-11-27 10:44:55 +00:00
" # Le noeud est sondable, on passe au noeud suivant\n",
2021-11-26 16:57:38 +00:00
" listvars, listvals, stop = ExplorerAutreNoeud_relaxlin(listvars, listvals)\n",
2021-11-27 15:33:20 +00:00
"\n",
" pop!(listnodes)\n",
2021-11-26 16:57:38 +00:00
" end\n",
"\n",
" current_node_number += 1\n",
" end\n",
"\n",
2021-12-05 15:21:15 +00:00
" if affich\n",
" println(\"\\n******\\n\\nOptimal value = \", BestProfit, \"\\n\\nOptimal x = \", Bestsol)\n",
" end\n",
" \n",
" return trParentnodes, trChildnodes, trNamenodes\n",
2021-11-27 15:33:20 +00:00
"end"
]
},
2021-12-05 15:21:15 +00:00
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"600\" height=\"400\" viewBox=\"0 0 2400 1600\">\n<defs>\n <clipPath id=\"clip020\">\n <rect x=\"0\" y=\"0\" width=\"2400\" height=\"1600\"/>\n </clipPath>\n</defs>\n<path clip-path=\"url(#clip020)\" d=\"\nM0 1600 L2400 1600 L2400 0 L0 0 Z\n \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n<defs>\n <clipPath id=\"clip021\">\n <rect x=\"480\" y=\"0\" width=\"1681\" height=\"1600\"/>\n </clipPath>\n</defs>\n<path clip-path=\"url(#clip020)\" d=\"\nM447.244 1552.76 L1952.76 1552.76 L1952.76 47.2441 L447.244 47.2441 Z\n \" fill=\"#ffffff\" fill-rule=\"evenodd\" fill-opacity=\"1\"/>\n<defs>\n <clipPath id=\"clip022\">\n <rect x=\"447\" y=\"47\" width=\"1507\" height=\"1507\"/>\n </clipPath>\n</defs>\n<polyline clip-path=\"url(#clip022)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 1202,262.317 1202.54,272.583 1204.12,282.81 1206.66,293.002 1210.08,303.16 1214.3,313.289 1219.26,323.39 1224.87,333.466 1231.05,343.522 1237.73,353.558 \n 1244.83,363.579 1252.28,373.587 1260,383.585 1267.91,393.576 1275.93,403.563 1283.99,413.548 1292.01,423.534 1299.92,433.525 1307.64,443.523 1315.09,453.531 \n 1322.19,463.552 1328.87,473.589 1335.05,483.644 1340.66,493.721 1345.62,503.822 1349.84,513.95 1353.26,524.109 1355.8,534.3 1357.38,544.527 1357.92,554.793 \n \n \"/>\n<polyline clip-path=\"url(#clip022)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 1202,262.317 1201.46,272.171 1199.91,281.977 1197.42,291.737 1194.07,301.456 1189.92,311.137 1185.06,320.784 1179.56,330.4 1173.5,339.989 1166.94,349.555 \n 1159.97,359.1 1152.67,368.63 1145.1,378.147 1137.34,387.655 1129.47,397.157 1121.56,406.658 1113.69,416.161 1105.93,425.669 1098.36,435.186 1091.05,444.715 \n 1084.08,454.261 1077.53,463.827 1071.46,473.416 1065.96,483.032 1061.1,492.679 1056.96,502.36 1053.6,512.079 1051.11,521.839 1049.56,531.644 1049.03,541.499 \n \n \"/>\n<polyline clip-path=\"url(#clip022)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 1357.92,554.793 1357.8,560.931 1357.42,567.175 1356.83,573.519 1356.02,579.953 1355.03,586.472 1353.87,593.065 1352.55,599.727 1351.09,606.447 1349.52,613.22 \n 1347.85,620.036 1346.1,626.887 1344.29,633.767 1342.43,640.666 1340.54,647.577 1338.65,654.493 1336.76,661.404 1334.9,668.303 1333.09,675.183 1331.34,682.035 \n 1329.67,688.851 1328.1,695.623 1326.65,702.344 1325.33,709.005 1324.16,715.599 1323.17,722.117 1322.37,728.552 1321.77,734.895 1321.4,741.14 1321.27,747.277 \n \n \"/>\n<polyline clip-path=\"url(#clip022)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 1357.92,554.793 1358.42,562.835 1359.85,570.771 1362.16,578.609 1365.28,586.358 1369.12,594.025 1373.63,601.617 1378.74,609.143 1384.36,616.61 1390.44,624.027 \n 1396.91,631.4 1403.69,638.739 1410.71,646.05 1417.91,653.341 1425.21,660.621 1432.55,667.897 1439.85,675.177 1447.05,682.469 1454.07,689.78 1460.85,697.118 \n 1467.31,704.492 1473.39,711.908 1479.02,719.376 1484.12,726.902 1488.63,734.494 1492.48,742.16 1495.59,749.909 1497.9,757.747 1499.34,765.684 1499.83,773.725 \n \n \"/>\n<polyline clip-path=\"url(#clip022)\" style=\"stroke:#000000; stroke-linecap:butt; stroke-linejoin:round; stroke-width:4; stroke-opacity:1; fill:none\" points=\"\n 1499.83,773.725 1499.72,782.018 1499.4,790.473 1498.89,799.08 1498.2,807.826 1497.34,816.698 1496.34,825.686 1495.2,834.776 1493.95,843.956 1492.6,853.215 \n 1491.16,862.541 1489.65,871.921 1488.09,881.343 1486.49,890.796 1484.87,900.266 1483.24,909.743 1481.61,919.213 1480.01,928.666 1478.45,938.088 1476.94,947.468 \n 1475.5,956.794 1474.15,966.053 1472.9,975.233 1471.77,984.323 1470.76,993.311 14
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"trParentnodes, trChildnodes, trNamenodes = SolveKnapInstance(\"data/test.opb\")\n",
"graphplot(trParentnodes, trChildnodes, names = trNamenodes, method = :tree)"
]
},
2021-11-27 15:33:20 +00:00
{
"cell_type": "code",
2021-12-05 14:25:55 +00:00
"execution_count": null,
2021-11-27 15:33:20 +00:00
"metadata": {},
"outputs": [],
"source": [
2021-12-05 15:21:15 +00:00
"# # on teste de résoudre tous les fichiers .opb\n",
"# for (root, dirs, files) in walkdir(\"data\")\n",
"# for file in files\n",
"# if endswith(file, \".opb\")\n",
"# SolveKnapInstance(root * \"/\" * file)\n",
"# end\n",
"# end\n",
"# end"
2021-11-26 16:57:38 +00:00
]
2021-11-26 13:51:01 +00:00
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.6.3",
"language": "julia",
"name": "julia-1.6"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
2021-12-05 14:25:55 +00:00
"version": "1.6.3"
2021-11-26 13:51:01 +00:00
}
},
"nbformat": 4,
"nbformat_minor": 4
}