4. Integer constraints¶
Seven people buy tickets. An adult ticket costs 8 units and a child ticket costs 5 units. The total is 47 units. Find the number of each kind of ticket.
Prerequisite: Boolean constraints.
Write the mathematical model¶
Let adult and child be integer counts. Their constraints are:
adult >= 0
child >= 0
adult + child = 7
8*adult + 5*child = 47
Nonnegativity is part of the problem, not something inferred from a symbol's
name. Sort Int includes negative integers. The equations happen to have
a nonnegative solution here, but the bounds remain necessary in a model
that will be reused with different totals.
"""Recover ticket counts from a head count and a total price."""
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("produce-models", "true")
var adult = tm.mkConst(tm.getIntegerSort(), "adult")
var child = tm.mkConst(tm.getIntegerSort(), "child")
var zero = tm.mkInteger(0)
solver.assertFormula(tm.mkTerm(Kind.GEQ, adult, zero))
solver.assertFormula(tm.mkTerm(Kind.GEQ, child, zero))
solver.assertFormula(tm.mkTerm(Kind.EQUAL,
tm.mkTerm(Kind.ADD, adult, child), tm.mkInteger(7)))
var price = tm.mkTerm(Kind.ADD,
tm.mkTerm(Kind.MULT, tm.mkInteger(8), adult),
tm.mkTerm(Kind.MULT, tm.mkInteger(5), child))
solver.assertFormula(tm.mkTerm(Kind.EQUAL, price, tm.mkInteger(47)))
var result = solver.checkSat()
assert_true(result.isSat())
var a = solver.getValue(adult)
var c = solver.getValue(child)
assert_equal(String(a), "4")
assert_equal(String(c), "3")
print("tickets:", result)
print("adults:", a, "children:", c)
Run from the repository root pixi run --locked mojo run -I src docs/examples/integers.mojo
Output
tickets: sat
adults: 4 children: 3
Each assertion is one requirement. The price expression contains
multiplication, but only by fixed coefficients. It is linear arithmetic,
so the example uses QF_LIA.
Verify the answer independently¶
Subtract five times the head-count equation from the price equation:
3*adult = 12. Therefore adult = 4 and child = 3. Substitution gives
4 + 3 = 7 and 8*4 + 5*3 = 47, so the returned counts meet both totals.
This reasoning also shows that the model is unique. In general, a solver returns one satisfying assignment; uniqueness requires a separate argument or another query.
The input coefficients in this example are small Mojo Int values passed
to mkInteger. Do not infer from that constructor's input type that
symbolic integer arithmetic wraps like machine arithmetic. These constraints
describe mathematical integers. If the application is about fixed-width
overflow, it needs a bit-vector model and bindings that support that model.
Exercise: an impossible total¶
Could the same seven tickets cost 48 units?
Worked answer. Subtracting the head-count equation now gives
3*adult = 13, which has no integer solution. Change 47 to 48 and expect
unsat. Remove the model reads in that variation. Over the reals the
equations would have a solution, but fractional people would not answer
the original question.
Next: real arithmetic.