Skip to content

Slack Range in a Quadratic Model ​

QuadraticSlackRangeFunction is the exact distance from a quadratic expression to the closed interval [lower,upper]:

s=max(lower−p(x), p(x)−upper, 0).

The constructor requires lower <= upper.

Solver mathematical model ​

For a=lower−p(x) and b=p(x)−upper, the implementation registers exact positive-part encodings for a and b and returns their sum. Equivalently, with s≥0, binary selectors z0,z1,z2, and valid bounds Mi:

s≥a,s≥b,s≥0,s≤a+M0(1−z0),s≤b+M1(1−z1),s≤M2(1−z2),z0+z1+z2=1.

Thus the result is zero inside the interval and is the exact one-sided violation outside it.

Kotlin API ​

Kotlin provides a native quadratic symbol:

kotlin
val slack = QuadraticSlackRangeFunction(
    input = inputQuadratic,
    lower = Flt64(1.0),
    upper = Flt64(2.0),
    bigM = Flt64(100.0),
    converter = IntoValue.Identity,
    name = "quadratic-slack-range"
)

For a linear expression, use SlackRangeFunction(input, lower, upper, bigM, converter, name). It has the same exact max formulation and exposes the result polynomial of the internal MaxFunction.

Rust API ​

rust
let slack = QuadraticSlackRangeFunction::new(
    23, "quadratic_slack_range", input, 1.0_f64, 2.0_f64,
);
assert_eq!(slack.calculate_value(&tokens, false), Some(1.0));

The exact linear range-distance formulation is fed by the original linear input when it has no quadratic terms. A genuinely quadratic input receives one signed linear bridge variable and one quadratic bridge equality first. with_big_m can provide an explicit Big-M; otherwise registered token bounds are used when available. result_variable() is continuous.

Kotlin/Rust example ​

kotlin
import fuookami.ospf.kotlin.core.solver.value.IntoValue
import fuookami.ospf.kotlin.core.symbol.function.QuadraticSlackRangeFunction
import fuookami.ospf.kotlin.math.algebra.number.Flt64

val slack = QuadraticSlackRangeFunction(
    input = inputQuadratic,
    lower = Flt64(1.0),
    upper = Flt64(2.0),
    bigM = Flt64(100.0),
    converter = IntoValue.Identity,
    name = "quadratic-slack-range"
)
check(slack.evaluate(values, null, IntoValue.Identity) == Flt64(1.0))
rust
use ospf_rust_core::symbol::FunctionSymbol;
use ospf_rust_core::symbol::function::QuadraticSlackRangeFunction;

let slack = QuadraticSlackRangeFunction::new(
    23, "quadratic_slack_range", input, 1.0_f64, 2.0_f64,
);
assert_eq!(slack.calculate_value(&tokens, false), Some(1.0));

Tests and references ​