static void add_ineq(opt::model_based_opt& mbo, unsigned x, int a, unsigned y, int b, int k, opt::ineq_type rel) { vector<var> vars; vars.push_back(var(x, rational(a))); vars.push_back(var(y, rational(b))); mbo.add_constraint(vars, rational(k), rel); }
static void add_random_ineq(opt::model_based_opt& mbo, random_gen& r, svector<int> const& values, unsigned max_vars, unsigned max_coeff) { unsigned num_vars = values.size(); uint_set used_vars; vector<var> vars; int value = 0; for (unsigned i = 0; i < max_vars; ++i) { unsigned x = r(num_vars); if (used_vars.contains(x)) { continue; } used_vars.insert(x); int coeff = r(max_coeff + 1); if (coeff == 0) { continue; } unsigned sign = r(2); coeff = sign == 0 ? coeff : -coeff; vars.push_back(var(x, rational(coeff))); value += coeff*values[x]; } unsigned abs_value = value < 0 ? - value : value; // value + k <= 0 // k <= - value // range for k is 2*|value| // k <= - value - range opt::ineq_type rel = opt::t_le; int coeff = 0; if (r(4) == 0) { rel = opt::t_eq; coeff = -value; } else { if (abs_value > 0) { coeff = -value - r(2*abs_value); } else { coeff = 0; } if (coeff != -value && r(3) == 0) { rel = opt::t_lt; } } mbo.add_constraint(vars, rational(coeff), rel); }
static void add_random_ineq( expr_ref_vector& fmls, opt::model_based_opt& mbo, random_gen& r, svector<int> const& values, unsigned max_vars, unsigned max_coeff) { ast_manager& m = fmls.get_manager(); arith_util a(m); unsigned num_vars = values.size(); uint_set used_vars; vector<var_t> vars; int value = 0; for (unsigned i = 0; i < max_vars; ++i) { unsigned x = r(num_vars); if (used_vars.contains(x)) { continue; } used_vars.insert(x); int coeff = r(max_coeff + 1); if (coeff == 0) { continue; } unsigned sign = r(2); coeff = sign == 0 ? coeff : -coeff; vars.push_back(var_t(x, rational(coeff))); value += coeff*values[x]; } unsigned abs_value = value < 0 ? - value : value; // value + k <= 0 // k <= - value // range for k is 2*|value| // k <= - value - range opt::ineq_type rel = opt::t_le; int coeff = 0; if (r(4) == 0) { rel = opt::t_eq; coeff = -value; } else { if (abs_value > 0) { coeff = -value - r(2*abs_value); } else { coeff = 0; } if (coeff != -value && r(3) == 0) { rel = opt::t_lt; } } expr_ref fml(m); app_ref t1(m); app_ref t2(a.mk_numeral(rational(0), a.mk_real()), m); mk_term(vars, rational(coeff), t1); switch (rel) { case opt::t_eq: fml = m.mk_eq(t1, t2); break; case opt::t_lt: fml = a.mk_lt(t1, t2); break; case opt::t_le: fml = a.mk_le(t1, t2); break; case opt::t_mod: NOT_IMPLEMENTED_YET(); break; } fmls.push_back(fml); mbo.add_constraint(vars, rational(coeff), rel); }