jarredbarber / eigen-qp Goto Github PK
View Code? Open in Web Editor NEWQuadradic Programming solver using Eigen
License: MIT License
Quadradic Programming solver using Eigen
License: MIT License
Hi.
Thanks for great library.
I start to use this library, but have trouble.
I want to solve least square Ax=b under constraint Ex โฆ (1,1,1....), all variables are under 1.
Code is following.
void test()
{
// construct Ax = b
const int32_t numSample = 128;
Eigen::SparseMatrix<double> A;
Eigen::VectorXd b;
A.resize(numSample,3);
b.resize(numSample);
for(int32_t sn=0;sn<numSample;++sn)
{
// something funtion
const float x = float(sn)/float(numSample);
const float y = 2.5f * x * x + 2.4f * x + 2.3f;
//
A.insert(sn,0) = x * x;
A.insert(sn,1) = x;
A.insert(sn,2) = 1.0f;
b(sn) = y;
}
A.makeCompressed();
// Make constraint. All coeffs are under 1. (Ex<I)
// https://scicomp.stackexchange.com/a/11588
Eigen::MatrixXd Q = A.transpose() * A;
Eigen::VectorXd c = -A.transpose() * b;
Eigen::MatrixXd A2;
Eigen::VectorXd b2;
A2.resize(3,3);
b2.resize(3);
A2.setIdentity();
b2.setOnes();
//
Eigen::VectorXd x;
x.resize(3);
EigenQP::quadprog(Q, c, A2, b2, x);
std::cout << x << std::endl;
}
I get x as (2.5,2.4,2.3). But this is not constraint.
And "b2.setOnes();" rewrite to "b2 << 10.0,10.0,10.0;", I got x as (0.106648, 0.355828, 0.216527).
Totally bad results, I might did bad operation, bud I can't figure out it.
Thanks.
I have attached a minimal example that demonstrates issues with quadprog.
The solution to this QP should be equal to the value of c, but quadprog sets x to nan.
#include "eigen-qp.hpp"
#include <Eigen/Core>
#include <iostream>
using namespace std;
int main(int argc, char **argv){
Eigen::Matrix<double, 2,1> x;
Eigen::Matrix<double, 2,2> Q = 2*Eigen::Matrix<double, 2,2>::Identity();
Eigen::Matrix<double, 2,1> c = 2*Eigen::Matrix<double, 2,1>::Random();
Eigen::Matrix<double, 2,2> A; A << 0,0,0,0;
Eigen::Matrix<double, 2,1> b; b << 1.0,1.0;
EigenQP::quadprog(Q,c,A,b,x);
cout << x << endl;
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.