Skip to content

Commit 5425ded

Browse files
committed
Sync python notebook
cmd: ./tools/generate_all_notebooks.sh
1 parent de1b3cb commit 5425ded

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2168
-1649
lines changed

examples/notebook/examples/appointments.ipynb

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,13 @@
162162
" Each collection may be selected more than one time.\n",
163163
"\n",
164164
" Args:\n",
165-
" item_collections: a list of item collections. Each item collection is a\n",
166-
" list of integers [#item0, ..., #itemN-1], where #itemK is the number\n",
167-
" of times item #K appears in the collection, and N is the number of\n",
168-
" distinct items.\n",
169-
" max_num_collections: an integer, the maximum number of item collections\n",
170-
" that may be selected (counting repetitions of the same collection).\n",
165+
" item_collections: a list of item collections. Each item collection is a list\n",
166+
" of integers [#item0, ..., #itemN-1], where #itemK is the number of times\n",
167+
" item #K appears in the collection, and N is the number of distinct items.\n",
168+
" max_num_collections: an integer, the maximum number of item collections that\n",
169+
" may be selected (counting repetitions of the same collection).\n",
171170
" ideal_item_ratios: A list of N float which sums to 1.0: the K-th element is\n",
172-
" the ideal ratio of item #K in the whole aggregated selection.\n",
171+
" the ideal ratio of item #K in the whole aggregated selection.\n",
173172
"\n",
174173
" Returns:\n",
175174
" A pair (objective value, list of pairs (item collection, num_selections)),\n",
@@ -246,10 +245,10 @@
246245
" \"\"\"Computes the optimal schedule for the installation input.\n",
247246
"\n",
248247
" Args:\n",
249-
" demand: a list of \"appointment types\". Each \"appointment type\" is\n",
250-
" a triple (ideal_ratio_pct, name, duration_minutes), where\n",
251-
" ideal_ratio_pct is the ideal percentage (in [0..100.0]) of that\n",
252-
" type of appointment among all appointments scheduled.\n",
248+
" demand: a list of \"appointment types\". Each \"appointment type\" is a triple\n",
249+
" (ideal_ratio_pct, name, duration_minutes), where ideal_ratio_pct is the\n",
250+
" ideal percentage (in [0..100.0]) of that type of appointment among all\n",
251+
" appointments scheduled.\n",
253252
"\n",
254253
" Returns:\n",
255254
" The same output type as EnumerateAllKnapsacksWithRepetition.\n",

examples/notebook/examples/arc_flow_cutting_stock_sat.ipynb

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -83,21 +83,25 @@
8383
"metadata": {},
8484
"outputs": [],
8585
"source": [
86-
"import argparse\n",
8786
"import collections\n",
8887
"import time\n",
88+
"import numpy as np\n",
8989
"\n",
90-
"from ortools.linear_solver import pywraplp\n",
90+
"from google.protobuf import text_format\n",
91+
"from ortools.linear_solver.python import model_builder as mb\n",
9192
"from ortools.sat.python import cp_model\n",
9293
"\n",
93-
"PARSER = argparse.ArgumentParser()\n",
94+
"class FLAGS: pass\n",
95+
"\n",
96+
"_OUTPUT_PROTO = flags.DEFINE_string(\n",
97+
" 'output_proto', '', 'Output file to write the cp_model proto to.')\n",
98+
"_PARAMS = flags.DEFINE_string(\n",
99+
" 'params',\n",
100+
" 'num_search_workers:8,log_search_progress:true,max_time_in_seconds:10',\n",
101+
" 'Sat solver parameters.')\n",
102+
"_SOLVER = flags.DEFINE_string(\n",
103+
" 'solver', 'sat', 'Method used to solve: sat, mip.')\n",
94104
"\n",
95-
"PARSER.add_argument(\n",
96-
" '--solver', default='sat', help='Method used to solve: sat, mip.')\n",
97-
"PARSER.add_argument(\n",
98-
" '--output_proto_file',\n",
99-
" default='',\n",
100-
" help='Output file to write the cp_model proto to.')\n",
101105
"\n",
102106
"DESIRED_LENGTHS = [\n",
103107
" 2490, 3980, 2490, 3980, 2391, 2391, 2391, 596, 596, 596, 2456, 2456, 3018,\n",
@@ -175,7 +179,7 @@
175179
" return states, transitions\n",
176180
"\n",
177181
"\n",
178-
"def solve_cutting_stock_with_arc_flow_and_sat(output_proto_file):\n",
182+
"def solve_cutting_stock_with_arc_flow_and_sat(output_proto_file: str, params: str):\n",
179183
" \"\"\"Solve the cutting stock with arc-flow and the CP-SAT solver.\"\"\"\n",
180184
" items = regroup_and_count(DESIRED_LENGTHS)\n",
181185
" print('Items:', items)\n",
@@ -243,16 +247,14 @@
243247
"\n",
244248
" # Output model proto to file.\n",
245249
" if output_proto_file:\n",
246-
" output_file = open(output_proto_file, 'w')\n",
247-
" output_file.write(str(model.Proto()))\n",
248-
" output_file.close()\n",
250+
" model.ExportToFile(output_proto_file)\n",
249251
"\n",
250252
" # Solve model.\n",
251253
" solver = cp_model.CpSolver()\n",
254+
" if params:\n",
255+
" text_format.Parse(params, solver.parameters)\n",
252256
" solver.parameters.log_search_progress = True\n",
253-
" solver.parameters.num_search_workers = 8\n",
254-
" status = solver.Solve(model)\n",
255-
" print(solver.ResponseStats())\n",
257+
" solver.Solve(model)\n",
256258
"\n",
257259
"\n",
258260
"def solve_cutting_stock_with_arc_flow_and_mip():\n",
@@ -273,17 +275,15 @@
273275
" item_coeffs = collections.defaultdict(list)\n",
274276
"\n",
275277
" start_time = time.time()\n",
276-
" solver = pywraplp.Solver.CreateSolver('cbc')\n",
277-
" if not solver:\n",
278-
" return\n",
278+
" model = mb.ModelBuilder()\n",
279279
"\n",
280280
" objective_vars = []\n",
281281
" objective_coeffs = []\n",
282282
"\n",
283283
" var_index = 0\n",
284284
" for outgoing, incoming, item_index, card in transitions:\n",
285285
" count = items[item_index][1]\n",
286-
" count_var = solver.IntVar(\n",
286+
" count_var = model.new_int_var(\n",
287287
" 0, count, 'a%i_i%i_f%i_t%i_c%i' % (var_index, item_index, incoming,\n",
288288
" outgoing, card))\n",
289289
" var_index += 1\n",
@@ -295,7 +295,7 @@
295295
" for state_index, state in enumerate(states):\n",
296296
" if state_index == 0:\n",
297297
" continue\n",
298-
" exit_var = solver.IntVar(0, num_items, 'e%i' % state_index)\n",
298+
" exit_var = model.new_int_var(0, num_items, 'e%i' % state_index)\n",
299299
" outgoing_vars[state_index].append(exit_var)\n",
300300
" incoming_sink_vars.append(exit_var)\n",
301301
" price = price_usage(state, POSSIBLE_CAPACITIES)\n",
@@ -304,44 +304,47 @@
304304
"\n",
305305
" # Flow conservation\n",
306306
" for state_index in range(1, len(states)):\n",
307-
" solver.Add(\n",
308-
" sum(incoming_vars[state_index]) == sum(outgoing_vars[state_index]))\n",
307+
" model.add(\n",
308+
" mb.LinearExpr.sum(incoming_vars[state_index]) == mb.LinearExpr.sum(\n",
309+
" outgoing_vars[state_index]))\n",
309310
"\n",
310311
" # Flow going out of the source must go in the sink\n",
311-
" solver.Add(sum(outgoing_vars[0]) == sum(incoming_sink_vars))\n",
312+
" model.add(\n",
313+
" mb.LinearExpr.sum(outgoing_vars[0]) == mb.LinearExpr.sum(\n",
314+
" incoming_sink_vars))\n",
312315
"\n",
313316
" # Items must be placed\n",
314317
" for item_index, size_and_count in enumerate(items):\n",
315318
" num_arcs = len(item_vars[item_index])\n",
316-
" solver.Add(\n",
317-
" sum(item_vars[item_index][i] * item_coeffs[item_index][i]\n",
318-
" for i in range(num_arcs)) == size_and_count[1])\n",
319+
" model.add(\n",
320+
" mb.LinearExpr.sum([item_vars[item_index][i] * item_coeffs[item_index][i]\n",
321+
" for i in range(num_arcs)]) == size_and_count[1])\n",
319322
"\n",
320323
" # Objective is the sum of waste\n",
321-
" solver.Minimize(\n",
322-
" sum(objective_vars[i] * objective_coeffs[i]\n",
323-
" for i in range(len(objective_vars))))\n",
324-
" solver.EnableOutput()\n",
324+
" model.minimize(np.dot(objective_vars, objective_coeffs))\n",
325325
"\n",
326-
" status = solver.Solve()\n",
326+
" solver = mb.ModelSolver('scip')\n",
327+
" solver.enable_output(True)\n",
328+
" status = solver.solve(model)\n",
327329
"\n",
328330
" ### Output the solution.\n",
329-
" if status == pywraplp.Solver.OPTIMAL:\n",
331+
" if status == mb.SolveStatus.OPTIMAL or status == mb.SolveStatus.FEASIBLE:\n",
330332
" print('Objective value = %f found in %.2f s' %\n",
331-
" (solver.Objective().Value(), time.time() - start_time))\n",
333+
" (solver.objective_value, time.time() - start_time))\n",
332334
" else:\n",
333335
" print('No solution')\n",
334336
"\n",
335337
"\n",
336-
"def main(args):\n",
338+
"def main(_):\n",
337339
" \"\"\"Main function\"\"\"\n",
338-
" if args.solver == 'sat':\n",
339-
" solve_cutting_stock_with_arc_flow_and_sat(args.output_proto_file)\n",
340+
" if _SOLVER.value == 'sat':\n",
341+
" solve_cutting_stock_with_arc_flow_and_sat(_OUTPUT_PROTO.value,\n",
342+
" _PARAMS.value)\n",
340343
" else: # 'mip'\n",
341344
" solve_cutting_stock_with_arc_flow_and_mip()\n",
342345
"\n",
343346
"\n",
344-
"main(PARSER.parse_args())\n",
347+
"main()\n",
345348
"\n"
346349
]
347350
}

examples/notebook/examples/assignment2_sat.ipynb

Lines changed: 0 additions & 164 deletions
This file was deleted.

0 commit comments

Comments
 (0)