<commit_before>.. _deadlock-detection:

=============================
Deadlock Detection Capability
=============================

Ciw's has built in deadlock detection capability. With Ciw, a queueing network can be simulated until it reaches deadlock. Ciw then records the time until deadlock from each state.

In order to take advantage of this feature, set deadlock detection option to True in the parameters file::

    Detect_deadlock: True

Then use the :code:`simulate_until_deadlock` method. The attribute :code:`times_to_deadlock` contains the times to deadlock from each state::

   >>> import ciw
   >>> Q = ciw.Simulation(deadlock_params) # doctest:+SKIP
   >>> Q.simulate_until_deadlock() # doctest:+SKIP
   >>> times = Q.times_to_deadlock # doctest:+SKIP

where :code:`times` is a dictionary with states as keys and times to deadlock as values. Note that :code:`Simulation_time` is ingnored in this case.



------------------
Example - Deadlock
------------------

Consider the M/M/1/3 queue where customers have probability 0.5 of rejoining the queue after service. If the queue is full then that customer gets blocked, and hence the system deadlocks.

Parameters::

    >>> params = {'Arrival_distributions': {'Class 0': [['Exponential', 6.0]]},
    ...           'Number_of_nodes': 1,
    ...           'Detect_deadlock': True,
    ...           'Number_of_servers': [1],
    ...           'Queue_capacities': [3],
    ...           'Number_of_classes': 1,
    ...           'Service_distributions': {'Class 0': [['Exponential', 5.0]]},
    ...           'Transition_matrices': {'Class 0': [[0.5]]}}

Running until deadlock::

    >>> import ciw
    >>> from random import seed
    >>> seed(99)
    >>> Q = ciw.Simulation(params)
    >>> Q.simulate_until_deadlock()
    >>> self.times_to_deadlock # doctest:+SKIP
    {((1, 0),): 1.0845416939916719, ((3, 0),): 0.5436399978272065, ((0, 0),): 1.1707879982560288, ((4, 0),): 0.15650986183172932, ((3, 1),): 0.0, ((2, 0),): 1.0517097907100657}

Here the state :code:`((i, j),)` denotes the state where there are `i` customers at the node, `j` of which are blocked<commit_msg>fix docs to reflect recent deadlock change<commit_after>.. _deadlock-detection:

=============================
Deadlock Detection Capability
=============================

Ciw's has built in deadlock detection capability. With Ciw, a queueing network can be simulated until it reaches deadlock. Ciw then records the time until deadlock from each state.

In order to take advantage of this feature, set deadlock detection option to True in the parameters file::

    Detect_deadlock: StateDigraph

Then use the :code:`simulate_until_deadlock` method. The attribute :code:`times_to_deadlock` contains the times to deadlock from each state::

   >>> import ciw
   >>> Q = ciw.Simulation(deadlock_params) # doctest:+SKIP
   >>> Q.simulate_until_deadlock() # doctest:+SKIP
   >>> times = Q.times_to_deadlock # doctest:+SKIP

where :code:`times` is a dictionary with states as keys and times to deadlock as values. Note that :code:`Simulation_time` is ingnored in this case.



------------------
Example - Deadlock
------------------

Consider the M/M/1/3 queue where customers have probability 0.5 of rejoining the queue after service. If the queue is full then that customer gets blocked, and hence the system deadlocks.

Parameters::

    >>> params = {'Arrival_distributions': {'Class 0': [['Exponential', 6.0]]},
    ...           'Number_of_nodes': 1,
    ...           'Detect_deadlock': 'StateDigraph',
    ...           'Number_of_servers': [1],
    ...           'Queue_capacities': [3],
    ...           'Number_of_classes': 1,
    ...           'Service_distributions': {'Class 0': [['Exponential', 5.0]]},
    ...           'Transition_matrices': {'Class 0': [[0.5]]}}

Running until deadlock::

    >>> import ciw
    >>> from random import seed
    >>> seed(99)
    >>> Q = ciw.Simulation(params)
    >>> Q.simulate_until_deadlock()
    >>> self.times_to_deadlock # doctest:+SKIP
    {((1, 0),): 1.0845416939916719, ((3, 0),): 0.5436399978272065, ((0, 0),): 1.1707879982560288, ((4, 0),): 0.15650986183172932, ((3, 1),): 0.0, ((2, 0),): 1.0517097907100657}

Here the state :code:`((i, j),)` denotes the state where there are `i` customers at the node, `j` of which are blocked
{'fraction_non_alphanumeric': 0.17240582389646406, 'fraction_numerical': 0.05500346660503813, 'mean_word_length': 3.341023069207623, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2981050', 'n_tokens_mistral': 1484, 'n_tokens_neox': 1306, 'n_words': 435}