331 lines
11 KiB
Plaintext
331 lines
11 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"##### Copyright 2020 Google LLC."
|
|
]
|
|
},
|
|
{
|
|
"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": [
|
|
"# 3_jugs_regular"
|
|
]
|
|
},
|
|
{
|
|
"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/3_jugs_regular.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/3_jugs_regular.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",
|
|
" 3 jugs problem using regular constraint in Google CP Solver.\n",
|
|
"\n",
|
|
" A.k.a. water jugs problem.\n",
|
|
"\n",
|
|
" Problem from Taha 'Introduction to Operations Research',\n",
|
|
" page 245f .\n",
|
|
"\n",
|
|
" For more info about the problem, see:\n",
|
|
" http://mathworld.wolfram.com/ThreeJugProblem.html\n",
|
|
"\n",
|
|
" This model use a regular constraint for handling the\n",
|
|
" transitions between the states. Instead of minimizing\n",
|
|
" the cost in a cost matrix (as shortest path problem),\n",
|
|
" we here call the model with increasing length of the\n",
|
|
" sequence array (x).\n",
|
|
"\n",
|
|
" Compare with other models that use MIP/CP approach,\n",
|
|
" as a shortest path problem:\n",
|
|
" * Comet: http://www.hakank.org/comet/3_jugs.co\n",
|
|
" * Comet: http://www.hakank.org/comet/water_buckets1.co\n",
|
|
" * MiniZinc: http://www.hakank.org/minizinc/3_jugs.mzn\n",
|
|
" * MiniZinc: http://www.hakank.org/minizinc/3_jugs2.mzn\n",
|
|
" * SICStus: http://www.hakank.org/sicstus/3_jugs.pl\n",
|
|
" * ECLiPSe: http://www.hakank.org/eclipse/3_jugs.ecl\n",
|
|
" * ECLiPSe: http://www.hakank.org/eclipse/3_jugs2.ecl\n",
|
|
" * Gecode: http://www.hakank.org/gecode/3_jugs2.cpp\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",
|
|
"\n",
|
|
"\"\"\"\n",
|
|
"\n",
|
|
"from __future__ import print_function\n",
|
|
"from ortools.constraint_solver import pywrapcp\n",
|
|
"from collections import defaultdict\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",
|
|
"\n",
|
|
"# Create the solver.\n",
|
|
"solver = pywrapcp.Solver('3 jugs problem using regular constraint')\n",
|
|
"\n",
|
|
"#\n",
|
|
"# data\n",
|
|
"#\n",
|
|
"\n",
|
|
"# the DFA (for regular)\n",
|
|
"n_states = 14\n",
|
|
"input_max = 15\n",
|
|
"initial_state = 1 # 0 is for the failing state\n",
|
|
"accepting_states = [15]\n",
|
|
"\n",
|
|
"##\n",
|
|
"# Manually crafted DFA\n",
|
|
"# (from the adjacency matrix used in the other models)\n",
|
|
"##\n",
|
|
"# transition_fn = [\n",
|
|
"# # 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5\n",
|
|
"# [0, 2, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0], # 1\n",
|
|
"# [0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # 2\n",
|
|
"# [0, 0, 0, 4, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0], # 3\n",
|
|
"# [0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], # 4\n",
|
|
"# [0, 0, 0, 0, 0, 6, 0, 0, 9, 0, 0, 0, 0, 0, 0], # 5\n",
|
|
"# [0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0], # 6\n",
|
|
"# [0, 0, 0, 0, 0, 0, 0, 8, 9, 0, 0, 0, 0, 0, 0], # 7\n",
|
|
"# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15], # 8\n",
|
|
"# [0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0], # 9\n",
|
|
"# [0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0], # 10\n",
|
|
"# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 0, 0, 0], # 11\n",
|
|
"# [0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 0, 0], # 12\n",
|
|
"# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 0], # 13\n",
|
|
"# [0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15], # 14\n",
|
|
"# # 15\n",
|
|
"# ]\n",
|
|
"\n",
|
|
"#\n",
|
|
"# However, the DFA is easy to create from adjacency lists.\n",
|
|
"#\n",
|
|
"states = [\n",
|
|
" [2, 9], # state 1\n",
|
|
" [3], # state 2\n",
|
|
" [4, 9], # state 3\n",
|
|
" [5], # state 4\n",
|
|
" [6, 9], # state 5\n",
|
|
" [7], # state 6\n",
|
|
" [8, 9], # state 7\n",
|
|
" [15], # state 8\n",
|
|
" [10], # state 9\n",
|
|
" [11], # state 10\n",
|
|
" [12], # state 11\n",
|
|
" [13], # state 12\n",
|
|
" [14], # state 13\n",
|
|
" [15] # state 14\n",
|
|
"]\n",
|
|
"\n",
|
|
"transition_fn = []\n",
|
|
"for i in range(n_states):\n",
|
|
" row = []\n",
|
|
" for j in range(1, input_max + 1):\n",
|
|
" if j in states[i]:\n",
|
|
" row.append(j)\n",
|
|
" else:\n",
|
|
" row.append(0)\n",
|
|
" transition_fn.append(row)\n",
|
|
"\n",
|
|
"#\n",
|
|
"# The name of the nodes, for printing\n",
|
|
"# the solution.\n",
|
|
"#\n",
|
|
"nodes = [\n",
|
|
" '8,0,0', # 1 start\n",
|
|
" '5,0,3', # 2\n",
|
|
" '5,3,0', # 3\n",
|
|
" '2,3,3', # 4\n",
|
|
" '2,5,1', # 5\n",
|
|
" '7,0,1', # 6\n",
|
|
" '7,1,0', # 7\n",
|
|
" '4,1,3', # 8\n",
|
|
" '3,5,0', # 9\n",
|
|
" '3,2,3', # 10\n",
|
|
" '6,2,0', # 11\n",
|
|
" '6,0,2', # 12\n",
|
|
" '1,5,2', # 13\n",
|
|
" '1,4,3', # 14\n",
|
|
" '4,4,0' # 15 goal\n",
|
|
"]\n",
|
|
"\n",
|
|
"#\n",
|
|
"# declare variables\n",
|
|
"#\n",
|
|
"x = [solver.IntVar(1, input_max, 'x[%i]' % i) for i in range(n)]\n",
|
|
"\n",
|
|
"#\n",
|
|
"# constraints\n",
|
|
"#\n",
|
|
"regular(x, n_states, input_max, transition_fn, initial_state,\n",
|
|
" accepting_states)\n",
|
|
"\n",
|
|
"#\n",
|
|
"# solution and search\n",
|
|
"#\n",
|
|
"db = solver.Phase(x, solver.INT_VAR_DEFAULT, solver.INT_VALUE_DEFAULT)\n",
|
|
"\n",
|
|
"solver.NewSearch(db)\n",
|
|
"\n",
|
|
"num_solutions = 0\n",
|
|
"x_val = []\n",
|
|
"while solver.NextSolution():\n",
|
|
" num_solutions += 1\n",
|
|
" x_val = [1] + [x[i].Value() for i in range(n)]\n",
|
|
" print('x:', x_val)\n",
|
|
" for i in range(1, n + 1):\n",
|
|
" print('%s -> %s' % (nodes[x_val[i - 1] - 1], nodes[x_val[i] - 1]))\n",
|
|
"\n",
|
|
"solver.EndSearch()\n",
|
|
"\n",
|
|
"if num_solutions > 0:\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",
|
|
"# return the solution (or an empty array)\n",
|
|
"return x_val\n",
|
|
"\n",
|
|
"\n",
|
|
"# Search for a minimum solution by increasing\n",
|
|
"# the length of the state array."
|
|
]
|
|
}
|
|
],
|
|
"metadata": {},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 4
|
|
}
|