The source code and dockerfile for the GSW2024 AI Lab.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

480 lines
12 KiB

2 months ago
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# Getting Started\n",
  8. "\n",
  9. "Before starting with this guide, one should follow the instructions for [Installation](installation.ipynb)."
  10. ]
  11. },
  12. {
  13. "cell_type": "markdown",
  14. "metadata": {},
  15. "source": [
  16. "## A Quick Tour through Stormpy\n",
  17. "\n",
  18. "This guide is intended for people which have a basic understanding of probabilistic models and their verification. More details and further pointers to literature can be found on the\n",
  19. "[Storm website](http://www.stormchecker.org/).\n",
  20. "While we assume some very basic programming concepts, we refrain from using more advanced concepts of python throughout the guide.\n",
  21. "\n",
  22. "We start with a selection of high-level constructs in stormpy, and go into more details afterwards. More in-depth examples can be found in the [Advanced Examples](advanced_topics.ipynb).\n",
  23. "\n",
  24. "The code examples are also given in the [examples/](https://github.com/moves-rwth/stormpy/blob/master/examples/) folder. These boxes throughout the text will tell you which example contains the code discussed.\n",
  25. "\n",
  26. "We start by launching the python 3 interpreter:"
  27. ]
  28. },
  29. {
  30. "cell_type": "markdown",
  31. "metadata": {
  32. "hide-output": false
  33. },
  34. "source": [
  35. "```\n",
  36. "$ python3\n",
  37. "```"
  38. ]
  39. },
  40. {
  41. "cell_type": "markdown",
  42. "metadata": {},
  43. "source": [
  44. "First we import stormpy:"
  45. ]
  46. },
  47. {
  48. "cell_type": "code",
  49. "execution_count": null,
  50. "metadata": {
  51. "hide-output": false
  52. },
  53. "outputs": [],
  54. "source": [
  55. ">>> import stormpy"
  56. ]
  57. },
  58. {
  59. "cell_type": "markdown",
  60. "metadata": {},
  61. "source": [
  62. "### Building models\n",
  63. "\n",
  64. "[01-getting-started.py](https://github.com/moves-rwth/stormpy/blob/master/examples/01-getting-started.py)\n",
  65. "\n",
  66. "There are several ways to create a Markov chain.\n",
  67. "One of the easiest is to parse a description of such a Markov chain and to let Storm build the chain.\n",
  68. "\n",
  69. "Here, we build a Markov chain from a prism program.\n",
  70. "Stormpy comes with a small set of examples, which we use here:"
  71. ]
  72. },
  73. {
  74. "cell_type": "code",
  75. "execution_count": null,
  76. "metadata": {
  77. "hide-output": false
  78. },
  79. "outputs": [],
  80. "source": [
  81. ">>> import stormpy.examples\n",
  82. ">>> import stormpy.examples.files"
  83. ]
  84. },
  85. {
  86. "cell_type": "markdown",
  87. "metadata": {},
  88. "source": [
  89. "With this, we can now import the path of our prism file:"
  90. ]
  91. },
  92. {
  93. "cell_type": "code",
  94. "execution_count": null,
  95. "metadata": {
  96. "hide-output": false
  97. },
  98. "outputs": [],
  99. "source": [
  100. ">>> path = stormpy.examples.files.prism_dtmc_die\n",
  101. ">>> prism_program = stormpy.parse_prism_program(path)"
  102. ]
  103. },
  104. {
  105. "cell_type": "markdown",
  106. "metadata": {},
  107. "source": [
  108. "The `prism_program` can be translated into a Markov chain:"
  109. ]
  110. },
  111. {
  112. "cell_type": "code",
  113. "execution_count": null,
  114. "metadata": {
  115. "hide-output": false
  116. },
  117. "outputs": [],
  118. "source": [
  119. ">>> model = stormpy.build_model(prism_program)\n",
  120. ">>> print(\"Number of states: {}\".format(model.nr_states))"
  121. ]
  122. },
  123. {
  124. "cell_type": "code",
  125. "execution_count": null,
  126. "metadata": {},
  127. "outputs": [],
  128. "source": [
  129. ">>> print(\"Number of transitions: {}\".format(model.nr_transitions))"
  130. ]
  131. },
  132. {
  133. "cell_type": "markdown",
  134. "metadata": {},
  135. "source": [
  136. "This tells us that the model has 13 states and 20 transitions.\n",
  137. "\n",
  138. "Moreover, initial states and deadlocks are indicated with a labelling function. We can see the labels present in the model by:"
  139. ]
  140. },
  141. {
  142. "cell_type": "code",
  143. "execution_count": null,
  144. "metadata": {
  145. "hide-output": false
  146. },
  147. "outputs": [],
  148. "source": [
  149. ">>> print(\"Labels: {}\".format(model.labeling.get_labels()))"
  150. ]
  151. },
  152. {
  153. "cell_type": "markdown",
  154. "metadata": {},
  155. "source": [
  156. "We will investigate ways to examine the model in more detail later in [Investigating the model](#getting-started-investigating-the-model).\n",
  157. "\n",
  158. "\n"
  159. ]
  160. },
  161. {
  162. "cell_type": "markdown",
  163. "metadata": {},
  164. "source": [
  165. "### Building properties\n",
  166. "\n",
  167. "[02-getting-started.py](https://github.com/moves-rwth/stormpy/blob/master/examples/02-getting-started.py)\n",
  168. "\n",
  169. "Storm takes properties in the prism-property format.\n",
  170. "To express that one is interested in the reachability of any state where the prism program variable `s` is 2, one would formulate:"
  171. ]
  172. },
  173. {
  174. "cell_type": "markdown",
  175. "metadata": {
  176. "hide-output": false
  177. },
  178. "source": [
  179. "```\n",
  180. "P=? [F s=2]\n",
  181. "```"
  182. ]
  183. },
  184. {
  185. "cell_type": "markdown",
  186. "metadata": {},
  187. "source": [
  188. "Stormpy can be used to parse this. As the variables in the property refer to a program, the program has to be passed as an additional parameter:"
  189. ]
  190. },
  191. {
  192. "cell_type": "code",
  193. "execution_count": null,
  194. "metadata": {
  195. "hide-output": false
  196. },
  197. "outputs": [],
  198. "source": [
  199. ">>> formula_str = \"P=? [F s=2]\"\n",
  200. ">>> properties = stormpy.parse_properties(formula_str, prism_program)"
  201. ]
  202. },
  203. {
  204. "cell_type": "markdown",
  205. "metadata": {},
  206. "source": [
  207. "Notice that properties is now a list of properties containing a single element.\n",
  208. "\n",
  209. "However, if we build the model as before, then the appropriate information that the variable `s=2` in some states is not present.\n",
  210. "In order to label the states accordingly, we should notify Storm upon building the model that we would like to preserve given properties.\n",
  211. "Storm will then add the labels accordingly:"
  212. ]
  213. },
  214. {
  215. "cell_type": "code",
  216. "execution_count": null,
  217. "metadata": {
  218. "hide-output": false
  219. },
  220. "outputs": [],
  221. "source": [
  222. ">>> model = stormpy.build_model(prism_program, properties)\n",
  223. ">>> print(\"Labels in the model: {}\".format(sorted(model.labeling.get_labels())))"
  224. ]
  225. },
  226. {
  227. "cell_type": "markdown",
  228. "metadata": {},
  229. "source": [
  230. "Model building however now behaves slightly different: Only the properties passed are preserved, which means that model building might skip parts of the model.\n",
  231. "In particular, to check the probability of eventually reaching a state `x` where `s=2`, successor states of `x` are not relevant:"
  232. ]
  233. },
  234. {
  235. "cell_type": "code",
  236. "execution_count": null,
  237. "metadata": {
  238. "hide-output": false
  239. },
  240. "outputs": [],
  241. "source": [
  242. ">>> print(\"Number of states: {}\".format(model.nr_states))"
  243. ]
  244. },
  245. {
  246. "cell_type": "markdown",
  247. "metadata": {},
  248. "source": [
  249. "If we consider another property, however, such as:"
  250. ]
  251. },
  252. {
  253. "cell_type": "markdown",
  254. "metadata": {
  255. "hide-output": false
  256. },
  257. "source": [
  258. "```\n",
  259. "P=? [F s=7 & d=2]\n",
  260. "```"
  261. ]
  262. },
  263. {
  264. "cell_type": "markdown",
  265. "metadata": {},
  266. "source": [
  267. "then Storm is only skipping exploration of successors of the particular state `y` where `s=7` and `d=2`. In this model, state `y` has a self-loop, so effectively, the whole model is explored.\n",
  268. "\n",
  269. "\n",
  270. "<a id='getting-started-checking-properties'></a>"
  271. ]
  272. },
  273. {
  274. "cell_type": "markdown",
  275. "metadata": {},
  276. "source": [
  277. "### Checking properties\n",
  278. "\n",
  279. "[03-getting-started.py](https://github.com/moves-rwth/stormpy/blob/master/examples/03-getting-started.py)\n",
  280. "\n",
  281. "The last lesson taught us to construct properties and models with matching state labels.\n",
  282. "Now default checking routines are just a simple command away:"
  283. ]
  284. },
  285. {
  286. "cell_type": "code",
  287. "execution_count": null,
  288. "metadata": {
  289. "hide-output": false
  290. },
  291. "outputs": [],
  292. "source": [
  293. ">>> properties = stormpy.parse_properties(formula_str, prism_program)\n",
  294. ">>> model = stormpy.build_model(prism_program, properties)\n",
  295. ">>> result = stormpy.model_checking(model, properties[0])"
  296. ]
  297. },
  298. {
  299. "cell_type": "markdown",
  300. "metadata": {},
  301. "source": [
  302. "The result may contain information about all states.\n",
  303. "Instead, we can iterate over the results:"
  304. ]
  305. },
  306. {
  307. "cell_type": "code",
  308. "execution_count": null,
  309. "metadata": {
  310. "hide-output": false
  311. },
  312. "outputs": [],
  313. "source": [
  314. ">>> assert result.result_for_all_states\n",
  315. ">>> for x in result.get_values():\n",
  316. "... pass # do something with x"
  317. ]
  318. },
  319. {
  320. "cell_type": "markdown",
  321. "metadata": {},
  322. "source": [
  323. "#### Results for all states\n",
  324. "\n",
  325. "Some model checking algorithms do not provide results for all states. In those cases, the result is not valid for all states, and to iterate over them, a different method is required. We will explain this later.\n",
  326. "\n",
  327. "A good way to get the result for the initial states is as follows:"
  328. ]
  329. },
  330. {
  331. "cell_type": "code",
  332. "execution_count": null,
  333. "metadata": {
  334. "hide-output": false
  335. },
  336. "outputs": [],
  337. "source": [
  338. ">>> initial_state = model.initial_states[0]\n",
  339. ">>> print(result.at(initial_state))"
  340. ]
  341. },
  342. {
  343. "cell_type": "markdown",
  344. "metadata": {},
  345. "source": [
  346. "\n",
  347. "<a id='getting-started-investigating-the-model'></a>"
  348. ]
  349. },
  350. {
  351. "cell_type": "markdown",
  352. "metadata": {},
  353. "source": [
  354. "### Investigating the model\n",
  355. "\n",
  356. "[04-getting-started.py](https://github.com/moves-rwth/stormpy/blob/master/examples/04-getting-started.py)\n",
  357. "\n",
  358. "One powerful part of the Storm model checker is to quickly create the Markov chain from higher-order descriptions, as seen above:"
  359. ]
  360. },
  361. {
  362. "cell_type": "code",
  363. "execution_count": null,
  364. "metadata": {
  365. "hide-output": false
  366. },
  367. "outputs": [],
  368. "source": [
  369. ">>> path = stormpy.examples.files.prism_dtmc_die\n",
  370. ">>> prism_program = stormpy.parse_prism_program(path)\n",
  371. ">>> model = stormpy.build_model(prism_program)"
  372. ]
  373. },
  374. {
  375. "cell_type": "markdown",
  376. "metadata": {},
  377. "source": [
  378. "In this example, we will exploit this, and explore the underlying Markov chain of the model.\n",
  379. "The most basic question might be what the type of the constructed model is:"
  380. ]
  381. },
  382. {
  383. "cell_type": "code",
  384. "execution_count": null,
  385. "metadata": {
  386. "hide-output": false
  387. },
  388. "outputs": [],
  389. "source": [
  390. ">>> print(model.model_type)"
  391. ]
  392. },
  393. {
  394. "cell_type": "markdown",
  395. "metadata": {},
  396. "source": [
  397. "We can also directly explore the underlying state space/matrix.\n",
  398. "Notice that this code can be applied to both deterministic and non-deterministic models:"
  399. ]
  400. },
  401. {
  402. "cell_type": "code",
  403. "execution_count": null,
  404. "metadata": {
  405. "hide-output": false
  406. },
  407. "outputs": [],
  408. "source": [
  409. ">>> for state in model.states:\n",
  410. "... for action in state.actions:\n",
  411. "... for transition in action.transitions:\n",
  412. "... print(\"From state {}, with probability {}, go to state {}\".format(state, transition.value(), transition.column))"
  413. ]
  414. },
  415. {
  416. "cell_type": "markdown",
  417. "metadata": {},
  418. "source": [
  419. "Let us go into some more details. For DTMCs, each state has (at most) one outgoing probability distribution.\n",
  420. "Thus:"
  421. ]
  422. },
  423. {
  424. "cell_type": "code",
  425. "execution_count": null,
  426. "metadata": {
  427. "hide-output": false
  428. },
  429. "outputs": [],
  430. "source": [
  431. ">>> for state in model.states:\n",
  432. "... assert len(state.actions) <= 1"
  433. ]
  434. },
  435. {
  436. "cell_type": "markdown",
  437. "metadata": {},
  438. "source": [
  439. "We can also check if a state is indeed an initial state. Notice that `model.initial_states` contains state ids, not states.:"
  440. ]
  441. },
  442. {
  443. "cell_type": "code",
  444. "execution_count": null,
  445. "metadata": {
  446. "hide-output": false
  447. },
  448. "outputs": [],
  449. "source": [
  450. ">>> for state in model.states:\n",
  451. "... if state.id in model.initial_states:\n",
  452. "... pass"
  453. ]
  454. }
  455. ],
  456. "metadata": {
  457. "date": 1598188121.7690735,
  458. "filename": "getting_started.rst",
  459. "kernelspec": {
  460. "display_name": "Python 3",
  461. "language": "python",
  462. "name": "python3"
  463. },
  464. "language_info": {
  465. "codemirror_mode": {
  466. "name": "ipython",
  467. "version": 3
  468. },
  469. "file_extension": ".py",
  470. "mimetype": "text/x-python",
  471. "name": "python",
  472. "nbconvert_exporter": "python",
  473. "pygments_lexer": "ipython3",
  474. "version": "3.8.2"
  475. },
  476. "title": "Getting Started"
  477. },
  478. "nbformat": 4,
  479. "nbformat_minor": 4
  480. }