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