Balance ternaryzation
Contract
BalanceTernaryzationFunction maps a linear expression to the three values
The endpoints null/None) in direct evaluation and infeasible in the registered model. Inputs outside every defined branch, including a missing variable value, also return no value.
API
BalanceTernaryzationFunction(
x: LinearPolynomial<V>,
epsilon: Flt64 = Flt64(1e-6),
converter: IntoValue<V>,
name: String = "bter",
displayName: String? = null,
fallbackBigM: Flt64 = Flt64(1e6),
strictBoundary: Flt64 = Flt64(NONZERO_TOLERANCE)
)BalanceTernaryzationFunction::new(
id: u64,
name: &str,
input: Linear<V>,
epsilon: V,
fallback_big_m: V,
) -> SelfBoth implementations expose the result variable and the mutually exclusive positive and negative binary variables. A finite bound inferred from the input is preferred; the fallback Big-M is used only when such a bound is unavailable.
Mathematical model passed to the solver
Let 1e-10 in the current implementations). The generated model is
Thus
Minimal example
import fuookami.ospf.kotlin.core.solver.value.IntoValue
import fuookami.ospf.kotlin.core.symbol.function.BalanceTernaryzationFunction
import fuookami.ospf.kotlin.math.algebra.number.Flt64
import fuookami.ospf.kotlin.math.symbol.polynomial.LinearPolynomial
val function = BalanceTernaryzationFunction(
x = LinearPolynomial(emptyList(), Flt64(2.0)),
epsilon = Flt64(0.1),
converter = IntoValue.Identity,
name = "direction"
)
check(function.evaluate(emptyMap()) == Flt64.one)use ospf_rust_core::symbol::FunctionSymbol;
use ospf_rust_core::symbol::flatten::Linear;
use ospf_rust_core::symbol::function::BalanceTernaryzationFunction;
use ospf_rust_core::token::VecTokenList;
let function = BalanceTernaryzationFunction::new(
1,
"direction",
Linear::new(vec![], 2.0),
0.1,
1_000_000.0,
);
let tokens = VecTokenList::<f64>::new();
assert_eq!(function.calculate_value(&tokens, false), Some(1.0));Source and tests
- Kotlin implementation
- Kotlin dedicated test
- Kotlin example
- Rust implementation
- Rust dedicated test
- Rust examples