83 lines
2.7 KiB
Plaintext
83 lines
2.7 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Copyright 2010-2018 Google LLC\n",
|
|
"# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
|
|
"# you may not use this file except in compliance with the License.\n",
|
|
"# You may obtain a copy of the License at\n",
|
|
"#\n",
|
|
"# http://www.apache.org/licenses/LICENSE-2.0\n",
|
|
"#\n",
|
|
"# Unless required by applicable law or agreed to in writing, software\n",
|
|
"# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
|
|
"# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
|
|
"# See the License for the specific language governing permissions and\n",
|
|
"# limitations under the License.\n",
|
|
"# [START program]\n",
|
|
"\"\"\"Simple Constraint optimization example.\"\"\"\n",
|
|
"\n",
|
|
"# [START import]\n",
|
|
"from __future__ import print_function\n",
|
|
"from ortools.constraint_solver import pywrapcp\n",
|
|
"# [END import]\n",
|
|
"\n",
|
|
"\n",
|
|
"\"\"\"Entry point of the program.\"\"\"\n",
|
|
"# Instantiate the solver.\n",
|
|
"# [START solver]\n",
|
|
"solver = pywrapcp.Solver('CPSimple')\n",
|
|
"# [END solver]\n",
|
|
"\n",
|
|
"# Create the variables.\n",
|
|
"# [START variables]\n",
|
|
"num_vals = 3\n",
|
|
"x = solver.IntVar(0, num_vals - 1, 'x')\n",
|
|
"y = solver.IntVar(0, num_vals - 1, 'y')\n",
|
|
"z = solver.IntVar(0, num_vals - 1, 'z')\n",
|
|
"# [END variables]\n",
|
|
"\n",
|
|
"# Constraint 0: x != y.\n",
|
|
"# [START constraints]\n",
|
|
"solver.Add(x != y)\n",
|
|
"print('Number of constraints: ', solver.Constraints())\n",
|
|
"# [END constraints]\n",
|
|
"\n",
|
|
"# Solve the problem.\n",
|
|
"# [START solve]\n",
|
|
"decision_builder = solver.Phase([x, y, z], solver.CHOOSE_FIRST_UNBOUND,\n",
|
|
" solver.ASSIGN_MIN_VALUE)\n",
|
|
"# [END solve]\n",
|
|
"\n",
|
|
"# Print solution on console.\n",
|
|
"# [START print_solution]\n",
|
|
"count = 0\n",
|
|
"solver.NewSearch(decision_builder)\n",
|
|
"while solver.NextSolution():\n",
|
|
" count += 1\n",
|
|
" solution = 'Solution {}:\\n'.format(count)\n",
|
|
" for var in [x, y, z]:\n",
|
|
" solution += ' {} = {}'.format(var.Name(), var.Value())\n",
|
|
" print(solution)\n",
|
|
"solver.EndSearch()\n",
|
|
"print('Number of solutions found: ', count)\n",
|
|
"# [END print_solution]\n",
|
|
"\n",
|
|
"# [START advanced]\n",
|
|
"print('Advanced usage:')\n",
|
|
"print('Problem solved in ', solver.WallTime(), 'ms')\n",
|
|
"print('Memory usage: ', pywrapcp.Solver.MemoryUsage(), 'bytes')\n",
|
|
"# [END advanced]\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|