def get_5node_cid_with_scaled_utility() -> CID: cid = CID([('S1', 'D'), ('S1', 'U1'), ('S2', 'D'), ('S2', 'U2'), ('D', 'U1'), ('D', 'U2')], decision_nodes=['D'], utility_nodes=['U1', 'U2']) cpd_s1 = UniformRandomCPD('S1', [0, 1]) cpd_s2 = UniformRandomCPD('S2', [0, 1]) cpd_u1 = FunctionCPD('U1', lambda s1, d: 10 * int(s1 == d), evidence=['S1', 'D']) cpd_u2 = FunctionCPD('U2', lambda s2, d: 2 * int(s2 == d), evidence=['S2', 'D']) cpd_d = DecisionDomain('D', [0, 1]) cid.add_cpds(cpd_d, cpd_s1, cpd_s2, cpd_u1, cpd_u2) return cid
def get_sequential_cid() -> CID: """ This CID is a subtle case of sufficient recall, as the strategy for D1 influences the expected utility of D2, but D2 can still be chosen without knowing D1, since D1 does not influence any utility nodes descending from D2. """ cid = CID([ ('S1', 'D1'), ('D1', 'U1'), ('S1', 'U1'), ('D1', 'S2'), ('S2', 'D2'), ('D2', 'U2'), ('S2', 'U2'), ], decision_nodes=['D1', 'D2'], utility_nodes=['U1', 'U2']) cid.add_cpds( UniformRandomCPD('S1', [0, 1]), DecisionDomain('D1', [0, 1]), FunctionCPD('U1', lambda s1, d1: int(s1 == d1), evidence=['S1', 'D1']), FunctionCPD('S2', lambda d1: d1, evidence=['D1']), DecisionDomain('D2', [0, 1]), FunctionCPD('U2', lambda s2, d2: int(s2 == d2), evidence=['S2', 'D2']), ) return cid
def impute_random_decision(self, d: str) -> None: """Impute a random policy to the given decision node""" current_cpd = self.get_cpds(d) if current_cpd: sn = current_cpd.state_names[d] else: raise Exception(f"can't figure out domain for {d}, did you forget to specify DecisionDomain?") self.add_cpds(UniformRandomCPD(d, sn))
def get_3node_cid() -> CID: cid = CID([('S', 'D'), ('S', 'U'), ('D', 'U')], decision_nodes=['D'], utility_nodes=['U']) cpd_s = UniformRandomCPD('S', [0, 1]) cpd_u = FunctionCPD('U', lambda s, d: int(s == d), evidence=['S', 'D']) cpd_d = DecisionDomain('D', [0, 1]) cid.add_cpds(cpd_d, cpd_s, cpd_u) return cid
def get_introduced_bias() -> CID: cid = CID( [ ('A', 'X'), # defining the graph's nodes and edges ('Z', 'X'), ('Z', 'Y'), ('X', 'D'), ('X', 'Y'), ('D', 'U'), ('Y', 'U') ], decision_nodes=['D'], utility_nodes=['U']) cpd_a = UniformRandomCPD('A', [0, 1]) cpd_z = UniformRandomCPD('Z', [0, 1]) cpd_x = FunctionCPD('X', lambda a, z: a * z, evidence=['A', 'Z']) cpd_d = DecisionDomain('D', [0, 1]) cpd_y = FunctionCPD('Y', lambda x, z: x + z, evidence=['X', 'Z']) cpd_u = FunctionCPD('U', lambda d, y: -(d - y)**2, evidence=['D', 'Y']) cid.add_cpds(cpd_a, cpd_d, cpd_z, cpd_x, cpd_y, cpd_u) return cid
def get_2dec_cid() -> CID: cid = CID([('S1', 'S2'), ('S1', 'D1'), ('D1', 'S2'), ('S2', 'U'), ('S2', 'D2'), ('D2', 'U')], decision_nodes=['D1', 'D2'], utility_nodes=['U']) cpd_s1 = UniformRandomCPD('S1', [0, 1]) cpd_d1 = DecisionDomain('D1', [0, 1]) cpd_d2 = DecisionDomain('D2', [0, 1]) cpd_s2 = FunctionCPD('S2', lambda s2, d1: int(s2 == d1), evidence=['S1', 'D1']) cpd_u = FunctionCPD('U', lambda s2, d2: int(s2 == d2), evidence=['S2', 'D2']) cid.add_cpds(cpd_s1, cpd_d1, cpd_s2, cpd_d2, cpd_u) return cid
def random_cid(n_all: int, n_decisions: int, n_utilities: int, edge_density: float = 0.4, add_sr_edges: bool = True, add_cpds: bool = True, seed: int = None) -> CID: """Generates a random Cid with the specified number of nodes and edges""" all_names, decision_names, utility_names = get_node_names( n_all, n_decisions, n_utilities) edges = get_edges(all_names, utility_names, edge_density, seed=seed, allow_u_edges=False) cid = CID(edges, decision_names, utility_names) for uname in utility_names: for edge in edges: assert uname != edge[0] for i, d1 in enumerate(decision_names): for j, d2 in enumerate(decision_names[i + 1:]): assert d2 not in cid._get_ancestors_of(d1) if add_sr_edges: add_sufficient_recalls(cid) if add_cpds: for node in cid.nodes: if node in cid.all_decision_nodes: cid.add_cpds(DecisionDomain(node, [0, 1])) elif not cid.get_parents(node): # node is a root node cid.add_cpds(UniformRandomCPD(node, [0, 1])) else: cid.add_cpds( RandomlySampledFunctionCPD(node, cid.get_parents(node))) return cid
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_a.get_values() == np.array([[0.5, 0.5], [0.5, 0.5]])).all())