]> gitweb.michael.orlitzky.com - dunshire.git/blobdiff - errors.py
More work-in-progress on solutions and their errors.
[dunshire.git] / errors.py
index cc3b57ebc10faefe2a17158ec4ab2743eccc130e..028f5e72ca5a8f223fe1766a95c9de5cf25e3ec9 100644 (file)
--- a/errors.py
+++ b/errors.py
@@ -2,55 +2,55 @@
 Errors that can occur when solving a linear game.
 """
 
-class GameUnsolvableException(Exception):
+from cvxopt import matrix
+
+class GameException(Exception):
     """
-    Every linear game has a solution (this follows from a general
-    min-max theorem). If we can't solve the conic program associated
-    with a linear game, then something is wrong with either the model of
-    the input.
+    The base class for all exceptions that can occur during the solution
+    of a linear game.
     """
-    def __init__(self, solution_dict):
+    def pretty_print_dict(self, solution_dict):
         """
-        Create a new GameUnsolvableException object.
-
-        INPUT:
-
-        - ``primal`` -- the objective value of the primal cone program.
-
-        - ``dual`` -- the objective value of the dual cone program.
-
-        - ``solution_dict`` -- the solution dictionary returned from the cone program.
-
+        Return a pretty-printed string representation of a CVXOPT
+        solution dictionary.
         """
-        tpl = 'solution failed with error: {:s}\n' \
-              'CVXOPT returned:\n{!s}'
-        self.message = tpl.format(solution_dict['status'], solution_dict)
+        result = ''
+        for (k,v) in solution_dict.items():
+            if isinstance(v, matrix):
+                # Try to display vectors as rows on one line.
+                result += '  {:s}: {!s}'.format(k,v.trans())
+            else:
+                result += '  {:s}: {!s}\n'.format(k,v)
 
+        return result
 
-class GameValueMismatchException(Exception):
+
+class GameUnsolvableException(GameException):
     """
-    This error occurs when the primal and dual objective value of the
-    conic program associated with a linear game do not agree. By
-    construction, every conic program derived from a linear game must
-    have a solution and the same objective value (the "value of the
-    game") is shared by both players.
-
-    Each instance of this class will know the two mismatched values, and
-    its ``message`` field will explain why they can't be so.
+    Every linear game has a solution (this follows from a general
+    min-max theorem). If we can't solve the conic program associated
+    with a linear game, then something is wrong with either the model of
+    the input.
     """
-    def __init__(self, primal, dual, solution_dict):
+    def __init__(self, status, solution, solution_dict):
         """
-        Create a new GameValueMismatchException.
+        Create a new GameUnsolvableException object.
 
         INPUT:
 
-        - ``primal`` -- the objective value of the primal cone program.
+        - ``status`` -- the failure status code returned by CVXOPT.
 
-        - ``dual`` -- the objective value of the dual cone program.
+        - ``solution`` -- a Solution object.
 
-        - ``solution_dict`` -- the solution dictionary returned from the cone program.
+        - ``solution_dict`` -- the solution dictionary returned from the
+          cone program.
 
         """
-        tpl = 'game value mismatch for player1={:.7f}, player2={:.7f}\n' \
+        tpl = 'Solution failed with error "{:s}".\n' \
+              '{!s}\n' \
               'CVXOPT returned:\n{!s}'
-        self.message = tpl.format(primal, dual, solution_dict)
+              # TODO: dont convert the solution to a string, we need
+              # to output the two values as well.
+        self.message = tpl.format(status,
+                                  solution,
+                                  self.pretty_print_dict(solution_dict))