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
with a linear game, then something is wrong with either the model of
the input.
"""
- 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.
cone program.
"""
+ super().__init__()
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
+ 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))