392 lines
12 KiB
Plaintext
392 lines
12 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# TP 2-3 : Branch-and-bound applied to a knapsack problem"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Initialisation (à faire une seule fois)"
|
|
]
|
|
},
|
|
{
|
|
"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",
|
|
"\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",
|
|
"\u001b[32m\u001b[1mPrecompiling\u001b[22m\u001b[39m project...\n",
|
|
"\u001b[32m ✓ \u001b[39mTestOptinum\n",
|
|
" 1 dependency successfully precompiled in 6 seconds (158 already precompiled)\n",
|
|
"\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",
|
|
"\u001b[32m\u001b[1mPrecompiling\u001b[22m\u001b[39m project...\n",
|
|
"\u001b[32m ✓ \u001b[39mTestOptinum\n",
|
|
" 1 dependency successfully precompiled in 3 seconds (158 already precompiled)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import Pkg; \n",
|
|
"Pkg.add(\"GraphRecipes\"); Pkg.add(\"Plots\"); \n",
|
|
"using GraphRecipes, Plots #only used to visualize the search tree at the end of the branch-and-bound"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Récupération des données"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"readKnapInstance (generic function with 1 method)"
|
|
]
|
|
},
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"function readKnapInstance(filename)\n",
|
|
" price=[]\n",
|
|
" weight=[]\n",
|
|
" capacity = -1\n",
|
|
" open(filename) do f\n",
|
|
" for i in 1:3\n",
|
|
" tok = split(readline(f))\n",
|
|
" if (tok[1] == \"ListPrices=\")\n",
|
|
" for i in 2:(length(tok)-1)\n",
|
|
" push!(price,parse(Int64, tok[i]))\n",
|
|
" end\n",
|
|
" elseif (tok[1] == \"ListWeights=\")\n",
|
|
" for i in 2:(length(tok)-1)\n",
|
|
" push!(weight,parse(Int64, tok[i]))\n",
|
|
" end\n",
|
|
" elseif (tok[1] == \"Capacity=\")\n",
|
|
" capacity = parse(Int64, tok[2])\n",
|
|
" else\n",
|
|
" println(\"Unknown read :\", tok)\n",
|
|
" end \n",
|
|
" end\n",
|
|
" end\n",
|
|
" \n",
|
|
" return price, weight, capacity\n",
|
|
"end\n",
|
|
"\n",
|
|
"# readKnapInstance(\"data/test.opb\")\n",
|
|
"# readKnapInstance(\"data/almost_strongly_correlated/knapPI_5_50_1000_1_-2096.opb\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Procédure d'application des tests de sondabilités TA, TO et TR pour le cas de la relaxation linéaire"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"TestsSondabilite_relaxlin (generic function with 1 method)"
|
|
]
|
|
},
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"function TestsSondabilite_relaxlin(x, price, weight, capacity, BestProfit, Bestsol, affich)\n",
|
|
" TA, TO, TR = false, false, false\n",
|
|
" if (!Constraints(x, weight, capacity)) # Test de faisabilite\n",
|
|
" TA = true\n",
|
|
" if affich\n",
|
|
" println(\"TA\")\n",
|
|
" end\n",
|
|
" elseif (Objective(x, price) <= BestProfit) # Test d'optimalite\n",
|
|
" TO = true\n",
|
|
" if affich\n",
|
|
" println(\"TO\")\n",
|
|
" end\n",
|
|
" elseif (AllDef(x)) # Test de resolution\n",
|
|
" TR = true\n",
|
|
" if affich\n",
|
|
" println(\"TR : solution \", \" de profit \", Objective(x, price))\n",
|
|
" end\n",
|
|
" #if (value(benef) >= BestProfit)\n",
|
|
" if (Objective(x, price) >= BestProfit)\n",
|
|
" if affich\n",
|
|
" println(\"\\t-> Cette solution a un meilleur profit.\")\n",
|
|
" end\n",
|
|
" Bestsol = x\n",
|
|
" #BestProfit=value(benef)\n",
|
|
" BestProfit = Objective(x, price)\n",
|
|
" else\n",
|
|
" if affich\n",
|
|
" println(\"\\t-> Cette solution est moins bonne.\")\n",
|
|
" end\n",
|
|
" end\n",
|
|
" else\n",
|
|
" if affich \n",
|
|
" println(\"non sondable\")\n",
|
|
" end\n",
|
|
" end\n",
|
|
" if affich\n",
|
|
" println(\"\\n\")\n",
|
|
" end\n",
|
|
" TA, TO, TR, Bestsol, BestProfit\n",
|
|
"end"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"### Procédure de séparation et stratégie d'exploration permettant de se placer au prochain noeud à traiter"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"ExplorerAutreNoeud_relaxlin (generic function with 1 method)"
|
|
]
|
|
},
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"\n",
|
|
"function SeparerNoeud_relaxlin(price, listvars, listvals)\n",
|
|
" # le noeud est non-sondable. Appliquer le critère de séparation pour le séparer en sous-noeuds et choisir un noeud-fils le plus à gauche \n",
|
|
"\n",
|
|
" # Cas du noeud le plus à gauche\n",
|
|
" predX = pop!(listvars)\n",
|
|
" n = length(predX)\n",
|
|
" nextX0 = copy(predX)\n",
|
|
" nextX1 = copy(predX)\n",
|
|
" val0 = 0\n",
|
|
" val1 = 0\n",
|
|
" for i in 1:n\n",
|
|
" if predX[i] == -1\n",
|
|
" nextX0[i] = 0\n",
|
|
" nextX1[i] = 1\n",
|
|
"\n",
|
|
" val0 = Objective(nextX0, price)\n",
|
|
" val1 = Objective(nextX1, price)\n",
|
|
" break\n",
|
|
" end\n",
|
|
" end\n",
|
|
" push!(listvars, nextX1)\n",
|
|
" push!(listvars, nextX0)\n",
|
|
" push!(listvals, val1)\n",
|
|
" push!(listvals, val0)\n",
|
|
" listvars, listvals\n",
|
|
"end\n",
|
|
"\n",
|
|
"\n",
|
|
"function ExplorerAutreNoeud_relaxlin(listvars, listvals)\n",
|
|
" # this node is sondable, go back to parent node then right child if possible\n",
|
|
" \n",
|
|
" stop = false\n",
|
|
" # check if we are not at the root node\n",
|
|
" if (length(listvars) > 1)\n",
|
|
" # go back to parent node\n",
|
|
" var = pop!(listvars)\n",
|
|
" val = pop!(listvals)\n",
|
|
" else\n",
|
|
" # the root node was sondable\n",
|
|
" println(\"\\nFINISHED\")\n",
|
|
" stop = true\n",
|
|
" end\n",
|
|
" listvars, listvals, stop \n",
|
|
"end"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"AllDef (generic function with 1 method)"
|
|
]
|
|
},
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"# fonction objectif que l'on souhaite maximiser/minimiser\n",
|
|
"Objective(x, price) = \n",
|
|
" sum(\n",
|
|
" if x[i] < 0\n",
|
|
" price[i]\n",
|
|
" else\n",
|
|
" price[i]*x[i] \n",
|
|
" end\n",
|
|
" for i in 1:length(x)\n",
|
|
" )\n",
|
|
"\n",
|
|
"# fonction permettant de vérfier toutes les contraintes du modèle\n",
|
|
"Constraints(x, weight, capacity) =\n",
|
|
" sum(\n",
|
|
" if x[i] < 0\n",
|
|
" 0\n",
|
|
" else \n",
|
|
" weight[i]*x[i]\n",
|
|
" end\n",
|
|
" for i in 1:length(x)\n",
|
|
" ) <= capacity\n",
|
|
"\n",
|
|
"\n",
|
|
"function AllDef(x)\n",
|
|
" for i in 1:length(x)\n",
|
|
" if x[i] < 0\n",
|
|
" return false\n",
|
|
" end\n",
|
|
" end\n",
|
|
" return true\n",
|
|
"end"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"ename": "Error",
|
|
"evalue": "Session cannot generate requests",
|
|
"output_type": "error",
|
|
"traceback": [
|
|
"Error: Session cannot generate requests",
|
|
"at S.executeCodeCell (/home/damien/.vscode/extensions/ms-toolsai.jupyter-2021.10.1101450599/out/client/extension.js:66:301742)",
|
|
"at S.execute (/home/damien/.vscode/extensions/ms-toolsai.jupyter-2021.10.1101450599/out/client/extension.js:66:300732)",
|
|
"at S.start (/home/damien/.vscode/extensions/ms-toolsai.jupyter-2021.10.1101450599/out/client/extension.js:66:296408)",
|
|
"at processTicksAndRejections (internal/process/task_queues.js:93:5)",
|
|
"at async t.CellExecutionQueue.executeQueuedCells (/home/damien/.vscode/extensions/ms-toolsai.jupyter-2021.10.1101450599/out/client/extension.js:66:312326)",
|
|
"at async t.CellExecutionQueue.start (/home/damien/.vscode/extensions/ms-toolsai.jupyter-2021.10.1101450599/out/client/extension.js:66:311862)"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"function SolveKnapInstance(filename)\n",
|
|
"\n",
|
|
" price, weight, capacity = readKnapInstance(filename)\n",
|
|
"\n",
|
|
" println(\"Capacity : \", capacity, \" | Number of objects : \", length(price), \"\\n\")\n",
|
|
"\n",
|
|
" #create the structure to memorize the search tree for visualization at the end\n",
|
|
" trParentnodes=Int64[] #will store orig node of arc in search tree\n",
|
|
" trChildnodes=Int64[] #will store destination node of arc in search tree\n",
|
|
" trNamenodes=[] #will store names of nodes in search tree\n",
|
|
" \n",
|
|
" #intermediate structure to navigate in the search tree\n",
|
|
" listvars=[]\n",
|
|
" listvals=[]\n",
|
|
"\n",
|
|
" BestProfit=-1\n",
|
|
" Bestsol=[]\n",
|
|
"\n",
|
|
" current_node_number=0\n",
|
|
" stop = false\n",
|
|
" affich = false\n",
|
|
"\n",
|
|
" push!(listvars, [-1 for p in price])\n",
|
|
" push!(listvals, Objective(last(listvars), price))\n",
|
|
"\n",
|
|
" while (!stop)\n",
|
|
" x = last(listvars)\n",
|
|
"\n",
|
|
" if affich\n",
|
|
" print(\"----------\\nNode n°\", current_node_number, \" : \")\n",
|
|
" println(\" \")\n",
|
|
" println(\"\\nPrevious Solution memorized \", \" with bestprofit \", BestProfit, \"\\n\")\n",
|
|
" end\n",
|
|
"\n",
|
|
" TA, TO, TR, Bestsol, BestProfit = TestsSondabilite_relaxlin(x, price, weight, capacity, BestProfit, Bestsol, affich)\n",
|
|
" \n",
|
|
" is_node_sondable = TA || TO || TR\n",
|
|
" if (!is_node_sondable)\n",
|
|
" listvars, listvals = SeparerNoeud_relaxlin(price, listvars, listvals)\n",
|
|
" else\n",
|
|
" listvars, listvals, stop = ExplorerAutreNoeud_relaxlin(listvars, listvals)\n",
|
|
" end\n",
|
|
"\n",
|
|
" if current_node_number % 1000000 == 1000\n",
|
|
" affich = true\n",
|
|
" else\n",
|
|
" affich = false\n",
|
|
" end\n",
|
|
" current_node_number += 1\n",
|
|
" end\n",
|
|
"\n",
|
|
" println(\"\\n******\\n\\nOptimal value = \", BestProfit, \"\\n\\nOptimal x=\", Bestsol)\n",
|
|
"\n",
|
|
"end\n",
|
|
"\n",
|
|
"SolveKnapInstance(\"data/subset_sum/knapPI_6_100_10000_2_-10726.opb\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"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.4"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|