{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SAT-based minimization of deterministic ω-automata\n", "\n", "The `spot.sat_minimize()` Python function is the main entry point for minimizing any **deterministic** ω-automaton. This notebook demonstrates how to use that function.\n", "\n", "**Warning:** while the automata used in this notebook are quite small, working with large automata can require a lot of RAM and take huge amount of time. In its most straightforward variant, `sat_minimize()` takes a input automaton (called *reference*) and then makes a loop to ask a SAT-solver for an equivalent automaton (called *candidate*) with 1 fewer state at each iteration. If the reference has size ($n_i$, $s_i$), i.e. $n_i$ states, $s_i$ acceptance sets, and the candidate has size $(n_o, s_o)$, the SAT encoding uses $\\mathrm{O}(n_i^2\\times n_o^2\\times 2^{s_i+s_o})$ variables and $\\mathrm{O}(n_i^2 \\times n_o^3\\times 2^{s_i+2s_o}\\times |\\Sigma|)$ clauses. Reducing the number of acceptance set the therefore the most important way to simplify a problem." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import spot\n", "spot.setup(show_default='.b')\n", "from IPython.display import display" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Minimizing DBA\n", "\n", "Let's take a simple formula and translate it into a DBA:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\mathsf{G} \\mathsf{F} (a \\leftrightarrow \\mathsf{X} \\mathsf{X} b)$" ], "text/plain": [ "GF(a <-> XXb)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f = spot.formula('GF(a <-> XXb)'); f" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Inf(\n", "\n", ")\n", "[Büchi]\n", "\n", "\n", "\n", "6\n", "\n", "6\n", "\n", "\n", "\n", "\n", "I->6\n", "\n", "\n", "\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "\n", "6->0\n", "\n", "\n", "a\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "\n", "6->1\n", "\n", "\n", "!a\n", "\n", "\n", "\n", "4\n", "\n", "4\n", "\n", "\n", "\n", "0->4\n", "\n", "\n", "a\n", "\n", "\n", "\n", "5\n", "\n", "5\n", "\n", "\n", "\n", "0->5\n", "\n", "\n", "!a\n", "\n", "\n", "\n", "4->6\n", "\n", "\n", "b\n", "\n", "\n", "\n", "4->4\n", "\n", "\n", "a & !b\n", "\n", "\n", "\n", "4->5\n", "\n", "\n", "!a & !b\n", "\n", "\n", "\n", "5->6\n", "\n", "\n", "b\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "5->2\n", "\n", "\n", "a & !b\n", "\n", "\n", "\n", "3\n", "\n", "3\n", "\n", "\n", "\n", "5->3\n", "\n", "\n", "!a & !b\n", "\n", "\n", "\n", "1->2\n", "\n", "\n", "a\n", "\n", "\n", "\n", "1->3\n", "\n", "\n", "!a\n", "\n", "\n", "\n", "2->6\n", "\n", "\n", "!b\n", "\n", "\n", "\n", "2->4\n", "\n", "\n", "a & b\n", "\n", "\n", "\n", "2->5\n", "\n", "\n", "!a & b\n", "\n", "\n", "\n", "3->6\n", "\n", "\n", "!b\n", "\n", "\n", "\n", "3->2\n", "\n", "\n", "a & b\n", "\n", "\n", "\n", "3->3\n", "\n", "\n", "!a & b\n", "\n", "\n", "\n" ], "text/plain": [ " *' at 0x7f7b085aaea0> >" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "aut = f.translate('det', 'BA'); aut" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The above automaton is not minimal and is easily reduced by `sat_minimize()`:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Inf(\n", "\n", ")\n", "[Büchi]\n", "\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "\n", "I->0\n", "\n", "\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "!a & !b\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "!a & b\n", "\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "a & b\n", "\n", "\n", "\n", "\n", "3\n", "\n", "3\n", "\n", "\n", "\n", "0->3\n", "\n", "\n", "a & !b\n", "\n", "\n", "\n", "1->1\n", "\n", "\n", "!a & b\n", "\n", "\n", "\n", "1->3\n", "\n", "\n", "a & b\n", "\n", "\n", "\n", "1->3\n", "\n", "\n", "!b\n", "\n", "\n", "\n", "\n", "2->0\n", "\n", "\n", "!a & !b\n", "\n", "\n", "\n", "2->0\n", "\n", "\n", "!a & b\n", "\n", "\n", "\n", "\n", "2->2\n", "\n", "\n", "a & !b\n", "\n", "\n", "\n", "2->3\n", "\n", "\n", "a & b\n", "\n", "\n", "\n", "\n", "3->0\n", "\n", "\n", "!a & !b\n", "\n", "\n", "\n", "\n", "3->0\n", "\n", "\n", "!a & b\n", "\n", "\n", "\n", "3->2\n", "\n", "\n", "a & !b\n", "\n", "\n", "\n", "\n", "3->2\n", "\n", "\n", "a & b\n", "\n", "\n", "\n" ], "text/plain": [ " *' at 0x7f7b0861fc90> >" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "spot.sat_minimize(aut)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that by default SAT-minimize produces a transition-based automaton with the same acceptance condition. State-based acceptance can be requested with the `state_based` option:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Inf(\n", "\n", ")\n", "[Büchi]\n", "\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "\n", "I->0\n", "\n", "\n", "\n", "\n", "\n", "0->0\n", "\n", "\n", "!a & b\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "a | !b\n", "\n", "\n", "\n", "1->1\n", "\n", "\n", "a & b\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "1->2\n", "\n", "\n", "a & !b\n", "\n", "\n", "\n", "5\n", "\n", "5\n", "\n", "\n", "\n", "1->5\n", "\n", "\n", "!a\n", "\n", "\n", "\n", "2->0\n", "\n", "\n", "!a & b\n", "\n", "\n", "\n", "2->1\n", "\n", "\n", "a & b\n", "\n", "\n", "\n", "2->2\n", "\n", "\n", "a & !b\n", "\n", "\n", "\n", "2->5\n", "\n", "\n", "!a & !b\n", "\n", "\n", "\n", "5->0\n", "\n", "\n", "!a & b\n", "\n", "\n", "\n", "5->1\n", "\n", "\n", "a & b\n", "\n", "\n", "\n", "3\n", "\n", "3\n", "\n", "\n", "\n", "5->3\n", "\n", "\n", "a & !b\n", "\n", "\n", "\n", "4\n", "\n", "4\n", "\n", "\n", "\n", "5->4\n", "\n", "\n", "!a & !b\n", "\n", "\n", "\n", "3->0\n", "\n", "\n", "a & !b\n", "\n", "\n", "\n", "3->1\n", "\n", "\n", "!a & !b\n", "\n", "\n", "\n", "3->2\n", "\n", "\n", "a & b\n", "\n", "\n", "\n", "3->5\n", "\n", "\n", "!a & b\n", "\n", "\n", "\n", "4->0\n", "\n", "\n", "!a & !b\n", "\n", "\n", "\n", "4->1\n", "\n", "\n", "a & !b\n", "\n", "\n", "\n", "4->3\n", "\n", "\n", "a & b\n", "\n", "\n", "\n", "4->4\n", "\n", "\n", "!a & b\n", "\n", "\n", "\n" ], "text/plain": [ " *' at 0x7f7b085b5060> >" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "spot.sat_minimize(aut, state_based=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Minimizing deterministic ω-automata with arbitrary acceptance condition\n", "\n", "Now let's look at examples with more complicated acceptance conditions. \n", "The following Rabin automaton was produced using ltl2dstar 0.5.4 and spot 2.5.2 with\n", "```\n", "ltlfilt --lbt -f '(FGa | Fb) & FGc' | ltl2dstar -H --ltl2nba=spin:ltl2tgba@-Ds - -\n", "```\n", "however we hardcode it so that the notebook can be used even with `ltl2dstar` installed." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "& | F G a F b F G c\n", "\n", "(Fin(\n", "\n", ") & Inf(\n", "\n", ")) | (Fin(\n", "\n", ") & Inf(\n", "\n", "))\n", "[Rabin 2]\n", "\n", "\n", "\n", "5\n", "\n", "5\n", "\n", "\n", "\n", "\n", "\n", "I->5\n", "\n", "\n", "\n", "\n", "\n", "5->5\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "4\n", "\n", "4\n", "\n", "\n", "\n", "\n", "\n", "5->4\n", "\n", "\n", "b & !c\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "\n", "\n", "5->2\n", "\n", "\n", "b & c\n", "\n", "\n", "\n", "3\n", "\n", "3\n", "\n", "\n", "\n", "\n", "\n", "5->3\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "\n", "\n", "\n", "0->0\n", "\n", "\n", "a & c\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "!a & c\n", "\n", "\n", "\n", "0->4\n", "\n", "\n", "!c\n", "\n", "\n", "\n", "1->1\n", "\n", "\n", "c\n", "\n", "\n", "\n", "1->4\n", "\n", "\n", "!c\n", "\n", "\n", "\n", "4->4\n", "\n", "\n", "!c\n", "\n", "\n", "\n", "4->2\n", "\n", "\n", "c\n", "\n", "\n", "\n", "2->4\n", "\n", "\n", "!c\n", "\n", "\n", "\n", "2->2\n", "\n", "\n", "c\n", "\n", "\n", "\n", "3->5\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "3->0\n", "\n", "\n", "a & b & c\n", "\n", "\n", "\n", "3->4\n", "\n", "\n", "(!a & b) | (b & !c)\n", "\n", "\n", "\n", "3->3\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n" ], "text/plain": [ " *' at 0x7f7b08561b70> >" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "large = spot.automaton('''\n", "HOA: v1 States: 6 properties: implicit-labels trans-labels no-univ-branch\n", "deterministic complete stutter-invariant tool: \"ltl2dstar\" \"0.5.4\"\n", "name: \"& | F G a F b F G c\" comment: \"Safra[NBA=4]\" acc-name: Rabin 2\n", "Acceptance: 4 (Fin(0)&Inf(1))|(Fin(2)&Inf(3)) Start: 5 AP: 3 \"a\" \"b\"\n", "\"c\" --BODY-- State: 0 {1 3} 4 4 4 4 1 0 1 0 State: 1 {0 3} 4 4 4 4 1 1\n", "1 1 State: 2 {1 2} 4 4 4 4 2 2 2 2 State: 3 {1 2} 5 5 4 4 5 3 4 0 State:\n", "4 {0 2} 4 4 4 4 2 2 2 2 State: 5 {0 2} 5 5 4 4 5 3 2 2 --END--''')\n", "large.merge_edges()\n", "large" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It can be minimized as a 2-state transition-based Rabin:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "(Fin(\n", "\n", ") & Inf(\n", "\n", ")) | (Fin(\n", "\n", ") & Inf(\n", "\n", "))\n", "[Rabin 2]\n", "\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "\n", "I->0\n", "\n", "\n", "\n", "\n", "\n", "0->0\n", "\n", "\n", "!a & !b & c\n", "\n", "\n", "\n", "\n", "0->0\n", "\n", "\n", "!b & !c\n", "\n", "\n", "\n", "\n", "\n", "0->0\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "b & !c\n", "\n", "\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "b & c\n", "\n", "\n", "\n", "\n", "\n", "1->1\n", "\n", "\n", "!c\n", "\n", "\n", "\n", "\n", "\n", "1->1\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "\n", "\n", "1->1\n", "\n", "\n", "(!a & c) | (b & c)\n", "\n", "\n", "\n", "\n", "\n" ], "text/plain": [ " *' at 0x7f7b08561b40> >" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "small = spot.sat_minimize(large); small" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Or as a 4-state state-based Rabin:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "(Fin(\n", "\n", ") & Inf(\n", "\n", ")) | (Fin(\n", "\n", ") & Inf(\n", "\n", "))\n", "[Rabin 2]\n", "\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "\n", "\n", "\n", "I->0\n", "\n", "\n", "\n", "\n", "\n", "0->0\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "b & c\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "b & !c\n", "\n", "\n", "\n", "3\n", "\n", "3\n", "\n", "\n", "\n", "\n", "0->3\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "1->1\n", "\n", "\n", "!a & b & !c\n", "\n", "\n", "\n", "1->2\n", "\n", "\n", "a | !b | c\n", "\n", "\n", "\n", "2->1\n", "\n", "\n", "!c\n", "\n", "\n", "\n", "2->2\n", "\n", "\n", "c\n", "\n", "\n", "\n", "3->0\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "3->1\n", "\n", "\n", "b & !c\n", "\n", "\n", "\n", "3->2\n", "\n", "\n", "b & c\n", "\n", "\n", "\n", "3->3\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n" ], "text/plain": [ " *' at 0x7f7b08561c00> >" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "spot.sat_minimize(large, state_based=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But do we really need 2 rabin pairs? Let's ask if we can get an equivalent with only one pair. (Note that reducing the number of pairs might require more state, but the `sat_minimize()` function will never attempt to add state unless explicitely instructed to do so. In this case we are therefore looking for a state-based Rabin-1 automaton with at most 4 states.)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Fin(\n", "\n", ") & Inf(\n", "\n", ")\n", "[Rabin 1]\n", "\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "\n", "\n", "I->0\n", "\n", "\n", "\n", "\n", "\n", "0->0\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "(!a & b) | (b & c)\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "3\n", "\n", "3\n", "\n", "\n", "\n", "\n", "0->3\n", "\n", "\n", "a & b & !c\n", "\n", "\n", "\n", "1->1\n", "\n", "\n", "c\n", "\n", "\n", "\n", "1->3\n", "\n", "\n", "!c\n", "\n", "\n", "\n", "2->0\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "2->1\n", "\n", "\n", "b & c\n", "\n", "\n", "\n", "2->2\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "2->3\n", "\n", "\n", "b & !c\n", "\n", "\n", "\n", "3->1\n", "\n", "\n", "!a | b | c\n", "\n", "\n", "\n", "3->3\n", "\n", "\n", "a & !b & !c\n", "\n", "\n", "\n" ], "text/plain": [ " *' at 0x7f7b08561bd0> >" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "spot.sat_minimize(large, state_based=True, acc='Rabin 1')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using the `display_log` option, we can have a hint of what is going on under the hood. Each line in the table shows one call to the SAT solver. The column labeled `target.states` gives the size of the equivalent automaton we ask the SAT-solver to produce, but some of these states may actually be unreachable in the result. The `variables` and `clauses` columns give an indication of the size of the SAT problem. The `enc.*` and `sat.*` columns give the user and system time taken to encode and solve the SAT problem (the unit is \"ticks\", which usually is 1/100 of seconds).\n", "\n", "Below we see that the minimization procedure first tried to squeeze the 6-state input into a 3-state automaton, which failed, and then into a 5-state automaton, which was successful. This 5-state automaton was used as input to produce a smaller 4-state automaton.. Essentially this procedure is doing a binary search towards the minimal size. \n", "\n", "(In this case it does not matter, but be aware that the number of states displayed in the log table are those of complete automata, while the output of `sat_minimize()` is trimmed by default.)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
input.statestarget.statesreachable.statesedgestransitionsvariablesclausesenc.userenc.syssat.usersat.sys
063NaNNaNNaN1428734304010
165516403960336307131190
2544123220081163725040
\n", "
" ], "text/plain": [ " input.states target.states reachable.states edges transitions variables \\\n", "0 6 3 NaN NaN NaN 1428 \n", "1 6 5 5 16 40 3960 \n", "2 5 4 4 12 32 2008 \n", "\n", " clauses enc.user enc.sys sat.user sat.sys \n", "0 73430 4 0 1 0 \n", "1 336307 13 1 19 0 \n", "2 116372 5 0 4 0 " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Fin(\n", "\n", ") & Inf(\n", "\n", ")\n", "[Rabin 1]\n", "\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "\n", "\n", "I->0\n", "\n", "\n", "\n", "\n", "\n", "0->0\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "(!a & b) | (b & c)\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "3\n", "\n", "3\n", "\n", "\n", "\n", "\n", "0->3\n", "\n", "\n", "a & b & !c\n", "\n", "\n", "\n", "1->1\n", "\n", "\n", "c\n", "\n", "\n", "\n", "1->3\n", "\n", "\n", "!c\n", "\n", "\n", "\n", "2->0\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "2->1\n", "\n", "\n", "b & c\n", "\n", "\n", "\n", "2->2\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "2->3\n", "\n", "\n", "b & !c\n", "\n", "\n", "\n", "3->1\n", "\n", "\n", "!a | b | c\n", "\n", "\n", "\n", "3->3\n", "\n", "\n", "a & !b & !c\n", "\n", "\n", "\n" ], "text/plain": [ " *' at 0x7f7b0cb8d6f0> >" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "spot.sat_minimize(large, state_based=True, acc='Rabin 1', display_log=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that we already had a smaller transition-based automaton for this language (in the `small` variable), and that it actually is more efficient to work from that, as seen in problem sizes displayed in the following log." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
input.statestarget.statesreachable.statesedgestransitionsvariablesclausesenc.userenc.syssat.usersat.sys
023NaNNaNNaN483244702000
1255164013351111875040
22441232856573323010
\n", "
" ], "text/plain": [ " input.states target.states reachable.states edges transitions variables \\\n", "0 2 3 NaN NaN NaN 483 \n", "1 2 5 5 16 40 1335 \n", "2 2 4 4 12 32 856 \n", "\n", " clauses enc.user enc.sys sat.user sat.sys \n", "0 24470 2 0 0 0 \n", "1 111187 5 0 4 0 \n", "2 57332 3 0 1 0 " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Fin(\n", "\n", ") & Inf(\n", "\n", ")\n", "[Rabin 1]\n", "\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "\n", "\n", "I->0\n", "\n", "\n", "\n", "\n", "\n", "0->0\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "(!a & b) | (b & c)\n", "\n", "\n", "\n", "3\n", "\n", "3\n", "\n", "\n", "\n", "\n", "0->3\n", "\n", "\n", "a & b & !c\n", "\n", "\n", "\n", "1->0\n", "\n", "\n", "!b & c\n", "\n", "\n", "\n", "1->1\n", "\n", "\n", "!b & !c\n", "\n", "\n", "\n", "1->2\n", "\n", "\n", "b & c\n", "\n", "\n", "\n", "1->3\n", "\n", "\n", "b & !c\n", "\n", "\n", "\n", "2->2\n", "\n", "\n", "c\n", "\n", "\n", "\n", "2->3\n", "\n", "\n", "!c\n", "\n", "\n", "\n", "3->2\n", "\n", "\n", "b | c\n", "\n", "\n", "\n", "3->3\n", "\n", "\n", "!b & !c\n", "\n", "\n", "\n" ], "text/plain": [ " *' at 0x7f7b0cb8dc00> >" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "spot.sat_minimize(small, state_based=True, acc='Rabin 1', display_log=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "How did the procedure look for a complete automaton of size 5 when the input had only 2 states? It's because the input uses transition-based acceptance: to estimate an upper bound of the size of the state-based output, the `sat_minimize()` procedure converted its transition-based input to state-based acceptance (using the `spot.sbacc()` function) and counted the number of states in the result.\n", "\n", "Such an estimate is not necessarily correct of we request a different acceptance condition. In that case We can actually change the upper-bound using `max_states`. Below we additionally demonstrate the use of the `colored` option, to request all transition to belong to exactly one set, as customary in parity automata." ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
input.statestarget.statesreachable.statesedgestransitionsvariablesclausesenc.userenc.syssat.usersat.sys
025518403050437037200170
1222616488284492000
221NaNNaNNaN6812850000
\n", "
" ], "text/plain": [ " input.states target.states reachable.states edges transitions variables \\\n", "0 2 5 5 18 40 3050 \n", "1 2 2 2 6 16 488 \n", "2 2 1 NaN NaN NaN 68 \n", "\n", " clauses enc.user enc.sys sat.user sat.sys \n", "0 437037 20 0 17 0 \n", "1 28449 2 0 0 0 \n", "2 1285 0 0 0 0 " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Fin(\n", "\n", ") & (Inf(\n", "\n", ") | Fin(\n", "\n", "))\n", "[parity min odd 3]\n", "\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "\n", "I->0\n", "\n", "\n", "\n", "\n", "\n", "0->0\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "\n", "0->0\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "b & !c\n", "\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "b & c\n", "\n", "\n", "\n", "\n", "1->1\n", "\n", "\n", "!c\n", "\n", "\n", "\n", "\n", "1->1\n", "\n", "\n", "c\n", "\n", "\n", "\n", "\n" ], "text/plain": [ " *' at 0x7f7b0cb8dd50> >" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "spot.sat_minimize(small, max_states=9, acc='parity min odd 3', colored=True, display_log=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There are a couple of ways in which we can influence the search for the minimum automaton. We can disable the binary search with `sat_naive`. In this case, the procedure will try to remove one state at a time. This is not necessary slower than the default binary search, because satisfiable problems are often solved more quickly than unsatisfiable ones." ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
input.statestarget.statesreachable.statesedgestransitionsvariablesclausesenc.userenc.syssat.usersat.sys
0666194838942587199060
1655154020051063025020
25441132676246321010
343NaNNaNNaN363105530000
\n", "
" ], "text/plain": [ " input.states target.states reachable.states edges transitions variables \\\n", "0 6 6 6 19 48 3894 \n", "1 6 5 5 15 40 2005 \n", "2 5 4 4 11 32 676 \n", "3 4 3 NaN NaN NaN 363 \n", "\n", " clauses enc.user enc.sys sat.user sat.sys \n", "0 258719 9 0 6 0 \n", "1 106302 5 0 2 0 \n", "2 24632 1 0 1 0 \n", "3 10553 0 0 0 0 " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Fin(\n", "\n", ")\n", "[co-Büchi]\n", "\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "\n", "\n", "I->0\n", "\n", "\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "!b\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "(!a & b) | (b & c)\n", "\n", "\n", "\n", "3\n", "\n", "3\n", "\n", "\n", "\n", "\n", "0->3\n", "\n", "\n", "a & b & !c\n", "\n", "\n", "\n", "1->0\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "1->1\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "1->2\n", "\n", "\n", "(!a & b) | (b & c)\n", "\n", "\n", "\n", "1->3\n", "\n", "\n", "a & b & !c\n", "\n", "\n", "\n", "2->2\n", "\n", "\n", "c\n", "\n", "\n", "\n", "2->3\n", "\n", "\n", "!c\n", "\n", "\n", "\n", "3->2\n", "\n", "\n", "b | c\n", "\n", "\n", "\n", "3->3\n", "\n", "\n", "!b & !c\n", "\n", "\n", "\n" ], "text/plain": [ " *' at 0x7f7b0cb8dde0> >" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "spot.sat_minimize(large, acc='co-Buchi', sat_naive=True, state_based=True, display_log=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Variant for incremental SAT solving\n", "\n", "Using `sat_incr=1`, we encode the problem of finding an equivalent automaton with $n$ states, and add 6 additional variables and some additional constraints to the problem:\n", "\n", "| variable | implied constraints |\n", "| :--- | ---: |\n", "| $v_1$ | transitions to state $(n-1)$ must not be used | \n", "| $v_2$ | $v_1\\land{}$ transitions to state $(n-2)$ must not be used |\n", "| ... | |\n", "| $v_6$ | $v_5\\land{}$ transitions to state $(n-5)$ must not be used |\n", "\n", "Now using `assume` directives on variable $v_i$ amounts to testing whether the problem is solved with $n-i$ states, but we do not have to reencode the problem for each test, and the solver can (probably) reuse some of the knowledge it gathered during a previous attempt. We do a binary search on these 6 assumptions, to find some $i$ such that the problem is satisfiable with assumption $v_i$ but not with $v_{i+1}$. If such cast exists, we have found the minimal automaton. If assumption $v_6$ is satisfiable, we re-encode the problem with $n-7$ states and start over. Watch how the number of variables and clauses do not change in the following log.\n", "\n", "The number of assumption variables to use in a one encoding can be set with the `sat_incr_steps` argument. Its default value of 6 was chosen empirically by benchmarking different values." ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
input.statestarget.statesreachable.statesedgestransitionsvariablesclausesenc.userenc.syssat.usersat.sys
061NaNNaNNaN389925896310030
163NaNNaNNaN38992589630010
2644113238992589630000
\n", "
" ], "text/plain": [ " input.states target.states reachable.states edges transitions variables \\\n", "0 6 1 NaN NaN NaN 3899 \n", "1 6 3 NaN NaN NaN 3899 \n", "2 6 4 4 11 32 3899 \n", "\n", " clauses enc.user enc.sys sat.user sat.sys \n", "0 258963 10 0 3 0 \n", "1 258963 0 0 1 0 \n", "2 258963 0 0 0 0 " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Fin(\n", "\n", ")\n", "[co-Büchi]\n", "\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "\n", "\n", "I->0\n", "\n", "\n", "\n", "\n", "\n", "0->0\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "!a & b & !c\n", "\n", "\n", "\n", "3\n", "\n", "3\n", "\n", "\n", "\n", "\n", "0->3\n", "\n", "\n", "(a & b) | (b & c)\n", "\n", "\n", "\n", "1->0\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "1->1\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "1->2\n", "\n", "\n", "b\n", "\n", "\n", "\n", "2->2\n", "\n", "\n", "c\n", "\n", "\n", "\n", "2->3\n", "\n", "\n", "!c\n", "\n", "\n", "\n", "3->2\n", "\n", "\n", "a | !b | c\n", "\n", "\n", "\n", "3->3\n", "\n", "\n", "!a & b & !c\n", "\n", "\n", "\n" ], "text/plain": [ " *' at 0x7f7b08561690> >" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "spot.sat_minimize(large, acc='co-Buchi', sat_incr=1, state_based=True, display_log=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Another incremental variant consists is the equivalent of forcing $v_1$, $v_2$, ... in order. But to do that we do not need to use any assumption. We just add the constraints that transitions going to state $n-i$ are forbidden. This variant is enabled by option `sat_incr=2`. As in the previous case, we do a few of those incremental steps (2 by default, but that can be changed with the `sat_incr_steps` parameter) and then we reencode the problem to reduce its size.\n", "\n", "In the log below, line 0 corresponds to the search of an equivalent automaton with the same size, but the simpler co-Büchi acceptance. It works, and most of the time was spent encoding the problem. Then for the next two lines, the minimization function looks for automata of size 5 and 4 without reencoding the problem but simply adding a few constraints to disable the relevant transitions." ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
input.statestarget.statesreachable.statesedgestransitionsvariablesclausesenc.userenc.syssat.usersat.sys
06661948389425871910060
1655144038942587670000
2544103238942588151000
343NaNNaNNaN363103250010
\n", "
" ], "text/plain": [ " input.states target.states reachable.states edges transitions variables \\\n", "0 6 6 6 19 48 3894 \n", "1 6 5 5 14 40 3894 \n", "2 5 4 4 10 32 3894 \n", "3 4 3 NaN NaN NaN 363 \n", "\n", " clauses enc.user enc.sys sat.user sat.sys \n", "0 258719 10 0 6 0 \n", "1 258767 0 0 0 0 \n", "2 258815 1 0 0 0 \n", "3 10325 0 0 1 0 " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Fin(\n", "\n", ")\n", "[co-Büchi]\n", "\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "\n", "\n", "I->0\n", "\n", "\n", "\n", "\n", "\n", "0->0\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "b\n", "\n", "\n", "\n", "3\n", "\n", "3\n", "\n", "\n", "\n", "0->3\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "2->2\n", "\n", "\n", "(!a & !c) | (!b & !c)\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "\n", "2->1\n", "\n", "\n", "c | (a & b)\n", "\n", "\n", "\n", "3->0\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "3->2\n", "\n", "\n", "b\n", "\n", "\n", "\n", "3->3\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "1->2\n", "\n", "\n", "!c\n", "\n", "\n", "\n", "1->1\n", "\n", "\n", "c\n", "\n", "\n", "\n" ], "text/plain": [ " *' at 0x7f7b08561ab0> >" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "spot.sat_minimize(large, acc='co-Buchi', sat_incr=2, state_based=True, display_log=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Miscellaneous options\n", "\n", "### `return_log`\n", "\n", "The `return_log` can be used to obtain the log table as an object. In that case, `sat_minimize()` returns a pair, `(aut,log)` where `aut` can be `None` if the minimization failed. Also, the `log` table contains an extra column that is hidden by `display_log`: it contains the corresponding automaton in HOA format." ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Fin(\n", "\n", ")\n", "[co-Büchi]\n", "\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "\n", "\n", "I->0\n", "\n", "\n", "\n", "\n", "\n", "0->0\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "b\n", "\n", "\n", "\n", "3\n", "\n", "3\n", "\n", "\n", "\n", "0->3\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "2->2\n", "\n", "\n", "(!a & !c) | (!b & !c)\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "\n", "2->1\n", "\n", "\n", "c | (a & b)\n", "\n", "\n", "\n", "3->0\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "3->2\n", "\n", "\n", "b\n", "\n", "\n", "\n", "3->3\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "1->2\n", "\n", "\n", "!c\n", "\n", "\n", "\n", "1->1\n", "\n", "\n", "c\n", "\n", "\n", "\n" ], "text/plain": [ " *' at 0x7f7b0cb610f0> >" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
input.statestarget.statesreachable.statesedgestransitionsvariablesclausesenc.userenc.syssat.usersat.sysautomaton
06661948389425871910050HOA: v1 States: 6 Start: 0 AP: 3 \"a\" \"c\" \"b\" a...
1655144038942587670000HOA: v1 States: 5 Start: 0 AP: 3 \"a\" \"c\" \"b\" a...
2544103238942588150000HOA: v1 States: 4 Start: 0 AP: 3 \"a\" \"c\" \"b\" a...
343NaNNaNNaN363103250010NaN
\n", "
" ], "text/plain": [ " input.states target.states reachable.states edges transitions variables \\\n", "0 6 6 6 19 48 3894 \n", "1 6 5 5 14 40 3894 \n", "2 5 4 4 10 32 3894 \n", "3 4 3 NaN NaN NaN 363 \n", "\n", " clauses enc.user enc.sys sat.user sat.sys \\\n", "0 258719 10 0 5 0 \n", "1 258767 0 0 0 0 \n", "2 258815 0 0 0 0 \n", "3 10325 0 0 1 0 \n", "\n", " automaton \n", "0 HOA: v1 States: 6 Start: 0 AP: 3 \"a\" \"c\" \"b\" a... \n", "1 HOA: v1 States: 5 Start: 0 AP: 3 \"a\" \"c\" \"b\" a... \n", "2 HOA: v1 States: 4 Start: 0 AP: 3 \"a\" \"c\" \"b\" a... \n", "3 NaN " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "aut, log = spot.sat_minimize(large, acc='co-Buchi', sat_incr=2, state_based=True, return_log=True)\n", "display(aut)\n", "display(log)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is how we can extract the automata from that log:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "automaton from line 0:\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Fin(\n", "\n", ")\n", "[co-Büchi]\n", "\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "\n", "\n", "I->0\n", "\n", "\n", "\n", "\n", "\n", "0->0\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "b\n", "\n", "\n", "\n", "4\n", "\n", "4\n", "\n", "\n", "\n", "\n", "0->4\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "\n", "2->1\n", "\n", "\n", "c | (a & b)\n", "\n", "\n", "\n", "5\n", "\n", "5\n", "\n", "\n", "\n", "2->5\n", "\n", "\n", "(!a & !c) | (!b & !c)\n", "\n", "\n", "\n", "4->0\n", "\n", "\n", "!a & !b & c\n", "\n", "\n", "\n", "4->2\n", "\n", "\n", "(!a & b) | (b & !c)\n", "\n", "\n", "\n", "4->4\n", "\n", "\n", "!b & !c\n", "\n", "\n", "\n", "4->5\n", "\n", "\n", "a & b & c\n", "\n", "\n", "\n", "3\n", "\n", "3\n", "\n", "\n", "\n", "4->3\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "1->2\n", "\n", "\n", "!c\n", "\n", "\n", "\n", "1->1\n", "\n", "\n", "c\n", "\n", "\n", "\n", "5->2\n", "\n", "\n", "(!a & !c) | (b & !c)\n", "\n", "\n", "\n", "5->1\n", "\n", "\n", "a & !b & !c\n", "\n", "\n", "\n", "5->5\n", "\n", "\n", "c\n", "\n", "\n", "\n", "3->2\n", "\n", "\n", "b & !c\n", "\n", "\n", "\n", "3->4\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "3->5\n", "\n", "\n", "b & c\n", "\n", "\n", "\n", "3->3\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n" ], "text/plain": [ " *' at 0x7f7b0cb61300> >" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "automaton from line 1:\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Fin(\n", "\n", ")\n", "[co-Büchi]\n", "\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "\n", "\n", "I->0\n", "\n", "\n", "\n", "\n", "\n", "0->0\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "b\n", "\n", "\n", "\n", "4\n", "\n", "4\n", "\n", "\n", "\n", "\n", "0->4\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "2->2\n", "\n", "\n", "(!a & !c) | (!b & !c)\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "\n", "2->1\n", "\n", "\n", "c | (a & b)\n", "\n", "\n", "\n", "4->0\n", "\n", "\n", "!a & !b & c\n", "\n", "\n", "\n", "4->2\n", "\n", "\n", "b\n", "\n", "\n", "\n", "4->4\n", "\n", "\n", "!b & !c\n", "\n", "\n", "\n", "3\n", "\n", "3\n", "\n", "\n", "\n", "4->3\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "1->2\n", "\n", "\n", "!c\n", "\n", "\n", "\n", "1->1\n", "\n", "\n", "c\n", "\n", "\n", "\n", "3->2\n", "\n", "\n", "b\n", "\n", "\n", "\n", "3->4\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "3->3\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n" ], "text/plain": [ " *' at 0x7f7b0cb613f0> >" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "automaton from line 2:\n" ] }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Fin(\n", "\n", ")\n", "[co-Büchi]\n", "\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "\n", "\n", "I->0\n", "\n", "\n", "\n", "\n", "\n", "0->0\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "b\n", "\n", "\n", "\n", "3\n", "\n", "3\n", "\n", "\n", "\n", "0->3\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "2->2\n", "\n", "\n", "(!a & !c) | (!b & !c)\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "\n", "2->1\n", "\n", "\n", "c | (a & b)\n", "\n", "\n", "\n", "3->0\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "3->2\n", "\n", "\n", "b\n", "\n", "\n", "\n", "3->3\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "1->2\n", "\n", "\n", "!c\n", "\n", "\n", "\n", "1->1\n", "\n", "\n", "c\n", "\n", "\n", "\n" ], "text/plain": [ " *' at 0x7f7b0cb61480> >" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "for line, data in log.iterrows():\n", " if type(data.automaton) is str:\n", " print(\"automaton from line {}:\".format(line))\n", " display(spot.automaton(data.automaton + \"\\n\"))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `sat_langmap`\n", "\n", "When using the default binary search approach, the `sat_langmap=True` can help refine the lower bound by first testing the language-equivalence of all states in the automaton. This allows to form equivalence classes of states, and clearly the minimal automaton needs at least as many states as the number of equivalence states.\n", "\n", "For instance in the `large` automaton we use as example, the 6 states correspond to only two different languages. This can be seen with the `highlight_language()` function, which colors states with identical languages. This information can be used by the minimization function to search a minimal automaton between 2 and 6 states." ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "& | F G a F b F G c\n", "\n", "(Fin(\n", "\n", ") & Inf(\n", "\n", ")) | (Fin(\n", "\n", ") & Inf(\n", "\n", "))\n", "[Rabin 2]\n", "\n", "\n", "\n", "5\n", "\n", "5\n", "\n", "\n", "\n", "\n", "\n", "I->5\n", "\n", "\n", "\n", "\n", "\n", "5->5\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "4\n", "\n", "4\n", "\n", "\n", "\n", "\n", "\n", "5->4\n", "\n", "\n", "b & !c\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "\n", "\n", "5->2\n", "\n", "\n", "b & c\n", "\n", "\n", "\n", "3\n", "\n", "3\n", "\n", "\n", "\n", "\n", "\n", "5->3\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "\n", "\n", "\n", "0->0\n", "\n", "\n", "a & c\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "!a & c\n", "\n", "\n", "\n", "0->4\n", "\n", "\n", "!c\n", "\n", "\n", "\n", "1->1\n", "\n", "\n", "c\n", "\n", "\n", "\n", "1->4\n", "\n", "\n", "!c\n", "\n", "\n", "\n", "4->4\n", "\n", "\n", "!c\n", "\n", "\n", "\n", "4->2\n", "\n", "\n", "c\n", "\n", "\n", "\n", "2->4\n", "\n", "\n", "!c\n", "\n", "\n", "\n", "2->2\n", "\n", "\n", "c\n", "\n", "\n", "\n", "3->5\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "3->0\n", "\n", "\n", "a & b & c\n", "\n", "\n", "\n", "3->4\n", "\n", "\n", "(!a & b) | (b & !c)\n", "\n", "\n", "\n", "3->3\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n" ], "text/plain": [ " *' at 0x7f7b08561b70> >" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "spot.highlight_languages(large); large" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compare the next two logs, with and without `sat_langmap`." ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
input.statestarget.statesreachable.statesedgestransitionsvariablesclausesenc.userenc.syssat.usersat.sys
063NaNNaNNaN975329121010
1654103227051502576030
\n", "
" ], "text/plain": [ " input.states target.states reachable.states edges transitions variables \\\n", "0 6 3 NaN NaN NaN 975 \n", "1 6 5 4 10 32 2705 \n", "\n", " clauses enc.user enc.sys sat.user sat.sys \n", "0 32912 1 0 1 0 \n", "1 150257 6 0 3 0 " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Fin(\n", "\n", ")\n", "[co-Büchi]\n", "\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "\n", "\n", "I->0\n", "\n", "\n", "\n", "\n", "\n", "0->0\n", "\n", "\n", "!a & !b & !c\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "b\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "(a & !b) | (!b & c)\n", "\n", "\n", "\n", "1->1\n", "\n", "\n", "c\n", "\n", "\n", "\n", "3\n", "\n", "3\n", "\n", "\n", "\n", "\n", "1->3\n", "\n", "\n", "!c\n", "\n", "\n", "\n", "2->0\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "2->1\n", "\n", "\n", "b\n", "\n", "\n", "\n", "2->2\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "3->1\n", "\n", "\n", "a | c\n", "\n", "\n", "\n", "3->3\n", "\n", "\n", "!a & !c\n", "\n", "\n", "\n" ], "text/plain": [ " *' at 0x7f7b0cb61510> >" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Binary search between 1 and 6\n", "spot.sat_minimize(large, acc='co-Buchi', state_based=True, display_log=True)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
input.statestarget.statesreachable.statesedgestransitionsvariablesclausesenc.userenc.syssat.usersat.sys
064412321732773403010
142NaNNaNNaN16231290000
243NaNNaNNaN363104960000
\n", "
" ], "text/plain": [ " input.states target.states reachable.states edges transitions variables \\\n", "0 6 4 4 12 32 1732 \n", "1 4 2 NaN NaN NaN 162 \n", "2 4 3 NaN NaN NaN 363 \n", "\n", " clauses enc.user enc.sys sat.user sat.sys \n", "0 77340 3 0 1 0 \n", "1 3129 0 0 0 0 \n", "2 10496 0 0 0 0 " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Fin(\n", "\n", ")\n", "[co-Büchi]\n", "\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "\n", "I->0\n", "\n", "\n", "\n", "\n", "\n", "0->0\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "b & !c\n", "\n", "\n", "\n", "3\n", "\n", "3\n", "\n", "\n", "\n", "0->3\n", "\n", "\n", "b & c\n", "\n", "\n", "\n", "1->0\n", "\n", "\n", "(!a & !b) | (!b & c)\n", "\n", "\n", "\n", "1->1\n", "\n", "\n", "a & !b & !c\n", "\n", "\n", "\n", "1->2\n", "\n", "\n", "b & !c\n", "\n", "\n", "\n", "1->3\n", "\n", "\n", "b & c\n", "\n", "\n", "\n", "2->2\n", "\n", "\n", "b & !c\n", "\n", "\n", "\n", "2->3\n", "\n", "\n", "!b | c\n", "\n", "\n", "\n", "3->2\n", "\n", "\n", "!c\n", "\n", "\n", "\n", "3->3\n", "\n", "\n", "c\n", "\n", "\n", "\n" ], "text/plain": [ " *' at 0x7f7b085aaf30> >" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Binary search between 2 and 6 thanks to sat_langmap\n", "spot.sat_minimize(large, acc='co-Buchi', sat_langmap=True, state_based=True, display_log=True)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `states`\n", "\n", "Sometimes we do not want a minimization loop, we just want to generate an equivalent automaton with a given number of states. In that case, we use the `states` option. However there is no constraint that all state should be reachable, so in the end, you could end with an automaton with fewer states than requested." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
input.statestarget.statesreachable.statesedgestransitionsvariablesclausesenc.userenc.syssat.usersat.sys
0275144018691350325020
\n", "
" ], "text/plain": [ " input.states target.states reachable.states edges transitions variables \\\n", "0 2 7 5 14 40 1869 \n", "\n", " clauses enc.user enc.sys sat.user sat.sys \n", "0 135032 5 0 2 0 " ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "Fin(\n", "\n", ")\n", "[co-Büchi]\n", "\n", "\n", "\n", "0\n", "\n", "0\n", "\n", "\n", "\n", "\n", "I->0\n", "\n", "\n", "\n", "\n", "\n", "0->0\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "1\n", "\n", "1\n", "\n", "\n", "\n", "\n", "0->1\n", "\n", "\n", "!a & b & !c\n", "\n", "\n", "\n", "2\n", "\n", "2\n", "\n", "\n", "\n", "\n", "0->2\n", "\n", "\n", "!a & b & c\n", "\n", "\n", "\n", "3\n", "\n", "3\n", "\n", "\n", "\n", "0->3\n", "\n", "\n", "a & b\n", "\n", "\n", "\n", "4\n", "\n", "4\n", "\n", "\n", "\n", "0->4\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n", "1->1\n", "\n", "\n", "!a & !b & !c\n", "\n", "\n", "\n", "1->2\n", "\n", "\n", "!b & c\n", "\n", "\n", "\n", "1->3\n", "\n", "\n", "b | (a & !c)\n", "\n", "\n", "\n", "2->3\n", "\n", "\n", "1\n", "\n", "\n", "\n", "3->2\n", "\n", "\n", "!c\n", "\n", "\n", "\n", "3->3\n", "\n", "\n", "c\n", "\n", "\n", "\n", "4->0\n", "\n", "\n", "(!a & !b) | (!b & !c)\n", "\n", "\n", "\n", "4->3\n", "\n", "\n", "b\n", "\n", "\n", "\n", "4->4\n", "\n", "\n", "a & !b & c\n", "\n", "\n", "\n" ], "text/plain": [ " *' at 0x7f7b0cb61660> >" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "spot.sat_minimize(small, acc=\"co-Buchi\", states=7, state_based=True, display_log=True)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.5" } }, "nbformat": 4, "nbformat_minor": 2 }