249 lines
8.7 KiB
Plaintext
249 lines
8.7 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": [
|
|
"# wedding_optimal_chart"
|
|
]
|
|
},
|
|
{
|
|
"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/wedding_optimal_chart.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/wedding_optimal_chart.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": "code",
|
|
"execution_count": null,
|
|
"id": "code",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"#\n",
|
|
"# Copyright 2018 Turadg Aleahmad\n",
|
|
"#\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",
|
|
"\n",
|
|
"from ortools.constraint_solver import pywrapcp\n",
|
|
"from ortools.constraint_solver import solver_parameters_pb2\n",
|
|
"\"\"\"Finding an optimal wedding seating chart.\n",
|
|
"\n",
|
|
"From\n",
|
|
"Meghan L. Bellows and J. D. Luc Peterson\n",
|
|
"\"Finding an optimal seating chart for a wedding\"\n",
|
|
"http://www.improbable.com/news/2012/Optimal-seating-chart.pdf\n",
|
|
"http://www.improbable.com/2012/02/12/finding-an-optimal-seating-chart-for-a-wedding\n",
|
|
"\n",
|
|
"Every year, millions of brides (not to mention their mothers, future\n",
|
|
"mothers-in-law, and occasionally grooms) struggle with one of the\n",
|
|
"most daunting tasks during the wedding-planning process: the\n",
|
|
"seating chart. The guest responses are in, banquet hall is booked,\n",
|
|
"menu choices have been made. You think the hard parts are over,\n",
|
|
"but you have yet to embark upon the biggest headache of them all.\n",
|
|
"In order to make this process easier, we present a mathematical\n",
|
|
"formulation that models the seating chart problem. This model can\n",
|
|
"be solved to find the optimal arrangement of guests at tables.\n",
|
|
"At the very least, it can provide a starting point and hopefully\n",
|
|
"minimize stress and arguments.\n",
|
|
"\n",
|
|
"Adapted from\n",
|
|
"https://github.com/google/or-tools/blob/main/examples/csharp/wedding_optimal_chart.cs\n",
|
|
"\"\"\"\n",
|
|
"\n",
|
|
"\n",
|
|
"def main():\n",
|
|
" # Instantiate a CP solver.\n",
|
|
" parameters = pywrapcp.Solver.DefaultSolverParameters()\n",
|
|
" solver = pywrapcp.Solver(\"WeddingOptimalChart\", parameters)\n",
|
|
"\n",
|
|
" #\n",
|
|
" # Data\n",
|
|
" #\n",
|
|
"\n",
|
|
" # Easy problem (from the paper)\n",
|
|
" # n = 2 # number of tables\n",
|
|
" # a = 10 # maximum number of guests a table can seat\n",
|
|
" # b = 1 # minimum number of people each guest knows at their table\n",
|
|
"\n",
|
|
" # Slightly harder problem (also from the paper)\n",
|
|
" n = 5 # number of tables\n",
|
|
" a = 4 # maximum number of guests a table can seat\n",
|
|
" b = 1 # minimum number of people each guest knows at their table\n",
|
|
"\n",
|
|
" # Connection matrix: who knows who, and how strong\n",
|
|
" # is the relation\n",
|
|
" C = [[1, 50, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" [50, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" [1, 1, 1, 50, 1, 1, 1, 1, 10, 0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" [1, 1, 50, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" [1, 1, 1, 1, 1, 50, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" [1, 1, 1, 1, 50, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" [1, 1, 1, 1, 1, 1, 1, 50, 1, 0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" [1, 1, 1, 1, 1, 1, 50, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" [1, 1, 10, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 50, 1, 1, 1, 1, 1, 1],\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 50, 1, 1, 1, 1, 1, 1, 1],\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],\n",
|
|
" [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]]\n",
|
|
"\n",
|
|
" # Names of the guests. B: Bride side, G: Groom side\n",
|
|
" names = [\n",
|
|
" \"Deb (B)\", \"John (B)\", \"Martha (B)\", \"Travis (B)\", \"Allan (B)\",\n",
|
|
" \"Lois (B)\", \"Jayne (B)\", \"Brad (B)\", \"Abby (B)\", \"Mary Helen (G)\",\n",
|
|
" \"Lee (G)\", \"Annika (G)\", \"Carl (G)\", \"Colin (G)\", \"Shirley (G)\",\n",
|
|
" \"DeAnn (G)\", \"Lori (G)\"\n",
|
|
" ]\n",
|
|
"\n",
|
|
" m = len(C)\n",
|
|
"\n",
|
|
" NRANGE = range(n)\n",
|
|
" MRANGE = range(m)\n",
|
|
"\n",
|
|
" #\n",
|
|
" # Decision variables\n",
|
|
" #\n",
|
|
" tables = [solver.IntVar(0, n - 1, \"x[%i]\" % i) for i in MRANGE]\n",
|
|
"\n",
|
|
" z = solver.Sum([\n",
|
|
" C[j][k] * (tables[j] == tables[k])\n",
|
|
" for j in MRANGE\n",
|
|
" for k in MRANGE\n",
|
|
" if j < k\n",
|
|
" ])\n",
|
|
"\n",
|
|
" #\n",
|
|
" # Constraints\n",
|
|
" #\n",
|
|
" for i in NRANGE:\n",
|
|
" minGuests = [(tables[j] == i) * (tables[k] == i)\n",
|
|
" for j in MRANGE\n",
|
|
" for k in MRANGE\n",
|
|
" if j < k and C[j][k] > 0]\n",
|
|
" solver.Add(solver.Sum(minGuests) >= b)\n",
|
|
"\n",
|
|
" maxGuests = [tables[j] == i for j in MRANGE]\n",
|
|
" solver.Add(solver.Sum(maxGuests) <= a)\n",
|
|
"\n",
|
|
" # Symmetry breaking\n",
|
|
" solver.Add(tables[0] == 0)\n",
|
|
"\n",
|
|
" #\n",
|
|
" # Objective\n",
|
|
" #\n",
|
|
" objective = solver.Maximize(z, 1)\n",
|
|
"\n",
|
|
" #\n",
|
|
" # Search\n",
|
|
" #\n",
|
|
" db = solver.Phase(tables, solver.INT_VAR_DEFAULT, solver.INT_VALUE_DEFAULT)\n",
|
|
"\n",
|
|
" solver.NewSearch(db, [objective])\n",
|
|
"\n",
|
|
" while solver.NextSolution():\n",
|
|
" print(\"z:\", z)\n",
|
|
" print(\"Table: \")\n",
|
|
" for j in MRANGE:\n",
|
|
" print(tables[j].Value(), \" \")\n",
|
|
" print()\n",
|
|
"\n",
|
|
" for i in NRANGE:\n",
|
|
" print(\"Table %d: \" % i)\n",
|
|
" for j in MRANGE:\n",
|
|
" if tables[j].Value() == i:\n",
|
|
" print(names[j] + \" \")\n",
|
|
" print()\n",
|
|
"\n",
|
|
" print()\n",
|
|
"\n",
|
|
" solver.EndSearch()\n",
|
|
"\n",
|
|
" print()\n",
|
|
" print(\"Solutions: %d\" % solver.Solutions())\n",
|
|
" print(\"WallTime: %dms\" % solver.WallTime())\n",
|
|
" print(\"Failures: %d\" % solver.Failures())\n",
|
|
" print(\"Branches: %d\" % solver.Branches())\n",
|
|
"\n",
|
|
"\n",
|
|
"main()\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|