def test_probability_distribution_two_entangled_variables_renamed(self): interpreter = PythonDiceInterpreter() program = [ "VAR a = 1d2", "VAR b = a + a", "VAR c = a + b", "VAR d = c >= ( 1d2 * b )", "VAR e = d == a", ] probability_distribution_a = interpreter.get_probability_distributions(program)["a"] probability_distribution_b = interpreter.get_probability_distributions(program)["b"] probability_distribution_c = interpreter.get_probability_distributions(program)["c"] probability_distribution_d = interpreter.get_probability_distributions(program)["d"] probability_distribution_e = interpreter.get_probability_distributions(program)["e"] self.assertEqual( { 1: 1, 2: 1, }, probability_distribution_a.get_result_map(), ) self.assertEqual( { 2: 1, 4: 1, }, probability_distribution_b.get_result_map(), ) self.assertEqual( { 3: 1, 6: 1, }, probability_distribution_c.get_result_map(), ) self.assertEqual( { 0: 2, 1: 2, }, probability_distribution_d.get_result_map(), ) self.assertEqual( { 0: 3, 1: 1, }, probability_distribution_e.get_result_map(), )
def test_get_probability_distribution(self): interpreter = PythonDiceInterpreter() program = [ "VAR save_roll = d20 + 8", "VAR burning_arch_damage = 2d6", "VAR pass_save = ( save_roll >= 19 ) ", "VAR damage_half_on_save = burning_arch_damage // (pass_save + 1)", ] self.assertEqual( { 1: 0.041666666666666664, 2: 0.1111111111111111, 3: 0.18055555555555555, 4: 0.16666666666666666, 5: 0.125, 6: 0.08333333333333333, 7: 0.08333333333333333, 8: 0.06944444444444445, 9: 0.05555555555555555, 10: 0.041666666666666664, 11: 0.027777777777777776, 12: 0.013888888888888888, }, interpreter.get_probability_distributions(program)["damage_half_on_save"].get_dict_form(), )
def test_uses_starting_state(self): test_interpreter_one = PythonDiceInterpreter() probability_distribution = test_interpreter_one.get_probability_distributions(["d12"])["stdout"] mock_state = create_autospec(IProbabilityDistributionState) mock_state.get_var.return_value = probability_distribution mock_state.get_constant_dict.return_value = {"stored_value": 12} interpreter = PythonDiceInterpreter(starting_state=mock_state) program = ["d12 + stored_value"] self.assertEqual(24, interpreter.max(program)["stdout"])
def test_probability_distribution_binary_operator(self): interpreter = PythonDiceInterpreter() operator_map = { "==": {0: 2, 1: 1}, "!=": {0: 1, 1: 2}, "<=": {1: 3}, "<": {0: 1, 1: 2}, ">=": {0: 2, 1: 1}, ">": {0: 3}, "AND": {0: 2, 1: 1}, "OR": {1: 3}, } for operator, distribution in operator_map.items(): program = [f"d[-1-1] {operator} 1"] probability_distribution = interpreter.get_probability_distributions(program)["stdout"] self.assertEqual(distribution, probability_distribution.get_result_map())
def test_probability_distribution_two_entangled_variables_example(self): interpreter = PythonDiceInterpreter() program = [ "VAR roll_one = 1d3", "VAR roll_two = ((1d4) * (roll_one == 3)) + ((roll_one) * (roll_one != 3))", "roll_two", ] probability_distribution = interpreter.get_probability_distributions(program)["stdout"] self.assertEqual( { 1: 5, 2: 5, 3: 1, 4: 1, }, probability_distribution.get_result_map(), )
def test_probability_distribution_two_entangled_variables_simple(self): interpreter = PythonDiceInterpreter() program = [ "VAR a = 2d3", "VAR b = a + a", ] probability_distribution_b = interpreter.get_probability_distributions(program)["b"] print(probability_distribution_b) self.assertEqual( { 4: 1, 6: 4, 8: 9, 10: 4, 12: 1, }, probability_distribution_b.get_result_map(), )
def test_probability_distribution_two_entangled_variables_chain(self): interpreter = PythonDiceInterpreter() program = [ "VAR a = 2d3", "VAR b = a + a", "VAR a = b + a", "VAR b = b + a", ] probability_distribution_b = interpreter.get_probability_distributions(program)["b"] print(probability_distribution_b) self.assertEqual( { 10: 1, 15: 32, 20: 243, 25: 32, 30: 1, }, probability_distribution_b.get_result_map(), )
def test_probability_distribution_subtract_example(self): interpreter = PythonDiceInterpreter() program = ["34 - 2"] probability_distribution = interpreter.get_probability_distributions(program)["stdout"] self.assertEqual({32: 1}, probability_distribution.get_result_map())
def test_probability_distribution_not_example(self): interpreter = PythonDiceInterpreter() program = ["! (d[0,1*3])"] probability_distribution = interpreter.get_probability_distributions(program)["stdout"] self.assertEqual({0: 3, 1: 1}, probability_distribution.get_result_map())
def test_probability_distribution_multiply_example(self): interpreter = PythonDiceInterpreter() program = ["d[2,4] * 2"] probability_distribution = interpreter.get_probability_distributions(program)["stdout"] self.assertEqual({4: 1, 8: 1}, probability_distribution.get_result_map())
def test_probability_distribution_var_example(self): interpreter = PythonDiceInterpreter() program = ["VAR a = d2", "a + 1"] probability_distribution = interpreter.get_probability_distributions(program)["stdout"] self.assertEqual({2: 1, 3: 1}, probability_distribution.get_result_map())
def test_probability_distribution_constant_binary(self): interpreter = PythonDiceInterpreter() program = ["2d[-1, 1]k1 == True"] probability_distribution = interpreter.get_probability_distributions(program)["stdout"] self.assertEqual({1: 3, 0: 1}, probability_distribution.get_result_map())
def test_probability_distribution_add(self): interpreter = PythonDiceInterpreter() program = ["d[-1, 1] + 3"] probability_distribution = interpreter.get_probability_distributions(program)["stdout"] self.assertEqual({2: 0.5, 4: 0.5}, probability_distribution.get_dict_form())
def test_probability_distribution_abs(self): interpreter = PythonDiceInterpreter() program = ["ABS( 1d[-1, 1])"] probability_distribution = interpreter.get_probability_distributions(program)["stdout"] self.assertEqual({1: 1}, probability_distribution.get_dict_form())
def test_probability_distribution_min(self): interpreter = PythonDiceInterpreter() program = ["MIN( d[-1, 1] , 0)"] probability_distribution = interpreter.get_probability_distributions(program)["stdout"] self.assertEqual({0: 0.5, -1: 0.5}, probability_distribution.get_dict_form())
def do_not_run_test_estimated_cost_in_order(): programs: List[List[str]] = [ ["25d25"], ["25d30"], ["30d30"], ["30d25"], ["25d20"], ["20d25"], ["25d25 + 30"], ["25d30 + 30"], ["30d25 + 30"], ["30d30 + 30"], ["25d25 - 30"], ["25d30 - 30"], ["30d25 - 30"], ["30d30 - 30"], ["25d25 * 30"], ["25d30 * 30"], ["30d25 * 30"], ["30d30 * 30"], ["25d25 // 30"], ["25d30 // 30"], ["30d25 // 30"], ["30d30 // 30"], ["30d30 + 10d10"], ["30d35 + 10d10"], ["35d35 + 10d10"], ["35d30 + 10d10"], ["30d30 - 10d10"], ["30d35 - 10d10"], ["35d35 - 10d10"], ["35d30 - 10d10"], ["30d30 * 10d10"], ["30d35 * 10d10"], ["35d35 * 10d10"], ["35d30 * 10d10"], ["30d30 // 10d10"], ["30d35 // 10d10"], ["35d35 // 10d10"], ["35d30 // 10d10"], ["(30d30)"], ["(25d35)"], ["(30d30 + 30)"], ["(35d40 - 30)"], ["(35d35 * 30)"], ["(35d30 // 30)"], ["(35d35 - 10d10)"], ["(35d35 * 10d10)"], ["10d[0--10*2,1-10]"], ["ABS( 10d[0--10*2,1-10] )"], ["MAX( 10d[0--10*2,1-10] , 2d%)"], ["MIN( 10d[0--10*2,1-10] , 2d%)"], ["10d[2,4,6,8,10] == 20d10"], ["10d[2,4,6,8,10] <= 20d10"], ["10d[2,4,6,8,10] < 20d10"], ["10d[2,4,6,8,10] > 20d10"], ["10d[2,4,6,8,10] >= 20d10"], ["10d[2,4,6,8,10] != 20d10"], ["10d[-10,-8,-4,-2,0,2,4,6,8,10] AND 20d[-5-5]"], ["10d[-10,-8,-4,-2,0,2,4,6,8,10] OR 20d[-5-5]"], ["!(10d[2,4,6,8,10] == 20d10)"], ["!(10d[2,4,6,8,10] <= 20d10)"], ["!(10d[2,4,6,8,10] < 20d10)"], ["!(10d[2,4,6,8,10] > 20d10)"], ["!(10d[2,4,6,8,10] >= 20d10)"], ["!(10d[2,4,6,8,10] != 20d10)"], ["!(10d[-10,-8,-4,-2,0,2,4,6,8,10] AND 20d[-5-5])"], ["!(10d[-10,-8,-4,-2,0,2,4,6,8,10] OR 20d[-5-5])"], [ "VAR save_roll = d20", "VAR burning_arch_damage = 10d6 + 10", "VAR pass_save = ( save_roll >= 10 ) ", "VAR damage_half_on_save = burning_arch_damage // (pass_save + 1)", "damage_half_on_save", ], ["15d5k1"], ["15d5k5"], ["15d5k10"], ["15d5k15"], ["15d5d1"], ["15d5d5"], ["15d5d10"], ["15d5d15"], ["10d5k1"], ["10d5k5"], ["10d5k10"], ["10d5d1"], ["10d5d5"], ["10d5d10"], ["20d5k1"], ["20d5k5"], ["20d5k10"], ["20d5k15"], ["20d5k20"], ["20d5d1"], ["20d5d5"], ["20d5d10"], ["20d5d15"], ["20d5d20"], ["15d6k1"], ["15d6k5"], ["15d6k10"], ["15d6k15"], ["15d6k20"], ["15d6d1"], ["15d6d5"], ["15d6d10"], ["15d6d15"], ["15d4k1"], ["15d4k5"], ["15d4k10"], ["15d4k15"], ["15d4d1"], ["15d4d5"], ["15d4d10"], ["15d4d15"], ] costs_and_times = {} for index, program in enumerate(programs): interpreter = PythonDiceInterpreter() cost = interpreter.get_estimated_cost(program) interpreter = PythonDiceInterpreter() start = timeit.default_timer() interpreter.get_probability_distributions(program) stop = timeit.default_timer() time = stop - start costs_and_times[index] = {"cost": cost, "time": time} for index, program in enumerate(programs): print( str(program), "|", costs_and_times[index]["cost"], "|", costs_and_times[index]["time"], flush=True, )