Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
h = array([3., 2., -2.]).reshape((3,))
P_csc = csc_matrix(P)
G_csc = csc_matrix(G)
if __name__ == "__main__":
if get_ipython() is None:
print("Usage: ipython -i %s" % basename(__file__))
exit()
dense_instr = {
solver: "u = solve_qp(P, q, G, h, solver='%s')" % solver
for solver in dense_solvers}
sparse_instr = {
solver: "u = solve_qp(P_csc, q, G_csc, h, solver='%s')" % solver
for solver in sparse_solvers}
print("\nTesting all QP solvers on a dense quadratic program...")
sol0 = solve_qp(P, q, G, h, solver=dense_solvers[0])
for solver in dense_solvers:
sol = solve_qp(P, q, G, h, solver=solver)
delta = norm(sol - sol0)
assert delta < 1e-4, "%s's solution offset by %.1e" % (solver, delta)
for solver in sparse_solvers:
sol = solve_qp(P_csc, q, G_csc, h, solver=solver)
delta = norm(sol - sol0)
assert delta < 1e-4, "%s's solution offset by %.1e" % (solver, delta)
print("\nDense solvers\n-------------")
for solver, instr in dense_instr.items():
print("%s: " % solver, end='')
def time_sparse_solvers():
instructions = {
solver: "u = solve_qp(P, q, G, h, solver='%s')" % solver
for solver in sparse_solvers}
print("\nSparse solvers\n--------------")
for solver, instr in instructions.items():
print("%s: " % solver, end='')
get_ipython().magic('timeit %s' % instr)