Esempio n. 1
0
    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})
Esempio n. 2
0
 def test_initialize_uniform_random_cpd(self) -> None:
     cid = get_minimal_cid()
     cpd_a = ConstantCPD("A", {}, cid, [0, 2])
     self.assertTrue((cpd_a.get_values() == np.array([[0.5], [0.5]])).all())
     self.assertEqual(cpd_a.get_state_names("A", 1), 2)
     cpd_b = ConstantCPD("B", {}, cid, [0, 1])
     self.assertTrue((cpd_b.get_values() == np.array([[0.5, 0.5],
                                                      [0.5, 0.5]])).all())
Esempio n. 3
0
 def test_initialize_uniform_random_cpd(self) -> None:
     cid = get_minimal_cid()
     cpd_a = UniformRandomCPD("A", [0, 2])
     cpd_a.initialize_tabular_cpd(cid)
     self.assertTrue((cpd_a.get_values() == np.array([[0.5], [0.5]])).all())
     self.assertEqual(cpd_a.get_state_names("A", 1), 2)
     cpd_b = UniformRandomCPD("B", [0, 1])
     cpd_b.initialize_tabular_cpd(cid)
     self.assertTrue((cpd_b.get_values() == np.array([[0.5, 0.5],
                                                      [0.5, 0.5]])).all())
Esempio n. 4
0
 def test_initialize_function_cpd(self) -> None:
     cid = get_minimal_cid()
     cpd_a = StochasticFunctionCPD("A", lambda: 2, cid)
     self.assertEqual(cpd_a.get_values(), np.array([[1]]))
     self.assertEqual(cpd_a.get_cardinality(["A"])["A"], 1)
     self.assertEqual(cpd_a.get_state_names("A", 0), 2)
     cid.add_cpds(cpd_a)
     cpd_b = StochasticFunctionCPD("B", lambda a: a, cid)
     self.assertEqual(cpd_b.get_values(), np.array([[1]]))
     self.assertEqual(cpd_b.get_cardinality(["B"])["B"], 1)
     self.assertEqual(cpd_b.get_state_names("B", 0), 2)
Esempio n. 5
0
 def test_initialize_function_cpd(self) -> None:
     cid = get_minimal_cid()
     cpd_a = FunctionCPD("A", lambda: 2)
     cpd_a.initialize_tabular_cpd(cid)
     self.assertTrue(cpd_a.get_values(), np.array([[1]]))
     self.assertEqual(cpd_a.get_cardinality(["A"])["A"], 1)
     self.assertEqual(cpd_a.get_state_names("A", 0), 2)
     cpd_b = FunctionCPD("B", lambda a: a)  # type: ignore
     cpd_b.initialize_tabular_cpd(cid)
     self.assertTrue(cpd_a.get_values(), np.array([[1]]))
     self.assertEqual(cpd_a.get_cardinality(["A"])["A"], 1)
     self.assertEqual(cpd_a.get_state_names("A", 0), 2)
Esempio n. 6
0
    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)
Esempio n. 7
0
def cid_minimal() -> CID:
    return get_minimal_cid()