Esempio n. 1
0
int main() {
  // print seed
  std::cout << "Random seed: " << rd.get_seed() << std::endl;

  // options
  CGAL::Quadratic_program_options options;
  options.set_auto_validation(true);

  // generate a set of small random qp's
  for (int i=0; i<tries; ++i) {
    int  Ax[] = {random_signed(), random_signed()};         
    int  Ay[] = {random_signed(), random_signed()};         
    int*  A[] = {Ax, Ay};                       
    int   b[] = {random_signed(), random_signed()};         
    CGAL::Comparison_result
      r[] = {random_rel(), random_rel()};
    bool fl[] = {rd.get_bool(), rd.get_bool()};                   
    int   l[] = {random_signed(),random_signed()};
    bool fu[] = {rd.get_bool(), rd.get_bool()};               
    int   u[] = {random_signed(),random_signed()};   
    // make sure that l<=u
    if (l[0] > u[0]) {int h = l[0]; l[0] = u[0]; u[0] = h;}
    if (l[1] > u[1]) {int h = l[1]; l[1] = u[1]; u[1] = h;}
    int  D1[] = {random_unsigned()};                    
    int  D2[] = {0, random_unsigned()};
    // can still change D_21 as long as D remains positive-semidefinite
    if (D1[0] < D2[1]) 
      D2[0] = rd.get_int(-D1[0], D1[0]+1);
    else
      D2[0] = rd.get_int(-D2[1], D2[1]+1);
    assert(D1[0] * D2[1] >= D2[0] * D2[0]);  
    int*  D[] = {D1, D2};                      
    int   c[] = {random_signed(), random_signed()};
    int  c0   = random_signed();                     

    // now construct the quadratic program; the first two parameters are
    // the number of variables and the number of constraints (rows of A)
    Program qp (2, 2, A, b, r, fl, l, fu, u, D, c, c0);

    // write/read it and check equality
    std::stringstream inout;
    CGAL::print_quadratic_program (inout, qp);
    CGAL::Quadratic_program_from_mps<int> qp2 (inout);
    assert(CGAL::QP_functions_detail::are_equal_qp (qp, qp2));
  
    // solve it
    Solution s = CGAL::solve_quadratic_program (qp, ET(), options);
    assert(s.is_valid());
    statistics (s, qp_optimal, qp_infeasible, qp_unbounded);

    // also solve it as nqp, lp, nlp
    s = CGAL::solve_nonnegative_quadratic_program (qp, ET(), options); 
    assert(s.is_valid());
    statistics (s, nqp_optimal, nqp_infeasible, nqp_unbounded);
    s = CGAL::solve_linear_program (qp, ET(), options);    
    assert(s.is_valid()); 
    statistics (s, lp_optimal, lp_infeasible, lp_unbounded);
    s = CGAL::solve_nonnegative_linear_program (qp, ET(), options);   
    assert(s.is_valid());  
    statistics (s, nlp_optimal, nlp_infeasible, nlp_unbounded);
  }
  
  // output statistics
  std::cout << "Solved " << tries 
	    << " random QP / NQP  / LP / NLP .\n"
	    << " Optimal:    " 
	    << qp_optimal << " / " 
	    << nqp_optimal << " / " 
	    << lp_optimal << " / " 
	    << nlp_optimal << "\n"
	    << " Infeasible: "
	    << qp_infeasible << " / " 
	    << nqp_infeasible << " / " 
	    << lp_infeasible << " / " 
	    << nlp_infeasible << "\n"
	    << " Unbounded:  "
	    << qp_unbounded << " / " 
	    << nqp_unbounded << " / " 
	    << lp_unbounded << " / " 
	    << nlp_unbounded << std::endl;
  return 0;
}
Esempio n. 2
0
int main() {  
  // print seed
  std::cout << "Random seed: " << rd.get_seed() << std::endl;

  // options
  CGAL::Quadratic_program_options options;
  options.set_auto_validation(true);

  // generate a set of small random qp's
  for (int i=0; i<tries; ++i) {
    // first choose dimensions
    int n = rd.get_int(1,max_dim);
    int m = rd.get_int(1,max_dim);

    // construct matrix D as C^T C, for C randomly chosen with n columns
    int k = rd.get_int (1, 2*n); // number of rows of C
    std::vector<std::vector<int> > C (k, std::vector<int>(n, 0));
    for (int j=0; j<k+n; ++j)  // sparse C
      C[rd.get_int(0, k)][rd.get_int(0,n)] = 
	rd.get_int(-max_entry, max_entry);

    // now fill the program 
    Program p;
    // A
    for (int j=0; j<n+m; ++j)
      p.set_a (rd.get_int(0,n), rd.get_int(0,m), rd.get_double());
    // b, r
    for (int i=0; i<m/2; ++i) {
      p.set_b (rd.get_int(0,m), rd.get_double());
      p.set_r (rd.get_int(0,m), random_rel());
    }
    // fl, l, fu, u
    for (int j=0; j<n; ++j) {
      double l = rd.get_double();
      double u = rd.get_double();
      if (l > u) std::swap (l, u); 
      p.set_l(j, rd.get_bool(), l);
      p.set_u(j, rd.get_bool(), u);
    }
    // D
    for (int i=0; i<n; ++i)
      for (int j=0; j<=i; ++j) {
	double entry = 0;
	for (int l=0; l<k; ++l) 
	  entry += C[l][i] * C[l][j];
	p.set_d(i, j, entry);
      }
    // c
    for (int j=0; j<n/2; ++j)
      p.set_c (rd.get_int(0, n), rd.get_double());
    // c0
    p.set_c0(rd.get_double());
    
    // solve it
    Solution s = CGAL::solve_quadratic_program (p, ET(), options);
    assert(s.is_valid());
    statistics (s, qp_optimal, qp_infeasible, qp_unbounded);

    // also solve it as nqp, lp, nlp
    s = CGAL::solve_nonnegative_quadratic_program (p, ET(), options); 
    assert(s.is_valid());
    statistics (s, nqp_optimal, nqp_infeasible, nqp_unbounded);
    s = CGAL::solve_linear_program (p, ET(), options);    
    assert(s.is_valid()); 
    statistics (s, lp_optimal, lp_infeasible, lp_unbounded);
    s = CGAL::solve_nonnegative_linear_program (p, ET(), options);   
    assert(s.is_valid());  
    statistics (s, nlp_optimal, nlp_infeasible, nlp_unbounded);
  }
  
  // output statistics
  std::cout << "Solved " << tries 
	    << " random QP / NQP  / LP / NLP .\n"
	    << " Optimal:    " 
	    << qp_optimal << " / " 
	    << nqp_optimal << " / " 
	    << lp_optimal << " / " 
	    << nlp_optimal << "\n"
	    << " Infeasible: "
	    << qp_infeasible << " / " 
	    << nqp_infeasible << " / " 
	    << lp_infeasible << " / " 
	    << nlp_infeasible << "\n"
	    << " Unbounded:  "
	    << qp_unbounded << " / " 
	    << nqp_unbounded << " / " 
	    << lp_unbounded << " / " 
	    << nlp_unbounded << std::endl;

  return 0;
}