-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtest_macid.py
214 lines (194 loc) · 9.58 KB
/
test_macid.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import sys
import unittest
import numpy as np
import pytest
from pycid.core.relevance_graph import CondensedRelevanceGraph
from pycid.examples.simple_macids import (
basic_different_dec_cardinality,
get_basic_subgames,
get_basic_subgames3,
subgame_mixed_spe,
two_agents_three_actions,
)
from pycid.examples.story_macids import (
battle_of_the_sexes,
matching_pennies,
modified_taxi_competition,
prisoners_dilemma,
taxi_competition,
)
class TestMACID(unittest.TestCase):
# @unittest.skip("")
def test_decs_in_each_maid_subgame(self) -> None:
macid = prisoners_dilemma()
self.assertCountEqual(macid.decs_in_each_maid_subgame(), [{"D1", "D2"}])
macid = get_basic_subgames()
self.assertTrue(len(macid.decs_in_each_maid_subgame()) == 4)
macid = get_basic_subgames3()
self.assertTrue(len(macid.decs_in_each_maid_subgame()) == 5)
# @unittest.skip("")
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.decisions]
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.domain, ["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])))
# @unittest.skip("")
def test_get_ne(self) -> None:
macid = prisoners_dilemma()
self.assertEqual(len(macid.get_ne()), 1)
pne = macid.get_ne()[0]
macid.add_cpds(*pne)
self.assertEqual(macid.expected_utility({}, agent=1), -2)
self.assertEqual(macid.expected_utility({}, agent=2), -2)
macid2 = battle_of_the_sexes()
self.assertEqual(len(macid2.get_ne()), 3)
self.assertEqual(len(macid2.get_ne("enumpure")), 2)
self.assertEqual(len(macid2.get_ne(solver="enummixed")), 3)
nes = macid2.get_ne(solver="enummixed")
joint_policy = macid2.policy_profile_assignment(nes[1]) # mixed ne (order not guaranteed?)
cpd_df = joint_policy["D_F"]
cpd_dm = joint_policy["D_M"]
self.assertTrue(np.allclose(cpd_df.values, np.array([0.6, 0.4])))
self.assertTrue(np.allclose(cpd_dm.values, np.array([0.4, 0.6])))
macid3 = matching_pennies()
self.assertEqual(len(macid3.get_ne()), 1) # gets the mixed NE by default
self.assertEqual(len(macid3.get_ne("enumpure")), 0) # no NE if overriden solver to pure NE only
self.assertEqual(len(macid3.get_ne(solver="enummixed")), 1)
joint_policy = macid3.policy_profile_assignment(macid3.get_ne(solver="enummixed")[0])
cpd_d1 = joint_policy["D1"]
cpd_d2 = joint_policy["D2"]
self.assertTrue(np.allclose(cpd_d1.values, np.array([0.5, 0.5])))
self.assertTrue(np.allclose(cpd_d2.values, np.array([0.5, 0.5])))
self.assertEqual(len(macid3.get_ne(solver="lp")), 1)
joint_policy = macid3.policy_profile_assignment(macid3.get_ne(solver="lp")[0])
cpd_d1 = joint_policy["D1"]
cpd_d2 = joint_policy["D2"]
self.assertTrue(np.allclose(cpd_d1.values, np.array([0.5, 0.5])))
self.assertTrue(np.allclose(cpd_d2.values, np.array([0.5, 0.5])))
self.assertEqual(len(macid3.get_ne(solver="lcp")), 1)
joint_policy = macid3.policy_profile_assignment(macid3.get_ne(solver="lcp")[0])
cpd_d1 = joint_policy["D1"]
cpd_d2 = joint_policy["D2"]
self.assertTrue(np.allclose(cpd_d1.values, np.array([0.5, 0.5])))
self.assertTrue(np.allclose(cpd_d2.values, np.array([0.5, 0.5])))
self.assertEqual(len(macid3.get_ne(solver="simpdiv")), 1)
joint_policy = macid3.policy_profile_assignment(macid3.get_ne(solver="simpdiv")[0])
cpd_d1 = joint_policy["D1"]
cpd_d2 = joint_policy["D2"]
self.assertTrue(np.allclose(cpd_d1.values, np.array([0.5, 0.5])))
self.assertTrue(np.allclose(cpd_d2.values, np.array([0.5, 0.5])))
self.assertEqual(len(macid3.get_ne(solver="gnm")), 1)
joint_policy = macid3.policy_profile_assignment(macid3.get_ne(solver="gnm")[0])
cpd_d1 = joint_policy["D1"]
cpd_d2 = joint_policy["D2"]
self.assertTrue(np.allclose(cpd_d1.values, np.array([0.5, 0.5])))
self.assertTrue(np.allclose(cpd_d2.values, np.array([0.5, 0.5])))
self.assertEqual(len(macid3.get_ne(solver="ipa")), 1)
joint_policy = macid3.policy_profile_assignment(macid3.get_ne(solver="ipa")[0])
cpd_d1 = joint_policy["D1"]
cpd_d2 = joint_policy["D2"]
self.assertTrue(np.allclose(cpd_d1.values, np.array([0.5, 0.5])))
self.assertTrue(np.allclose(cpd_d2.values, np.array([0.5, 0.5])))
macid4 = two_agents_three_actions()
self.assertEqual(len(macid4.get_ne()), 1)
# @unittest.skip("")
def test_create_subgame(self) -> None:
macid = subgame_mixed_spe()
sccs = list(reversed(CondensedRelevanceGraph(macid).get_scc_topological_ordering()))
self.assertEqual(len(sccs), 4)
subgame = macid.create_subgame(sccs[3])
self.assertEqual(len(subgame.nodes), 8)
self.assertEqual(len(subgame.agents), 1)
self.assertEqual(len(subgame.decisions), 1)
self.assertEqual(len(subgame.utilities), 1)
ne_policy_in_subgame = macid.policy_profile_assignment(subgame.get_ne()[0])
cpd_d1 = ne_policy_in_subgame["D1"]
self.assertTrue(np.array_equal(cpd_d1.values, np.array([0, 1])))
self.assertTrue(type(subgame), "pycid.core.macid.MACID")
# @unittest.skip("")
def test_get_ne_in_sg(self) -> None:
macid = taxi_competition()
ne_in_subgame = macid.get_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_ne_in_sg()
self.assertEqual(len(ne_in_full_macid), 4)
with self.assertRaises(KeyError):
macid.get_ne_in_sg(decisions_in_sg=["D3"])
mixed_ne_in_subgame = macid.get_ne_in_sg(decisions_in_sg=["D2"], solver="enummixed")
self.assertEqual(len(mixed_ne_in_subgame), 1)
# @unittest.skip("")
def test_get_spe(self) -> None:
macid = taxi_competition()
all_spe = macid.get_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 = subgame_mixed_spe()
joint_policy = macid.policy_profile_assignment(macid.get_spe()[0])
cpd_d1 = joint_policy["D1"]
cpd_d2 = joint_policy["D2"]
cpd_d3 = joint_policy["D3"]
cpd_d4 = joint_policy["D4"]
cpd_d5 = joint_policy["D5"]
cpd_d6 = joint_policy["D6"]
cpd_d7 = joint_policy["D7"]
self.assertTrue(np.allclose(cpd_d1.values, np.array([0, 1])))
self.assertTrue(np.allclose(cpd_d2.values, np.array([0.5, 0.5])))
self.assertTrue(np.allclose(cpd_d3.values, np.array([0.5, 0.5])))
self.assertTrue(np.allclose(cpd_d4.values, np.array([0.5, 0.5])))
self.assertTrue(np.allclose(cpd_d5.values, np.array([0.5, 0.5])))
self.assertTrue(np.allclose(cpd_d6.values, np.array([0.5, 0.5])))
self.assertTrue(np.allclose(cpd_d7.values, np.array([0.5, 0.5])))
macid = modified_taxi_competition()
all_spe = macid.get_spe("enumpure")
self.assertTrue(len(all_spe) == 2)
macid = prisoners_dilemma()
all_spe = macid.get_spe("enumpure")
self.assertTrue(len(all_spe) == 1)
macid = battle_of_the_sexes()
all_spe = macid.get_spe("enumpure")
self.assertTrue(len(all_spe) == 2) # only 2 pure subgame perfect NE
macid3 = basic_different_dec_cardinality()
all_spe = macid3.get_spe("enumpure")
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]])))
macid = battle_of_the_sexes()
all_spe = macid.get_spe(solver="enummixed")
self.assertTrue(len(all_spe) == 3)
macid = prisoners_dilemma()
all_spe = macid.get_spe(solver="enummixed")
self.assertTrue(len(all_spe) == 1)
macid = taxi_competition()
all_spe = macid.get_spe(solver="enummixed")
self.assertTrue(len(all_spe) == 1)
macid = modified_taxi_competition()
all_spe = macid.get_spe(solver="enummixed")
self.assertTrue(len(all_spe) == 2)
macid = basic_different_dec_cardinality()
all_spe = macid.get_spe(solver="enummixed")
self.assertTrue(len(all_spe) == 1)
if __name__ == "__main__":
pytest.main(sys.argv)