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.
 
 
 
 
 
 

189 lines
4.4 KiB

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Continuous-time Markov chains (CTMCs)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Background\n",
"\n",
"In this section, we explain how Stormpy can be used to build a simple CTMC.\n",
"Building CTMCs works similar to building DTMCs as in [Discrete-time Markov chains (DTMCs)](building_dtmcs.ipynb), but additionally every state is equipped with an exit rate.\n",
"\n",
"[01-building-ctmcs.py](https://github.com/moves-rwth/stormpy/blob/master/examples/building_ctmcs/01-building-ctmcs.py)\n",
"\n",
"First, we import Stormpy:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
">>> import stormpy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Transition Matrix\n",
"\n",
"In this example, we build the transition matrix using a numpy array"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
">>> import numpy as np\n",
">>> transitions = np.array([\n",
"... [0, 1.5, 0, 0],\n",
"... [3, 0, 1.5, 0],\n",
"... [0, 3, 0, 1.5],\n",
"... [0, 0, 3, 0], ], dtype='float64')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following function call returns a sparse matrix with default row groups:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
">>> transition_matrix = stormpy.build_sparse_matrix(transitions)\n",
">>> print(transition_matrix) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Labeling\n",
"\n",
"The state labeling is created analogously to the previous example in [building DTMCs](building_dtmcs.ipynb#labeling):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
">>> state_labeling = stormpy.storage.StateLabeling(4)\n",
">>> state_labels = {'empty', 'init', 'deadlock', 'full'}\n",
">>> for label in state_labels:\n",
"... state_labeling.add_label(label)\n",
">>> state_labeling.add_label_to_state('init', 0)\n",
">>> state_labeling.add_label_to_state('empty', 0)\n",
">>> state_labeling.add_label_to_state('full', 3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exit Rates\n",
"\n",
"Lastly, we initialize a list to equip every state with an exit rate:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
">>> exit_rates = [1.5, 4.5, 4.5, 3.0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Building the Model\n",
"\n",
"Now, we can collect all components, including the choice labeling and the exit rates.\n",
"To let the transition values be interpreted as rates we set rate_transitions to True:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
">>> components = stormpy.SparseModelComponents(transition_matrix=transition_matrix, state_labeling=state_labeling, rate_transitions=True)\n",
">>> components.exit_rates = exit_rates"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And finally, we can build the model:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"hide-output": false
},
"outputs": [],
"source": [
">>> ctmc = stormpy.storage.SparseCtmc(components)\n",
">>> print(ctmc) "
]
}
],
"metadata": {
"date": 1598178167.1853151,
"filename": "building_ctmcs.rst",
"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.8.2"
},
"title": "Continuous-time Markov chains (CTMCs)"
},
"nbformat": 4,
"nbformat_minor": 4
}