256 lines
7.9 KiB
Plaintext
256 lines
7.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": [
|
|
"# contiguity_regular"
|
|
]
|
|
},
|
|
{
|
|
"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/contiguity_regular.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/contiguity_regular.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",
|
|
" Global constraint contiguity using regularin Google CP Solver.\n",
|
|
"\n",
|
|
" This is a decomposition of the global constraint\n",
|
|
" global contiguity.\n",
|
|
"\n",
|
|
" From Global Constraint Catalogue\n",
|
|
" http://www.emn.fr/x-info/sdemasse/gccat/Cglobal_contiguity.html\n",
|
|
" '''\n",
|
|
" Enforce all variables of the VARIABLES collection to be assigned to 0 or 1.\n",
|
|
" In addition, all variables assigned to value 1 appear contiguously.\n",
|
|
"\n",
|
|
" Example:\n",
|
|
" (<0, 1, 1, 0>)\n",
|
|
"\n",
|
|
" The global_contiguity constraint holds since the sequence 0 1 1 0 contains\n",
|
|
" no more than one group of contiguous 1.\n",
|
|
" '''\n",
|
|
"\n",
|
|
" Compare with the following model:\n",
|
|
" * MiniZinc: http://www.hakank.org/minizinc/contiguity_regular.mzn\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",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "code",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from ortools.constraint_solver import pywrapcp\n",
|
|
"\n",
|
|
"#\n",
|
|
"# Global constraint regular\n",
|
|
"#\n",
|
|
"# This is a translation of MiniZinc's regular constraint (defined in\n",
|
|
"# lib/zinc/globals.mzn), via the Comet code refered above.\n",
|
|
"# All comments are from the MiniZinc code.\n",
|
|
"# '''\n",
|
|
"# The sequence of values in array 'x' (which must all be in the range 1..S)\n",
|
|
"# is accepted by the DFA of 'Q' states with input 1..S and transition\n",
|
|
"# function 'd' (which maps (1..Q, 1..S) -> 0..Q)) and initial state 'q0'\n",
|
|
"# (which must be in 1..Q) and accepting states 'F' (which all must be in\n",
|
|
"# 1..Q). We reserve state 0 to be an always failing state.\n",
|
|
"# '''\n",
|
|
"#\n",
|
|
"# x : IntVar array\n",
|
|
"# Q : number of states\n",
|
|
"# S : input_max\n",
|
|
"# d : transition matrix\n",
|
|
"# q0: initial state\n",
|
|
"# F : accepting states\n",
|
|
"\n",
|
|
"\n",
|
|
"def regular(x, Q, S, d, q0, F):\n",
|
|
"\n",
|
|
" solver = x[0].solver()\n",
|
|
"\n",
|
|
" assert Q > 0, 'regular: \"Q\" must be greater than zero'\n",
|
|
" assert S > 0, 'regular: \"S\" must be greater than zero'\n",
|
|
"\n",
|
|
" # d2 is the same as d, except we add one extra transition for\n",
|
|
" # each possible input; each extra transition is from state zero\n",
|
|
" # to state zero. This allows us to continue even if we hit a\n",
|
|
" # non-accepted input.\n",
|
|
"\n",
|
|
" # Comet: int d2[0..Q, 1..S]\n",
|
|
" d2 = []\n",
|
|
" for i in range(Q + 1):\n",
|
|
" row = []\n",
|
|
" for j in range(S):\n",
|
|
" if i == 0:\n",
|
|
" row.append(0)\n",
|
|
" else:\n",
|
|
" row.append(d[i - 1][j])\n",
|
|
" d2.append(row)\n",
|
|
"\n",
|
|
" d2_flatten = [d2[i][j] for i in range(Q + 1) for j in range(S)]\n",
|
|
"\n",
|
|
" # If x has index set m..n, then a[m-1] holds the initial state\n",
|
|
" # (q0), and a[i+1] holds the state we're in after processing\n",
|
|
" # x[i]. If a[n] is in F, then we succeed (ie. accept the\n",
|
|
" # string).\n",
|
|
" x_range = list(range(0, len(x)))\n",
|
|
" m = 0\n",
|
|
" n = len(x)\n",
|
|
"\n",
|
|
" a = [solver.IntVar(0, Q + 1, 'a[%i]' % i) for i in range(m, n + 1)]\n",
|
|
"\n",
|
|
" # Check that the final state is in F\n",
|
|
" solver.Add(solver.MemberCt(a[-1], F))\n",
|
|
" # First state is q0\n",
|
|
" solver.Add(a[m] == q0)\n",
|
|
" for i in x_range:\n",
|
|
" solver.Add(x[i] >= 1)\n",
|
|
" solver.Add(x[i] <= S)\n",
|
|
"\n",
|
|
" # Determine a[i+1]: a[i+1] == d2[a[i], x[i]]\n",
|
|
" solver.Add(\n",
|
|
" a[i + 1] == solver.Element(d2_flatten, ((a[i]) * S) + (x[i] - 1)))\n",
|
|
"\n",
|
|
"\n",
|
|
"def main():\n",
|
|
"\n",
|
|
" # Create the solver.\n",
|
|
" solver = pywrapcp.Solver('Global contiguity using regular')\n",
|
|
"\n",
|
|
" #\n",
|
|
" # data\n",
|
|
" #\n",
|
|
" # the DFA (for regular)\n",
|
|
" n_states = 3\n",
|
|
" input_max = 2\n",
|
|
" initial_state = 1 # 0 is for the failing state\n",
|
|
"\n",
|
|
" # all states are accepting states\n",
|
|
" accepting_states = [1, 2, 3]\n",
|
|
"\n",
|
|
" # The regular expression 0*1*0*\n",
|
|
" transition_fn = [\n",
|
|
" [1, 2], # state 1 (start): input 0 -> state 1, input 1 -> state 2 i.e. 0*\n",
|
|
" [3, 2], # state 2: 1*\n",
|
|
" [3, 0], # state 3: 0*\n",
|
|
" ]\n",
|
|
"\n",
|
|
" n = 7\n",
|
|
"\n",
|
|
" #\n",
|
|
" # declare variables\n",
|
|
" #\n",
|
|
"\n",
|
|
" # We use 1..2 and subtract 1 in the solution\n",
|
|
" reg_input = [solver.IntVar(1, 2, 'x[%i]' % i) for i in range(n)]\n",
|
|
"\n",
|
|
" #\n",
|
|
" # constraints\n",
|
|
" #\n",
|
|
" regular(reg_input, n_states, input_max, transition_fn, initial_state,\n",
|
|
" accepting_states)\n",
|
|
"\n",
|
|
" #\n",
|
|
" # solution and search\n",
|
|
" #\n",
|
|
" db = solver.Phase(reg_input, solver.CHOOSE_FIRST_UNBOUND,\n",
|
|
" solver.ASSIGN_MIN_VALUE)\n",
|
|
"\n",
|
|
" solver.NewSearch(db)\n",
|
|
"\n",
|
|
" num_solutions = 0\n",
|
|
" while solver.NextSolution():\n",
|
|
" num_solutions += 1\n",
|
|
" # Note: here we subract 1 from the solution\n",
|
|
" print('reg_input:', [int(reg_input[i].Value() - 1) for i in range(n)])\n",
|
|
"\n",
|
|
" solver.EndSearch()\n",
|
|
" print()\n",
|
|
" print('num_solutions:', num_solutions)\n",
|
|
" print('failures:', solver.Failures())\n",
|
|
" print('branches:', solver.Branches())\n",
|
|
" print('wall_time:', solver.WallTime(), 'ms')\n",
|
|
"\n",
|
|
"\n",
|
|
"main()\n",
|
|
"\n"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|