279 lines
9.6 KiB
Plaintext
279 lines
9.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": [
|
|
"# einav_puzzle"
|
|
]
|
|
},
|
|
{
|
|
"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/einav_puzzle.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/einav_puzzle.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",
|
|
" A programming puzzle from Einav in Google CP Solver.\n",
|
|
"\n",
|
|
" From\n",
|
|
" 'A programming puzzle from Einav'\n",
|
|
" http://gcanyon.wordpress.com/2009/10/28/a-programming-puzzle-from-einav/\n",
|
|
" '''\n",
|
|
" My friend Einav gave me this programming puzzle to work on. Given\n",
|
|
" this array of positive and negative numbers:\n",
|
|
" 33 30 -10 -6 18 7 -11 -23 6\n",
|
|
" ...\n",
|
|
" -25 4 16 30 33 -23 -4 4 -23\n",
|
|
"\n",
|
|
" You can flip the sign of entire rows and columns, as many of them\n",
|
|
" as you like. The goal is to make all the rows and columns sum to positive\n",
|
|
" numbers (or zero), and then to find the solution (there are more than one)\n",
|
|
" that has the smallest overall sum. So for example, for this array:\n",
|
|
" 33 30 -10\n",
|
|
" -16 19 9\n",
|
|
" -17 -12 -14\n",
|
|
" You could flip the sign for the bottom row to get this array:\n",
|
|
" 33 30 -10\n",
|
|
" -16 19 9\n",
|
|
" 17 12 14\n",
|
|
" Now all the rows and columns have positive sums, and the overall total is\n",
|
|
" 108.\n",
|
|
" But you could instead flip the second and third columns, and the second\n",
|
|
" row, to get this array:\n",
|
|
" 33 -30 10\n",
|
|
" 16 19 9\n",
|
|
" -17 12 14\n",
|
|
" All the rows and columns still total positive, and the overall sum is just\n",
|
|
" 66. So this solution is better (I don't know if it's the best)\n",
|
|
" A pure brute force solution would have to try over 30 billion solutions.\n",
|
|
" I wrote code to solve this in J. I'll post that separately.\n",
|
|
" '''\n",
|
|
"\n",
|
|
" Compare with the following models:\n",
|
|
" * MiniZinc http://www.hakank.org/minizinc/einav_puzzle.mzn\n",
|
|
" * SICStus: http://hakank.org/sicstus/einav_puzzle.pl\n",
|
|
"\n",
|
|
" Note:\n",
|
|
" einav_puzzle2.py is Laurent Perron version, which don't use as many\n",
|
|
" decision variables as this version.\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": [
|
|
"from ortools.constraint_solver import pywrapcp\n",
|
|
"\n",
|
|
"\n",
|
|
"def main():\n",
|
|
"\n",
|
|
" # Create the solver.\n",
|
|
" solver = pywrapcp.Solver('Einav puzzle')\n",
|
|
"\n",
|
|
" #\n",
|
|
" # data\n",
|
|
" #\n",
|
|
"\n",
|
|
" # small problem\n",
|
|
" # rows = 3;\n",
|
|
" # cols = 3;\n",
|
|
" # data = [\n",
|
|
" # [ 33, 30, -10],\n",
|
|
" # [-16, 19, 9],\n",
|
|
" # [-17, -12, -14]\n",
|
|
" # ]\n",
|
|
"\n",
|
|
" # Full problem\n",
|
|
" rows = 27\n",
|
|
" cols = 9\n",
|
|
" data = [[33, 30, 10, -6, 18, -7, -11, 23, -6],\n",
|
|
" [16, -19, 9, -26, -8, -19, -8, -21, -14],\n",
|
|
" [17, 12, -14, 31, -30, 13, -13, 19, 16],\n",
|
|
" [-6, -11, 1, 17, -12, -4, -7, 14, -21],\n",
|
|
" [18, -31, 34, -22, 17, -19, 20, 24, 6],\n",
|
|
" [33, -18, 17, -15, 31, -5, 3, 27, -3],\n",
|
|
" [-18, -20, -18, 31, 6, 4, -2, -12, 24],\n",
|
|
" [27, 14, 4, -29, -3, 5, -29, 8, -12],\n",
|
|
" [-15, -7, -23, 23, -9, -8, 6, 8, -12],\n",
|
|
" [33, -23, -19, -4, -8, -7, 11, -12, 31],\n",
|
|
" [-20, 19, -15, -30, 11, 32, 7, 14, -5],\n",
|
|
" [-23, 18, -32, -2, -31, -7, 8, 24, 16],\n",
|
|
" [32, -4, -10, -14, -6, -1, 0, 23, 23],\n",
|
|
" [25, 0, -23, 22, 12, 28, -27, 15, 4],\n",
|
|
" [-30, -13, -16, -3, -3, -32, -3, 27, -31],\n",
|
|
" [22, 1, 26, 4, -2, -13, 26, 17, 14],\n",
|
|
" [-9, -18, 3, -20, -27, -32, -11, 27, 13],\n",
|
|
" [-17, 33, -7, 19, -32, 13, -31, -2, -24],\n",
|
|
" [-31, 27, -31, -29, 15, 2, 29, -15, 33],\n",
|
|
" [-18, -23, 15, 28, 0, 30, -4, 12, -32],\n",
|
|
" [-3, 34, 27, -25, -18, 26, 1, 34, 26],\n",
|
|
" [-21, -31, -10, -13, -30, -17, -12, -26, 31],\n",
|
|
" [23, -31, -19, 21, -17, -10, 2, -23, 23],\n",
|
|
" [-3, 6, 0, -3, -32, 0, -10, -25, 14],\n",
|
|
" [-19, 9, 14, -27, 20, 15, -5, -27, 18],\n",
|
|
" [11, -6, 24, 7, -17, 26, 20, -31, -25],\n",
|
|
" [-25, 4, -16, 30, 33, 23, -4, -4, 23]]\n",
|
|
"\n",
|
|
" #\n",
|
|
" # variables\n",
|
|
" #\n",
|
|
" x = {}\n",
|
|
" for i in range(rows):\n",
|
|
" for j in range(cols):\n",
|
|
" x[i, j] = solver.IntVar(-100, 100, 'x[%i,%i]' % (i, j))\n",
|
|
"\n",
|
|
" x_flat = [x[i, j] for i in range(rows) for j in range(cols)]\n",
|
|
"\n",
|
|
" row_sums = [solver.IntVar(0, 300, 'row_sums(%i)' % i) for i in range(rows)]\n",
|
|
" col_sums = [solver.IntVar(0, 300, 'col_sums(%i)' % j) for j in range(cols)]\n",
|
|
"\n",
|
|
" row_signs = [solver.IntVar([-1, 1], 'row_signs(%i)' % i) for i in range(rows)]\n",
|
|
" col_signs = [solver.IntVar([-1, 1], 'col_signs(%i)' % j) for j in range(cols)]\n",
|
|
"\n",
|
|
" # total sum: to be minimized\n",
|
|
" total_sum = solver.IntVar(0, 1000, 'total_sum')\n",
|
|
"\n",
|
|
" #\n",
|
|
" # constraints\n",
|
|
" #\n",
|
|
" for i in range(rows):\n",
|
|
" for j in range(cols):\n",
|
|
" solver.Add(x[i, j] == data[i][j] * row_signs[i] * col_signs[j])\n",
|
|
"\n",
|
|
" total_sum_a = [\n",
|
|
" data[i][j] * row_signs[i] * col_signs[j]\n",
|
|
" for i in range(rows)\n",
|
|
" for j in range(cols)\n",
|
|
" ]\n",
|
|
" solver.Add(total_sum == solver.Sum(total_sum_a))\n",
|
|
"\n",
|
|
" # row sums\n",
|
|
" for i in range(rows):\n",
|
|
" s = [row_signs[i] * col_signs[j] * data[i][j] for j in range(cols)]\n",
|
|
" solver.Add(row_sums[i] == solver.Sum(s))\n",
|
|
"\n",
|
|
" # column sums\n",
|
|
" for j in range(cols):\n",
|
|
" s = [row_signs[i] * col_signs[j] * data[i][j] for i in range(rows)]\n",
|
|
" solver.Add(col_sums[j] == solver.Sum(s))\n",
|
|
"\n",
|
|
" # objective\n",
|
|
" objective = solver.Minimize(total_sum, 1)\n",
|
|
"\n",
|
|
" #\n",
|
|
" # search and result\n",
|
|
" #\n",
|
|
" # Note: The order of the variables makes a big difference.\n",
|
|
" # If row_signs are before col_sign it is much slower.\n",
|
|
" db = solver.Phase(col_signs + row_signs, solver.CHOOSE_MIN_SIZE_LOWEST_MIN,\n",
|
|
" solver.ASSIGN_MAX_VALUE)\n",
|
|
"\n",
|
|
" solver.NewSearch(db, [objective])\n",
|
|
"\n",
|
|
" num_solutions = 0\n",
|
|
" while solver.NextSolution():\n",
|
|
" num_solutions += 1\n",
|
|
" print('total_sum:', total_sum.Value())\n",
|
|
" print('row_sums:', [row_sums[i].Value() for i in range(rows)])\n",
|
|
" print('col_sums:', [col_sums[j].Value() for j in range(cols)])\n",
|
|
" print('row_signs:', [row_signs[i].Value() for i in range(rows)])\n",
|
|
" print('col_signs:', [col_signs[j].Value() for j in range(cols)])\n",
|
|
" print('x:')\n",
|
|
" for i in range(rows):\n",
|
|
" for j in range(cols):\n",
|
|
" print('%3i' % x[i, j].Value(), end=' ')\n",
|
|
" print()\n",
|
|
" print()\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(), 'ms')\n",
|
|
"\n",
|
|
"\n",
|
|
"main()\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|