ca marche po

This commit is contained in:
gdamms 2021-11-26 17:57:38 +01:00
parent 4a5e0a46de
commit 21646a6fde

View file

@ -27,9 +27,15 @@
"\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\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/Project.toml`\n",
"\u001b[32m\u001b[1m No Changes\u001b[22m\u001b[39m to `~/.julia/environments/v1.6/Manifest.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 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/Project.toml`\n",
"\u001b[32m\u001b[1m No Changes\u001b[22m\u001b[39m to `~/.julia/environments/v1.6/Manifest.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 2 seconds (158 already precompiled)\n"
] ]
} }
], ],
@ -48,16 +54,16 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 3,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"(Any[42, 40, 12, 25], Any[7, 4, 3, 5], 10)" "readKnapInstance (generic function with 1 method)"
] ]
}, },
"execution_count": 9, "execution_count": 3,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -117,7 +123,7 @@
} }
], ],
"source": [ "source": [
"function TestsSondabilite_relaxlin(x, price, weight, capacity, varsbin, BestProfit, Bestsol)\n", "function TestsSondabilite_relaxlin(x, price, weight, capacity, BestProfit, Bestsol)\n",
" TA, TO, TR = false, false, false\n", " TA, TO, TR = false, false, false\n",
" if (!Constraints(x, weight, capacity)) # Test de faisabilite\n", " if (!Constraints(x, weight, capacity)) # Test de faisabilite\n",
" TA = true\n", " TA = true\n",
@ -130,6 +136,7 @@
" println(\"TR\")\n", " println(\"TR\")\n",
" #if (value(benef) >= BestProfit)\n", " #if (value(benef) >= BestProfit)\n",
" if (Objective(x, price) >= BestProfit)\n", " if (Objective(x, price) >= BestProfit)\n",
" println(\"oiuiiiiii\")\n",
" Bestsol = x\n", " Bestsol = x\n",
" #BestProfit=value(benef)\n", " #BestProfit=value(benef)\n",
" BestProfit = Objective(x, price)\n", " BestProfit = Objective(x, price)\n",
@ -166,74 +173,65 @@
], ],
"source": [ "source": [
"\n", "\n",
"function SeparerNoeud_relaxlin(varsshouldbebinary, listvars, listvals)\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", " # 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", "\n",
" # Cas du noeud le plus à gauche\n", " # Cas du noeud le plus à gauche\n",
" predX = first(listvars)\n", " predX = first(listvars)\n",
" n = length(predX)\n",
" nextX0 = predX\n", " nextX0 = predX\n",
" for xi in predX\n", " nextX1 = 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", "\n",
" val0 = Objective(nextX0, price)\n",
" val1 = Objective(nextX1, price)\n",
" break\n",
" end\n",
" end\n", " end\n",
"\n", "\n",
"\n", " push!(listvars, nextX0)\n",
" push!(listvars,var) #stocker l'identite de la variable choisie pour la séparation\n", " push!(listvars, nextX1)\n",
" push!(listvals,1.0) #stocker la branche choisie, identifiee par la valeur de la variable choisie\n", " push!(listvals, val0)\n",
" push!(listvals, val1)\n",
" listvars, listvals\n", " listvars, listvals\n",
"end\n", "end\n",
"\n", "\n",
"\n", "\n",
"function ExplorerAutreNoeud_relaxlin(listvars, listvals, listnodes)\n", "function ExplorerAutreNoeud_relaxlin(listvars, listvals)\n",
" # this node is sondable, go back to parent node then right child if possible\n", " # this node is sondable, go back to parent node then right child if possible\n",
" \n", " \n",
" stop = false\n", " stop = false\n",
" # check if we are not at the root node\n", " # check if we are not at the root node\n",
" if (length(listvars)>= 1)\n", " if (length(listvars) > 1)\n",
" # go back to parent node\n", " # go back to parent node\n",
" var = pop!(listvars)\n", " var = pop!(listvars)\n",
" theval=pop!(listvals)\n", " val = pop!(listvals)\n",
" tmp=pop!(listnodes)\n",
" set_lower_bound(var,0.0)\n",
" set_upper_bound(var,1.0)\n",
"\n",
" #go to right child if possible, otherwise go back to parent\n",
" while ( (theval==0.0) && (length(listvars)>= 1))\n",
" var=pop!(listvars)\n",
" theval=pop!(listvals)\n",
" tmp=pop!(listnodes)\n",
" set_lower_bound(var,0.0) \n",
" set_upper_bound(var,1.0)\n",
" end\n",
" if theval==1.0\n",
" set_lower_bound(var,0.0)\n",
" set_upper_bound(var,0.0)\n",
" push!(listvars,var)\n",
" push!(listvals,0.0)\n",
" else\n",
" println(\"\\nFINISHED\")\n",
" stop=true\n",
" end\n",
" else\n", " else\n",
" # the root node was sondable\n", " # the root node was sondable\n",
" println(\"\\nFINISHED\")\n", " println(\"\\nFINISHED\")\n",
" stop = true\n", " stop = true\n",
" end\n", " end\n",
" listvars, listvals, listnodes, stop \n", " listvars, listvals, stop \n",
"end" "end"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 6,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"Constraints (generic function with 1 method)" "true"
] ]
}, },
"execution_count": 10, "execution_count": 6,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -270,6 +268,166 @@
" end\n", " end\n",
" return true" " return true"
] ]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Node number 0: \n",
"-----\n",
"\n",
"\n",
"Noeud actuel\tx_1 = -1\tx_2 = -1\tx_3 = -1\tx_4 = -1 \n",
"\n",
"Previous Solution memorized Any[] with bestprofit -1\n",
"\n",
"non sondable\n",
"\n",
"Node number 1: \n",
"-----\n",
"\n",
"\n",
"Noeud actuel\tx_1 = 1\tx_2 = -1\tx_3 = -1\tx_4 = -1 \n",
"\n",
"Previous Solution memorized Any[] with bestprofit -1\n",
"\n",
"non sondable\n",
"\n",
"Node number 2: \n",
"-----\n",
"\n",
"\n",
"Noeud actuel : NOT AVAILABLE (probably infeasible or ressources limit reached) \n",
"\n",
"Previous Solution memorized Any[] with bestprofit -1\n",
"\n",
"TA\n",
"\n",
"Node number 3: \n",
"-----\n",
"\n",
"\n",
"Noeud actuel : NOT AVAILABLE (probably infeasible or ressources limit reached) \n",
"\n",
"Previous Solution memorized Any[] with bestprofit -1\n",
"\n",
"TA\n",
"\n",
"Node number 4: \n",
"-----\n",
"\n",
"\n",
"Noeud actuel : NOT AVAILABLE (probably infeasible or ressources limit reached) \n",
"\n",
"Previous Solution memorized Any[] with bestprofit -1\n",
"\n",
"TA\n",
"\n",
"Node number 5: \n",
"-----\n",
"\n",
"\n",
"Noeud actuel : NOT AVAILABLE (probably infeasible or ressources limit reached) \n",
"\n",
"Previous Solution memorized Any[] with bestprofit -1\n",
"\n",
"TA\n",
"\n",
"Node number 6: \n",
"-----\n",
"\n",
"\n",
"Noeud actuel : NOT AVAILABLE (probably infeasible or ressources limit reached) \n",
"\n",
"Previous Solution memorized Any[] with bestprofit -1\n",
"\n",
"TA\n",
"\n",
"FINISHED\n",
"\n",
"******\n",
"\n",
"Optimal value = -1\n",
"\n",
"Optimal x=Any[]\n"
]
},
{
"data": {
"text/plain": [
"(-1, Any[], Int64[], Int64[], Any[])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function SolveKnapInstance(filename)\n",
"\n",
" price, weight, capacity = readKnapInstance(filename)\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",
"\n",
" push!(listvars, [-1 for p in price])\n",
" push!(listvals, Objective(first(listvars), price))\n",
"\n",
" while (!stop)\n",
"\n",
" println(\"\\nNode number \", current_node_number, \": \\n-----\\n\")\n",
"\n",
" x = first(listvars)\n",
"\n",
" print(\"\\nNoeud actuel\"); \n",
" if (!Constraints(x, weight, capacity)) # (has_values(model2))\n",
" print(\" : NOT AVAILABLE (probably infeasible or ressources limit reached)\")\n",
" else\n",
" [print(\"\\tx_\", i, \" = \", x[i]) for i in 1:length(x)] \n",
" end\n",
" println(\" \");\n",
" println(\"\\nPrevious Solution memorized \", Bestsol, \" with bestprofit \", BestProfit, \"\\n\")\n",
"\n",
" TA, TO, TR, Bestsol, BestProfit = TestsSondabilite_relaxlin(x, price, weight, capacity, BestProfit, Bestsol)\n",
" \n",
" is_node_sondable = TA || TO || TR\n",
"\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",
" current_node_number += 1\n",
" end\n",
"\n",
" println(\"\\n******\\n\\nOptimal value = \", BestProfit, \"\\n\\nOptimal x=\", Bestsol)\n",
"\n",
" return BestProfit, Bestsol, trParentnodes, trChildnodes, trNamenodes\n",
"\n",
"end\n",
"\n",
"SolveKnapInstance(\"data/test.opb\")"
]
} }
], ],
"metadata": { "metadata": {
@ -282,7 +440,7 @@
"file_extension": ".jl", "file_extension": ".jl",
"mimetype": "application/julia", "mimetype": "application/julia",
"name": "julia", "name": "julia",
"version": "1.6.3" "version": "1.6.4"
} }
}, },
"nbformat": 4, "nbformat": 4,