]> gitweb.michael.orlitzky.com - dunshire.git/blob - errors.py
028f5e72ca5a8f223fe1766a95c9de5cf25e3ec9
[dunshire.git] / errors.py
1 """
2 Errors that can occur when solving a linear game.
3 """
4
5 from cvxopt import matrix
6
7 class GameException(Exception):
8 """
9 The base class for all exceptions that can occur during the solution
10 of a linear game.
11 """
12 def pretty_print_dict(self, solution_dict):
13 """
14 Return a pretty-printed string representation of a CVXOPT
15 solution dictionary.
16 """
17 result = ''
18 for (k,v) in solution_dict.items():
19 if isinstance(v, matrix):
20 # Try to display vectors as rows on one line.
21 result += ' {:s}: {!s}'.format(k,v.trans())
22 else:
23 result += ' {:s}: {!s}\n'.format(k,v)
24
25 return result
26
27
28 class GameUnsolvableException(GameException):
29 """
30 Every linear game has a solution (this follows from a general
31 min-max theorem). If we can't solve the conic program associated
32 with a linear game, then something is wrong with either the model of
33 the input.
34 """
35 def __init__(self, status, solution, solution_dict):
36 """
37 Create a new GameUnsolvableException object.
38
39 INPUT:
40
41 - ``status`` -- the failure status code returned by CVXOPT.
42
43 - ``solution`` -- a Solution object.
44
45 - ``solution_dict`` -- the solution dictionary returned from the
46 cone program.
47
48 """
49 tpl = 'Solution failed with error "{:s}".\n' \
50 '{!s}\n' \
51 'CVXOPT returned:\n{!s}'
52 # TODO: dont convert the solution to a string, we need
53 # to output the two values as well.
54 self.message = tpl.format(status,
55 solution,
56 self.pretty_print_dict(solution_dict))