cgal icon indicating copy to clipboard operation
cgal copied to clipboard

Cannot solve quadratic programming problem with QP solver.

Open EdXian opened this issue 8 years ago • 3 comments

Hi all, I want solve the following problem: min 1/2 *X_transpose D X s.t AX=b However , the qp solver always says the problem is infeasible.

qp=new Program(CGAL::EQUAL, false, 0, false, 0);
double t =1 ;

double b0 = 24,
       b1 = 120,
       b2 = 360,
       b3 = 840;

Eigen::MatrixXd D(8,8);
Eigen::MatrixXd d(4,4);
Eigen::MatrixXd A(8,8);
Eigen::MatrixXd B(8,1);


D.setZero();
d.setZero();
A.setZero();
B.setZero();
double d11 = b0*b0*t        , d12 = b0*b1*t*t        , d13 = b0*b2*t*t*t         , d14 = b0*b3*t*t*t*t ;
double d21 = b0*b1*t*t      , d22 = b1*b1*t*t*t      , d23 = b1*b2*t*t*t*t       , d24 = b1*b3*t*t*t*t*t ;
double d31 = b0*b2*t*t*t    , d32 = b2*b1*t*t*t*t    , d33 = b2*b2*t*t*t*t*t     , d34 = b2*b3*t*t*t*t*t*t;
double d41 = b3*b0*t*t*t*t  , d42 = b3*b1*t*t*t*t*t  , d43 = b3*b2*t*t*t*t*t*t   , d44 = b3*b3*t*t*t*t*t*t*t ;


d << (1/1.0)*d11*1.0 , (1/2.0)*d21*1.0 ,(1/3.0)*d31*1.0  ,      (1/4.0)*d41*1.0     ,
     (1/2.0)*d21*1.0 , (1/3.0)*d22*1.0 ,(1/4.0)*d32*1.0  ,      (1/5.0)*d42*1.0     ,
     (1/3.0)*d31*1.0 , (1/4.0)*d32*1.0 , (1/5.0)*d33*1.0 ,     (1/6.0)*d43*1.0    ,
     (1/4.0)*d41*1.0 , (1/5.0)*d42*1.0 , (1/6.0)*d43*1.0 , (1/7.0)*d44*1.0 ;

    D.block<4,4>(4,4) = d;



     A<< 1 ,  0  ,   0  ,  0  , 0 , 0 , 0  , 0 ,
         0 ,  1  ,   0  ,  0  , 0 , 0 , 0  , 0 ,
         0 ,  0  ,   2  ,  0  , 0 , 0 , 0  , 0 ,
         0 ,  0  ,   0  ,  6  , 0 , 0 , 0  , 0 ,
         1 , 1*t , 1*t*t , 1*t*t*t , 1*t*t*t*t , 1*t*t*t*t*t , 1*t*t*t*t*t*t , 1*t*t*t*t*t*t*t,
         0 ,  1  ,   2*t , 3*t*t   , 4*t*t*t   , 5*t*t*t*t   , 6*t*t*t*t*t   , 7*t*t*t*t*t*t  ,
         0 ,  0  ,    2 ,    6*t    ,12*t*t    , 20*t*t*t     ,30*t*t*t*t    , 42*t*t*t*t*t   ,
         0 ,  0  ,    0  ,    6     ,24*t      , 60*t*t       ,120*t*t*t     , 210*t*t*t*t    ;


     //boundary condition.
     //
     B <<  1.0, 0.0 , 0.0  , 0.0 ,  1.4, 0.0 , 0.0 , 0.0 ;


     for(int i=0 ; i<8; i++){
         for(int j=0;j<(i+1);j++){
             qp->set_d(i, j,0);
         }
     }


     for(int i=0 ; i<8; i++){
         for(int j=0;j<(i+1);j++){

             qp->set_d(i, j, D(i,j));
         }
     }

     for(int i=0;i<5;i++){
         for(int j=0;j<8;j++){
             qp->set_a(i, j,  A(i,j));
         }
     }
     for(int i=0;i<8;i++){
            qp->set_b(i,B(i,0));
     }

    Solution s =CGAL::solve_quadratic_program(*qp, ET());
    assert (s.solves_quadratic_program(*qp));
    //std::cout <<std::endl <<    <<std::endl;

    std::cout<<"B" <<std::endl <<  B  <<std::endl;
    std::cout<<"A" <<std::endl <<  A  <<std::endl;
    std::cout<<"D" <<std::endl << D  <<std::endl;


    if (s.is_infeasible()) {
          std::cout << "matrix is singular" << std::endl;
    }
    std::cout << s <<std::endl;

Results:

