]> gitweb.michael.orlitzky.com - dunshire.git/blob - test/symmetric_linear_game_test.py
Add the player1_start() method and two tests for it.
[dunshire.git] / test / symmetric_linear_game_test.py
1 """
2 Unit tests for the :class:`SymmetricLinearGame` class.
3 """
4
5 from unittest import TestCase
6
7 from dunshire.games import SymmetricLinearGame
8 from dunshire.matrices import eigenvalues_re, inner_product, norm
9 from dunshire import options
10 from .randomgen import (random_icecream_game, random_ll_icecream_game,
11 random_ll_orthant_game, random_nn_scaling,
12 random_orthant_game, random_positive_orthant_game,
13 random_translation)
14
15
16 # Tell pylint to shut up about the large number of methods.
17 class SymmetricLinearGameTest(TestCase): # pylint: disable=R0904
18 """
19 Tests for the SymmetricLinearGame and Solution classes.
20 """
21 def assert_within_tol(self, first, second, modifier=1):
22 """
23 Test that ``first`` and ``second`` are equal within a multiple of
24 our default tolerances.
25
26 Parameters
27 ----------
28
29 first : float
30 The first number to compare.
31
32 second : float
33 The second number to compare.
34
35 modifier : float
36 A scaling factor (default: 1) applied to the default
37 tolerance for this comparison. If you have a poorly-
38 conditioned matrix, for example, you may want to set this
39 greater than one.
40
41 """
42 self.assertTrue(abs(first - second) < options.ABS_TOL*modifier)
43
44
45 def test_solutions_dont_change_orthant(self):
46 G = random_orthant_game()
47 self.assert_solutions_dont_change(G)
48
49 def test_solutions_dont_change_icecream(self):
50 G = random_icecream_game()
51 self.assert_solutions_dont_change(G)
52
53 def assert_solutions_dont_change(self, G):
54 """
55 If we solve the same problem twice, we should get
56 the same answer both times.
57 """
58 soln1 = G.solution()
59 soln2 = G.solution()
60 p1_diff = norm(soln1.player1_optimal() - soln2.player1_optimal())
61 p2_diff = norm(soln1.player2_optimal() - soln2.player2_optimal())
62 gv_diff = abs(soln1.game_value() - soln2.game_value())
63
64 p1_close = p1_diff < options.ABS_TOL
65 p2_close = p2_diff < options.ABS_TOL
66 gv_close = gv_diff < options.ABS_TOL
67
68 self.assertTrue(p1_close and p2_close and gv_close)
69
70
71 def assert_player1_start_valid(self, G):
72 x = G.player1_start()['x']
73 s = G.player1_start()['s']
74 s1 = s[0:G.dimension()]
75 s2 = s[G.dimension():]
76 self.assert_within_tol(norm(G.A()*x - G.b()), 0)
77 self.assertTrue((s1,s2) in G.C())
78
79
80 def test_player1_start_valid_orthant(self):
81 """
82 Ensure that player one's starting point is in the orthant.
83 """
84 G = random_orthant_game()
85 self.assert_player1_start_valid(G)
86
87
88 def test_player1_start_valid_icecream(self):
89 """
90 Ensure that player one's starting point is in the ice-cream cone.
91 """
92 G = random_icecream_game()
93 self.assert_player1_start_valid(G)
94
95
96 def test_condition_lower_bound(self):
97 """
98 Ensure that the condition number of a game is greater than or
99 equal to one.
100
101 It should be safe to compare these floats directly: we compute
102 the condition number as the ratio of one nonnegative real number
103 to a smaller nonnegative real number.
104 """
105 G = random_orthant_game()
106 self.assertTrue(G.condition() >= 1.0)
107 G = random_icecream_game()
108 self.assertTrue(G.condition() >= 1.0)
109
110
111 def assert_scaling_works(self, G):
112 """
113 Test that scaling ``L`` by a nonnegative number scales the value
114 of the game by the same number.
115 """
116 (alpha, H) = random_nn_scaling(G)
117 value1 = G.solution().game_value()
118 value2 = H.solution().game_value()
119 modifier = 4*max(abs(alpha), 1)
120 self.assert_within_tol(alpha*value1, value2, modifier)
121
122
123 def test_scaling_orthant(self):
124 """
125 Test that scaling ``L`` by a nonnegative number scales the value
126 of the game by the same number over the nonnegative orthant.
127 """
128 G = random_orthant_game()
129 self.assert_scaling_works(G)
130
131
132 def test_scaling_icecream(self):
133 """
134 The same test as :meth:`test_nonnegative_scaling_orthant`,
135 except over the ice cream cone.
136 """
137 G = random_icecream_game()
138 self.assert_scaling_works(G)
139
140
141 def assert_translation_works(self, G):
142 """
143 Check that translating ``L`` by alpha*(e1*e2.trans()) increases
144 the value of the associated game by alpha.
145 """
146 # We need to use ``L`` later, so make sure we transpose it
147 # before passing it in as a column-indexed matrix.
148 soln1 = G.solution()
149 value1 = soln1.game_value()
150 x_bar = soln1.player1_optimal()
151 y_bar = soln1.player2_optimal()
152
153 # This is the "correct" representation of ``M``, but COLUMN
154 # indexed...
155 (alpha, H) = random_translation(G)
156 value2 = H.solution().game_value()
157
158 modifier = 4*max(abs(alpha), 1)
159 self.assert_within_tol(value1 + alpha, value2, modifier)
160
161 # Make sure the same optimal pair works.
162 self.assert_within_tol(value2, H.payoff(x_bar, y_bar), modifier)
163
164
165 def test_translation_orthant(self):
166 """
167 Test that translation works over the nonnegative orthant.
168 """
169 G = random_orthant_game()
170 self.assert_translation_works(G)
171
172
173 def test_translation_icecream(self):
174 """
175 The same as :meth:`test_translation_orthant`, except over the
176 ice cream cone.
177 """
178 G = random_icecream_game()
179 self.assert_translation_works(G)
180
181
182 def assert_opposite_game_works(self, G):
183 """
184 Check the value of the "opposite" game that gives rise to a
185 value that is the negation of the original game. Comes from
186 some corollary.
187 """
188 # This is the "correct" representation of ``M``, but
189 # COLUMN indexed...
190 M = -G.L().trans()
191
192 # so we have to transpose it when we feed it to the constructor.
193 # Note: the condition number of ``H`` should be comparable to ``G``.
194 H = SymmetricLinearGame(M.trans(), G.K(), G.e2(), G.e1())
195
196 soln1 = G.solution()
197 x_bar = soln1.player1_optimal()
198 y_bar = soln1.player2_optimal()
199 soln2 = H.solution()
200
201 # The modifier of 4 is because each could be off by 2*ABS_TOL,
202 # which is how far apart the primal/dual objectives have been
203 # observed being.
204 self.assert_within_tol(-soln1.game_value(), soln2.game_value(), 4)
205
206 # Make sure the switched optimal pair works. Since x_bar and
207 # y_bar come from G, we use the same modifier.
208 self.assert_within_tol(soln2.game_value(), H.payoff(y_bar, x_bar), 4)
209
210
211
212 def test_opposite_game_orthant(self):
213 """
214 Test the value of the "opposite" game over the nonnegative
215 orthant.
216 """
217 G = random_orthant_game()
218 self.assert_opposite_game_works(G)
219
220
221 def test_opposite_game_icecream(self):
222 """
223 Like :meth:`test_opposite_game_orthant`, except over the
224 ice-cream cone.
225 """
226 G = random_icecream_game()
227 self.assert_opposite_game_works(G)
228
229
230 def assert_orthogonality(self, G):
231 """
232 Two orthogonality relations hold at an optimal solution, and we
233 check them here.
234 """
235 soln = G.solution()
236 x_bar = soln.player1_optimal()
237 y_bar = soln.player2_optimal()
238 value = soln.game_value()
239
240 ip1 = inner_product(y_bar, G.L()*x_bar - value*G.e1())
241 ip2 = inner_product(value*G.e2() - G.L().trans()*y_bar, x_bar)
242
243 # Huh.. well, y_bar and x_bar can each be epsilon away, but
244 # x_bar is scaled by L, so that's (norm(L) + 1), and then
245 # value could be off by epsilon, so that's another norm(e1) or
246 # norm(e2). On the other hand, this test seems to pass most of
247 # the time even with a modifier of one. How about.. four?
248 self.assert_within_tol(ip1, 0, 4)
249 self.assert_within_tol(ip2, 0, 4)
250
251
252 def test_orthogonality_orthant(self):
253 """
254 Check the orthgonality relationships that hold for a solution
255 over the nonnegative orthant.
256 """
257 G = random_orthant_game()
258 self.assert_orthogonality(G)
259
260
261 def test_orthogonality_icecream(self):
262 """
263 Check the orthgonality relationships that hold for a solution
264 over the ice-cream cone.
265 """
266 G = random_icecream_game()
267 self.assert_orthogonality(G)
268
269
270 def test_positive_operator_value(self):
271 """
272 Test that a positive operator on the nonnegative orthant gives
273 rise to a a game with a nonnegative value.
274
275 This test theoretically applies to the ice-cream cone as well,
276 but we don't know how to make positive operators on that cone.
277 """
278 G = random_positive_orthant_game()
279 self.assertTrue(G.solution().game_value() >= -options.ABS_TOL)
280
281
282 def assert_lyapunov_works(self, G):
283 """
284 Check that Lyapunov games act the way we expect.
285 """
286 soln = G.solution()
287
288 # We only check for positive/negative stability if the game
289 # value is not basically zero. If the value is that close to
290 # zero, we just won't check any assertions.
291 #
292 # See :meth:`assert_within_tol` for an explanation of the
293 # fudge factors.
294 eigs = eigenvalues_re(G.L())
295
296 if soln.game_value() > options.ABS_TOL:
297 # L should be positive stable
298 positive_stable = all([eig > -options.ABS_TOL for eig in eigs])
299 self.assertTrue(positive_stable)
300 elif soln.game_value() < -options.ABS_TOL:
301 # L should be negative stable
302 negative_stable = all([eig < options.ABS_TOL for eig in eigs])
303 self.assertTrue(negative_stable)
304
305 # The dual game's value should always equal the primal's.
306 # The modifier of 4 is because even though the games are dual,
307 # CVXOPT doesn't know that, and each could be off by 2*ABS_TOL.
308 dualsoln = G.dual().solution()
309 self.assert_within_tol(dualsoln.game_value(), soln.game_value(), 4)
310
311
312 def test_lyapunov_orthant(self):
313 """
314 Test that a Lyapunov game on the nonnegative orthant works.
315 """
316 G = random_ll_orthant_game()
317 self.assert_lyapunov_works(G)
318
319
320 def test_lyapunov_icecream(self):
321 """
322 Test that a Lyapunov game on the ice-cream cone works.
323 """
324 G = random_ll_icecream_game()
325 self.assert_lyapunov_works(G)