unsigned MPILock::roulette(MPIResource *resource, vector<unsigned> &choices) { if (choices.size() == 1) { return choices.front(); } else { vector<unsigned> dist; dist.reserve(choices.size()); for (unsigned i = 0, len = choices.size(); i < len; ++i) { dist.push_back(resource->get_no_tokens(choices[i])); } for (unsigned i = 1, len = choices.size(); i < len; ++i) { dist[i] += dist[i - 1]; } unsigned random_case = random_unsigned(0, dist.back()); for (unsigned i = 0, len = dist.size(); i < len; ++i) { if (random_case < dist[i]) return choices[i]; } return choices.back(); } }
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; }