]> gitweb.michael.orlitzky.com - dunshire.git/blobdiff - src/dunshire/errors.py
Reorganize the source under src/dunshire.
[dunshire.git] / src / dunshire / errors.py
diff --git a/src/dunshire/errors.py b/src/dunshire/errors.py
new file mode 100644 (file)
index 0000000..bb52cac
--- /dev/null
@@ -0,0 +1,52 @@
+"""
+Errors that can occur when solving a linear game.
+"""
+
+from cvxopt import matrix
+
+
+def _pretty_print_dict(dictionary):
+    """
+    Return a pretty-printed string representation of a dictionary
+    containing CVXOPT matrices.
+    """
+    result = ''
+    for (key, value) in dictionary.items():
+        if isinstance(value, matrix):
+            # Display matrices on their own lines, indented.
+            result += '  {:s}:'.format(key)
+            colvec = '\n{!s}'.format(value)
+            result += '\n    '.join(colvec.splitlines())
+            result += '\n'
+        else:
+            result += '  {:s}: {!s}\n'.format(key, value)
+
+    return result
+
+
+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, solution_dict):
+        """
+        Create a new GameUnsolvableException object.
+
+        INPUT:
+
+        - ``solution_dict`` -- the solution dictionary returned from the
+          cone program.
+
+        """
+        super().__init__()
+        self._solution_dict = solution_dict
+
+
+    def __str__(self):
+        tpl = 'Solution failed with result "{:s}."\n' \
+              'CVXOPT returned:\n{!s}'
+        return tpl.format(self._solution_dict['status'],
+                          _pretty_print_dict(self._solution_dict))