Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Lower bound

lower_bound bounds the optimum for equal-size parts: below for minimization and above for maximization. Use it to assess the remaining gap of a heuristic solution.

Signature

The Python declarations show public defaults. The Rust declaration shows the entry point; its types are imported from kap.

def lower_bound(cost_matrices, *, maximize=False, max_iter=300) -> int | float:
    ...
#![allow(unused)]
fn main() {
pub fn lower_bound<T: Cost>(
    mats: &[MatrixView<'_, T>],
    opts: &LowerBoundOptions<T::Acc>,
) -> Result<T::Acc, KapError>
where
    T::Acc: LowerBoundAcc;
}

Parameters

PythonRustDefaultMeaning
cost_matricesmatsRequiredPairwise matrices in combinations order.
maximizeopts.senseFalse / Sense::MinimizeMaximize the original costs when true / Sense::Maximize.
max_iteropts.max_iter300Nonnegative subgradient updates per triple; zero still evaluates the initial seeds.
-opts.upperNoneOptional finite numerical upper bound on the whole optimum, in the original objective sense. The caller must establish its validity.

Returns

Python returns an int for integer/boolean inputs and a float otherwise. Rust returns Result<T::Acc, KapError> with i64 or f64 accumulation. Integer costs give a rigorous bound. Float bounds are computed in floating point and can exceed the true minimum (or fall below the true maximum) by rounding error on the order of machine epsilon times the sum of the cost magnitudes involved. This error can be large when costs of opposite sign nearly cancel.

For a positive minimization solution cost c and bound b, (c-b)/c bounds the relative improvement still possible. The example uses this definition; division by zero or a negative c needs a different normalization.

Errors

PythonRustCondition
ValueErrorKapError::BadCountThe matrix count does not describe 2 through 64 parts in the native core.
ValueErrorKapError::ShapeMismatchMatrix dimensions disagree about a part’s size.
TypeErrorRejected by the Cost trait at compilationA matrix has an unsupported element type.
OverflowError-All Python matrices are integer or boolean and a uint64 value exceeds int64.
ValueErrorKapError::TooManyVerticesA native part exceeds u32 vertex capacity.
ValueErrorKapError::LapA constituent LAP encounters an invalid numeric entry or an infeasible assignment.

Python raises RuntimeError if the required native core is unavailable.

Python raises TypeError for non-integer or boolean max_iter, and ValueError for negative values or unequal part sizes. Rust returns KapError::Unsupported for unequal sizes, a non-finite upper, or exhausted arithmetic headroom. Invalid numeric entries and infeasible pairwise LAPs produce KapError::Lap. If a multiplier update exhausts headroom after valid bounds were evaluated, that triple retains its best evaluated bound.

Example

import numpy as np
import kap

C01 = np.array([[19, 22, 6], [27, 16, 22], [16, 26, 10]], dtype=np.int64)
C02 = np.array([[7, 10, 22], [15, 19, 26], [9, 27, 9]], dtype=np.int64)
C12 = np.array([[6, 25, 5], [7, 3, 12], [19, 13, 2]], dtype=np.int64)
costs = [C01, C02, C12]

result = kap.local_search(costs)
cost = result.cost
bound = kap.lower_bound(costs, max_iter=30)
print("cost =", cost)
print("bound =", bound)
print(f"gap = {(cost - bound) / cost:.3f}")
use kap::{Algo, LowerBoundOptions, MatrixView, Options, k_assignment, lower_bound};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let c01 = [19, 22, 6, 27, 16, 22, 16, 26, 10i64];
    let c02 = [7, 10, 22, 15, 19, 26, 9, 27, 9i64];
    let c12 = [6, 25, 5, 7, 3, 12, 19, 13, 2i64];
    let costs = [
        MatrixView::from_row_major(&c01, 3, 3)?,
        MatrixView::from_row_major(&c02, 3, 3)?,
        MatrixView::from_row_major(&c12, 3, 3)?,
    ];
    let result = k_assignment(&costs, Algo::Dv1, &Options::default())?;
    let opts = LowerBoundOptions {
        max_iter: 30,
        ..LowerBoundOptions::default()
    };
    let bound = lower_bound(&costs, &opts)?;
    println!("cost = {}", result.cost);
    println!("bound = {bound}");
    println!(
        "gap = {:.3}",
        (result.cost - bound) as f64 / result.cost as f64
    );
    Ok(())
}

Python output:

cost = 97
bound = 91
gap = 0.062

The Rust example prints the same cost, bound, and gap.

Notes and limits

  • For k=2, the bound is the exact LAP optimum. For k=3, it is an axial Lagrangian bound, at best equal to the LP relaxation bound.
  • For k≥4, the function averages all triple bounds with scale 1/(k−2). Random non-metric costs can leave a large gap; measured metric instances gave smaller gaps.
  • Each active triple uses O(n²) scratch space. The number of triples is k(k−1)(k−2)/6.
  • The initial evaluations include a pairwise-LAP sum and zero/dual multiplier seeds. Increasing max_iter retains all earlier evaluations; the returned bound does not weaken.
  • Rust upper clamps the final numerical bound in either objective sense. It is not a feasible-solution argument and is not used as the per-triple Polyak target.
  • The method is deterministic and has no time-limit parameter. Equal zero-sized parts return zero.

See also

Proven-optimum benchmarks · Exact solver · Input and dtypes