164 lines
4.5 KiB
Plaintext
164 lines
4.5 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "google",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### Copyright 2025 Google LLC."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "apache",
|
|
"metadata": {},
|
|
"source": [
|
|
"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"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "basename",
|
|
"metadata": {},
|
|
"source": [
|
|
"# toNum"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "link",
|
|
"metadata": {},
|
|
"source": [
|
|
"<table align=\"left\">\n",
|
|
"<td>\n",
|
|
"<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/toNum.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
|
|
"</td>\n",
|
|
"<td>\n",
|
|
"<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/toNum.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
|
|
"</td>\n",
|
|
"</table>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "doc",
|
|
"metadata": {},
|
|
"source": [
|
|
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "install",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"%pip install ortools"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "description",
|
|
"metadata": {},
|
|
"source": [
|
|
"\n",
|
|
"\n",
|
|
" toNum in Google CP Solver.\n",
|
|
"\n",
|
|
" Convert a number <-> array of int in a specific base.\n",
|
|
"\n",
|
|
" This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
|
|
" Also see my other Google CP Solver models:\n",
|
|
" http://www.hakank.org/google_or_tools/\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "code",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from ortools.constraint_solver import pywrapcp\n",
|
|
"\n",
|
|
"#\n",
|
|
"# converts a number (s) <-> an array of integers (t) in the specific base.\n",
|
|
"#\n",
|
|
"\n",
|
|
"\n",
|
|
"def toNum(solver, t, s, base):\n",
|
|
" tlen = len(t)\n",
|
|
" solver.Add(\n",
|
|
" s == solver.Sum([(base**(tlen - i - 1)) * t[i] for i in range(tlen)]))\n",
|
|
"\n",
|
|
"\n",
|
|
"def main(unused_argv):\n",
|
|
" # Create the solver.\n",
|
|
" solver = pywrapcp.Solver(\"toNum test\")\n",
|
|
"\n",
|
|
" # data\n",
|
|
" n = 4\n",
|
|
" base = 10\n",
|
|
"\n",
|
|
" # declare variables\n",
|
|
" x = [solver.IntVar(0, n - 1, \"x%i\" % i) for i in range(n)]\n",
|
|
" y = solver.IntVar(0, 10**n - 1, \"y\")\n",
|
|
"\n",
|
|
" #\n",
|
|
" # constraints\n",
|
|
" #\n",
|
|
" # solver.Add(solver.AllDifferent([x[i] for i in range(n)]))\n",
|
|
" solver.Add(solver.AllDifferent(x))\n",
|
|
" # solver.Add(x[0] > 0) # just for fun\n",
|
|
"\n",
|
|
" toNum(solver, x, y, base)\n",
|
|
"\n",
|
|
" #\n",
|
|
" # solution and search\n",
|
|
" #\n",
|
|
" solution = solver.Assignment()\n",
|
|
" solution.Add([x[i] for i in range(n)])\n",
|
|
" solution.Add(y)\n",
|
|
"\n",
|
|
" collector = solver.AllSolutionCollector(solution)\n",
|
|
" solver.Solve(\n",
|
|
" solver.Phase([x[i] for i in range(n)], solver.CHOOSE_FIRST_UNBOUND,\n",
|
|
" solver.ASSIGN_MIN_VALUE), [collector])\n",
|
|
"\n",
|
|
" num_solutions = collector.SolutionCount()\n",
|
|
" for s in range(num_solutions):\n",
|
|
" print(\"x:\", [collector.Value(s, x[i]) for i in range(n)])\n",
|
|
" print(\"y:\", collector.Value(s, y))\n",
|
|
" print()\n",
|
|
"\n",
|
|
" print(\"failures:\", solver.Failures())\n",
|
|
" print(\"branches:\", solver.Branches())\n",
|
|
" print(\"WallTime:\", solver.WallTime())\n",
|
|
"\n",
|
|
"\n",
|
|
"main(\"cp sample\")\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|