]> gitweb.michael.orlitzky.com - dunshire.git/commitdiff
Simplify the model a bit.
authorMichael Orlitzky <michael@orlitzky.com>
Wed, 5 Oct 2016 02:11:48 +0000 (22:11 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Wed, 5 Oct 2016 02:11:48 +0000 (22:11 -0400)
matrices.py
symmetric_linear_game.py

index 01ab35274ed2157cac71af13dd84b0fdf3e4dfd4..82944ee72ab77ca676054601991afadcd804ae77 100644 (file)
@@ -6,21 +6,6 @@ def append_col(A,b):
     """
     return matrix([A.trans(),b.trans()]).trans()
 
-def append_cols(cols):
-    """
-    Append a bunch of columns together, left to right.
-    """
-    if len(cols) == 0:
-        return cols
-
-    result = cols[0]
-    del(cols[0])
-    for column in cols:
-        result = append_col(result, column)
-
-    return result
-
-
 def append_row(A,b):
     """
     Append the row ``b`` to the bottom of the matrix ``A``.
index 09b63aaf1da6e2057fbb30e613955bd4ee47907c..a8cdabc71dac1f39d5578df00bab13e157de0104 100644 (file)
@@ -1,7 +1,10 @@
 from cvxopt import matrix, printing, solvers
 
-from cones import CartesianProduct, NonnegativeOrthant
-from matrices import append_cols, append_row, identity
+from cones import CartesianProduct
+from matrices import append_col, append_row, identity
+
+printing.options['dformat'] = '%.7f'
+solvers.options['show_progress'] = False
 
 class SymmetricLinearGame:
     """
@@ -39,64 +42,39 @@ class SymmetricLinearGame:
 
         """
         self._K  = K
-        self._C = CartesianProduct(NonnegativeOrthant(2), K, K)
+        self._C = CartesianProduct(K, K)
         n = self._K.dimension()
         self._L  = matrix(L,  (n,n))
         self._e1 = matrix(e1, (n,1)) # TODO: check that e1 and e2
         self._e2 = matrix(e2, (n,1)) # are in the interior of K...
-        self._h  = matrix(0,  (self._C.dimension(),1), 'd')
+        self._h  = matrix(0,  (2*n,1), 'd')
         self._b = matrix(1,   (1,1), 'd')
-        self._c = matrix([-1,1] + ([0]*n), (n+2,1), 'd')
-        self._G = append_row(-identity(n+2),
-                             append_cols([self._e1, -self._e1, -self._L]))
-        self._A = matrix([0,0] + e1, (1, n+2), 'd')
-
-    def e1(self):
-        return self._e1
-
-    def e2(self):
-        return self._e2
-
-    def L(self):
-        return self._L
-
-    def h(self):
-        return self._h
-
-    def b(self):
-        return self._b
-
-    def c(self):
-        return self._c
-
-    def G(self):
-        return self._G
-
-    def A(self):
-        return self._A
-
-    def C(self):
-        return self._C
+        self._c = matrix([-1] + [0]*n, (n+1,1), 'd')
+        self._G = append_row(append_col(matrix(0,(n,1)), -identity(n)),
+                             append_col(self._e1, -self._L))
+        self._A = matrix([0] + e1, (1, n+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):
-        solvers.options['show_progress'] = False
-        soln = solvers.conelp(self.c(),
-                              self.G(),
-                              self.h(),
-                              self.C().cvxopt_dims(),
-                              self.A(),
-                              self.b())
-
-        printing.options['dformat'] = '%.7f'
-        value = soln['x'][0] - soln['x'][1]
-        print('Value of the game: {:f}'.format(value))
-
-        opt1 = soln['x'][2:]
+        soln = self.solution()
+        nu = soln['x'][0]
+        print('Value of the game (player one): {:f}'.format(nu))
+
+        opt1 = soln['x'][1:]
         print('Optimal strategy (player one):')
         print(opt1)
 
-        #opt2 = soln['y'][2:]
-        #print('Optimal strategy (player two):')
-        #print(opt2)
-
-        return soln
+        omega = soln['y'][0]
+        n = self._K.dimension()
+        opt2 = soln['z'][n:]
+        print('Value of the game (player two): {:f}'.format(omega))
+        print('Optimal strategy (player two):')
+        print(opt2)