""" Errors that can occur when solving a linear game. """ from cvxopt import matrix class GameException(Exception): """ The base class for all exceptions that can occur during the solution of a linear game. """ def pretty_print_dict(self, solution_dict): """ Return a pretty-printed string representation of a CVXOPT solution dictionary. """ result = '' for (k,v) in solution_dict.items(): if isinstance(v, matrix): # Try to display vectors as rows on one line. result += ' {:s}: {!s}'.format(k,v.trans()) else: result += ' {:s}: {!s}\n'.format(k,v) return result class GameUnsolvableException(GameException): """ Every linear game has a solution (this follows from a general min-max theorem). If we can't solve the conic program associated with a linear game, then something is wrong with either the model of the input. """ def __init__(self, status, solution, solution_dict): """ Create a new GameUnsolvableException object. INPUT: - ``status`` -- the failure status code returned by CVXOPT. - ``solution`` -- a Solution object. - ``solution_dict`` -- the solution dictionary returned from the cone program. """ tpl = 'Solution failed with error "{:s}".\n' \ '{!s}\n' \ 'CVXOPT returned:\n{!s}' # TODO: dont convert the solution to a string, we need # to output the two values as well. self.message = tpl.format(status, solution, self.pretty_print_dict(solution_dict))