304 lines
9.3 KiB
Plaintext
304 lines
9.3 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": [
|
|
"# 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/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/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 regular in Google CP Solver.\n",
|
|
"\n",
|
|
" This is a translation of MiniZinc's regular constraint (defined in\n",
|
|
" lib/zinc/globals.mzn). 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",
|
|
" It is, however, translated from the Comet model:\n",
|
|
" * Comet: http://www.hakank.org/comet/regular.co\n",
|
|
"\n",
|
|
" Here we test with the following regular expression:\n",
|
|
" 0*1{3}0+1{2}0+1{1}0*\n",
|
|
" using an array of size 10.\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",
|
|
"#\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",
|
|
"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",
|
|
" # 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",
|
|
"#\n",
|
|
"# Make a transition (automaton) matrix from a\n",
|
|
"# single pattern, e.g. [3,2,1]\n",
|
|
"#\n",
|
|
"def make_transition_matrix(pattern):\n",
|
|
"\n",
|
|
" p_len = len(pattern)\n",
|
|
" print('p_len:', p_len)\n",
|
|
" num_states = p_len + sum(pattern)\n",
|
|
" print('num_states:', num_states)\n",
|
|
" t_matrix = []\n",
|
|
" for i in range(num_states):\n",
|
|
" row = []\n",
|
|
" for j in range(2):\n",
|
|
" row.append(0)\n",
|
|
" t_matrix.append(row)\n",
|
|
"\n",
|
|
" # convert pattern to a 0/1 pattern for easy handling of\n",
|
|
" # the states\n",
|
|
" tmp = [0 for i in range(num_states)]\n",
|
|
" c = 0\n",
|
|
" tmp[c] = 0\n",
|
|
" for i in range(p_len):\n",
|
|
" for j in range(pattern[i]):\n",
|
|
" c += 1\n",
|
|
" tmp[c] = 1\n",
|
|
" if c < num_states - 1:\n",
|
|
" c += 1\n",
|
|
" tmp[c] = 0\n",
|
|
" print('tmp:', tmp)\n",
|
|
"\n",
|
|
" t_matrix[num_states - 1][0] = num_states\n",
|
|
" t_matrix[num_states - 1][1] = 0\n",
|
|
"\n",
|
|
" for i in range(num_states):\n",
|
|
" if tmp[i] == 0:\n",
|
|
" t_matrix[i][0] = i + 1\n",
|
|
" t_matrix[i][1] = i + 2\n",
|
|
" else:\n",
|
|
" if i < num_states - 1:\n",
|
|
" if tmp[i + 1] == 1:\n",
|
|
" t_matrix[i][0] = 0\n",
|
|
" t_matrix[i][1] = i + 2\n",
|
|
" else:\n",
|
|
" t_matrix[i][0] = i + 2\n",
|
|
" t_matrix[i][1] = 0\n",
|
|
"\n",
|
|
" print('The states:')\n",
|
|
" for i in range(num_states):\n",
|
|
" for j in range(2):\n",
|
|
" print(t_matrix[i][j], end=' ')\n",
|
|
" print()\n",
|
|
" print()\n",
|
|
"\n",
|
|
" return t_matrix\n",
|
|
"\n",
|
|
"\n",
|
|
"def main():\n",
|
|
"\n",
|
|
" # Create the solver.\n",
|
|
" solver = pywrapcp.Solver('Regular test')\n",
|
|
"\n",
|
|
" #\n",
|
|
" # data\n",
|
|
" #\n",
|
|
"\n",
|
|
" this_len = 10\n",
|
|
" pp = [3, 2, 1]\n",
|
|
"\n",
|
|
" transition_fn = make_transition_matrix(pp)\n",
|
|
" n_states = len(transition_fn)\n",
|
|
" input_max = 2\n",
|
|
"\n",
|
|
" # Note: we use '1' and '2' (rather than 0 and 1)\n",
|
|
" # since 0 represents the failing state.\n",
|
|
" initial_state = 1\n",
|
|
"\n",
|
|
" accepting_states = [n_states]\n",
|
|
"\n",
|
|
" # declare variables\n",
|
|
" reg_input = [\n",
|
|
" solver.IntVar(1, input_max, 'reg_input[%i]' % i) for i in range(this_len)\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_MIN_SIZE_HIGHEST_MAX,\n",
|
|
" solver.ASSIGN_MIN_VALUE)\n",
|
|
"\n",
|
|
" solver.NewSearch(db)\n",
|
|
"\n",
|
|
" num_solutions = 0\n",
|
|
" while solver.NextSolution():\n",
|
|
" print('reg_input:', [reg_input[i].Value() - 1 for i in range(this_len)])\n",
|
|
" num_solutions += 1\n",
|
|
"\n",
|
|
" solver.EndSearch()\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
|
|
}
|