1. Build a scheduling application¶
The two-job tutorial introduced machine conflicts. This three-part application adds dependencies, explains an impossible request, and searches for the minimum integer deadline. All three parts use one model and one maintained program.
Prerequisites: incremental solving, unsat cores, and scheduling.
Describe the jobs¶
A small workshop has two machines and four jobs:
| Job | Index in the program | Duration | Machine | Required predecessor |
|---|---|---|---|---|
| A: prepare | 0 | 2 | 0 | None |
| B: cut | 1 | 3 | 1 | A |
| C: finish | 2 | 2 | 0 | B |
| D: inspect | 3 | 2 | 1 | A |
Time starts at zero and advances in integer slots. Each job runs without
interruption on its assigned machine. Finishing at a time allows another
job to start then, so job occupancy uses half-open intervals [start, end).
The model covers these requirements; it does not include setup times,
optional jobs, alternate machines, or preemption.
Keep each job's start as a symbol and its end as an expression. The requirements list owns copies of its terms, and the parallel labels list holds explanations for people:
var durations: List[Int] = [2, 3, 2, 2]
var starts = List[Term]()
var ends = List[Term]()
for i in range(len(durations)):
var start = tm.mkConst(tm.getIntegerSort(), "start_" + String(i))
starts.append(start.copy())
ends.append(tm.mkTerm(Kind.ADD, start, tm.mkInteger(durations[i])))
add_requirement(requirements, labels,
tm.mkTerm(Kind.GEQ, start, tm.mkInteger(0)),
"job " + String(i) + " starts at or after zero")
Term supports implicit copies and .copy(). The example writes .copy()
explicitly when storing a term it also needs afterward. Either form retains
a reference to the same symbolic expression; it does not create another
decision variable.
See ownership for the runtime contract.
Add dependencies and machine capacity¶
Precedence is an inequality between one end and another start. For jobs sharing a machine, use a disjunction allowing either order. Here A precedes C through B, so their machine conflict is already implied by the chain. Keeping that condition makes the resource model explicit, but it need not appear in an unsat core.
# Jobs A=0, B=1, C=2, D=3. The chain A -> B -> C takes seven units.
add_requirement(requirements, labels,
tm.mkTerm(Kind.LEQ, ends[0], starts[1]), "A before B")
add_requirement(requirements, labels,
tm.mkTerm(Kind.LEQ, ends[1], starts[2]), "B before C")
add_requirement(requirements, labels,
tm.mkTerm(Kind.LEQ, ends[0], starts[3]), "A before D")
# A and C use machine 0; B and D use machine 1.
add_requirement(requirements, labels, tm.mkTerm(Kind.OR,
tm.mkTerm(Kind.LEQ, ends[0], starts[2]),
tm.mkTerm(Kind.LEQ, ends[2], starts[0])), "machine 0 does not overlap")
add_requirement(requirements, labels, tm.mkTerm(Kind.OR,
tm.mkTerm(Kind.LEQ, ends[1], starts[3]),
tm.mkTerm(Kind.LEQ, ends[3], starts[1])), "machine 1 does not overlap")
for requirement in requirements:
solver.assertFormula(requirement)
The helper add_requirement appends a formula and its label together.
Only after the model is assembled does the program assert those formulas.
This keeps the same requirements available for model validation and for
mapping a returned core back to the application.
Establish a feasible upper bound¶
The check_deadline helper pushes a scope, constrains every end to be at
most the candidate deadline, and checks the result. For sat, it evaluates
both the shared requirements and the temporary deadline constraints in the
model before popping. It returns the result after removing the temporary
constraints, ready for another query.
A deadline of ten is feasible. One schedule is A at [0,2), B at [2,5),
C at [5,7), and D at [5,7). C and D overlap in time on different machines,
which the model permits. The tests validate requirements rather than
requiring the solver to choose this particular schedule.
Run the complete application¶
The later chapters explain the conflict and optimization sections of the same file. The full source and its expected output are available here:
Complete scheduling application and expected output
"""Build, diagnose, and optimize a four-job schedule with one shared model."""
from std.testing import assert_equal, assert_true
from cvc5_mojo import TermManager, Solver, Term, Result, Kind
def add_requirement(mut requirements: List[Term], mut labels: List[String], term: Term, label: String):
requirements.append(term.copy())
labels.append(label)
def check_deadline(
solver: Solver, tm: TermManager, ends: List[Term],
requirements: List[Term], deadline: Int,
) raises -> Result:
solver.push()
var bounds = List[Term]()
for end in ends:
var bound = tm.mkTerm(Kind.LEQ, end, tm.mkInteger(deadline))
bounds.append(bound.copy())
solver.assertFormula(bound)
var result = solver.checkSat()
if result.isSat():
for requirement in requirements:
assert_equal(String(solver.getValue(requirement)), "true")
for bound in bounds:
assert_equal(String(solver.getValue(bound)), "true")
solver.pop()
return result^
def main() raises:
var tm = TermManager()
var solver = Solver(tm)
solver.setLogic("QF_LIA")
solver.setOption("produce-models", "true")
solver.setOption("produce-unsat-cores", "true")
solver.setOption("incremental", "true")
solver.setOption("tlimit-per", "5000")
var requirements = List[Term]()
var labels = List[String]()
var durations: List[Int] = [2, 3, 2, 2]
var starts = List[Term]()
var ends = List[Term]()
for i in range(len(durations)):
var start = tm.mkConst(tm.getIntegerSort(), "start_" + String(i))
starts.append(start.copy())
ends.append(tm.mkTerm(Kind.ADD, start, tm.mkInteger(durations[i])))
add_requirement(requirements, labels,
tm.mkTerm(Kind.GEQ, start, tm.mkInteger(0)),
"job " + String(i) + " starts at or after zero")
# Jobs A=0, B=1, C=2, D=3. The chain A -> B -> C takes seven units.
add_requirement(requirements, labels,
tm.mkTerm(Kind.LEQ, ends[0], starts[1]), "A before B")
add_requirement(requirements, labels,
tm.mkTerm(Kind.LEQ, ends[1], starts[2]), "B before C")
add_requirement(requirements, labels,
tm.mkTerm(Kind.LEQ, ends[0], starts[3]), "A before D")
# A and C use machine 0; B and D use machine 1.
add_requirement(requirements, labels, tm.mkTerm(Kind.OR,
tm.mkTerm(Kind.LEQ, ends[0], starts[2]),
tm.mkTerm(Kind.LEQ, ends[2], starts[0])), "machine 0 does not overlap")
add_requirement(requirements, labels, tm.mkTerm(Kind.OR,
tm.mkTerm(Kind.LEQ, ends[1], starts[3]),
tm.mkTerm(Kind.LEQ, ends[3], starts[1])), "machine 1 does not overlap")
for requirement in requirements:
solver.assertFormula(requirement)
var initial = check_deadline(solver, tm, ends, requirements, 10)
assert_true(initial.isSat())
print("deadline 10:", initial)
solver.push()
var rush = tm.mkTerm(Kind.LEQ, ends[2], tm.mkInteger(6))
solver.assertFormula(rush)
var conflict = solver.checkSat()
assert_true(conflict.isUnsat())
var core = solver.getUnsatCore()
var core_labels = List[String]()
var includes_rush = False
for term in core:
if term == rush:
core_labels.append("C must finish by 6")
includes_rush = True
else:
var matched = False
for i in range(len(requirements)):
if term == requirements[i]:
core_labels.append(labels[i])
matched = True
break
assert_true(matched)
assert_true(includes_rush)
assert_equal(len(core_labels), len(core))
print("rush deadline:", conflict)
print("core labels recognized:", len(core_labels) == len(core))
solver.pop()
# Maintain: low is infeasible and high is feasible.
var low = 0
var high = 10
assert_true(check_deadline(solver, tm, ends, requirements, low).isUnsat())
assert_true(check_deadline(solver, tm, ends, requirements, high).isSat())
while high - low > 1:
var middle = low + (high - low) // 2
var candidate = check_deadline(solver, tm, ends, requirements, middle)
if candidate.isSat():
high = middle
elif candidate.isUnsat():
low = middle
else:
raise Error("deadline search inconclusive; keep the last certified bounds")
assert_equal(high, 7)
assert_true(check_deadline(solver, tm, ends, requirements, high).isSat())
assert_true(check_deadline(solver, tm, ends, requirements, high - 1).isUnsat())
print("minimum integer deadline:", high)
print("adjacent smaller deadline: unsat")
Run from the repository root pixi run --locked mojo run -I src docs/examples/schedule_application.mojo
Output
deadline 10: sat
rush deadline: unsat
core labels recognized: True
minimum integer deadline: 7
adjacent smaller deadline: unsat
Exercise: add a dependency¶
Suppose inspection D must finish before C starts. What happens to the minimum deadline?
Worked answer. It becomes nine. After A takes two units, B and D need five units on machine 1. C then takes two more units on machine 0. Either ordering of B and D gives this lower bound, and a schedule attains it. Add the new inequality to the requirements list, then update the expected optimal deadline in a copy of the program.