def test_make_chance(self) -> None: cid = get_3node_cid() self.assertCountEqual(cid.decisions, ["D"]) cid.make_decision("S") self.assertCountEqual(cid.decisions, ["D", "S"]) cid.make_chance("S") self.assertCountEqual(cid.decisions, ["D"])
def test_make_utility(self) -> None: cid = get_3node_cid() self.assertCountEqual(cid.utilities, ["U"]) cid.make_utility("S") self.assertCountEqual(cid.utilities, ["U", "S"]) self.assertEqual(cid.utility_agent["S"], 0) self.assertCountEqual(cid.agent_utilities[0], ["U", "S"])
def test_make_decision(self) -> None: cid = get_3node_cid() self.assertCountEqual(cid.decisions, ["D"]) cid.make_decision("S") self.assertCountEqual(cid.decisions, ["D", "S"]) self.assertEqual(cid.decision_agent["S"], 0) self.assertCountEqual(cid.agent_decisions[0], ["D", "S"])
def test_possible_pure_decision_rules(self) -> None: cid = get_minimal_cid() possible_pure_decision_rules = list(cid.pure_decision_rules("A")) self.assertEqual(len(possible_pure_decision_rules), 2) expected_utilities = [] for decision_rule in possible_pure_decision_rules: cid.add_cpds(decision_rule) cid.check_model() expected_utilities.append(cid.expected_utility({})) self.assertEqual(set(expected_utilities), {0, 1}) cid = get_3node_cid() possible_pure_decision_rules = list(cid.pure_decision_rules("D")) self.assertEqual(len(possible_pure_decision_rules), 4) expected_utilities = [] matrices = set() for decision_rule in possible_pure_decision_rules: cid.add_cpds(decision_rule) matrices.add(tuple(cid.get_cpds("D").values.flatten())) cid.check_model() expected_utilities.append(cid.expected_utility({})) self.assertEqual(set(expected_utilities), {-1, 0, 1}) self.assertEqual(len(matrices), 4) five_node = get_5node_cid() possible_pure_decision_rules = list(five_node.pure_decision_rules("D")) self.assertEqual(len(possible_pure_decision_rules), 16) expected_utilities = [] for decision_rule in possible_pure_decision_rules: five_node.add_cpds(decision_rule) five_node.check_model() expected_utilities.append(five_node.expected_utility({})) self.assertEqual(set(expected_utilities), {0.5, 1.0, 1.5})
def test_optimal_decision_rules(self) -> None: cid = get_minimal_cid() optimal_decision_rules = cid.optimal_pure_decision_rules("A") self.assertEqual(len(optimal_decision_rules), 1) for cpd in optimal_decision_rules: cid.add_cpds(cpd) self.assertEqual(cid.expected_utility({}), 1) cid = get_3node_cid() optimal_decision_rules = cid.optimal_pure_decision_rules("D") self.assertEqual(len(optimal_decision_rules), 1) for cpd in optimal_decision_rules: cid.add_cpds(cpd) self.assertEqual(cid.expected_utility({}), 1) five_node = get_5node_cid() optimal_decision_rules = five_node.optimal_pure_decision_rules("D") self.assertEqual(len(optimal_decision_rules), 4) five_node.impute_optimal_policy() for cpd in optimal_decision_rules: five_node.add_cpds(cpd) self.assertEqual(five_node.expected_utility({}), 1.5)
def test_expected_utility(self) -> None: three_node = get_3node_cid() five_node = get_5node_cid() eu00 = three_node.expected_utility({"D": -1, "S": -1}) self.assertEqual(eu00, 1) eu10 = three_node.expected_utility({"D": 1, "S": -1}) self.assertEqual(eu10, -1) eu000 = five_node.expected_utility({"D": 0, "S1": 0, "S2": 0}) self.assertEqual(eu000, 2) eu001 = five_node.expected_utility({"D": 0, "S1": 0, "S2": 1}) self.assertEqual(eu001, 1) macid_example = prisoners_dilemma() eu_agent0 = macid_example.expected_utility({ "D1": "d", "D2": "c" }, agent=1) self.assertEqual(eu_agent0, 0) eu_agent1 = macid_example.expected_utility({ "D1": "d", "D2": "c" }, agent=2) self.assertEqual(eu_agent1, -3)
def test_query(self) -> None: three_node = get_3node_cid() three_node.query(["U"], {"D": -1}, intervention={"S": 1}) self.assertTrue( three_node.query(["U"], { "D": -1 }, intervention={ "S": 1 }).values[0] == float(1.0)) # The following queries should not be allowed before a policy is specified with self.assertRaises(ValueError): three_node.query(["U"], {}) with self.assertRaises(ValueError): three_node.query(["U"], {"D": 1}) with self.assertRaises(ValueError): three_node.query(["U"], {"S": 1}) # but should be allowed after three_node.impute_random_policy() three_node.query(["U"], {}) three_node.query(["U"], {"D": 1}) three_node.query(["U"], {"S": 1}) # contexts still need be within the domain of the variable with self.assertRaises(ValueError): three_node.query(["U"], {"S": 0})
def cid_3node() -> CID: return get_3node_cid()
def test_copy_without_cpds(self) -> None: cid = get_3node_cid() cid_no_cpds = cid.copy_without_cpds() self.assertTrue(len(cid_no_cpds.cpds) == 0)