]> gitweb.michael.orlitzky.com - dunshire.git/blobdiff - src/dunshire/games.py
Get rid of the contains_strict() methods and compare against ABS_TOL.
[dunshire.git] / src / dunshire / games.py
index 9f0b0ced50628d387ef5fbe2b8d166aefc9bb6a8..4b5383dd2f12c1fe2d6de250760e5009639bfd5d 100644 (file)
@@ -319,10 +319,10 @@ class SymmetricLinearGame:
         # feeding it to CVXOPT.
         self._L = matrix(L, (K.dimension(), K.dimension())).trans()
 
-        if not K.contains_strict(self._e1):
+        if not self._e1 in K:
             raise ValueError('the point e1 must lie in the interior of K')
 
-        if not K.contains_strict(self._e2):
+        if not self._e2 in K:
             raise ValueError('the point e2 must lie in the interior of K')
 
     def __str__(self):
@@ -556,3 +556,71 @@ class SymmetricLinearGameTest(TestCase):
              for j in range(K.dimension())]
         self.assert_solution_exists(L, K, e1, e2)
 
+
+    def test_negative_value_Z_operator(self):
+        """
+        Test the example given in Gowda/Ravindran of a Z-matrix with
+        negative game value on the nonnegative orthant.
+        """
+        K = NonnegativeOrthant(2)
+        e1 = [1,1]
+        e2 = e1
+        L = [[1,-2],[-2,1]]
+        G = SymmetricLinearGame(L, K, e1, e2)
+        self.assertTrue(G.solution().game_value() < -options.ABS_TOL)
+
+
+    def test_nonnegative_scaling_orthant(self):
+        """
+        Test that scaling ``L`` by a nonnegative number scales the value
+        of the game by the same number. Use the nonnegative orthant as
+        our cone.
+        """
+        ambient_dim = randint(1, 10)
+        K = NonnegativeOrthant(ambient_dim)
+        e1 = [uniform(0.1, 10) for idx in range(K.dimension())]
+        e2 = [uniform(0.1, 10) for idx in range(K.dimension())]
+        L = matrix([[uniform(-10, 10) for i in range(K.dimension())]
+                    for j in range(K.dimension())])
+        G1 = SymmetricLinearGame(L, K, e1, e2)
+        value1 = G1.solution().game_value()
+        alpha = uniform(0.1, 10)
+
+        G2 = SymmetricLinearGame(alpha*L, K, e1, e2)
+        value2 = G2.solution().game_value()
+        self.assert_within_tol(alpha*value1, value2)
+
+
+    def test_nonnegative_scaling_icecream(self):
+        """
+        The same test as :meth:`test_nonnegative_scaling_orthant`,
+        except over the ice cream cone.
+        """
+                # Use a minimum dimension of two to avoid divide-by-zero in
+        # the fudge factor we make up later.
+        ambient_dim = randint(2, 10)
+        K = IceCream(ambient_dim)
+        e1 = [1] # Set the "height" of e1 to one
+        e2 = [1] # And the same for e2
+
+        # If we choose the rest of the components of e1,e2 randomly
+        # between 0 and 1, then the largest the squared norm of the
+        # non-height part of e1,e2 could be is the 1*(dim(K) - 1). We
+        # need to make it less than one (the height of the cone) so
+        # that the whole thing is in the cone. The norm of the
+        # non-height part is sqrt(dim(K) - 1), and we can divide by
+        # twice that.
+        fudge_factor = 1.0 / (2.0*sqrt(K.dimension() - 1.0))
+        e1 += [fudge_factor*uniform(0, 1) for idx in range(K.dimension() - 1)]
+        e2 += [fudge_factor*uniform(0, 1) for idx in range(K.dimension() - 1)]
+        L = matrix([[uniform(-10, 10) for i in range(K.dimension())]
+                    for j in range(K.dimension())])
+
+        G1 = SymmetricLinearGame(L, K, e1, e2)
+        value1 = G1.solution().game_value()
+        alpha = uniform(0.1, 10)
+
+        G2 = SymmetricLinearGame(alpha*L, K, e1, e2)
+        value2 = G2.solution().game_value()
+        self.assert_within_tol(alpha*value1, value2)
+