def test_convert_symbolic_transition_matrix(threshold):
    """
    Test that ensures that for fixed parameters and different values of the threshold
    the function that converts the symbolic matrix into a numeric one gives the
    same results as the get_transition_matrix function.
    """
    lambda_2 = 0.3
    lambda_1 = 0.2
    mu = 0.05
    num_of_servers = 10
    system_capacity = 8
    buffer_capacity = 2

    transition_matrix = get_transition_matrix(
        lambda_2=lambda_2,
        lambda_1=lambda_1,
        mu=mu,
        num_of_servers=num_of_servers,
        threshold=threshold,
        system_capacity=system_capacity,
        buffer_capacity=buffer_capacity,
    )

    sym_transition_matrix = get_symbolic_transition_matrix(
        num_of_servers=num_of_servers,
        threshold=threshold,
        system_capacity=system_capacity,
        buffer_capacity=buffer_capacity,
    )
    converted_matrix = convert_symbolic_transition_matrix(
        Q_sym=sym_transition_matrix, lambda_2=lambda_2, lambda_1=lambda_1, mu=mu
    )

    assert np.allclose(converted_matrix, transition_matrix)
def test_get_state_probabilities_array():
    """
    Test to ensure that the sum of elements of the pi array equate to 1
    """
    all_states = build_states(
        threshold=3,
        system_capacity=5,
        buffer_capacity=4,
    )
    transition_matrix = get_transition_matrix(
        lambda_2=0.1,
        lambda_1=0.2,
        mu=0.2,
        num_of_servers=3,
        threshold=3,
        system_capacity=5,
        buffer_capacity=4,
    )
    pi = get_steady_state_algebraically(
        Q=transition_matrix, algebraic_function=np.linalg.solve
    )
    pi_array = get_markov_state_probabilities(
        pi=pi, all_states=all_states, output=np.ndarray
    )

    assert round(np.nansum(pi_array), NUMBER_OF_DIGITS_TO_ROUND) == 1
def test_get_transition_matrix(
    system_capacity, buffer_capacity, lambda_2, lambda_1, mu
):
    """
    Test that ensures numeric transition matrix's shape is as expected and that
    some elements of the diagonal are what they should be. To be exact the first,
    last and middle row are check to see if the diagonal element of them equals
    to minus the sum of the entire row.
    """
    num_of_servers = 10
    threshold = 8

    states_after_threshold = system_capacity - threshold + 1
    s_2_size = states_after_threshold if states_after_threshold >= 0 else 0
    matrix_size = s_2_size * (buffer_capacity + 1) + threshold

    transition_matrix = get_transition_matrix(
        lambda_2=lambda_2,
        lambda_1=lambda_1,
        mu=mu,
        num_of_servers=num_of_servers,
        threshold=threshold,
        system_capacity=system_capacity,
        buffer_capacity=buffer_capacity,
    )

    assert matrix_size == np.shape(transition_matrix)[0]
    mid = int(matrix_size / 2)
    assert transition_matrix[0][0] == -sum(transition_matrix[0][1:])
    assert transition_matrix[-1][-1] == -sum(transition_matrix[-1][:-1])

    mid_row_sum = sum(transition_matrix[mid][:mid]) + sum(
        transition_matrix[mid][mid + 1 :]
    )
    assert np.isclose(transition_matrix[mid][mid], -mid_row_sum)
def test_get_mean_number_of_individuals_examples():
    """
    Some examples to ensure that the correct mean number of individuals are output
    """
    all_states = build_states(threshold=4, system_capacity=20, buffer_capacity=20)
    transition_matrix = get_transition_matrix(
        lambda_2=0.2,
        lambda_1=0.2,
        mu=0.2,
        num_of_servers=3,
        threshold=4,
        system_capacity=20,
        buffer_capacity=20,
    )
    pi = get_steady_state_algebraically(
        Q=transition_matrix, algebraic_function=np.linalg.solve
    )
    assert (
        round(
            get_mean_number_of_individuals_in_system(pi=pi, states=all_states),
            NUMBER_OF_DIGITS_TO_ROUND,
        )
        == 2.88827497
    )
    assert (
        round(
            get_mean_number_of_individuals_in_service_area(pi=pi, states=all_states),
            NUMBER_OF_DIGITS_TO_ROUND,
        )
        == 2.44439504
    )
    assert (
        round(
            get_mean_number_of_individuals_in_buffer_center(pi=pi, states=all_states),
            NUMBER_OF_DIGITS_TO_ROUND,
        )
        == 0.44387993
    )