feat: partie 1 faite TODO: le bonus
This commit is contained in:
commit
0ada659946
3
test1.opb
Normal file
3
test1.opb
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
ListPrices= 42 40 12 25 ;
|
||||||
|
ListWeights= 7 4 3 5 ;
|
||||||
|
Capacity= 10 ;
|
3
test2.opb
Normal file
3
test2.opb
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
ListPrices= 403 886 814 1151 983 629 848 1074 839 819 1062 762 994 950 111 914 737 1049 1152 1110 ;
|
||||||
|
ListWeights= 94 506 416 992 649 237 457 815 446 422 791 359 667 598 7 544 334 766 994 893 ;
|
||||||
|
Capacity= 994 ;
|
3
test3.opb
Normal file
3
test3.opb
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
ListPrices= 403 886 814 1151 983 629 848 1074 839 819 1062 762 994 950 111 914 737 1049 1152 1110 973 474 824 1013 963 1101 1024 816 1063 575 1153 447 1117 910 1017 931 909 1126 1027 871 1052 891 375 1131 318 705 1048 908 1026 1061 1133 1076 817 1066 805 1131 349 925 1064 940 948 1010 853 1036 539 755 1094 555 1107 956 858 1136 919 1048 876 766 944 607 1147 658 1063 1064 1101 750 1050 1059 1036 562 742 807 723 836 895 467 201 777 891 42 947 1005 ;
|
||||||
|
ListWeights= 94 506 416 992 649 237 457 815 446 422 791 359 667 598 7 544 334 766 994 893 633 131 428 700 617 874 720 419 794 196 997 116 908 539 707 569 537 931 726 487 772 513 81 943 58 303 764 536 724 789 946 819 420 800 406 941 70 560 795 583 595 695 463 742 171 352 858 182 887 606 470 955 552 764 493 363 588 220 982 261 793 796 872 347 768 785 741 187 339 408 320 442 518 127 23 375 513 1 593 686 ;
|
||||||
|
Capacity= 982 ;
|
3
test4.opb
Normal file
3
test4.opb
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
ListPrices= 403 886 814 1151 983 629 848 1074 839 819 1062 762 994 950 111 914 737 1049 1152 1110 973 474 824 1013 963 1101 1024 816 1063 575 1153 447 1117 910 1017 931 909 1126 1027 871 1052 891 375 1131 318 705 1048 908 1026 1061 865 493 742 978 575 877 339 1078 592 1019 1066 717 690 800 855 1006 1082 614 638 1090 441 780 764 337 871 1005 687 1108 205 875 1109 395 554 600 173 860 815 816 759 1002 708 582 786 1135 881 572 1033 727 1151 613 657 410 594 983 1121 332 1138 1068 895 824 909 749 511 1143 991 577 397 903 465 1039 857 1051 1128 1026 1111 930 540 1138 784 710 1142 940 290 1126 931 73 188 958 1107 1078 1126 1115 634 780 778 1139 1108 836 602 741 522 805 520 233 586 817 511 821 734 928 1085 126 1121 906 623 1137 946 936 1038 779 1094 386 829 923 679 1086 1032 934 465 1118 1034 1077 968 1120 582 1151 504 302 1045 665 652 1058 449 893 552 210 781 1020 664 602 ;
|
||||||
|
ListWeights= 94 506 416 992 649 237 457 815 446 422 791 359 667 598 7 544 334 766 994 893 633 131 428 700 617 874 720 419 794 196 997 116 908 539 707 569 537 931 726 487 772 513 81 943 58 303 764 536 724 789 479 142 339 641 196 494 66 824 208 711 800 314 289 401 466 689 833 225 244 849 113 379 361 65 486 686 286 889 24 491 891 90 181 214 17 472 418 419 356 682 306 201 385 952 500 194 737 324 992 224 260 97 210 649 919 63 958 804 518 428 537 346 153 971 662 197 91 529 126 747 469 770 934 723 895 568 172 958 383 308 970 583 48 930 569 3 20 609 887 825 930 904 241 379 376 962 889 443 216 338 160 406 159 31 204 420 153 425 331 565 838 9 918 533 232 957 591 576 746 377 858 86 434 558 279 840 735 574 126 912 739 821 625 917 201 993 149 52 759 267 256 783 117 516 180 25 380 712 266 216 ;
|
||||||
|
Capacity= 997 ;
|
144
tp4.ipynb
Normal file
144
tp4.ipynb
Normal file
|
@ -0,0 +1,144 @@
|
||||||
|
{
|
||||||
|
"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
|
||||||
|
}
|
Loading…
Reference in a new issue