2020-03-04 14:33:52 +01:00
{
"cells": [
2020-09-27 16:03:47 +02:00
{
"cell_type": "markdown",
2021-12-14 13:05:00 +01:00
"id": "google",
2020-09-27 16:03:47 +02:00
"metadata": {},
"source": [
2025-02-04 18:04:03 +01:00
"##### Copyright 2025 Google LLC."
2020-09-27 16:03:47 +02:00
]
},
{
"cell_type": "markdown",
2021-12-14 13:05:00 +01:00
"id": "apache",
2020-09-27 16:03:47 +02:00
"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",
2021-12-14 13:05:00 +01:00
"id": "basename",
2020-09-27 16:03:47 +02:00
"metadata": {},
"source": [
"# least_diff"
]
},
{
"cell_type": "markdown",
2021-12-14 13:05:00 +01:00
"id": "link",
2020-09-27 16:03:47 +02:00
"metadata": {},
"source": [
"<table align=\"left\">\n",
"<td>\n",
2022-06-27 15:42:26 +02:00
"<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/least_diff.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
2020-09-27 16:03:47 +02:00
"</td>\n",
"<td>\n",
2022-06-27 15:42:26 +02:00
"<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/least_diff.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
2020-09-27 16:03:47 +02:00
"</td>\n",
"</table>"
]
},
{
"cell_type": "markdown",
2021-12-14 13:05:00 +01:00
"id": "doc",
2020-09-27 16:03:47 +02:00
"metadata": {},
"source": [
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
]
},
{
"cell_type": "code",
"execution_count": null,
2021-12-14 13:05:00 +01:00
"id": "install",
2020-09-27 16:03:47 +02:00
"metadata": {},
"outputs": [],
"source": [
2023-10-09 18:30:05 +02:00
"%pip install ortools"
2020-09-27 16:03:47 +02:00
]
},
2020-03-04 14:33:52 +01:00
{
2022-04-14 14:07:58 +02:00
"cell_type": "markdown",
"id": "description",
2020-03-04 14:33:52 +01:00
"metadata": {},
"source": [
2022-04-14 14:07:58 +02:00
"\n",
2020-03-04 14:33:52 +01:00
"\n",
" Least diff problem in Google CP Solver.\n",
"\n",
" This model solves the following problem:\n",
"\n",
" What is the smallest difference between two numbers X - Y\n",
" if you must use all the digits (0..9) exactly once.\n",
"\n",
" Compare with the following models:\n",
" * Choco : http://www.hakank.org/choco/LeastDiff2.java\n",
" * ECLiPSE : http://www.hakank.org/eclipse/least_diff2.ecl\n",
" * Comet : http://www.hakank.org/comet/least_diff.co\n",
" * Tailor/Essence': http://www.hakank.org/tailor/leastDiff.eprime\n",
" * Gecode : http://www.hakank.org/gecode/least_diff.cpp\n",
" * Gecode/R: http://www.hakank.org/gecode_r/least_diff.rb\n",
" * JaCoP : http://www.hakank.org/JaCoP/LeastDiff.java\n",
" * MiniZinc: http://www.hakank.org/minizinc/least_diff.mzn\n",
" * SICStus : http://www.hakank.org/sicstus/least_diff.pl\n",
" * Zinc : http://hakank.org/minizinc/least_diff.zinc\n",
"\n",
" This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
" Also see my other Google CP Solver models:\n",
2022-04-14 14:07:58 +02:00
" http://www.hakank.org/google_cp_solver/\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "code",
"metadata": {},
"outputs": [],
"source": [
2020-03-04 14:33:52 +01:00
"from ortools.constraint_solver import pywrapcp\n",
"\n",
"\n",
2022-04-14 14:07:58 +02:00
"def main(unused_argv):\n",
" # Create the solver.\n",
" solver = pywrapcp.Solver(\"Least diff\")\n",
"\n",
" #\n",
" # declare variables\n",
" #\n",
" digits = list(range(0, 10))\n",
" a = solver.IntVar(digits, \"a\")\n",
" b = solver.IntVar(digits, \"b\")\n",
" c = solver.IntVar(digits, \"c\")\n",
" d = solver.IntVar(digits, \"d\")\n",
" e = solver.IntVar(digits, \"e\")\n",
"\n",
" f = solver.IntVar(digits, \"f\")\n",
" g = solver.IntVar(digits, \"g\")\n",
" h = solver.IntVar(digits, \"h\")\n",
" i = solver.IntVar(digits, \"i\")\n",
" j = solver.IntVar(digits, \"j\")\n",
"\n",
" letters = [a, b, c, d, e, f, g, h, i, j]\n",
"\n",
" digit_vector = [10000, 1000, 100, 10, 1]\n",
" x = solver.ScalProd(letters[0:5], digit_vector)\n",
" y = solver.ScalProd(letters[5:], digit_vector)\n",
" diff = x - y\n",
"\n",
" #\n",
" # constraints\n",
" #\n",
" solver.Add(diff > 0)\n",
" solver.Add(solver.AllDifferent(letters))\n",
"\n",
" # objective\n",
" objective = solver.Minimize(diff, 1)\n",
"\n",
" #\n",
" # solution\n",
" #\n",
" solution = solver.Assignment()\n",
" solution.Add(letters)\n",
" solution.Add(x)\n",
" solution.Add(y)\n",
" solution.Add(diff)\n",
"\n",
" # last solution since it's a minimization problem\n",
" collector = solver.LastSolutionCollector(solution)\n",
" search_log = solver.SearchLog(100, diff)\n",
" # Note: I'm not sure what CHOOSE_PATH do, but it is fast:\n",
" # find the solution in just 4 steps\n",
" solver.Solve(\n",
" solver.Phase(letters, solver.CHOOSE_PATH, solver.ASSIGN_MIN_VALUE),\n",
" [objective, search_log, collector])\n",
"\n",
" # get the first (and only) solution\n",
"\n",
" xval = collector.Value(0, x)\n",
" yval = collector.Value(0, y)\n",
" diffval = collector.Value(0, diff)\n",
" print(\"x:\", xval)\n",
" print(\"y:\", yval)\n",
" print(\"diff:\", diffval)\n",
" print(xval, \"-\", yval, \"=\", diffval)\n",
" print([(\"abcdefghij\" [i], collector.Value(0, letters[i])) for i in range(10)])\n",
" print()\n",
" print(\"failures:\", solver.Failures())\n",
" print(\"branches:\", solver.Branches())\n",
" print(\"WallTime:\", solver.WallTime())\n",
" print()\n",
"\n",
"\n",
"main(\"cp sample\")\n",
2020-03-04 14:33:52 +01:00
"\n"
]
}
],
2025-02-04 18:04:03 +01:00
"metadata": {
"language_info": {
"name": "python"
}
},
2020-03-04 14:33:52 +01:00
"nbformat": 4,
2021-09-30 01:24:08 +02:00
"nbformat_minor": 5
2020-03-04 14:33:52 +01:00
}