From 6723a0bac8e9559b4dfbaf6ec8522e86842a4fe0 Mon Sep 17 00:00:00 2001 From: fusang991 <18458252790@163.com> Date: Tue, 24 Nov 2026 22:23:52 +0800 Subject: [PATCH] Compute limiting soft costs after an unbounded lex objective --- src/opt/maxsmt.cpp | 5 +++ src/opt/opt_context.cpp | 77 +++++++++++++++++++++++++++++++++++++++-- src/opt/opt_context.h | 1 + src/opt/opt_params.hpp | 2 +- src/opt/optsmt.cpp | 3 -- src/opt/optsmt.h | 5 ++- src/test/opt_bounds.cpp | 44 +++++++++++++++++------ 7 files changed, 118 insertions(+), 19 deletions(-) diff --git a/src/opt/maxsmt.cpp b/src/opt/maxsmt.cpp index 665b7397832..70b23d981f2 100644 --- a/src/opt/maxsmt.cpp +++ b/src/opt/maxsmt.cpp @@ -240,6 +240,11 @@ namespace opt { m_msolver->reset_upper(); m_upper = m_msolver->get_upper(); } + else { + m_upper.reset(); + for (auto const& s : m_soft) + m_upper += s.weight; + } } void maxsmt::verify_assignment() { diff --git a/src/opt/opt_context.cpp b/src/opt/opt_context.cpp index 0ea7e7cbeb1..b9c724d6113 100644 --- a/src/opt/opt_context.cpp +++ b/src/opt/opt_context.cpp @@ -642,7 +642,67 @@ namespace opt { return r; } - // Objective unbounded_index has no finite optimum. Each later arithmetic + // With finitely many soft costs, the limiting optimum is the least cost + // cap under which the preceding objective is still unbounded. + lbool context::execute_soft_after_unbounded(objective const& unbounded, objective const& o, bool commit) { + if (o.m_terms.empty()) + return l_true; + rational num, den(1); + for (auto const& w : o.m_weights) { + num = gcd(num, numerator(w)); + den = lcm(den, denominator(w)); + } + rational unit = num / den; + bool is_int = m_arith.is_int(unbounded.m_term); + expr_ref zero(m_arith.mk_numeral(rational(0), is_int), m); + expr_ref_vector costs(m); + rational lo, hi; + for (unsigned i = 0; i < o.m_terms.size(); ++i) { + rational w = o.m_weights[i] / unit; + costs.push_back(m.mk_ite(o.m_terms[i], zero, m_arith.mk_numeral(w, is_int))); + hi += w; + } + expr_ref cost(m_arith.mk_add(costs), m); + maxsmt& ms = *m_maxsmts.find(o.m_id); + solver& s = get_solver(); + unsigned rounds = opt_params(m_params).optsmt_bisect_rounds(); + for (unsigned i = 0; lo < hi && i < rounds; ++i) { + rational mid = floor((lo + hi) / rational(2)); + solver::scoped_push scope(s); + s.assert_expr(m_arith.mk_le(cost, m_arith.mk_numeral(mid, is_int))); + model_ref witness; + switch (m_optsmt.reaches_infinity(unbounded.m_index, witness)) { + case l_true: + hi = mid; + ms.update_upper(hi * unit); + break; + case l_false: + lo = mid + 1; + ms.update_lower(lo * unit); + break; + case l_undef: + return l_undef; + } + } + if (lo != hi) + return l_true; + + // A model of a cheaper finite prefix need not realize the limiting cost. + expr_ref exact(m.mk_eq(cost, m_arith.mk_numeral(lo, is_int)), m); + { + solver::scoped_push scope(s); + s.assert_expr(exact); + if (s.check_sat(0, nullptr) != l_true) + return l_undef; + s.get_model(m_model); + s.get_labels(m_labels); + } + if (commit) + s.assert_expr(exact); + return l_true; + } + + // Objective unbounded_index has no finite optimum. Each later // objective takes the limit of its optima as that objective grows without // bound. A determined limit is committed before the next objective; an // objective left with an interval ends the search, and the later @@ -650,10 +710,23 @@ namespace opt { lbool context::execute_lex_after_unbounded(unsigned unbounded_index) { unsigned sz = m_objectives.size(); unsigned u = m_objectives[unbounded_index].m_index; + // A feasible model from before optimizing u gives no upper bound + // on the limiting penalty, including for objectives we may never visit. for (unsigned j = unbounded_index + 1; j < sz; ++j) { objective const& o = m_objectives[j]; if (o.m_type == O_MAXSMT) - return l_true; + m_maxsmts.find(o.m_id)->reset_upper(); + } + if (!opt_params(m_params).optsmt_lex_after_unbounded()) + return l_true; + for (unsigned j = unbounded_index + 1; j < sz; ++j) { + objective const& o = m_objectives[j]; + if (o.m_type == O_MAXSMT) { + lbool r = execute_soft_after_unbounded(m_objectives[unbounded_index], o, j + 1 < sz); + if (r != l_true || get_lower_value(j) != get_upper_value(j)) + return r; + continue; + } lbool r = m_optsmt.lex_after_unbounded(u, o.m_index, o.m_type == O_MAXIMIZE); model_ref mdl; svector labels; diff --git a/src/opt/opt_context.h b/src/opt/opt_context.h index 530ff5fc791..e62d935d1cd 100644 --- a/src/opt/opt_context.h +++ b/src/opt/opt_context.h @@ -308,6 +308,7 @@ namespace opt { lbool execute_min_max(unsigned index, bool committed, bool scoped, bool is_max); lbool execute_maxsat(symbol const& s, bool committed, bool scoped); lbool execute_lex(); + lbool execute_soft_after_unbounded(objective const& unbounded, objective const& o, bool commit); lbool execute_lex_after_unbounded(unsigned unbounded_index); lbool execute_box(); lbool execute_pareto(); diff --git a/src/opt/opt_params.hpp b/src/opt/opt_params.hpp index d9dc5c57e55..f5ae413c18d 100644 --- a/src/opt/opt_params.hpp +++ b/src/opt/opt_params.hpp @@ -22,7 +22,7 @@ Module Name: BOOL_ (optsmt_dual_bounds, "optsmt_dual_bounds", true, "retain exact rational nonlinear relaxation upper bounds, independently of hint attainability") \ UINT_ (optsmt_dual_bound_rlimit, "optsmt_dual_bound_rlimit", 100, "resource limit for a cross-branch nonlinear bound probe while no finite upper bound is known (0 disables the extra check)") \ UINT_ (optsmt_nlsat_supremum_rlimit, "optsmt_nlsat_supremum_rlimit", 100000, "resource limit for certifying finite open suprema with native nlsat projection (0 disables the extra check)") \ - BOOL_ (optsmt_lex_after_unbounded, "optsmt_lex_after_unbounded", true, "after an unbounded lexicographic objective, determine later arithmetic objectives as the limits of their optima; false leaves them as unrestricted intervals") \ + BOOL_ (optsmt_lex_after_unbounded, "optsmt_lex_after_unbounded", true, "after an unbounded lexicographic objective, determine later objectives as the limits of their optima; false leaves them as unrestricted intervals") \ BOOL_ (optsmt_unbounded_curve, "optsmt_unbounded_curve", true, "certify an unbounded objective along a polynomial curve through recent models (a ray, a quadratic, a cubic, or a unit direction) whose points all satisfy the hard constraints; applies to integer and mixed objectives as well") \ BOOL_ (pareto_nlsat, "pareto_nlsat", true, "enumerate Pareto fronts of pure NRA problems over an nlsat-backed solver, comparing objectives against exact algebraic model values instead of rounded isolating-interval endpoints") \ BOOL_ (pareto_nlsat_reuse, "pareto_nlsat_reuse", true, "reuse nlsat clauses across Pareto checks on polynomial real problems; false keeps the exact solver that starts each check from scratch") \ diff --git a/src/opt/optsmt.cpp b/src/opt/optsmt.cpp index e0c534820fc..3e615682926 100644 --- a/src/opt/optsmt.cpp +++ b/src/opt/optsmt.cpp @@ -860,8 +860,6 @@ namespace opt { */ lbool optsmt::lex_after_unbounded(unsigned unbounded_index, unsigned idx, bool is_maximize) { TRACE(opt, tout << "optsmt:lex after unbounded " << unbounded_index << " objective " << idx << "\n";); - if (!m_lex_after_unbounded) - return l_true; arith_util arith(m); app* obj = m_objs.get(idx); bool is_int = arith.is_int(obj); @@ -1075,7 +1073,6 @@ namespace opt { m_optsmt_nlsat = _p.optsmt_nlsat(); m_dual_bounds = _p.optsmt_dual_bounds(); m_nlsat_supremum_rlimit = _p.optsmt_nlsat_supremum_rlimit(); - m_lex_after_unbounded = _p.optsmt_lex_after_unbounded(); m_unbounded_curve = _p.optsmt_unbounded_curve(); } diff --git a/src/opt/optsmt.h b/src/opt/optsmt.h index 9547459805a..ca57515f6bd 100644 --- a/src/opt/optsmt.h +++ b/src/opt/optsmt.h @@ -42,7 +42,6 @@ namespace opt { bool m_optsmt_nlsat = true; bool m_dual_bounds = true; unsigned m_nlsat_supremum_rlimit = 100000; - bool m_lex_after_unbounded = true; bool m_unbounded_curve = true; model_ref m_model, m_best_model; svector m_labels; @@ -61,6 +60,8 @@ namespace opt { // then takes the limit of its suprema as the unbounded objective grows. lbool lex_after_unbounded(unsigned unbounded_index, unsigned obj_index, bool is_maximize); + lbool reaches_infinity(unsigned idx, model_ref& witness); + bool is_unbounded(unsigned obj_index, bool is_maximize); unsigned add(app* t); @@ -107,8 +108,6 @@ namespace opt { nlsat_outcome nlsat_cells(unsigned idx, bool is_maximize, inf_eps const& hi); bool prove_unbounded_above(unsigned idx, unsigned rlimit_budget, sref_vector const& recent_models); - lbool reaches_infinity(unsigned idx, model_ref& witness); - bool model_objective_value(unsigned idx, model_ref& mdl, rational& value); void publish_best_model(); diff --git a/src/test/opt_bounds.cpp b/src/test/opt_bounds.cpp index 411c28eea01..ccb2f8ea806 100644 --- a/src/test/opt_bounds.cpp +++ b/src/test/opt_bounds.cpp @@ -1941,16 +1941,40 @@ static void tst_lex_after_unbounded() { unsigned h = f.objective(Z3_mk_ite(f.ctx, b, hi, lo), maximize); ENSURE(f.check() == Z3_L_TRUE); ensure_symbolic_bounds(f, first, is_real ? 1 : -1, 0, 0); - Z3_ast lower = scalar_bound(f, h, true); - Z3_ast upper = scalar_bound(f, h, false); - ENSURE(Z3_algebraic_le(f.ctx, lower, upper)); - // The interval must contain the limiting optimum, even when this - // converted MaxSMT objective is not optimized after the unbounded one. - Z3_ast expected = maximize ? hi : lo; - ENSURE(Z3_algebraic_le(f.ctx, lower, expected)); - ENSURE(Z3_algebraic_le(f.ctx, expected, upper)); - ensure_vector(f, h, true, 0, lower, 0, Z3_INT_SORT); - ensure_vector(f, h, false, 0, upper, 0, Z3_INT_SORT); + ensure_finite_bounds(f, h, maximize ? hi : lo, Z3_INT_SORT); + } + } + { + // #11820: e = 1 is feasible, but cannot persist as e tends to -oo. + opt_fixture f; + Z3_ast e = f.real("e"); + f.objective(e, false); + Z3_ast cost = Z3_mk_ite(f.ctx, Z3_mk_eq(f.ctx, e, f.num(1)), f.num(1), f.num(3)); + unsigned h = f.objective(cost, false); + ENSURE(f.check() == Z3_L_TRUE); + ensure_finite_bounds(f, h, f.num(3), Z3_INT_SORT); + ensure_model_value(f, cost, f.num(3)); + } + for (unsigned rounds : {0u, 64u}) { + opt_fixture f("lex", rounds); + Z3_ast e = f.real("e"), y = f.real("y"); + Z3_ast b = Z3_mk_const(f.ctx, f.symbol("b"), Z3_mk_bool_sort(f.ctx)); + f.add(Z3_mk_le(f.ctx, y, Z3_mk_ite(f.ctx, b, f.num(2), f.num(5)))); + f.objective(e, false); + unsigned h = Z3_optimize_assert_soft(f.ctx, f.opt, Z3_mk_eq(f.ctx, e, f.num(1)), "1/2", nullptr); + Z3_optimize_assert_soft(f.ctx, f.opt, b, "3/4", nullptr); + unsigned last = f.objective(y); + ENSURE(f.check() == Z3_L_TRUE); + if (rounds == 0) { + // No probes: report the full penalty range, including its limiting optimum. + ensure_value(f, scalar_bound(f, h, true), f.num(0)); + ensure_value(f, scalar_bound(f, h, false), f.num(5, 4)); + } + else { + ensure_finite_bounds(f, h, f.num(1, 2)); + // The minimum limiting penalty forces b before maximizing y. + ensure_finite_bounds(f, last, f.num(2), Z3_INT_SORT); + ensure_model_value(f, y, f.num(2)); } } for (bool minimize_first : {false, true}) {