int main()
{
  const int d = 10;       // change this in order to experiment
  const int n = 100000;   // change this in order to experiment

  // generate n random d-dimensional points in [0,1]^d
  CGAL::Random rd;
  std::vector<Point_d> points;
  for (int j =0; j<n; ++j) {
    std::vector<double> coords;
    for (int i=0; i<d; ++i) 
      coords.push_back(rd.get_double());
    points.push_back (Point_d (d, coords.begin(), coords.end()));
  }
  
  // benchmark all pricing strategies in turn
  CGAL::Quadratic_program_pricing_strategy strategy[] = {
    CGAL::QP_CHOOSE_DEFAULT,              // QP_PARTIAL_FILTERED_DANTZIG
    CGAL::QP_DANTZIG,                     // Dantzig's pivot rule...
    CGAL::QP_PARTIAL_DANTZIG,             // ... with partial pricing
    CGAL::QP_BLAND,                       // Bland's pivot rule
    CGAL::QP_FILTERED_DANTZIG,            // Dantzig's filtered pivot rule...
    CGAL::QP_PARTIAL_FILTERED_DANTZIG     // ... with partial pricing
  };
  
  CGAL::Timer t;
  for (int i=0; i<6; ++i) {
    // test strategy i
    CGAL::Quadratic_program_options options;
    options.set_pricing_strategy (strategy[i]);
    t.reset(); t.start();
    // is origin in convex hull of the points? (most likely, not)
    solve_convex_hull_containment_lp 
      (Point_d (d, CGAL::ORIGIN), points.begin(), points.end(), 
       ET(0), options);
    t.stop();
    std::cout << "Time (s) = " << t.time() << std::endl;
  }

  return 0;
}
Beispiel #2
0
void testcase() {
  int h, t;

  cin >> h >> t;

  vector<vector<vector<ET> > > powers;
  for (int j=0; j < h+t; j++) {
    vector<vector<ET> > var;
    for (int i=0; i < 3; i++) {
      vector<ET> row(31, -1);
      var.push_back(row);
    }
    powers.push_back(var);
  }


  for (int i=0; i < h; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    powers[i][0][1] = x;
    powers[i][1][1] = y;
    powers[i][2][1] = z;
  }

  for (int i=0; i < t; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    powers[h+i][0][1] = x;
    powers[h+i][1][1] = y;
    powers[h+i][2][1] = z;
  }


  if (t == 0 || h == 0) {
    cout << "0\n";
    return;
  }

  int d;
  for (d=0; d <= 30; d += 5) {
    //cout << "try " << d << endl;
    Program qp (CGAL::LARGER, false, 0, false, 0); 
    int var_ind = 0;
    for (int i=0; i < h; i++) {
      add_polynom(qp, d, i, powers);
      qp.set_b(    i, -1); 
      qp.set_r(    i, CGAL::SMALLER);
    }

    for (int i=0; i < t; i++) {
      add_polynom(qp, d, h+i, powers);
      qp.set_b(    h+i, 1); 
      qp.set_r(    h+i, CGAL::LARGER);
    }

    CGAL::Quadratic_program_options options;
    options.set_pricing_strategy(CGAL::QP_BLAND);
    Solution s = CGAL::solve_linear_program(qp, ET(), options);
    // Solution s = CGAL::solve_quadratic_program(qp, ET());
    assert (s.solves_linear_program(qp));
    if (s.status() != CGAL::QP_INFEASIBLE) {
      break;
    }
  }

  if (d <= 30) {
    for (; d >= 0; d--) {
      Program qp (CGAL::LARGER, false, 0, false, 0); 
      int var_ind = 0;
      for (int i=0; i < h; i++) {
        add_polynom(qp, d, i, powers);
        qp.set_b(    i, -1); 
        qp.set_r(    i, CGAL::SMALLER);
      }

      for (int i=0; i < t; i++) {
        add_polynom(qp, d, h+i, powers);
        qp.set_b(    h+i, 1); 
        qp.set_r(    h+i, CGAL::LARGER);
      }

      CGAL::Quadratic_program_options options;
      options.set_pricing_strategy(CGAL::QP_BLAND);
      Solution s = CGAL::solve_linear_program(qp, ET(), options);
      // Solution s = CGAL::solve_quadratic_program(qp, ET());
      assert (s.solves_linear_program(qp));
      if (s.status() == CGAL::QP_INFEASIBLE) {
        cout << d+1 << endl;
        return;
      }
    }
  }

  cout << "Impossible!\n";

}
Beispiel #3
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;
}
Beispiel #4
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;
}
Beispiel #5
0
void algo() {
    int h,t; cin >> h >> t;

    vector<Cell> cells; cells.reserve(h + t);

    for (int i = 0; i < h + t; ++i) {
	int x,y,z; cin >> x >> y >> z;
	cells.PB(Cell(x,y,z));
    }

    const int gamma = 0;
    bool sol = false;
    int var = gamma + 1;

    Program lp (CGAL::SMALLER, false, 0, false, 0);
    lp.set_u(gamma, true, 1);
    lp.set_c(gamma, -1);

    for(int i = 0; i < h + t; ++i) {
	lp.set_a(gamma, i, 1);
    }

    int degree = -1;
    while(degree < 9 && !sol) {
    	++degree;
    	int varI = var;
    	for(int he = 0; he < max(h,t); ++he) {
    	    var = varI;
    	    for(int i = 0; i <= degree; ++i) {
    		for(int j = 0; j <= degree - i; ++j) {
    		    int l = degree - i - j; assert(l + i + j == degree);
    		    if(he < h) {
    			double result =
    			    pow(cells[he].x, i) * pow(cells[he].y, j)*
    			    pow(cells[he].z, l);

    			lp.set_a(var, he, result);
    		    }
    		    if(he < t) {
    			double result =
    			    pow(cells[he + h].x, i) * pow(cells[he + h].y, j)*
    			    pow(cells[he + h].z, l);

    			lp.set_a(var, he + h,  -result);
    		    }
    		    ++var;
    		}
    	    }
    	}
    	varI = var;

    	CGAL::Quadratic_program_options options;
    	options.set_pricing_strategy(CGAL::QP_BLAND);

    	Solution s = CGAL::solve_linear_program(lp, ET(), options);
    	assert (s.solves_linear_program(lp));

    	if(s.is_optimal() && s.objective_value() < 0) {
    	    sol = true;
    	}
    }

    if(!sol) {
	int min_degree = degree = 10;
	int max_degree = 30;

	while(min_degree <= max_degree) {
	    int d = (min_degree + max_degree) / 2;

	    Program lp (CGAL::SMALLER, false, 0, false, 0);
	    lp.set_u(gamma, true, 1);
	    lp.set_c(gamma, -1);

	    for(int i = 0; i < h + t; ++i) {
		lp.set_a(gamma, i, 1);
	    }

	    int varI = var;
	    for(int he = 0; he < max(h,t); ++he) {
		var = 1;
		for(int k = 0; k <= d; ++k) {
		    for(int i = 0; i <= k; ++i) {
			for(int j = 0; j <= k - i; ++j) {
			    int l = k - i - j; assert(l + i + j == k);
			    if(he < h) {
				double result = pow(cells[he].x, i) * pow(cells[he].y, j)*
				    pow(cells[he].z, l);

				lp.set_a(var, he, result);
			    }
			    if(he < t) {
				double result = pow(cells[he + h].x, i) *
				    pow(cells[he + h].y, j)* pow(cells[he + h].z, l);

				lp.set_a(var, he + h, -result);
			    }
			    ++var;
			}
		    }
		}
	    }
//	varI = var;

	    CGAL::Quadratic_program_options options;
	    options.set_pricing_strategy(CGAL::QP_BLAND);

	    Solution s = CGAL::solve_linear_program(lp, ET(), options);
	    assert (s.solves_linear_program(lp));

//	    cout << "Min degree " << min_degree << endl ;
//	cout << "Current degree " << d << " Min degree " << min_degree << endl;
	    if(s.is_optimal() && s.objective_value() < 0) {
		sol = true;
		max_degree = d - 1;
		degree = d;
	    }
	    else {
		min_degree = d + 1;
	    }
	}
    }

    if(!sol) {
	cout << "Impossible!\n";
    } else {
	cout << degree << "\n";
    }
}