]> gitweb.michael.orlitzky.com - dunshire.git/commitdiff
Remove member vars from SymmetricLinearCone and work on the solution dict.
authorMichael Orlitzky <michael@orlitzky.com>
Wed, 5 Oct 2016 21:25:08 +0000 (17:25 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Wed, 5 Oct 2016 21:25:08 +0000 (17:25 -0400)
symmetric_linear_game.py

index 29f64ad3339e4b7fe16f49b328fc2b0ed2969925..a33c34530b4b3ce23f4d2315128499994d741c94 100644 (file)
@@ -1,6 +1,7 @@
 from cvxopt import matrix, printing, solvers
 
 from cones import CartesianProduct
+from errors import GameUnsolvableException, GameValueMismatchException
 from matrices import append_col, append_row, identity
 
 printing.options['dformat'] = '%.7f'
@@ -42,41 +43,41 @@ class SymmetricLinearGame:
 
         """
         self._K = K
-        self._C = CartesianProduct(K, K)
         self._e1 = matrix(e1, (K.dimension(), 1))
         self._e2 = matrix(e2, (K.dimension(), 1))
+        self._L = matrix(L, (K.dimension(), K.dimension()))
 
         if not K.contains_strict(self._e1):
             raise ValueError('the point e1 must lie in the interior of K')
+
         if not K.contains_strict(self._e2):
             raise ValueError('the point e2 must lie in the interior of K')
 
-        self._L = matrix(L, (K.dimension(), K.dimension()))
-        self._b = matrix([1], tc='d')
+    def solution(self):
+
+        C = CartesianProduct(K, K)
+        b = matrix([1], tc='d')
         # A column of zeros that fits K.
-        zero = matrix(0, (K.dimension(), 1), tc='d')
-        self._h = matrix([zero, zero])
-        self._c = matrix([-1, zero])
-        self._G = append_row(append_col(zero, -identity(K.dimension())),
-                             append_col(self._e1, -self._L))
-        self._A = matrix([0, self._e1], (1, K.dimension() + 1), 'd')
+        zero = matrix(0, (self._K.dimension(), 1), tc='d')
+        h = matrix([zero, zero])
+        c = matrix([-1, zero])
+        G = append_row(append_col(zero, -identity(K.dimension())),
+                      append_col(self._e1, -self._L))
+        A = matrix([0, self._e1], (1, K.dimension() + 1), 'd')
 
-    def solution(self):
-        soln = solvers.conelp(self._c,
-                              self._G,
-                              self._h,
-                              self._C.cvxopt_dims(),
-                              self._A,
-                              self._b)
-        return soln
-
-    def solve(self):
-        soln = self.solution()
-
-        print('Value of the game (player one): {:f}'.format(soln['x'][0]))
-        print('Optimal strategy (player one):')
-        print(soln['x'][1:])
-
-        print('Value of the game (player two): {:f}'.format(soln['y'][0]))
-        print('Optimal strategy (player two):')
-        print(soln['z'][self._K.dimension():])
+        soln = solvers.conelp(c, G, h, C.cvxopt_dims(), A, b)
+
+        #if soln['status'] != 'optimal':
+        raise GameUnsolvableException(soln['status'], soln)
+
+        p1_value = soln['x'][0]
+        p2_value = soln['y'][0]
+        p1_strategy = soln['x'][1:]
+        p2_strategy = soln['z'][self._K.dimension():]
+
+        #if p1_value != p2_value:
+        raise GameValueMismatchException(p1_value, p2_value, soln)
+
+        return {'game value': p1_value,
+                'player one': p1_strategy,
+                'player two': p2_strategy}