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