Part 2: Solver control — incremental solving, unsat cores, optimization, and tactics¶
Real SMT workloads rarely stop at a single s.check(). You need to find out
why a system is unsatisfiable, relax constraints incrementally, optimize
soft preferences, and tune tactics for hard subproblems.
Full runnable file: docs/examples/tutorial_02_solver_control.mojo.
1. Diagnosing conflicts with unsat cores¶
Imagine you have a 16-core server and three services (web, db, ml) with
minimum CPU requests of 4, 6, and 8 cores. That adds up to 18 cores, so it is
unsatisfiable. To have Z3 tell you which constraints conflict, attach a Boolean
tracker to each assertion via assert_and_track:
from z3_mojo import BoolVar, Context, Ints, Solver, unsat
var ctx = Context()
var s = Solver.for_logic(ctx, "QF_LIA")
s.set_unsat_core(True)
s.set_timeout_ms(2000)
s.set_seed(0)
var cpus = Ints("web db ml", ctx)
var web = cpus[0]
var db = cpus[1]
var ml = cpus[2]
var p_budget = BoolVar("budget_16_cores", ctx)
var p_web = BoolVar("web_min_4", ctx)
var p_db = BoolVar("db_min_6", ctx)
var p_ml = BoolVar("ml_min_8", ctx)
s.assert_and_track(web + db + ml <= 16, p_budget)
s.assert_and_track(web >= 4, p_web)
s.assert_and_track(db >= 6, p_db)
s.assert_and_track(ml >= 8, p_ml)
assert_equal(s.check(), unsat)
var core = s.unsat_core()
assert_equal(len(core), 4)
print("Unsat core size:", len(core))
Use Solver.for_logic for unsat cores
A solver created with Solver.from_tactic(...) returns an empty core for
tracked assertions. Use Solver.for_logic (or plain Solver(ctx)) with
set_unsat_core(True) whenever you want unsat cores.
Notice s.set_timeout_ms(2000) and s.set_seed(0) as well. For deterministic,
machine-independent resource caps, you can also use s.set_rlimit(limit).
2. Incremental exploration with push and pop¶
Instead of rebuilding a solver from scratch for each what-if query, push a scope, add temporary assumptions, check, and pop them back off:
var s_inc = Solver(ctx)
s_inc.add(web >= 4, db >= 6, ml >= 8)
s_inc.push()
s_inc.add(web + db + ml <= 16)
assert_equal(s_inc.check(), unsat)
s_inc.pop()
s_inc.push()
s_inc.add(web + db + ml <= 20)
assert_equal(s_inc.check(), sat)
print(
"With 20 cores -> web:", s_inc.model().get_integer(web).value(),
"db:", s_inc.model().get_integer(db).value(),
"ml:", s_inc.model().get_integer(ml).value(),
)
s_inc.pop()
3. Weighted soft constraints and objectives with Optimize¶
When 16 cores is a hard cap, Optimize(ctx) lets you keep safety minimums as
hard constraints (opt.add), turn SLA targets into weighted soft constraints
(opt.add_soft), and maximize remaining capacity (opt.maximize):
from apn_mojo import Integer
from z3_mojo import Optimize, sat
var opt = Optimize(ctx)
opt.add(web >= 2, db >= 2, ml >= 2, web + db + ml <= 16)
_ = opt.add_soft(web >= 4, 10, "sla")
_ = opt.add_soft(db >= 6, 10, "sla")
_ = opt.add_soft(ml >= 8, 5, "sla") # Lower priority than web and db
var obj_ml = opt.maximize(ml)
assert_equal(opt.check(), sat)
var om = opt.model()
assert_equal(om.get_integer(web).value(), Integer(4))
assert_equal(om.get_integer(db).value(), Integer(6))
assert_equal(om.get_integer(ml).value(), Integer(6))
print("Optimal allocation -> ml:", opt.upper(obj_ml))
Z3 satisfies the higher-weight web >= 4 and db >= 6 requirements first,
then gives all remaining 6 cores to ml.
4. Custom Tactic, Probe, and Goal pipelines¶
For specialized domains, compose Z3 tactics with Then, OrElse, When,
Cond, TryFor, and With, inspect formulas in a Goal, and turn the final
pipeline into a solver via .solver():
from z3_mojo import Goal, Params, Probe, Real, Tactic, Then, When, With
var g = Goal(ctx)
var rx = Real("rx", ctx)
g.add(rx * rx + 1 < 0)
var is_qfnra = Probe("is-qfnra", ctx)
var p = Params(ctx)
p.set_bool("factor", True)
var pipeline = Then(
Tactic("simplify", ctx),
When(is_qfnra > 0, With(Tactic("qfnra-nlsat", ctx), p)),
)
var tac_solver = pipeline.solver()
tac_solver.add(g.as_expr())
assert_equal(tac_solver.check(), unsat)
Running tutorial_02_solver_control.mojo prints:
Unsat core size: 4
With 20 cores -> web: 4 db: 6 ml: 8
Optimal allocation -> web: 4 db: 6 ml: 6
Custom tactic pipeline: unsat
Try it yourself¶
- Add an unrelated tracked constraint
cache >= 1(cachenot in the 16-core sum) to the first solver and verify thats.unsat_core()omits it. - Swap the soft weights in
Optimizesoml >= 8has weight20andweb >= 4has weight5, and check how the optimal allocation changes.
Next: Part 3: Theories in practice — BitVectors, FPA, Strings, Arrays, and Datatypes.