Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if __name__ == "__main__":
cases = [
{'P': P, 'q': q},
{'P': P, 'q': q, 'G': G, 'h': h},
{'P': P, 'q': q, 'A': A, 'b': b},
{'P': P, 'q': q, 'G': G[0], 'h': h0},
{'P': P, 'q': q, 'A': A[0], 'b': b0},
{'P': P, 'q': q, 'G': G, 'h': h, 'A': A, 'b': b},
{'P': P, 'q': q, 'G': G[0], 'h': h0, 'A': A, 'b': b},
{'P': P, 'q': q, 'G': G, 'h': h, 'A': A[0], 'b': b0},
{'P': P, 'q': q, 'G': G[0], 'h': h0, 'A': A[0], 'b': b0},
]
for (i, case) in enumerate(cases):
print("\nTest %1d\n======\n" % i)
expected_sol = solve_qp(solver=available_solvers[0], **case)
for solver in available_solvers:
sol = solve_qp(solver=solver, **case)
delta = norm(sol - expected_sol)
print("%9s's solution: %s" % (solver, sol.round(decimals=5)))
assert delta < 1e-4, \
"%s's solution offset by %.1e on test #%d" % (solver, delta, i)
plot(sizes, perfs[solver], lw=2, color=colors[solver])
grid(True)
legend(list(perfs.keys()), loc='lower right')
xscale('log')
yscale('log')
for solver in perfs:
plot(sizes, perfs[solver], marker='o', color=colors[solver])
if __name__ == "__main__":
if get_ipython() is None:
print("Usage: ipython -i %s" % basename(__file__))
exit()
perfs = {}
print("\nTesting all QP solvers on a random quadratic programs...\n")
for solver in available_solvers:
try:
perfs[solver] = []
for size in sizes:
print("Running %s on problem size %d..." % (solver, size))
cum_time = timeit(
stmt="solve_random_qp(%d, '%s')" % (size, solver),
setup="from __main__ import solve_random_qp",
number=nb_iter)
perfs[solver].append(cum_time / nb_iter)
except Exception as e:
print("Warning: %s" % str(e))
if solver in perfs:
del perfs[solver]
plot_results(perfs)
def __init__(self, method='quadprog', *args, **kwargs):
"""
Initialize the QP solver.
Args:
method (str): QP method/library to use. Select between ['cvxopt', 'cvxpy', 'ecos', 'gurobi', 'mosek',
'osqp', 'qpoases', 'quadprog']
"""
super(QP, self).__init__(*args, **kwargs)
solvers = set(qpsolvers.available_solvers)
if len(solvers) == 0:
raise ValueError("No QP solvers have been found on this computer. Please install one of the QP modules")
if method not in solvers:
method = 'quadprog'
self.method = method
# check methods that require a symmetric matrix for P
methods = ['cvxopt', 'osqp', 'quadprog']
self.sym_proj = True if self.method in set(methods) else False