]>
gitweb.michael.orlitzky.com - dunshire.git/blob - src/dunshire/errors.py
3a5b601cec9ca0a2753e16ede6460a2ac6ca101a
2 Errors that can occur when solving a linear game.
5 from cvxopt
import matrix
8 def _pretty_format_dict(dictionary
):
10 Return a pretty-formatted string representation of a dictionary
11 containing CVXOPT matrices.
15 >>> d = {'foo': 1.234, 'bar': matrix([1,2,3])}
16 >>> print(_pretty_format_dict(d))
25 for (key
, value
) in dictionary
.items():
26 if isinstance(value
, matrix
):
27 # Display matrices on their own lines, indented.
28 result
+= '{:s}:'.format(key
)
29 colvec
= '\n{!s}'.format(value
)
30 result
+= '\n '.join(colvec
.splitlines())
33 result
+= '{:s}: {!s}\n'.format(key
, value
)
35 return result
.rstrip('\n') # Kills trailing newlines on matrices.
38 class GameUnsolvableException(Exception):
40 Every linear game has a solution (this follows from a general
41 min-max theorem). If we can't solve the conic program associated
42 with a linear game, then something is wrong with either the model of
45 def __init__(self
, solution_dict
):
47 Create a new GameUnsolvableException object.
51 - ``solution_dict`` -- the solution dictionary returned from the
56 self
._solution
_dict
= solution_dict
61 Return a string representation of this exception.
63 The returned representation highlights the "status" field of the
64 CVXOPT dictionary, since that should explain what went
65 wrong. The full CVXOPT solution dictionary is included after the
68 tpl
= 'Solution failed with result "{:s}."\n' \
69 'CVXOPT returned:\n {!s}'
70 cvx_lines
= _pretty_format_dict(self
._solution
_dict
).splitlines()
71 cvx_str
= '\n '.join(cvx_lines
) # Indent the whole dict by two spaces.
72 return tpl
.format(self
._solution
_dict
['status'], cvx_str
)