]>
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': 3.986246886102996e-09}
74 >>> print(GameUnsolvableException(d))
75 Solution failed with result "primal infeasible."
77 dual infeasibility: None
79 dual slack: 8.779496368228267e-10
82 primal infeasibility: None
83 primal objective: None
86 residual as dual infeasibility certificate: None
87 residual as primal infeasibility certificate: 3.986246886102996e-09
89 status: primal infeasible
100 def __init__(self
, solution_dict
):
102 Create a new GameUnsolvableException object.
105 self
._solution
_dict
= solution_dict
110 Return a string representation of this exception.
112 The returned representation highlights the "status" field of the
113 CVXOPT dictionary, since that should explain what went
114 wrong. The full CVXOPT solution dictionary is included after the
117 tpl
= 'Solution failed with result "{:s}."\n' \
118 'CVXOPT returned:\n {!s}'
119 cvx_lines
= _pretty_format_dict(self
._solution
_dict
).splitlines()
120 cvx_str
= '\n '.join(cvx_lines
) # Indent the whole dict by two spaces.
121 return tpl
.format(self
._solution
_dict
['status'], cvx_str
)