def test_matrix_structure_after_horizontal_transitions_excluding(self): b = Transition(D('0.4'), D('10')) a = Transition(D('0.6'), D('20')) f = Transition(D('0.4'), D('10')) g = Transition(D('0.2'), D('5')) d = Transition(D('0.4'), D('15')) e = Transition(D('0.8'), D('5')) z = Transition(D('0.2'), D('10')) trans_matrix = TransitionMatrix([ [_, _, b, a], [_, _, _, _], [_, f, g, d], [_, e, z, _], ]) cell_3_2 = transform_trans_while_excluding_loop(transition=f, loop=g) cell_3_4 = transform_trans_while_excluding_loop(transition=d, loop=g) trans_matrix_after_excluding = TransitionMatrix([ [_, _, b, a], [_, _, _, _], [_, cell_3_2, _, cell_3_4], [_, e, z, _], ]) trans_matrix.exclude_first_loop() self.assertEqual(repr(trans_matrix), repr(trans_matrix_after_excluding))
def test_values_after_horizontal_transitions_excluding(self): b = Transition(D('0.4'), D('10')) a = Transition(D('0.6'), D('20')) f = Transition(D('0.4'), D('10')) g = Transition(D('0.2'), D('5')) d = Transition(D('0.4'), D('15')) e = Transition(D('0.8'), D('5')) z = Transition(D('0.2'), D('10')) trans_matrix = TransitionMatrix([ [_, _, b, a], [_, _, _, _], [_, f, g, d], [_, e, z, _], ]) trans_matrix.exclude_first_loop() # Probability is f.P / (1 - g.P) = 0.4 / (1 - 0.2) = 0.5 # Resource is f.R + (g.R * g.P) / (1 - g.P) # 10 + (5 * 0.2) / 0.8 = 11.25 cell_3_2 = Transition(D('0.5'), D('11.25')) self.assertEqual(trans_matrix._matrix[2][1], cell_3_2) # Probability is d / (1 - g) = 0.4 / (1 - 0.2) = 0.5 # Resource is d.R + (g.R * g.P) / (1 - g.P) # 15 + (5 * 0.2) / 0.8 = 16.25 cell_3_4 = Transition(D('0.5'), D('16.25')) self.assertEqual(trans_matrix._matrix[2][3], cell_3_4)
def test_matrix_structure_after_vertical_transitions_excluding(self): b = Transition(D('0.4'), D('10')) a = Transition(D('0.6'), D('20')) g = Transition(D('1'), D('5')) e = Transition(D('0.8'), D('5')) z = Transition(D('0.2'), D('10')) trans_matrix = TransitionMatrix([ [_, _, b, a], [_, _, _, _], [_, _, g, _], [_, e, z, _], ]) trans_matrix_after_excluding = TransitionMatrix([ [_, _, b, a], [_, _, _, _], [_, _, _, _], [_, e, z, _], ]) trans_matrix.exclude_first_loop() self.assertEqual(repr(trans_matrix), repr(trans_matrix_after_excluding))
def test_values_after_horizontal_transitions_excluding(self): b = Transition(D('0.4'), D('10')) a = Transition(D('0.6'), D('20')) f = Transition(D('0.4'), D('10')) g = Transition(D('0.2'), D('5')) d = Transition(D('0.4'), D('15')) e = Transition(D('0.8'), D('5')) z = Transition(D('0.2'), D('10')) trans_matrix = TransitionMatrix([ [_, _, b, a], [_, _, _, _], [_, f, g, d], [_, e, z, _], ]) trans_matrix.exclude_first_loop() # Probability is f.P / (1 - g.P) = 0.4 / (1 - 0.2) = 0.5 # Resource is f.R + (g.R * g.P) / (1 - g.P) # 10 + (5 * 0.2) / 0.8 = 11.25 cell_3_2 = Transition(D('0.5'), D('11.25')) self.assertEqual(trans_matrix._matrix[2][1], cell_3_2) # Probability is d / (1 - g) = 0.4 / (1 - 0.2) = 0.5 # Resource is d.R + (g.R * g.P) / (1 - g.P) # 15 + (5 * 0.2) / 0.8 = 16.25 cell_3_4 = Transition(D('0.5'), D('16.25')) self.assertEqual(trans_matrix._matrix[2][3], cell_3_4)
def test_matrix_structure_after_horizontal_transitions_excluding(self): b = Transition(D('0.4'), D('10')) a = Transition(D('0.6'), D('20')) f = Transition(D('0.4'), D('10')) g = Transition(D('0.2'), D('5')) d = Transition(D('0.4'), D('15')) e = Transition(D('0.8'), D('5')) z = Transition(D('0.2'), D('10')) trans_matrix = TransitionMatrix([ [_, _, b, a], [_, _, _, _], [_, f, g, d], [_, e, z, _], ]) cell_3_2 = transform_trans_while_excluding_loop(transition=f, loop=g) cell_3_4 = transform_trans_while_excluding_loop(transition=d, loop=g) trans_matrix_after_excluding = TransitionMatrix([ [_, _, b, a], [_, _, _, _], [_, cell_3_2, _, cell_3_4], [_, e, z, _], ]) trans_matrix.exclude_first_loop() self.assertEqual(repr(trans_matrix), repr(trans_matrix_after_excluding))
def test_matrix_structure_after_vertical_transitions_excluding(self): b = Transition(D('0.4'), D('10')) a = Transition(D('0.6'), D('20')) g = Transition(D('1'), D('5')) e = Transition(D('0.8'), D('5')) z = Transition(D('0.2'), D('10')) trans_matrix = TransitionMatrix([ [_, _, b, a], [_, _, _, _], [_, _, g, _], [_, e, z, _], ]) trans_matrix_after_excluding = TransitionMatrix([ [_, _, b, a], [_, _, _, _], [_, _, _, _], [_, e, z, _], ]) trans_matrix.exclude_first_loop() self.assertEqual(repr(trans_matrix), repr(trans_matrix_after_excluding))
def test_matrix_2x2_source_has_loop(self): a = (D('0.6'), 10) b = (D('0.4'), 7) matrix = [[a, b], [_, _]] trans_matrix = TransitionMatrix(matrix) trans_matrix.exclude_first_loop() # Probability is 1.0 = 0.4 / (1 - 0.6) # Resource is 22.0 = 7 + ((10 * 0.6) / (1 - 0.6)) self.assertEqual(repr(trans_matrix), '[[None, (P=1, R=22)], [None, None]]')
def test_matrix_2x2_source_has_loop(self): a = (D('0.6'), 10) b = (D('0.4'), 7) matrix = [ [a, b], [_, _] ] trans_matrix = TransitionMatrix(matrix) trans_matrix.exclude_first_loop() # Probability is 1.0 = 0.4 / (1 - 0.6) # Resource is 22.0 = 7 + ((10 * 0.6) / (1 - 0.6)) self.assertEqual(repr(trans_matrix), '[[None, (P=1, R=22)], [None, None]]')
def test_sum_of_probabilities_is_equal_to_one_or_zero(self): b = Transition(D('0.4'), D('10')) a = Transition(D('0.6'), D('20')) f = Transition(D('0.4'), D('10')) g = Transition(D('0.2'), D('5')) d = Transition(D('0.4'), D('15')) e = Transition(D('0.8'), D('5')) z = Transition(D('0.2'), D('10')) trans_matrix = TransitionMatrix([ [_, _, b, a], [_, _, _, _], [_, f, g, d], [_, e, z, _], ]) trans_matrix.exclude_first_loop() all_rows_are_ok_after_excluding_first_loop = all( check_sum_of_probabilities(row) for row in trans_matrix._matrix) self.assertTrue(all_rows_are_ok_after_excluding_first_loop)
def test_sum_of_probabilities_is_equal_to_one_or_zero(self): b = Transition(D('0.4'), D('10')) a = Transition(D('0.6'), D('20')) f = Transition(D('0.4'), D('10')) g = Transition(D('0.2'), D('5')) d = Transition(D('0.4'), D('15')) e = Transition(D('0.8'), D('5')) z = Transition(D('0.2'), D('10')) trans_matrix = TransitionMatrix([ [_, _, b, a], [_, _, _, _], [_, f, g, d], [_, e, z, _], ]) trans_matrix.exclude_first_loop() all_rows_are_ok_after_excluding_first_loop = all( check_sum_of_probabilities(row) for row in trans_matrix._matrix ) self.assertTrue(all_rows_are_ok_after_excluding_first_loop)