Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic usage #3

Open
ykozw opened this issue Feb 8, 2018 · 0 comments
Open

Basic usage #3

ykozw opened this issue Feb 8, 2018 · 0 comments

Comments

@ykozw
Copy link

ykozw commented Feb 8, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant