{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import spot\n",
"from buddy import bddtrue\n",
"spot.setup()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Support for games\n",
"\n",
"The support for games is currently quite rudimentary, as Spot currently only uses those games in `ltlsynt`.\n",
"\n",
"In essence, a game is just an ω-automaton with a property named `state-player` that stores the player owning each state. The players are named 0 and 1. The player owning a state can decide what the next transition from this state should be. The goal for player 1 is to force the play to be infinite and to satisfy the acceptance condition of the automaton, while the goal for player 0 is to prevent it by either forcing a finite play, or forcing an infinite play that does not satisfy the acceptance condition.\n",
"\n",
"The support is currently restricted to games that use:\n",
"- `t` acceptance: all infinite run are accepting, and player 0 can only win if it manages to force a finite play (this requires reaching states without successors).\n",
"- `parity` acceptance of any form (`max odd`, `max even`, `min odd`, `min even`): player 0 can win if the maximal (or minimal) value seen infinitely often is even (or odd)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Creating games from scratch\n",
"\n",
"Games can be [created like any automaton](https://spot.lrde.epita.fr/tut22.html). \n",
"Using `set_state_players()` will fix the state owners."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"bdict = spot.make_bdd_dict();\n",
"game = spot.make_twa_graph(bdict)\n",
"game.new_states(9)\n",
"for (s, d) in ((0,1), (0, 3), \n",
" (1, 0), (1, 2),\n",
" (2, 1), (2, 5),\n",
" (3, 2), (3, 4), (3, 6),\n",
" (6, 7),\n",
" (7, 6), (7, 8),\n",
" (8, 5)):\n",
" game.new_edge(s, d, bddtrue)\n",
"spot.set_state_players(game, [True, False, True, False, True, True, True, False, False])\n",
"game.show('.g') # Use \"g\" to hide the irrelevant edge labels."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The `set_state_players()` function takes a list of owner for each of the states in the automaton. In the output,\n",
"states from player 0 use circles, ellipses, or rectangle with rounded corners (mnemonic: 0 is round) while states from player 1 have a losanse shape (1 has only straight lines). \n",
"\n",
"\n",
"State ownership can also be manipulated by the following functions:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(True, False, True, False, True, True, True, False, False)"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"spot.get_state_players(game)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"spot.get_state_player(game, 4)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"spot.set_state_player(game, 4, False)\n",
"game.show('.g')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Solving a game\n",
"\n",
"Solving a game is done my calling `solve_game()`. This function actually dispatches to `solve_safety_game()` (if the acceptance is `t`) or to `solve_parity_game()` (if the acceptance is any parity condition). You may call these functions directly if desired, but using `solve_game()` makes it possible for future version of Spot to dispatch to some better function if we add some.\n",
"\n",
"These functions will attach two additional vectors into the game automaton: one vector stores the winner of each state, and one vector stores (memory-less) strategy for each state, i.e., the transition that should always be taken by the owner of this state in order to win. \n",
"\n",
"The return value of those functions is simply the winner for the initial state."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"spot.solve_game(game)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Calling the `highlight_strategy()` function can be used to decorate the `game` automaton using the winning regions and strategies. Below, green represent the winning region/strategy for player 1 and red those for player 0."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"spot.highlight_strategy(game)\n",
"game.show('.g')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Input/Output in HOA format\n",
"\n",
"An extension of the HOA format makes it possible to store the `state-player` property. This allows us to read the parity game constructed by `ltlsynt` using `spot.automaton()` like any other automaton."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
" *' at 0x7fcbe436f840> >"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"game = spot.automaton(\"ltlsynt --ins=a --outs=b -f '!b & GFa <-> Gb' --print-game-hoa |\");\n",
"game"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In the graphical output, player 0 is represented by circles (or ellipses or rounded rectangles depending on the situations), while player 1's states are diamond shaped. In the case of `ltlsynt`, player 0 plays the role of the environment, and player 1 plays the role of the controller.\n",
"\n",
"In the HOA output, a header `spot-state-player` (or `spot.state-player` in HOA 1.1) lists the owner of each state."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"HOA: v1\n",
"States: 11\n",
"Start: 0\n",
"AP: 2 \"b\" \"a\"\n",
"acc-name: co-Buchi\n",
"Acceptance: 1 Fin(0)\n",
"properties: trans-labels explicit-labels trans-acc complete\n",
"properties: deterministic\n",
"spot-state-player: 0 0 0 0 0 1 1 1 1 1 1\n",
"controllable-AP: 0\n",
"--BODY--\n",
"State: 0\n",
"[!1] 5\n",
"[1] 6\n",
"State: 1\n",
"[!1] 7\n",
"[1] 8 {0}\n",
"State: 2\n",
"[!1] 7\n",
"[1] 8 {0}\n",
"State: 3\n",
"[t] 9\n",
"State: 4\n",
"[t] 10\n",
"State: 5\n",
"[!0] 1\n",
"[0] 3\n",
"State: 6\n",
"[!0] 2\n",
"[0] 3\n",
"State: 7\n",
"[t] 1\n",
"State: 8\n",
"[t] 2 {0}\n",
"State: 9\n",
"[0] 3 {0}\n",
"[!0] 4\n",
"State: 10\n",
"[t] 4\n",
"--END--\n"
]
}
],
"source": [
"print(game.to_str('hoa'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here is the solution of this particular game."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"spot.solve_parity_game(game)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
" *' at 0x7fcbe436e9a0> >"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"spot.highlight_strategy(game)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Input/Output in PGSolver format\n",
"\n",
"The automaton parser is also able to parse the [PGSolver](https://github.com/tcsprojects/pgsolver) format. Here are two examples from the manual of PGSolver. The support for C-style comments is not part of the PGSolver format.\n",
"\n",
"Note that we use diamond node for player 1, while PGSolver use those of player 0. Also in Spot the acceptance condition is what Player 1 should satisfy; player 0 has two way to not satisfy it: leading to a rejecting cycle, or to a state without successor. In PGSolver, the graph is assumed to be total (i.e. each state has a successor), so player 0 can only win by reaching a rejecting cycle, which is equivalent to a `parity max even` acceptance."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"a,b = spot.automata(\"\"\"\n",
"parity 4; /* Example 6 in the manual for PGSolver 4.1 */\n",
"0 6 1 4,2 \"Africa\";\n",
"4 5 1 0 \"Antarctica\";\n",
"1 8 1 2,4,3 \"America\";\n",
"3 6 0 4,2 \"Australia\";\n",
"2 7 0 3,1,0,4 \"Asia\";\n",
"parity 8; /* Example 7 in the manual for PGSolver 4.1 */\n",
"0 0 0 1,2;\n",
"1 1 1 2,3;\n",
"2 0 0 3,4;\n",
"3 1 1 4,5;\n",
"4 0 0 5,6;\n",
"5 1 1 6,7;\n",
"6 0 0 7,0;\n",
"7 1 1 0,1;\n",
"\"\"\")\n",
"spot.solve_game(a)\n",
"spot.highlight_strategy(a)\n",
"spot.solve_game(b)\n",
"spot.highlight_strategy(b)\n",
"display(a.show('.g'), b.show('.g'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To output a parity-game in PG-solver format, use `to_str('pg')`."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"parity 4;\n",
"0 6 1 4,2 \"Africa\";\n",
"2 7 0 3,1,0,4 \"Asia\";\n",
"4 5 1 0 \"Antarctica\";\n",
"1 8 1 2,4,3 \"America\";\n",
"3 6 0 4,2 \"Australia\";\n",
"parity 7;\n",
"0 0 0 1,2;\n",
"2 0 0 3,4;\n",
"4 0 0 5,6;\n",
"6 0 0 7,0;\n",
"7 1 1 0,1;\n",
"1 1 1 2,3;\n",
"3 1 1 4,5;\n",
"5 1 1 6,7;\n",
"\n"
]
}
],
"source": [
"print(a.to_str('pg') + b.to_str('pg'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Global vs local solver\n",
"\n",
"The parity game solver now supports \"local\" and global solutions.\n",
"\n",
"- \"Local\" solutions are the ones computed so far. A strategy is only computed for the part of the automaton that is reachable from the initial state\n",
"- Global solutions can now be obtained by setting the argument \"solve_globally\" to true. In this case a strategy will be computed even for states not reachable in the original automaton.\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
" *' at 0x7fcbe4382370> >"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"arena = spot.make_twa_graph()\n",
"\n",
"arena.new_states(3*7)\n",
"arena.set_buchi()\n",
"\n",
"edges = [(0,1), (0,2), (1,3), (2,3), (3,4), (4,0), (5,0), (5,6), (6,5)]\n",
"\n",
"for src, dst in edges:\n",
" arena.new_edge(src, dst, bddtrue, [0] if src == 4 else [])\n",
" arena.new_edge(src + 7, dst + 7, bddtrue, [0] if src == 4 else [])\n",
" arena.new_edge(src + 14, dst + 14, bddtrue, [0] if src == 6 else [])\n",
"\n",
"arena.set_state_players(3*[False, True, True, False, True, True, False])\n",
"arena"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(0, 7, 10, 0, 16, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)\n"
]
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
" *' at 0x7fcbe4382370> >"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 1) Solving the game locally\n",
"# Unreachable parts are ignored, all of them are \"won\" by the env,\n",
"# the associated strategy is the 0 edges indicating no strategy\n",
"spot.solve_parity_game(arena)\n",
"spot.highlight_strategy(arena)\n",
"print(arena.get_strategy())\n",
"arena"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(0, 7, 10, 0, 16, 19, 0, 0, 8, 11, 0, 17, 20, 0, 3, 0, 0, 15, 0, 24, 0)\n"
]
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
" *' at 0x7fcbe4382370> >"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 1) Solving the game globally\n",
"# The whole automaton is considered in this case\n",
"spot.solve_parity_game(arena, True)\n",
"spot.highlight_strategy(arena)\n",
"print(arena.get_strategy())\n",
"arena"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}