Formal Design and Formal Verification
Optimization requirements already make extensive use of sets, quantifiers, equalities, and inequalities. They are therefore natural candidates for formal design before coding and for verification that domain rules, mathematical expressions, and program behavior agree afterward. The goal is not necessarily one machine-checked proof for the entire system. It is to give every important rule explicit premises, checkable semantics, and traceable evidence.
1. Three objects to align
Distinguish three levels for one business rule:
| Level | Notation | Example |
|---|---|---|
| Domain proposition | A point on a triangulated surface has one interpolated height | |
| Mathematical specification | The point belongs to one triangle and its height is the barycentric interpolation | |
| Executable implementation | Triangle selectors, barycentric weights, and their linear constraints |
Let
Implementation correctness requires:
or explicitly:
A unit, bound, integrality condition, or data assumption that is absent from
2. Formal design workflow
2.1 Give the rule an identity
Assign a stable ID, domain name, owning Context, source, and version. A name such as bandwidth.no_outflow_without_service expresses business meaning; an implementation sequence number does not.
2.2 Declare the universe and premises
Specify:
- sets, indices, and empty-set behavior;
- decision-variable domains;
- parameter units, ranges, and null policy;
- finite bounds and how they were derived;
- conversion of strict inequalities;
- solver and business tolerances.
2.3 Write the domain predicate
First write a proposition independent of Big-M, auxiliary variables, or a solver. Split a sentence that contains several conjunctions, exceptions, or “unless” clauses into independently named predicates.
2.4 Derive the mathematical implementation
Two workflows are valid:
- derive executable
deductively from and ; - propose
, then prove .
The second workflow cannot stop at “this is a common formulation” or “the sample solves.” It must state the equivalence conditions, especially finite bounds and variable domains.
2.5 Map it to OSPF elements
- an Aggregation or domain Model owns variables;
- reusable definitions become named intermediate values;
- a named Pipeline registers constraints;
- the Context selects registrations for a business mode or solve path;
- the Application orchestrates without redefining formulas.
2.6 Establish traceability
Link the proposition ID, formula, Pipeline, tests, benchmark data, and change record. Any formula change should reveal the affected tests and consuming contexts.
3. Equivalence obligations
Logical equivalence separates into two directions:
It can also be viewed as positive and negative specification branches:
| Specification | Implementation | Result |
|---|---|---|
| true | true | Positive branch passes |
| true | false | False negative; completeness violation |
| false | false | Negative branch passes |
| false | true | False positive; soundness violation |
Tests cannot cover only expected-feasible examples. Every constraint needs at least one candidate that violates that rule while satisfying as many other premises as possible.
4. Classic derivation I: bivariate linear piecewise function
This section follows the current triangulation-and-barycentric-interpolation implementation in ospf-kotlin. The goal is not merely to obtain approximately correct values at a few sampled points. Starting with the business semantics, it derives linear constraints from a logical disjunction and proves that every feasible symbolic solution describes exactly the piecewise-linear surface over the given triangulation.
4.1 Natural-language rule and premises
Let
For input
must lie in the two-dimensional projection of at least one triangle; - after selecting a triangle that contains
, must equal the barycentric interpolation of its three vertex heights; - if
belongs to no triangle, the function is undefined there and the symbolic model must be infeasible.
Every projected triangle must be non-degenerate:
Triangles that share an edge or vertex must agree on
4.2 Logical language
Do not introduce selectors yet. For every triangle
The first three conditions say that
This definition also expresses the domain: if no
4.3 Derivation of the linear inequalities
The “at least one branch holds” logic must become a mixed-integer linear model. Introduce a selector for every triangle:
Step 1: activate exactly one piece.
If the solver's standard form retains only inequalities, this equality is equivalent to:
Step 2: activate the corresponding barycentric weights. Introduce
When
Step 3: combine the coordinates and heights of all branches.
Define:
Link the inputs and output:
Each equality
The logical disjunction has now been converted completely into binary variables, non-negative variables, and linear inequalities. No arbitrarily large Big-M is needed. In ospf-kotlin, zVars play the role of lambdaVars play the role of
4.4 Equivalence proof
Soundness
Together with
Completeness
Under the non-degeneracy and overlap-consistency premises
Soundness ensures that the model accepts no point outside the domain or with an incorrect interpolated value. Completeness ensures that the linear model represents every surface point admitted by the logical specification.
4.5 Tests derived from the proof
Use the triangle:
The surface on this piece is
At minimum, verify:
| Class | Input | Verification |
|---|---|---|
| Three vertices | Weights are one-hot and the result equals the vertex height | |
| Interior point | Three weights are non-negative and sum to one; | |
| Shared edge | One weight is zero | Adjacent pieces return the same |
| Outside domain | Point belongs to no triangle | Direct evaluation returns empty and the symbolic model is infeasible |
| Degenerate triangle | Construction/evaluation rejects it according to contract, outside the proof domain | |
| Overlapping pieces | One | Either reject them or prove equal heights throughout the overlap |
| Dual-path consistency | Same input | evaluate agrees with |
See Bivariate Linear Piecewise Function for APIs and additional boundaries.
5. Classic derivation II: recommended load weight
This example focuses on the successive translation of one business rule into executable constraints. It does not address code reuse or implementation techniques for intermediate values.
5.1 Natural-language rule
For every loading position
- if the position is not selected for recommended loading, its recommended load weight must be zero;
- if it is selected, its recommended load weight must lie in the permitted interval
; - the interval uses one consistent weight unit and satisfies
.
The “if ... then ...” and interval clauses must first become logical propositions rather than jumping directly to a Big-M row.
5.2 Logical language
Define:
to indicate whether position
Equivalently, it is the conjunction of two implications:
Because
5.3 Derivation of the linear inequalities
First consider the upper bound. The two logical states require:
Since
For the lower bound, the two states require:
They combine into:
The final linear implementation is therefore:
Here
5.4 Equivalence proof
Soundness
- If
, the linear row becomes , so . - If
, it becomes .
Both binary branches satisfy the domain predicate, so the implementation accepts no rule-violating solution.
Completeness
- In the domain state
, substitution gives . - In the domain state
, substitution yields exactly that interval.
Under the premises
we therefore have:
5.5 Tests derived from the proof
For
| Expected | Proof branch | ||
|---|---|---|---|
| 0 | Feasible | Positive unselected case | |
| 0 | Infeasible | Negative unselected case | |
| 1 | Feasible | Closed lower boundary | |
| 1 | Feasible | Interval interior | |
| 1 | Feasible | Closed upper boundary | |
| 1 | Infeasible | Below lower bound | |
| 1 | Infeasible | Above upper bound |
- the equivalence test fails if
is incorrectly relaxed to a continuous variable; - inconsistent units among the bounds and weight are rejected or converted before model construction;
- data with
is rejected during initialization; - a small multi-position instance compares the truth values of
and point by point; - Kotlin and Rust solver tests reach the same feasibility verdict after fixing
.
6. Five verification layers
| Layer | Subject | Recommended evidence |
|---|---|---|
| 1. Domain predicate | Natural language agrees with the formal specification | Domain review, truth table, counterexamples |
| 2. Mathematical derivation | Algebraic proof, SMT/exhaustion, small counterexample model | |
| 3. Symbol construction | OSPF expression equals | Snapshot of coefficients, constants, bounds, and row sense |
| 4. Solver contract | Compiled model implements symbol semantics | Tiny feasible/infeasible models and multi-backend tests |
| 5. Application behavior | Context composition and analysis are correct | Benchmarks, regression suite, and domain invariants |
Passing layer 3 does not prove layer 2. Obtaining the expected objective from a solver does not replace negative-branch verification.
7. Consistency, redundancy, and conflict
Before adding
Consistency
If unsatisfiable, report an unsatisfiable core or minimal conflicting rule set using domain names.
Redundancy
A redundant constraint does not change the feasible region. It may remain as an LP strengthening, readable invariant, or diagnostic aid, but record that purpose instead of treating it as new business knowledge.
Conflict
The new rule rejects every currently feasible case. If that is an intended business change, replace or version the affected rules and benchmarks rather than simply stacking it on top.
8. Numerical semantics
A formal specification uses exact relations; a floating-point solver uses tolerances. A verification report should state:
- the specification relation, such as
; - solver tolerance
; - test-oracle tolerance
; - business tolerance
; - scaling and units.
Do not use a value inside the tolerance gray zone as the only negative test. For an equality, cover
9. Verification in decomposition algorithms
Column generation
Prove that the feasible columns produced by Pricing have the same definition as columns accepted by Master, and verify:
with one cost, coefficient, and dual-sign convention on both sides. Compare against complete column enumeration on small instances.
Benders
Prove that selective registration is equivalent to the complete model, fixed-variable mapping preserves semantics, and every cut is valid for all feasible original solutions. A feasibility cut must reject the current infeasible master candidate; an optimality cut must provide the correct recourse lower bound at its generating point.
10. Change control
When a rule changes:
- update its domain proposition, premises, and version;
- recheck consistency, redundancy, and affected deductions;
- update the mathematical derivation and intermediate-value interfaces;
- modify the Pipeline or function-symbol implementation;
- update tests from the new proof branches;
- run Context, complete-model, decomposition, and backend regressions;
- record whether the feasible region or objective semantics changed.
An implementation optimization that preserves the truth set of
11. Definition of done
A rule is complete only when:
- it has a stable ID, owner, and business description;
- premises, units, domains, and tolerances are explicit;
- both the mathematical specification and executable expression are recorded;
- both equivalence directions have a proof or sufficient checkable evidence;
- positive, negative, boundary, and invalid-input tests exist;
- documentation traces to intermediate values, Pipeline, tests, and benchmarks;
- contract tests pass on every solver backend used in production.