188 lines
6.3 KiB
Plaintext
188 lines
6.3 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### Copyright 2020 The OR-Tools Authors."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"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",
|
|
"metadata": {},
|
|
"source": [
|
|
"# least_diff"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"<table align=\"left\">\n",
|
|
"<td>\n",
|
|
"<a href=\"https://colab.research.google.com/github/google/or-tools/blob/master/examples/notebook/contrib/least_diff.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/master/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
|
|
"</td>\n",
|
|
"<td>\n",
|
|
"<a href=\"https://github.com/google/or-tools/blob/master/examples/contrib/least_diff.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/master/tools/github_32px.png\"/>View source on GitHub</a>\n",
|
|
"</td>\n",
|
|
"</table>"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"!pip install ortools"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Copyright 2010 Hakan Kjellerstrand hakank@gmail.com\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",
|
|
"\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",
|
|
" http://www.hakank.org/google_cp_solver/\n",
|
|
"\"\"\"\n",
|
|
"from __future__ import print_function\n",
|
|
"from ortools.constraint_solver import pywrapcp\n",
|
|
"\n",
|
|
"\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"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|