10. Enumerate a finite solution space¶
To find more than one model, exclude the assignment you have already seen
and query again. When the selected symbols have finitely many possible
assignments, a final unsat result establishes that enumeration is complete.
Prerequisites: results and models and incremental solving.
Block the tuple¶
Find nonnegative integer pairs (x, y) with x + y = 2. There are three:
(0, 2), (1, 1), and (2, 0).
"""Enumerate all nonnegative integer pairs whose sum is two."""
from std.testing import assert_equal, assert_true
from cvc5_mojo import TermManager, Solver, Kind
def main() raises:
var tm = TermManager()
var solver = Solver(tm)
solver.setLogic("QF_LIA")
solver.setOption("incremental", "true")
solver.setOption("produce-models", "true")
var x = tm.mkConst(tm.getIntegerSort(), "x")
var y = tm.mkConst(tm.getIntegerSort(), "y")
var zero = tm.mkInteger(0)
solver.assertFormula(tm.mkTerm(Kind.GEQ, x, zero))
solver.assertFormula(tm.mkTerm(Kind.GEQ, y, zero))
solver.assertFormula(tm.mkTerm(Kind.EQUAL,
tm.mkTerm(Kind.ADD, x, y), tm.mkInteger(2)))
var seen = List[String]()
while True:
var result = solver.checkSat()
if result.isUnsat():
break
if result.isUnknown():
raise Error("Enumeration stopped before completeness was established")
var vx = solver.getValue(x)
var vy = solver.getValue(y)
var pair = String(vx) + "," + String(vy)
assert_true(pair == "0,2" or pair == "1,1" or pair == "2,0")
for previous in seen:
assert_true(pair != previous)
seen.append(pair^)
# Exclude the entire pair, not each coordinate separately.
var same_pair = tm.mkTerm(Kind.AND,
tm.mkTerm(Kind.EQUAL, x, vx), tm.mkTerm(Kind.EQUAL, y, vy))
solver.assertFormula(tm.mkTerm(Kind.NOT, same_pair))
assert_equal(len(seen), 3)
print("distinct pairs:", len(seen))
print("all solutions found")
Run from the repository root pixi run --locked mojo run -I src docs/examples/enumeration.mojo
Output
distinct pairs: 3
all solutions found
After each satisfiable result, the program reads both coordinates and adds the negation of their conjunction:
NOT (x = previous_x AND y = previous_y)
This excludes exactly that pair. Requiring x != previous_x and
y != previous_y separately would exclude all assignments sharing either
coordinate, which can skip valid solutions in a general problem.
The example's test checks that every returned pair belongs to the known solution set, that no pair repeats, and that the final count is three. It prints the count rather than a solver-dependent enumeration order.
Know what completeness means¶
The nonnegative bounds and fixed sum bound both coordinates above by two. That mathematical fact makes the selected space finite. Removing those bounds admits infinitely many integer pairs, so this loop would no longer be a complete terminating enumeration procedure.
If the larger model has additional symbols, blocking only (x, y)
enumerates distinct projections onto those coordinates. Different internal
assignments with the same pair are deliberately combined. Include more
symbols in the blocking formula if their distinctions matter to your task.
An unknown result cannot be treated as exhaustion. The example raises
instead of printing “all solutions found” in that case. An application
could return the partial list together with an explicit incomplete status.
Exercise: change the sum¶
How many nonnegative integer pairs sum to three?
Worked answer. Four: (0, 3), (1, 2), (2, 1), (3, 0). More
generally, a nonnegative integer total n gives n + 1 ordered pairs.
Update the expected set as well as the total and count in a copy of the
program. The blocking formula does not change.
Next: graph coloring.