]> gitweb.michael.orlitzky.com - dunshire.git/blob - src/dunshire/errors.py
Fix lint warnings and a few variable errors in symmetric_linear_game.py.
[dunshire.git] / src / dunshire / errors.py
1 """
2 Errors that can occur when solving a linear game.
3 """
4
5 from cvxopt import matrix
6
7
8 def _pretty_print_dict(dictionary):
9 """
10 Return a pretty-printed string representation of a dictionary
11 containing CVXOPT matrices.
12 """
13 result = ''
14 for (key, value) in dictionary.items():
15 if isinstance(value, matrix):
16 # Display matrices on their own lines, indented.
17 result += ' {:s}:'.format(key)
18 colvec = '\n{!s}'.format(value)
19 result += '\n '.join(colvec.splitlines())
20 result += '\n'
21 else:
22 result += ' {:s}: {!s}\n'.format(key, value)
23
24 return result
25
26
27 class GameUnsolvableException(Exception):
28 """
29 Every linear game has a solution (this follows from a general
30 min-max theorem). If we can't solve the conic program associated
31 with a linear game, then something is wrong with either the model of
32 the input.
33 """
34 def __init__(self, solution_dict):
35 """
36 Create a new GameUnsolvableException object.
37
38 INPUT:
39
40 - ``solution_dict`` -- the solution dictionary returned from the
41 cone program.
42
43 """
44 super().__init__()
45 self._solution_dict = solution_dict
46
47
48 def __str__(self):
49 tpl = 'Solution failed with result "{:s}."\n' \
50 'CVXOPT returned:\n{!s}'
51 return tpl.format(self._solution_dict['status'],
52 _pretty_print_dict(self._solution_dict))