Giter Site home page Giter Site logo

osqp-eigen-bazel's Introduction

osqp-eigen-bazel

LICENSE Build Status Build Status

The osqp-cpp is built by bazel, which is easy to use osqp for novice.

We show below how to solve the problem in C++.

// osqp-eigen
#include "ThirdParty/osqp_eigen/OsqpEigen/OsqpEigen.h"

using namespace Eigen;
using namespace OsqpEigen;

int main()
{
    /*
     * @min_x 0.5 * x'Px + q'x
     * @s.t.  l <= Ax <= u
     * @objective_matrix: p
     * @objective_vector: q
     * @constraint_matrix: A
     * @lower_bounds: l
     * @upper_bounds: u
     */

    // Define problem data
    // P
    Eigen::SparseMatrix<double> P(2, 2);
    const Eigen::Triplet<double> p[] = {{0, 0, 4},
                                        {0, 1, 1},
                                        {1, 0, 1},
                                        {1, 1, 2}};
    P.setFromTriplets(std::begin(p),
                      std::end(p));

    // q
    Eigen::VectorXd q(2);
    q << 1, 1;

    // A
    Eigen::SparseMatrix<double> A(3, 2);
    const Eigen::Triplet<double> a[] = {{0, 0, 1},
                                        {0, 1, 1},
                                        {1, 0, 1},
                                        {1, 1, 0},
                                        {2, 0, 0},
                                        {2, 1, 1}};
    A.setFromTriplets(std::begin(a),
                      std::end(a));

    // l and u
    Eigen::VectorXd l(3);
    Eigen::VectorXd u(3);
    l << 1, 0, 0;
    u << 1, 0.7, 0.7;

    // instantiate the solver
    OsqpEigen::Solver solver;

    // settings
    // solver.settings()->setVerbosity(false);
    // solver.settings()->setWarmStart(true);
    solver.settings()->setAbsoluteTolerance(1e-04);
    solver.settings()->setMaxIteration(1e4);

    // set the initial data of the QP solver
    solver.data()->setNumberOfVariables(2);   // col of A
    solver.data()->setNumberOfConstraints(3); // row of A
    solver.data()->setHessianMatrix(P);
    solver.data()->setGradient(q);
    solver.data()->setLinearConstraintsMatrix(A);
    solver.data()->setLowerBound(l);
    solver.data()->setUpperBound(u);

    // instantiate the solver
    solver.initSolver();

    // solve the QP problem
    solver.solve();

    // get solution
    Eigen::VectorXd QPSolution;
    QPSolution = solver.getSolution();
    std::cout << "QPSolution: " << std::endl
              << QPSolution << std::endl;
    return 0;
}

Tutorial

  1. Set bazel sync.(Need to install bazel before)
bazel sync
  1. Build the code.
bazel build //:example_osqp_eigen
  1. Run the code.
bazel run //:example_osqp_eigen

Terminal print like this

-----------------------------------------------------------------
           OSQP v0.0.0  -  Operator Splitting QP Solver
              (c) Bartolomeo Stellato,  Goran Banjac
        University of Oxford  -  Stanford University 2021
-----------------------------------------------------------------
problem:  variables n = 2, constraints m = 3
          nnz(P) + nnz(A) = 9
settings: linear system solver = qdldl,
          eps_abs = 1.0e-04, eps_rel = 1.0e-03,
          eps_prim_inf = 1.0e-04, eps_dual_inf = 1.0e-04,
          rho = 1.00e-01 (adaptive),
          sigma = 1.00e-06, alpha = 1.60, max_iter = 10000
          check_termination: on (interval 25),
          scaling: on, scaled_termination: off
          warm start: on, polish: off, time_limit: off

iter   objective    pri res    dua res    rho        time
   1  -7.8808e-03   1.01e+00   2.00e+02   1.00e-01   1.68e-05s
  50   1.8800e+00   1.90e-04   3.80e-06   1.00e-01   2.93e-05s

status:               solved
number of iterations: 50
optimal objective:    1.8800
run time:             3.25e-05s
optimal rho estimate: 1.38e+00

QPSolution: 
0.30019
0.69981

Link

osqp-eigen-bazel's People

Contributors

yanghq13 avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.