3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-06-06 14:13:23 +00:00

Replace the use of optional<rational> by inf_eps_rational<rational>

Also handle composite objectives correctly.
This commit is contained in:
Phan Anh Dung 2013-10-19 06:03:21 +02:00
parent a44044fb15
commit 53f78f7d19
9 changed files with 50 additions and 43 deletions

View file

@ -31,7 +31,7 @@ namespace opt {
*/
lbool mathsat_style_opt(opt_solver& s,
expr_ref_vector& objectives, svector<bool> const& is_max,
vector<optional<rational> >& values) {
vector<inf_eps_rational<rational> >& values) {
enable_trace("maximize");
// First check_sat call for initialize theories
lbool is_sat = s.check_sat(0, 0);
@ -39,38 +39,47 @@ namespace opt {
return l_false;
}
s.push();
// Assume there is only one objective function
ast_manager& m = objectives.get_manager();
arith_util autil(m);
app* objective = is_max[0] ? (app*) objectives[0].get() : autil.mk_uminus(objectives[0].get());
app* objective = m.mk_fresh_const("objective", autil.mk_real());
if (is_max[0]) {
s.assert_expr(autil.mk_eq(objective, objectives[0].get()));
} else {
s.assert_expr(autil.mk_eq(objective, autil.mk_uminus(objectives[0].get())));
};
s.set_objective(objective);
s.toggle_objective(true);
is_sat = s.check_sat(0, 0);
while (is_sat != l_false) {
// Extract values for objectives.
optional<rational> rat;
rat = s.get_objective_value();
// Extract values for objectives
inf_eps_rational<rational> val;
val = is_max[0] ? s.get_objective_value() : -s.get_objective_value();
// Unbounded objective
if (!rat) {
// Check whether objective is unbounded
if (!val.is_rational()) {
values.reset();
values.push_back(rat);
return l_true;
values.push_back(val);
break;
}
// If values have initial data, they will be dropped.
// If values have initial data, they will be dropped
values.reset();
values.push_back(rat);
values.push_back(val);
// Assert there must be something better.
// Assert there must be something better
expr_ref_vector assumptions(m);
expr* bound = m.mk_fresh_const("bound", m.mk_bool_sort());
assumptions.push_back(bound);
expr* r = autil.mk_numeral(*rat, false);
expr* r = autil.mk_numeral(val.get_rational(), false);
s.assert_expr(m.mk_eq(bound, is_max[0] ? autil.mk_gt(objectives[0].get(), r) : autil.mk_lt(objectives[0].get(), r)));
is_sat = s.check_sat(1, assumptions.c_ptr());
}
}
s.pop(1);
return l_true;
}
@ -82,7 +91,7 @@ namespace opt {
lbool optimize_objectives(opt_solver& s,
expr_ref_vector& objectives, svector<bool> const& is_max,
vector<optional<rational> >& values) {
vector<inf_eps_rational<rational> >& values) {
return mathsat_style_opt(s, objectives, is_max, values);
}
}