B
  1
  0
  0
  0
1.4
  0
  0
  0
A
  1   0   0   0   0   0   0   0
  0   1   0   0   0   0   0   0
  0   0   2   0   0   0   0   0
  0   0   0   6   0   0   0   0
  1   1   1   1   1   1   1   1
  0   1   2   3   4   5   6   7
  0   0   2   6  12  20  30  42
  0   0   0   6  24  60 120 210
D
     0      0      0      0      0      0      0      0
     0      0      0      0      0      0      0      0
     0      0      0      0      0      0      0      0
     0      0      0      0      0      0      0      0
     0      0      0      0    576   1440   2880   5040
     0      0      0      0   1440   4800  10800  20160
     0      0      0      0   2880  10800  25920  50400
     0      0      0      0   5040  20160  50400 100800
matrix is singular
status:          INFEASIBLE

EdXian avatar Oct 16 '18 12:10 EdXian

I think it works. Here is my code:

#include <iostream>
#include <cassert>
#include <CGAL/QP_models.h>
#include <CGAL/QP_functions.h>
// choose exact integral type
#ifdef CGAL_USE_GMP
#include <CGAL/Gmpz.h>
typedef CGAL::Gmpz ET;
#else
#include <CGAL/MP_Float.h>
typedef CGAL::MP_Float ET;
#endif
// program and solution types
typedef CGAL::Quadratic_program<int> Program;
typedef CGAL::Quadratic_program_solution<ET> Solution;
int main() {
    // Ax = b, no upper and lower bound
    Program qp (CGAL::EQUAL, false, 0, false, 0);
    
    // now set the non-default entries:
    const int X0 = 0, X1 = 1, X2 = 2, X3 = 3, X4 = 4, X5 = 5, X6 = 6, X7 = 7;
    
    // A
    qp.set_a(X0, 0, 1);
    qp.set_a(X1, 1, 1);
    qp.set_a(X2, 2, 2);
    qp.set_a(X3, 3, 6);
    qp.set_a(X0, 4, 5); qp.set_a(X1, 4, 5); qp.set_a(X2, 4, 5); qp.set_a(X3, 4, 5); qp.set_a(X4, 4, 5)
; qp.set_a(X5, 4, 5); qp.set_a(X6, 4, 5); qp.set_a(X7, 4, 5);
    qp.set_a(X1, 5, 1); qp.set_a(X2, 5, 2); qp.set_a(X3, 5, 3); qp.set_a(X4, 5, 4); qp.set_a(X5, 5, 5)
; qp.set_a(X6, 5, 6); qp.set_a(X7, 5, 7);
    qp.set_a(X2, 6, 2); qp.set_a(X3, 6, 6); qp.set_a(X4, 6, 12); qp.set_a(X5, 6, 20); qp.set_a(X6, 6, 
30); qp.set_a(X7, 6, 42);
    qp.set_a(X3, 7, 6); qp.set_a(X4, 7, 24); qp.set_a(X5, 7, 60); qp.set_a(X6, 7, 120); qp.set_a(X7, 7
, 210);
    
    // D
    qp.set_d(X4, X4, 576); qp.set_d(X5, X4, 1440); qp.set_d(X6, X4, 2880); qp.set_d(X7, X4, 5040);
    qp.set_d(X5, X5, 4800); qp.set_d(X6, X5, 10800); qp.set_d(X7, X5, 20160);
    qp.set_d(X6, X6, 25920); qp.set_d(X7, X6, 50400);
    qp.set_d(X7, X7, 100800);
    
    // B
    qp.set_b(0,1);
    qp.set_b(1,0);
    qp.set_b(2,0);
    qp.set_b(3,0);
    qp.set_b(4,7);
    qp.set_b(5,0);
    qp.set_b(6,0);
    qp.set_b(7,0);
    
    // solve the program, using ET as the exact type
    Solution s = CGAL::solve_quadratic_program(qp, ET());
    assert (s.solves_quadratic_program(qp));
    // output solution
    std::cout << s;
    return 0;
}

Here is the solution:

status:          OPTIMAL
objective value: 8064/1
variable values:
  0: 1/1
  1: 0/1
  2: 0/1
  3: 0/1
  4: 14/1
  5: -168/5
  6: 28/1
  7: -8/1

ThongVoHien avatar Mar 31 '19 22:03 ThongVoHien

Hi @ThongVoHien , The solution helps me a lot. It works on my case well. Thank you very much!

EdXian avatar Apr 01 '19 03:04 EdXian

Note that set_a() takes a column ID first, line ID second.

Apologies for not having looked into this before.

MaelRL avatar Apr 01 '19 06:04 MaelRL