def test_story_macid_examples(self) -> None: prisoners_dilemma() battle_of_the_sexes() matching_pennies() taxi_competition() modified_taxi_competition() tree_doctor() forgetful_movie_star() subgame_difference() road_example() politician() umbrella() sequential() signal() triage()
def test_get_all_pure_spe(self) -> None: macid = taxi_competition() all_spe = macid.get_all_pure_spe() self.assertTrue(len(all_spe) == 1) spe = all_spe[0] joint_policy = macid.policy_profile_assignment(spe) cpd_d1 = joint_policy['D1'] cpd_d2 = joint_policy['D2'] self.assertTrue(np.array_equal(cpd_d1.values, np.array([1, 0]))) self.assertTrue( np.array_equal(cpd_d2.values, np.array([[0, 1], [1, 0]]))) macid = modified_taxi_competition() all_spe = macid.get_all_pure_spe() self.assertTrue(len(all_spe) == 2) macid = prisoners_dilemma() all_spe = macid.get_all_pure_spe() self.assertTrue(len(all_spe) == 1) macid = battle_of_the_sexes() all_spe = macid.get_all_pure_spe() self.assertTrue(len(all_spe) == 2) macid3 = basic_different_dec_cardinality() all_spe = macid3.get_all_pure_spe() spe = all_spe[0] joint_policy = macid3.policy_profile_assignment(spe) cpd_d1 = joint_policy['D1'] cpd_d2 = joint_policy['D2'] self.assertTrue(np.array_equal(cpd_d1.values, np.array([0, 1]))) self.assertTrue( np.array_equal(cpd_d2.values, np.array([[0, 0], [1, 0], [0, 1]])))
def test_is_s_reachable(self) -> None: example = taxi_competition() self.assertTrue(example.is_s_reachable('D1', 'D2')) self.assertFalse(example.is_s_reachable('D2', 'D1')) example2 = subgame_difference() self.assertTrue(example2.is_s_reachable('D1', 'D2')) self.assertFalse(example2.is_s_reachable('D2', 'D1'))
def test_relevance_graph(self) -> None: example = taxi_competition() rg = RelevanceGraph(example) self.assertTrue(rg.is_acyclic()) example2 = prisoners_dilemma() rg2 = RelevanceGraph(example2) self.assertFalse(rg2.is_acyclic()) self.assertTrue(len(rg.get_sccs()) == 2) self.assertEqual(rg2.get_sccs(), [{'D1', 'D2'}])
def test_find_active_path(self) -> None: example = taxi_competition() self.assertEqual(find_active_path(example, 'D1', 'U1', ['D2']), ['D1', 'U1']) self.assertFalse(find_active_path(example, 'D1', 'U1', ['D2', 'U1'])) with self.assertRaises(Exception): find_active_path(example, 'D1', 'U1', ['D3']) with self.assertRaises(Exception): find_active_path(example, 'D3', 'U1', ['D2']) with self.assertRaises(Exception): find_active_path(example, 'D1', 'U3', ['D2'])
def test_sufficient_recall(self) -> None: example = forgetful_movie_star() self.assertFalse(example.sufficient_recall(1)) self.assertTrue(example.sufficient_recall(2)) example2 = taxi_competition() self.assertTrue(example2.sufficient_recall(1)) self.assertTrue(example2.sufficient_recall(2)) with self.assertRaises(Exception): self.assertTrue(example2.sufficient_recall(3))
def test_intervention(self) -> None: cid = get_minimal_cid() cid.impute_random_policy() self.assertEqual(cid.expected_value(['B'], {})[0], 0.5) for a in [0, 1, 2]: cid.intervene({'A': a}) self.assertEqual(cid.expected_value(['B'], {})[0], a) self.assertEqual(cid.expected_value(['B'], {}, intervene={'A': 1})[0], 1) macid = taxi_competition() macid.impute_fully_mixed_policy_profile() self.assertEqual(macid.expected_value(['U1'], {}, intervene={'D1': 'c', 'D2': 'e'})[0], 3) self.assertEqual(macid.expected_value(['U2'], {}, intervene={'D1': 'c', 'D2': 'e'})[0], 5)
def test_get_all_pure_ne_in_sg(self) -> None: macid = taxi_competition() ne_in_subgame = macid.get_all_pure_ne_in_sg(decisions_in_sg=['D2']) policy_assignment = macid.policy_profile_assignment(ne_in_subgame[0]) cpd_d2 = policy_assignment['D2'] self.assertTrue( np.array_equal(cpd_d2.values, np.array([[0, 1], [1, 0]]))) self.assertFalse(policy_assignment['D1']) ne_in_full_macid = macid.get_all_pure_ne_in_sg() self.assertEqual(len(ne_in_full_macid), 3) with self.assertRaises(Exception): macid.get_all_pure_ne_in_sg(decisions_in_sg=['D3'])
def test_policy_profile_assignment(self) -> None: macid = taxi_competition() macid.impute_random_decision('D1') cpd = macid.get_cpds('D1') partial_policy = [cpd] policy_assignment = macid.policy_profile_assignment(partial_policy) self.assertTrue(policy_assignment['D1']) self.assertFalse(policy_assignment['D2']) macid.impute_fully_mixed_policy_profile() joint_policy = [macid.get_cpds(d) for d in macid.all_decision_nodes] joint_policy_assignment = macid.policy_profile_assignment(joint_policy) self.assertTrue(joint_policy_assignment['D1']) self.assertTrue(joint_policy_assignment['D2']) d1_cpd = joint_policy_assignment['D1'] self.assertEqual(d1_cpd.state_names, {'D1': ['e', 'c']}) print(d1_cpd.state_names) # can put this in the notebook too self.assertTrue(np.array_equal(d1_cpd.values, np.array([0.5, 0.5])))
def test_mechanism_graph(self) -> None: example = taxi_competition() mg = MechanismGraph(example) self.assertCountEqual(mg.all_decision_nodes, ['D1', 'D2']) self.assertCountEqual(mg.all_utility_nodes, ['U1', 'U2']) self.assertEqual(len(mg.nodes()), len(example.nodes()) * 2)
def test_condensed_relevance_graph(self) -> None: example = taxi_competition() crg = CondensedRelevanceGraph(example) self.assertEqual(crg.get_scc_topological_ordering(), [['D1'], ['D2']]) self.assertEqual(crg.get_decisions_in_scc()[0], ['D2'])