examples: Bump notebooks

This commit is contained in:
Corentin Le Molgat
2023-07-13 14:34:43 +02:00
parent 56f69732ed
commit 9179bad0ae
130 changed files with 6534 additions and 6362 deletions

View File

@@ -72,6 +72,7 @@
"id": "description",
"metadata": {},
"source": [
"\n",
"A simple knapsack problem."
]
},
@@ -82,43 +83,48 @@
"metadata": {},
"outputs": [],
"source": [
"from ortools.algorithms import pywrapknapsack_solver\n",
"from ortools.algorithms.python import knapsack_solver\n",
"\n",
"\n",
"def main():\n",
" # Create the solver.\n",
" solver = pywrapknapsack_solver.KnapsackSolver(\n",
" pywrapknapsack_solver.KnapsackSolver.\n",
" KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER, 'KnapsackExample')\n",
" solver = knapsack_solver.KnapsackSolver(\n",
" knapsack_solver.SolverType.KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER,\n",
" \"KnapsackExample\",\n",
" )\n",
"\n",
" values = [\n",
" 360, 83, 59, 130, 431, 67, 230, 52, 93, 125, 670, 892, 600, 38, 48, 147,\n",
" 78, 256, 63, 17, 120, 164, 432, 35, 92, 110, 22, 42, 50, 323, 514, 28,\n",
" 87, 73, 78, 15, 26, 78, 210, 36, 85, 189, 274, 43, 33, 10, 19, 389, 276,\n",
" 312\n",
" # fmt:off\n",
" 360, 83, 59, 130, 431, 67, 230, 52, 93, 125, 670, 892, 600, 38, 48, 147,\n",
" 78, 256, 63, 17, 120, 164, 432, 35, 92, 110, 22, 42, 50, 323, 514, 28,\n",
" 87, 73, 78, 15, 26, 78, 210, 36, 85, 189, 274, 43, 33, 10, 19, 389, 276,\n",
" 312\n",
" # fmt:on\n",
" ]\n",
" weights = [\n",
" # fmt: off\n",
" [7, 0, 30, 22, 80, 94, 11, 81, 70, 64, 59, 18, 0, 36, 3, 8, 15, 42, 9, 0,\n",
" 42, 47, 52, 32, 26, 48, 55, 6, 29, 84, 2, 4, 18, 56, 7, 29, 93, 44, 71, 3,\n",
" 86, 66, 31, 65, 0, 79, 20, 65, 52, 13],\n",
" # fmt: on\n",
" ]\n",
" weights = [[\n",
" 7, 0, 30, 22, 80, 94, 11, 81, 70, 64, 59, 18, 0, 36, 3, 8, 15, 42, 9, 0,\n",
" 42, 47, 52, 32, 26, 48, 55, 6, 29, 84, 2, 4, 18, 56, 7, 29, 93, 44, 71,\n",
" 3, 86, 66, 31, 65, 0, 79, 20, 65, 52, 13\n",
" ]]\n",
" capacities = [850]\n",
"\n",
" solver.Init(values, weights, capacities)\n",
" computed_value = solver.Solve()\n",
" solver.init(values, weights, capacities)\n",
" computed_value = solver.solve()\n",
"\n",
" packed_items = []\n",
" packed_weights = []\n",
" total_weight = 0\n",
" print('Total value =', computed_value)\n",
" print(\"Total value =\", computed_value)\n",
" for i in range(len(values)):\n",
" if solver.BestSolutionContains(i):\n",
" if solver.best_solution_contains(i):\n",
" packed_items.append(i)\n",
" packed_weights.append(weights[0][i])\n",
" total_weight += weights[0][i]\n",
" print('Total weight:', total_weight)\n",
" print('Packed items:', packed_items)\n",
" print('Packed_weights:', packed_weights)\n",
" print(\"Total weight:\", total_weight)\n",
" print(\"Packed items:\", packed_items)\n",
" print(\"Packed_weights:\", packed_weights)\n",
"\n",
"\n",
"main()\n",

View File

@@ -83,27 +83,29 @@
"metadata": {},
"outputs": [],
"source": [
"from ortools.algorithms import pywrapknapsack_solver\n",
"from ortools.algorithms.python import knapsack_solver\n",
"\n",
"\n",
"def main():\n",
" # Create the solver.\n",
" solver = pywrapknapsack_solver.KnapsackSolver(\n",
" pywrapknapsack_solver.KnapsackSolver.\n",
" KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER, \"test\")\n",
" solver = knapsack_solver.KnapsackSolver(\n",
" knapsack_solver.SolverType.KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER,\n",
" \"test\",\n",
" )\n",
"\n",
" weights = [[\n",
" 565, 406, 194, 130, 435, 367, 230, 315, 393, 125, 670, 892, 600, 293,\n",
" 712, 147, 421, 255\n",
" ]]\n",
" weights = [\n",
" # fmt:off\n",
" [565, 406, 194, 130, 435, 367, 230, 315, 393, 125, 670, 892, 600, 293, 712, 147, 421, 255],\n",
" # fmt:on\n",
" ]\n",
" capacities = [850]\n",
" values = weights[0]\n",
"\n",
" solver.Init(values, weights, capacities)\n",
" computed_value = solver.Solve()\n",
" solver.init(values, weights, capacities)\n",
" computed_value = solver.solve()\n",
"\n",
" packed_items = [\n",
" x for x in range(0, len(weights[0])) if solver.BestSolutionContains(x)\n",
" x for x in range(0, len(weights[0])) if solver.best_solution_contains(x)\n",
" ]\n",
" packed_weights = [weights[0][i] for i in packed_items]\n",
"\n",