]>
gitweb.michael.orlitzky.com - dunshire.git/blob - src/dunshire/errors.py
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.
13 The dictionary is also sorted so that it can be tested repeatably.
18 >>> d = {'foo': 1.234, 'bar': matrix([1,2,3])}
19 >>> print(_pretty_format_dict(d))
28 for (key
, value
) in sorted(dictionary
.items()):
29 if isinstance(value
, matrix
):
30 # Display matrices on their own lines, indented.
31 result
+= '{:s}:'.format(key
)
32 colvec
= '\n{!s}'.format(value
)
33 result
+= '\n '.join(colvec
.splitlines())
36 result
+= '{:s}: {!s}\n'.format(key
, value
)
38 return result
.rstrip('\n') # Kills trailing newlines on matrices.
41 class GameUnsolvableException(Exception):
43 An exception raised when a game cannot be solved.
45 Every linear game has a solution. If we can't solve the conic
46 program associated with a linear game, then something is wrong with
47 either the model or the input, and this exception should be raised.
53 The solution dictionary returned from the failed cone program.
58 >>> d = {'residual as dual infeasibility certificate': None,
59 ... 'y': matrix([1,1]),
60 ... 'dual slack': 8.779496368228267e-10,
61 ... 'z': matrix([1,1,0,0]),
63 ... 'primal infeasibility': None,
64 ... 'status': 'primal infeasible',
65 ... 'dual infeasibility': None,
66 ... 'relative gap': None,
68 ... 'primal slack': None,
70 ... 'dual objective': 1.0,
71 ... 'primal objective': None,
73 ... 'residual as primal infeasibility certificate':
74 ... 3.986246886102996e-09}
75 >>> print(GameUnsolvableException(d))
76 Solution failed with result "primal infeasible."
78 dual infeasibility: None
80 dual slack: 8.779496368228267e-10
83 primal infeasibility: None
84 primal objective: None
87 residual as dual infeasibility certificate: None
88 residual as primal infeasibility certificate: 3.986246886102996e-09
90 status: primal infeasible
101 def __init__(self
, solution_dict
):
103 Create a new GameUnsolvableException object.
106 self
._solution
_dict
= solution_dict
111 Return a string representation of this exception.
113 The returned representation highlights the "status" field of the
114 CVXOPT dictionary, since that should explain what went
115 wrong. The full CVXOPT solution dictionary is included after the
118 tpl
= 'Solution failed with result "{:s}."\n' \
119 'CVXOPT returned:\n {!s}'
120 cvx_lines
= _pretty_format_dict(self
._solution
_dict
).splitlines()
122 # Indent the whole dict by two spaces.
123 cvx_str
= '\n '.join(cvx_lines
)
125 return tpl
.format(self
._solution
_dict
['status'], cvx_str
)