Skip to content

Commit

Permalink
examples: Bump notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizux committed Jul 13, 2023
1 parent 56f6973 commit 9179bad
Show file tree
Hide file tree
Showing 130 changed files with 6,552 additions and 6,380 deletions.
46 changes: 26 additions & 20 deletions examples/notebook/algorithms/knapsack.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
"id": "description",
"metadata": {},
"source": [
"\n",
"A simple knapsack problem."
]
},
Expand All @@ -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",
Expand Down
24 changes: 13 additions & 11 deletions examples/notebook/algorithms/simple_knapsack_program.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
47 changes: 29 additions & 18 deletions examples/notebook/constraint_solver/cp_is_fun_cp.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -93,23 +93,23 @@
"\n",
"def main():\n",
" # Constraint programming engine\n",
" solver = pywrapcp.Solver('CP is fun!')\n",
" solver = pywrapcp.Solver(\"CP is fun!\")\n",
"\n",
" base = 10\n",
"\n",
" # Decision variables.\n",
" digits = list(range(0, base))\n",
" digits_without_zero = list(range(1, base))\n",
" c = solver.IntVar(digits_without_zero, 'C')\n",
" p = solver.IntVar(digits, 'P')\n",
" i = solver.IntVar(digits_without_zero, 'I')\n",
" s = solver.IntVar(digits, 'S')\n",
" f = solver.IntVar(digits_without_zero, 'F')\n",
" u = solver.IntVar(digits, 'U')\n",
" n = solver.IntVar(digits, 'N')\n",
" t = solver.IntVar(digits_without_zero, 'T')\n",
" r = solver.IntVar(digits, 'R')\n",
" e = solver.IntVar(digits, 'E')\n",
" c = solver.IntVar(digits_without_zero, \"C\")\n",
" p = solver.IntVar(digits, \"P\")\n",
" i = solver.IntVar(digits_without_zero, \"I\")\n",
" s = solver.IntVar(digits, \"S\")\n",
" f = solver.IntVar(digits_without_zero, \"F\")\n",
" u = solver.IntVar(digits, \"U\")\n",
" n = solver.IntVar(digits, \"N\")\n",
" t = solver.IntVar(digits_without_zero, \"T\")\n",
" r = solver.IntVar(digits, \"R\")\n",
" e = solver.IntVar(digits, \"E\")\n",
"\n",
" # We need to group variables in a list to use the constraint AllDifferent.\n",
" letters = [c, p, i, s, f, u, n, t, r, e]\n",
Expand All @@ -121,22 +121,33 @@
" solver.Add(solver.AllDifferent(letters))\n",
"\n",
" # CP + IS + FUN = TRUE\n",
" solver.Add(p + s + n + base * (c + i + u) + base * base * f == e +\n",
" base * u + base * base * r + base * base * base * t)\n",
" solver.Add(\n",
" p + s + n + base * (c + i + u) + base * base * f\n",
" == e + base * u + base * base * r + base * base * base * t\n",
" )\n",
"\n",
" solution_count = 0\n",
" db = solver.Phase(letters, solver.INT_VAR_DEFAULT, solver.INT_VALUE_DEFAULT)\n",
" solver.NewSearch(db)\n",
" while solver.NextSolution():\n",
" print(letters)\n",
" # Is CP + IS + FUN = TRUE?\n",
" assert (base * c.Value() + p.Value() + base * i.Value() + s.Value() +\n",
" base * base * f.Value() + base * u.Value() +\n",
" n.Value() == base * base * base * t.Value() +\n",
" base * base * r.Value() + base * u.Value() + e.Value())\n",
" assert (\n",
" base * c.Value()\n",
" + p.Value()\n",
" + base * i.Value()\n",
" + s.Value()\n",
" + base * base * f.Value()\n",
" + base * u.Value()\n",
" + n.Value()\n",
" == base * base * base * t.Value()\n",
" + base * base * r.Value()\n",
" + base * u.Value()\n",
" + e.Value()\n",
" )\n",
" solution_count += 1\n",
" solver.EndSearch()\n",
" print(f'Number of solutions found: {solution_count}')\n",
" print(f\"Number of solutions found: {solution_count}\")\n",
"\n",
"\n",
"main()\n",
Expand Down
Loading

0 comments on commit 9179bad

Please sign in to comment.