TP-recherche-operationnelle/tp4.ipynb

145 lines
3.9 KiB
Plaintext
Raw Normal View History

2021-12-15 08:45:27 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# TP 4 : Programmation dynamique"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"readKnapInstance"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"\"\"\"Open and read a KnapFile.\n",
"\n",
"Args: \\\\\n",
" - filename (String): the name of the file to read.\n",
"\n",
"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",
"\"\"\"\n",
"function readKnapInstance(filename)\n",
" price = []\n",
" weight = []\n",
" capacity = -1\n",
" open(filename) do f\n",
" for i = 1:3\n",
" tok = split(readline(f))\n",
" if (tok[1] == \"ListPrices=\")\n",
" for i = 2:(length(tok)-1)\n",
" push!(price, parse(Int64, tok[i]))\n",
" end\n",
" elseif (tok[1] == \"ListWeights=\")\n",
" for i = 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",
" return price, weight, capacity\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"analyseDynamique (generic function with 1 method)"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"function analyseDynamique(price, weight, i, j)\n",
" if i > 0\n",
" C1, sol1 = analyseDynamique(price, weight, i - 1, j)\n",
" if j - weight[i] >= 0\n",
" C2, sol2 = analyseDynamique(price, weight, i - 1, j - weight[i])\n",
" C2 += price[i]\n",
" if C1 >= C2\n",
" res = C1\n",
" sol = push!(sol1, 0)\n",
" else\n",
" res = C2\n",
" sol = push!(sol2, 1)\n",
" end\n",
" else\n",
" res = C1\n",
" sol = push!(sol1, 0)\n",
" end\n",
" else\n",
" res = 0\n",
" sol = Vector{Int8}()\n",
" end\n",
" \n",
" return res, sol\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3906\n",
"Int8[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0]\n"
]
}
],
"source": [
"price, weight, capacity = readKnapInstance(\"test4.opb\")\n",
"\n",
"res, sol = analyseDynamique(price, weight, length(price), capacity)\n",
"\n",
"println(res)\n",
"println(sol)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.6.4",
"language": "julia",
"name": "julia-1.6"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.6.4"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}