]>
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.
17 >>> d = {'foo': 1.234, 'bar': matrix([1,2,3])}
18 >>> print(_pretty_format_dict(d))
27 for (key
, value
) in sorted(dictionary
.items()):
28 if isinstance(value
, matrix
):
29 # Display matrices on their own lines, indented.
30 result
+= '{:s}:'.format(key
)
31 colvec
= '\n{!s}'.format(value
)
32 result
+= '\n '.join(colvec
.splitlines())
35 result
+= '{:s}: {!s}\n'.format(key
, value
)
37 return result
.rstrip('\n') # Kills trailing newlines on matrices.
40 class GameUnsolvableException(Exception):
42 Every linear game has a solution (this follows from a general
43 min-max theorem). If we can't solve the conic program associated
44 with a linear game, then something is wrong with either the model of
49 >>> d = {'residual as dual infeasibility certificate': None,
50 ... 'y': matrix([1,1]),
51 ... 'dual slack': 8.779496368228267e-10,
52 ... 'z': matrix([1,1,0,0]),
54 ... 'primal infeasibility': None,
55 ... 'status': 'primal infeasible',
56 ... 'dual infeasibility': None,
57 ... 'relative gap': None,
59 ... 'primal slack': None,
61 ... 'dual objective': 1.0,
62 ... 'primal objective': None,
64 ... 'residual as primal infeasibility certificate': 3.986246886102996e-09}
65 >>> print(GameUnsolvableException(d))
66 Solution failed with result "primal infeasible."
68 dual infeasibility: None
70 dual slack: 8.779496368228267e-10
73 primal infeasibility: None
74 primal objective: None
77 residual as dual infeasibility certificate: None
78 residual as primal infeasibility certificate: 3.986246886102996e-09
80 status: primal infeasible
91 def __init__(self
, solution_dict
):
93 Create a new GameUnsolvableException object.
97 - ``solution_dict`` -- the solution dictionary returned from the
102 self
._solution
_dict
= solution_dict
107 Return a string representation of this exception.
109 The returned representation highlights the "status" field of the
110 CVXOPT dictionary, since that should explain what went
111 wrong. The full CVXOPT solution dictionary is included after the
114 tpl
= 'Solution failed with result "{:s}."\n' \
115 'CVXOPT returned:\n {!s}'
116 cvx_lines
= _pretty_format_dict(self
._solution
_dict
).splitlines()
117 cvx_str
= '\n '.join(cvx_lines
) # Indent the whole dict by two spaces.
118 return tpl
.format(self
._solution
_dict
['status'], cvx_str
)