193 lines
5.6 KiB
Plaintext
193 lines
5.6 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": [
|
|
"# langford"
|
|
]
|
|
},
|
|
{
|
|
"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/langford.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/langford.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",
|
|
" Langford's number problem in Google CP Solver.\n",
|
|
"\n",
|
|
" Langford's number problem (CSP lib problem 24)\n",
|
|
" http://www.csplib.org/prob/prob024/\n",
|
|
" '''\n",
|
|
" Arrange 2 sets of positive integers 1..k to a sequence,\n",
|
|
" such that, following the first occurence of an integer i,\n",
|
|
" each subsequent occurrence of i, appears i+1 indices later\n",
|
|
" than the last.\n",
|
|
" For example, for k=4, a solution would be 41312432\n",
|
|
" '''\n",
|
|
"\n",
|
|
" * John E. Miller: Langford's Problem\n",
|
|
" http://www.lclark.edu/~miller/langford.html\n",
|
|
"\n",
|
|
" * Encyclopedia of Integer Sequences for the number of solutions for each k\n",
|
|
" http://www.research.att.com/cgi-bin/access.cgi/as/njas/sequences/eisA.cgi?Anum=014552\n",
|
|
"\n",
|
|
"\n",
|
|
" Also, see the following models:\n",
|
|
" * MiniZinc: http://www.hakank.org/minizinc/langford2.mzn\n",
|
|
" * Gecode/R: http://www.hakank.org/gecode_r/langford.rb\n",
|
|
" * ECLiPSe: http://hakank.org/eclipse/langford.ecl\n",
|
|
" * SICStus: http://hakank.org/sicstus/langford.pl\n",
|
|
"\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"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "code",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import sys\n",
|
|
"\n",
|
|
"from ortools.constraint_solver import pywrapcp\n",
|
|
"\n",
|
|
"\n",
|
|
"def main(k=8, num_sol=0):\n",
|
|
"\n",
|
|
" # Create the solver.\n",
|
|
" solver = pywrapcp.Solver(\"Langford\")\n",
|
|
"\n",
|
|
" #\n",
|
|
" # data\n",
|
|
" #\n",
|
|
" print(\"k:\", k)\n",
|
|
" p = list(range(2 * k))\n",
|
|
"\n",
|
|
" #\n",
|
|
" # declare variables\n",
|
|
" #\n",
|
|
" position = [solver.IntVar(0, 2 * k - 1, \"position[%i]\" % i) for i in p]\n",
|
|
" solution = [solver.IntVar(1, k, \"position[%i]\" % i) for i in p]\n",
|
|
"\n",
|
|
" #\n",
|
|
" # constraints\n",
|
|
" #\n",
|
|
" solver.Add(solver.AllDifferent(position))\n",
|
|
"\n",
|
|
" for i in range(1, k + 1):\n",
|
|
" solver.Add(position[i + k - 1] == position[i - 1] + i + 1)\n",
|
|
" solver.Add(solver.Element(solution, position[i - 1]) == i)\n",
|
|
" solver.Add(solver.Element(solution, position[k + i - 1]) == i)\n",
|
|
"\n",
|
|
" # symmetry breaking\n",
|
|
" solver.Add(solution[0] < solution[2 * k - 1])\n",
|
|
"\n",
|
|
" #\n",
|
|
" # search and result\n",
|
|
" #\n",
|
|
" db = solver.Phase(position, solver.CHOOSE_FIRST_UNBOUND,\n",
|
|
" solver.ASSIGN_MIN_VALUE)\n",
|
|
"\n",
|
|
" solver.NewSearch(db)\n",
|
|
" num_solutions = 0\n",
|
|
" while solver.NextSolution():\n",
|
|
" print(\"solution:\", \",\".join([str(solution[i].Value()) for i in p]))\n",
|
|
" num_solutions += 1\n",
|
|
" if num_sol > 0 and num_solutions >= num_sol:\n",
|
|
" break\n",
|
|
"\n",
|
|
" solver.EndSearch()\n",
|
|
"\n",
|
|
" print()\n",
|
|
" print(\"num_solutions:\", num_solutions)\n",
|
|
" print(\"failures:\", solver.Failures())\n",
|
|
" print(\"branches:\", solver.Branches())\n",
|
|
" print(\"WallTime:\", solver.WallTime())\n",
|
|
"\n",
|
|
"\n",
|
|
"k = 8\n",
|
|
"num_sol = 0\n",
|
|
"if len(sys.argv) > 1:\n",
|
|
" k = int(sys.argv[1])\n",
|
|
"if len(sys.argv) > 2:\n",
|
|
" num_sol = int(sys.argv[2])\n",
|
|
"\n",
|
|
"main(k, num_sol)\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|