Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
m variables, 2m+1 constraints (1 equation, 2m inequations).
:param X: array of size [n_samples, n_features] holding the training samples
:param y: array of size [n_samples] holding the class labels
"""
#
m = len(y) # m = n_samples
K = self.kernel(X) # gram matrix
P = K * np.outer(y, y)
q = -np.ones(m)
G = np.vstack((-np.identity(m), np.identity(m)))
h = np.hstack((np.zeros(m), np.ones(m) * self.C))
A = y.reshape((1, -1))
b = np.zeros(1)
# make sure P is positive definite
P += np.eye(P.shape[0]).__mul__(1e-3)
self.alphas = solve_qp(P, q, G, h, A, b, sym_proj=True)
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():
print("%s: " % solver, end='')
get_ipython().magic('timeit %s' % instr)
def _optimize(self, digraph, v_origin):
solver_matrices, variables = self.get_matrices(digraph, v_origin)
P = solver_matrices.P
q = solver_matrices.q
G = solver_matrices.G
h = solver_matrices.h
A = solver_matrices.A
b = solver_matrices.b
# PAG = np.concatenate([P, A, G])
# print('rank(A)', np.linalg.matrix_rank(A))
# print('shape(A)', A.shape)
# print('rank([P; A; G])', np.linalg.matrix_rank(PAG))
# print('shape([P; A; G])', PAG.shape)
x = solve_qp(P, q, G, h, A, b, solver=self.solver)
return x, variables
#
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)
a0 = dot(H_lambda(s0), n0)
b0 = dot(H_mu(s0), n0)
c0 = dot(H_cst(s0) - p0, n0)
h0 = takeoff_clearance
# a0 * lambda + b0 * mu + c0 >= h0
s1 = 3. / 4
a1 = dot(H_lambda(s1), n1)
b1 = dot(H_mu(s1), n1)
c1 = dot(H_cst(s1) - p1, n1)
h1 = landing_clearance
# a1 * lambda + b1 * mu + c1 >= h1
P = eye(2)
q = zeros(2)
G = array([[-a0, -b0], [-a1, -b1]])
h = array([c0 - h0, c1 - h1])
x = solve_qp(P, q, G, h)
# H = lambda s: H_lambda(s) * x[0] + H_mu(s) * x[1] + H_cst(s)
path = interpolate_cubic_hermite(p0, x[0] * n0, p1, x[1] * n1)
return path
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)
def solve_random_qp(n, solver):
M, b = random.random((n, n)), random.random(n)
P, q = dot(M.T, M), dot(b, M).reshape((n,))
G = toeplitz([1., 0., 0.] + [0.] * (n - 3), [1., 2., 3.] + [0.] * (n - 3))
h = ones(n)
return solve_qp(P, q, G, h, solver=solver)