2 Unit tests for the :class:`SymmetricLinearGame` class.
5 from unittest
import TestCase
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
,
16 # Tell pylint to shut up about the large number of methods.
17 class SymmetricLinearGameTest(TestCase
): # pylint: disable=R0904
19 Tests for the SymmetricLinearGame and Solution classes.
21 def assert_within_tol(self
, first
, second
, modifier
=1):
23 Test that ``first`` and ``second`` are equal within a multiple of
24 our default tolerances.
30 The first number to compare.
33 The second number to compare.
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
42 self
.assertTrue(abs(first
- second
) < options
.ABS_TOL
*modifier
)
45 def test_solutions_dont_change_orthant(self
):
46 G
= random_orthant_game()
47 self
.assert_solutions_dont_change(G
)
49 def test_solutions_dont_change_icecream(self
):
50 G
= random_icecream_game()
51 self
.assert_solutions_dont_change(G
)
53 def assert_solutions_dont_change(self
, G
):
55 If we solve the same problem twice, we should get
56 the same answer both times.
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())
64 p1_close
= p1_diff
< options
.ABS_TOL
65 p2_close
= p2_diff
< options
.ABS_TOL
66 gv_close
= gv_diff
< options
.ABS_TOL
68 self
.assertTrue(p1_close
and p2_close
and gv_close
)
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())
80 def test_player1_start_valid_orthant(self
):
82 Ensure that player one's starting point is in the orthant.
84 G
= random_orthant_game()
85 self
.assert_player1_start_valid(G
)
88 def test_player1_start_valid_icecream(self
):
90 Ensure that player one's starting point is in the ice-cream cone.
92 G
= random_icecream_game()
93 self
.assert_player1_start_valid(G
)
96 def assert_player2_start_valid(self
, G
):
97 z
= G
.player2_start()['z']
98 z1
= z
[0:G
.dimension()]
99 z2
= z
[G
.dimension():]
100 self
.assertTrue((z1
, z2
) in G
.C())
103 def test_player2_start_valid_orthant(self
):
105 Ensure that player two's starting point is in the orthant.
107 G
= random_orthant_game()
108 self
.assert_player2_start_valid(G
)
111 def test_player2_start_valid_icecream(self
):
113 Ensure that player two's starting point is in the ice-cream cone.
115 G
= random_icecream_game()
116 self
.assert_player2_start_valid(G
)
119 def test_condition_lower_bound(self
):
121 Ensure that the condition number of a game is greater than or
124 It should be safe to compare these floats directly: we compute
125 the condition number as the ratio of one nonnegative real number
126 to a smaller nonnegative real number.
128 G
= random_orthant_game()
129 self
.assertTrue(G
.condition() >= 1.0)
130 G
= random_icecream_game()
131 self
.assertTrue(G
.condition() >= 1.0)
134 def assert_scaling_works(self
, G
):
136 Test that scaling ``L`` by a nonnegative number scales the value
137 of the game by the same number.
139 (alpha
, H
) = random_nn_scaling(G
)
140 value1
= G
.solution().game_value()
141 value2
= H
.solution().game_value()
142 modifier
= 4*max(abs(alpha
), 1)
143 self
.assert_within_tol(alpha
*value1
, value2
, modifier
)
146 def test_scaling_orthant(self
):
148 Test that scaling ``L`` by a nonnegative number scales the value
149 of the game by the same number over the nonnegative orthant.
151 G
= random_orthant_game()
152 self
.assert_scaling_works(G
)
155 def test_scaling_icecream(self
):
157 The same test as :meth:`test_nonnegative_scaling_orthant`,
158 except over the ice cream cone.
160 G
= random_icecream_game()
161 self
.assert_scaling_works(G
)
164 def assert_translation_works(self
, G
):
166 Check that translating ``L`` by alpha*(e1*e2.trans()) increases
167 the value of the associated game by alpha.
169 # We need to use ``L`` later, so make sure we transpose it
170 # before passing it in as a column-indexed matrix.
172 value1
= soln1
.game_value()
173 x_bar
= soln1
.player1_optimal()
174 y_bar
= soln1
.player2_optimal()
176 # This is the "correct" representation of ``M``, but COLUMN
178 (alpha
, H
) = random_translation(G
)
179 value2
= H
.solution().game_value()
181 modifier
= 4*max(abs(alpha
), 1)
182 self
.assert_within_tol(value1
+ alpha
, value2
, modifier
)
184 # Make sure the same optimal pair works.
185 self
.assert_within_tol(value2
, H
.payoff(x_bar
, y_bar
), modifier
)
188 def test_translation_orthant(self
):
190 Test that translation works over the nonnegative orthant.
192 G
= random_orthant_game()
193 self
.assert_translation_works(G
)
196 def test_translation_icecream(self
):
198 The same as :meth:`test_translation_orthant`, except over the
201 G
= random_icecream_game()
202 self
.assert_translation_works(G
)
205 def assert_opposite_game_works(self
, G
):
207 Check the value of the "opposite" game that gives rise to a
208 value that is the negation of the original game. Comes from
211 # This is the "correct" representation of ``M``, but
215 # so we have to transpose it when we feed it to the constructor.
216 # Note: the condition number of ``H`` should be comparable to ``G``.
217 H
= SymmetricLinearGame(M
.trans(), G
.K(), G
.e2(), G
.e1())
220 x_bar
= soln1
.player1_optimal()
221 y_bar
= soln1
.player2_optimal()
224 # The modifier of 4 is because each could be off by 2*ABS_TOL,
225 # which is how far apart the primal/dual objectives have been
227 self
.assert_within_tol(-soln1
.game_value(), soln2
.game_value(), 4)
229 # Make sure the switched optimal pair works. Since x_bar and
230 # y_bar come from G, we use the same modifier.
231 self
.assert_within_tol(soln2
.game_value(), H
.payoff(y_bar
, x_bar
), 4)
235 def test_opposite_game_orthant(self
):
237 Test the value of the "opposite" game over the nonnegative
240 G
= random_orthant_game()
241 self
.assert_opposite_game_works(G
)
244 def test_opposite_game_icecream(self
):
246 Like :meth:`test_opposite_game_orthant`, except over the
249 G
= random_icecream_game()
250 self
.assert_opposite_game_works(G
)
253 def assert_orthogonality(self
, G
):
255 Two orthogonality relations hold at an optimal solution, and we
259 x_bar
= soln
.player1_optimal()
260 y_bar
= soln
.player2_optimal()
261 value
= soln
.game_value()
263 ip1
= inner_product(y_bar
, G
.L()*x_bar
- value
*G
.e1())
264 ip2
= inner_product(value
*G
.e2() - G
.L().trans()*y_bar
, x_bar
)
266 # Huh.. well, y_bar and x_bar can each be epsilon away, but
267 # x_bar is scaled by L, so that's (norm(L) + 1), and then
268 # value could be off by epsilon, so that's another norm(e1) or
269 # norm(e2). On the other hand, this test seems to pass most of
270 # the time even with a modifier of one. How about.. four?
271 self
.assert_within_tol(ip1
, 0, 4)
272 self
.assert_within_tol(ip2
, 0, 4)
275 def test_orthogonality_orthant(self
):
277 Check the orthgonality relationships that hold for a solution
278 over the nonnegative orthant.
280 G
= random_orthant_game()
281 self
.assert_orthogonality(G
)
284 def test_orthogonality_icecream(self
):
286 Check the orthgonality relationships that hold for a solution
287 over the ice-cream cone.
289 G
= random_icecream_game()
290 self
.assert_orthogonality(G
)
293 def test_positive_operator_value(self
):
295 Test that a positive operator on the nonnegative orthant gives
296 rise to a a game with a nonnegative value.
298 This test theoretically applies to the ice-cream cone as well,
299 but we don't know how to make positive operators on that cone.
301 G
= random_positive_orthant_game()
302 self
.assertTrue(G
.solution().game_value() >= -options
.ABS_TOL
)
305 def assert_lyapunov_works(self
, G
):
307 Check that Lyapunov games act the way we expect.
311 # We only check for positive/negative stability if the game
312 # value is not basically zero. If the value is that close to
313 # zero, we just won't check any assertions.
315 # See :meth:`assert_within_tol` for an explanation of the
317 eigs
= eigenvalues_re(G
.L())
319 if soln
.game_value() > options
.ABS_TOL
:
320 # L should be positive stable
321 positive_stable
= all([eig
> -options
.ABS_TOL
for eig
in eigs
])
322 self
.assertTrue(positive_stable
)
323 elif soln
.game_value() < -options
.ABS_TOL
:
324 # L should be negative stable
325 negative_stable
= all([eig
< options
.ABS_TOL
for eig
in eigs
])
326 self
.assertTrue(negative_stable
)
328 # The dual game's value should always equal the primal's.
329 # The modifier of 4 is because even though the games are dual,
330 # CVXOPT doesn't know that, and each could be off by 2*ABS_TOL.
331 dualsoln
= G
.dual().solution()
332 self
.assert_within_tol(dualsoln
.game_value(), soln
.game_value(), 4)
335 def test_lyapunov_orthant(self
):
337 Test that a Lyapunov game on the nonnegative orthant works.
339 G
= random_ll_orthant_game()
340 self
.assert_lyapunov_works(G
)
343 def test_lyapunov_icecream(self
):
345 Test that a Lyapunov game on the ice-cream cone works.
347 G
= random_ll_icecream_game()
348 self
.assert_lyapunov_works(G
)