]> gitweb.michael.orlitzky.com - dunshire.git/blobdiff - errors.py
Play with the error/solution formatting some more.
[dunshire.git] / errors.py
index 028f5e72ca5a8f223fe1766a95c9de5cf25e3ec9..f46c4a46b6cdf46d7e1ba37e29246163bb78a692 100644 (file)
--- a/errors.py
+++ b/errors.py
@@ -4,53 +4,48 @@ Errors that can occur when solving a linear game.
 
 from cvxopt import matrix
 
-class GameException(Exception):
-    """
-    The base class for all exceptions that can occur during the solution
-    of a linear game.
-    """
-    def pretty_print_dict(self, solution_dict):
-        """
-        Return a pretty-printed string representation of a CVXOPT
-        solution dictionary.
-        """
-        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 GameUnsolvableException(GameException):
+class GameUnsolvableException(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.
     """
-    def __init__(self, status, solution, solution_dict):
+    def __str__(self):
+        tpl = 'Solution failed with result "{:s}."\n' \
+              'CVXOPT returned:\n{!s}'
+        return tpl.format(self._solution_dict['status'],
+                          self._pretty_print_dict(self._solution_dict))
+
+
+    def __init__(self, solution_dict):
         """
         Create a new GameUnsolvableException object.
 
         INPUT:
 
-        - ``status`` -- the failure status code returned by CVXOPT.
-
-        - ``solution`` -- a Solution object.
-
         - ``solution_dict`` -- the solution dictionary returned from the
           cone program.
 
         """
-        tpl = 'Solution failed with error "{:s}".\n' \
-              '{!s}\n' \
-              'CVXOPT returned:\n{!s}'
-              # 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))
+        self._solution_dict = solution_dict
+
+
+    def _pretty_print_dict(self, solution_dict):
+        """
+        Return a pretty-printed string representation of a CVXOPT
+        solution dictionary.
+        """
+        result = ''
+        for (k,v) in solution_dict.items():
+            if isinstance(v, matrix):
+                # Display matrices on their own lines, indented.
+                result += '  {:s}:'.format(k)
+                colvec = '\n{!s}'.format(v)
+                result += '\n    '.join(colvec.splitlines())
+                result += '\n'
+            else:
+                result += '  {:s}: {!s}\n'.format(k,v)
+
+        return result