How to use qpsolvers - 10 common examples

To help you get started, we’ve selected a few qpsolvers examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github stephane-caron / qpsolvers / examples / test_solvers.py View on Github external
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)
github stephane-caron / qpsolvers / examples / dense_problem.py View on Github external
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():
github stephane-caron / qpsolvers / examples / random_problems.py View on Github external
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)
github stephane-caron / qpsolvers / examples / dense_problem.py View on Github external
[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)
github stephane-caron / qpsolvers / examples / dense_problem.py View on Github external
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='')
github aimacode / aima-python / learning4e.py View on Github external
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)
github stephane-caron / qpsolvers / examples / dense_problem.py View on Github external
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)
github lens-biophotonics / ZetaStitcher / zetastitcher / gaussian_stitcher / qp / stitching.py View on Github external
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
#
github stephane-caron / qpsolvers / examples / sparse_problem.py View on Github external
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)
github stephane-caron / pymanoid / pymanoid / swing_foot.py View on Github external
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

qpsolvers

Quadratic programming solvers in Python with a unified API.

LGPL-3.0
Latest version published 2 months ago

Package Health Score

79 / 100
Full package analysis

Similar packages