244 lines
6.9 KiB
Plaintext
244 lines
6.9 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": [
|
|
"# young_tableaux"
|
|
]
|
|
},
|
|
{
|
|
"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/young_tableaux.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/young_tableaux.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",
|
|
" Young tableaux in Google CP Solver.\n",
|
|
"\n",
|
|
" See\n",
|
|
" http://mathworld.wolfram.com/YoungTableau.html\n",
|
|
" and\n",
|
|
" http://en.wikipedia.org/wiki/Young_tableau\n",
|
|
" '''\n",
|
|
" The partitions of 4 are\n",
|
|
" {4}, {3,1}, {2,2}, {2,1,1}, {1,1,1,1}\n",
|
|
"\n",
|
|
" And the corresponding standard Young tableaux are:\n",
|
|
"\n",
|
|
"1. 1 2 3 4\n",
|
|
"\n",
|
|
"2. 1 2 3 1 2 4 1 3 4\n",
|
|
" 4 3 2\n",
|
|
"\n",
|
|
"3. 1 2 1 3\n",
|
|
" 3 4 2 4\n",
|
|
"\n",
|
|
"4 1 2 1 3 1 4\n",
|
|
" 3 2 2\n",
|
|
" 4 4 3\n",
|
|
"\n",
|
|
"5. 1\n",
|
|
" 2\n",
|
|
" 3\n",
|
|
" 4\n",
|
|
" '''\n",
|
|
"\n",
|
|
" Thanks to Laurent Perron for improving this model.\n",
|
|
"\n",
|
|
" Compare with the following models:\n",
|
|
" * MiniZinc: http://www.hakank.org/minizinc/young_tableaux.mzn\n",
|
|
" * Choco : http://www.hakank.org/choco/YoungTableuax.java\n",
|
|
" * JaCoP : http://www.hakank.org/JaCoP/YoungTableuax.java\n",
|
|
" * Comet : http://www.hakank.org/comet/young_tableaux.co\n",
|
|
" * Gecode : http://www.hakank.org/gecode/young_tableaux.cpp\n",
|
|
" * ECLiPSe : http://www.hakank.org/eclipse/young_tableaux.ecl\n",
|
|
" * Tailor/Essence' : http://www.hakank.org/tailor/young_tableaux.eprime\n",
|
|
" * SICStus: http://hakank.org/sicstus/young_tableaux.pl\n",
|
|
" * Zinc: http://hakank.org/minizinc/young_tableaux.zinc\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",
|
|
"from ortools.constraint_solver import pywrapcp\n",
|
|
"\n",
|
|
"\n",
|
|
"def main(n=5):\n",
|
|
"\n",
|
|
" # Create the solver.\n",
|
|
" solver = pywrapcp.Solver(\"Problem\")\n",
|
|
"\n",
|
|
" #\n",
|
|
" # data\n",
|
|
" #\n",
|
|
" print(\"n:\", n)\n",
|
|
"\n",
|
|
" #\n",
|
|
" # declare variables\n",
|
|
" #\n",
|
|
" x = {}\n",
|
|
" for i in range(n):\n",
|
|
" for j in range(n):\n",
|
|
" x[(i, j)] = solver.IntVar(1, n + 1, \"x(%i,%i)\" % (i, j))\n",
|
|
"\n",
|
|
" x_flat = [x[(i, j)] for i in range(n) for j in range(n)]\n",
|
|
"\n",
|
|
" # partition structure\n",
|
|
" p = [solver.IntVar(0, n + 1, \"p%i\" % i) for i in range(n)]\n",
|
|
"\n",
|
|
" #\n",
|
|
" # constraints\n",
|
|
" #\n",
|
|
"\n",
|
|
" # 1..n is used exactly once\n",
|
|
" for i in range(1, n + 1):\n",
|
|
" solver.Add(solver.Count(x_flat, i, 1))\n",
|
|
"\n",
|
|
" solver.Add(x[(0, 0)] == 1)\n",
|
|
"\n",
|
|
" # row wise\n",
|
|
" for i in range(n):\n",
|
|
" for j in range(1, n):\n",
|
|
" solver.Add(x[(i, j)] >= x[(i, j - 1)])\n",
|
|
"\n",
|
|
" # column wise\n",
|
|
" for j in range(n):\n",
|
|
" for i in range(1, n):\n",
|
|
" solver.Add(x[(i, j)] >= x[(i - 1, j)])\n",
|
|
"\n",
|
|
" # calculate the structure (the partition)\n",
|
|
" for i in range(n):\n",
|
|
" # MiniZinc/Zinc version:\n",
|
|
" # p[i] == sum(j in 1..n) (bool2int(x[i,j] <= n))\n",
|
|
"\n",
|
|
" b = [solver.IsLessOrEqualCstVar(x[(i, j)], n) for j in range(n)]\n",
|
|
" solver.Add(p[i] == solver.Sum(b))\n",
|
|
"\n",
|
|
" solver.Add(solver.Sum(p) == n)\n",
|
|
"\n",
|
|
" for i in range(1, n):\n",
|
|
" solver.Add(p[i - 1] >= p[i])\n",
|
|
"\n",
|
|
" #\n",
|
|
" # solution and search\n",
|
|
" #\n",
|
|
" solution = solver.Assignment()\n",
|
|
" solution.Add(x_flat)\n",
|
|
" solution.Add(p)\n",
|
|
"\n",
|
|
" # db: DecisionBuilder\n",
|
|
" db = solver.Phase(x_flat + p, solver.CHOOSE_FIRST_UNBOUND,\n",
|
|
" solver.ASSIGN_MIN_VALUE)\n",
|
|
"\n",
|
|
" solver.NewSearch(db)\n",
|
|
" num_solutions = 0\n",
|
|
" while solver.NextSolution():\n",
|
|
" print(\"p:\", [p[i].Value() for i in range(n)])\n",
|
|
" print(\"x:\")\n",
|
|
" for i in range(n):\n",
|
|
" for j in range(n):\n",
|
|
" val = x_flat[i * n + j].Value()\n",
|
|
" if val <= n:\n",
|
|
" print(val, end=\" \")\n",
|
|
" if p[i].Value() > 0:\n",
|
|
" print()\n",
|
|
" print()\n",
|
|
" num_solutions += 1\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",
|
|
"n = 5\n",
|
|
"if len(sys.argv) > 1:\n",
|
|
" n = int(sys.argv[1])\n",
|
|
"\n",
|
|
"main(n)\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|