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__":
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='')
get_ipython().magic('timeit %s' % instr)
print("\nSparse solvers\n--------------")
for solver, instr in sparse_instr.items():
[1., 2., 1.],
[2., 0., 1.],
[-1., 2., -1.]])
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)
def check_same_solutions(tol=0.05):
sol0 = solve_qp(P, q, G, h, solver=sparse_solvers[0])
for solver in sparse_solvers:
sol = solve_qp(P, q, G, h, solver=solver)
relvar = norm(sol - sol0) / norm(sol0)
assert relvar < tol, "%s's solution offset by %.1f%%" % (
solver, 100. * relvar)
for solver in dense_solvers:
sol = solve_qp(P_array, q, G_array, h, solver=solver)
relvar = norm(sol - sol0) / norm(sol0)
assert relvar < tol, "%s's solution offset by %.1f%%" % (
solver, 100. * relvar)