pub struct GeLineq {
pub coeffs: Vec<i64>,
pub bounds: Vec<(i64, i64)>,
pub bias: i64,
pub indices: Vec<u32>,
}Expand description
Data structure for linear inequalities on the following form
$$ c_0 * v_0 + c_1 * v_1 + … + c_n * v_n + bias \ge 0 $$ for $ c \in $ coeffs and $ v $ are variables which can take on the values
given by bounds. Indices represents the global indices of the variables. Note that the length of coeffs, bounds and indices must be the same.
Fields
coeffs: Vec<i64>bounds: Vec<(i64, i64)>bias: i64indices: Vec<u32>Implementations
sourceimpl GeLineq
impl GeLineq
sourcepub fn merge_disj(ge_lineq1: &GeLineq, ge_lineq2: &GeLineq) -> Option<GeLineq>
pub fn merge_disj(ge_lineq1: &GeLineq, ge_lineq2: &GeLineq) -> Option<GeLineq>
Takes two GeLineqs and merge those into one GeLineq under the condition that one of the GeLineqs must be satisfied.
If it’s not possible to merge the inequalities, i.e. it’s impossible to preserve the logic, none is returned.
Example:
If atleast one of the two linear inequalities $ x + y - 1 \ge 0 $ where $ x, y \in \{0, 1 \}$ and $ a + b - 1 \ge 0 $ where $ a, b \in \{0, 1\}$ must hold, then they can be merged into one linear inequality as $ x + y + a + b - 1 \ge 0$ where $ x, y, a, b \in \{0, 1\}$. Note that the logic is preserved, i.e the merged inequality will be valid if at least one of the two original inequalities are valid. Likewise, the merged constraint will not be valid if none of the original inequalites are.
use puanrs::GeLineq;
let ge_lineq1:GeLineq = GeLineq {
coeffs : vec![1, 1],
bounds : vec![(0, 1), (0, 1)],
bias : -1,
indices : vec![1, 2]
};
let ge_lineq2: GeLineq = GeLineq {
coeffs : vec![1, 1],
bounds : vec![(0, 1), (0, 1)],
bias : -1,
indices : vec![3, 4]
};
let expected: GeLineq = GeLineq {
coeffs : vec![1, 1, 1, 1],
bounds : vec![(0, 1), (0, 1), (0, 1), (0, 1)],
bias : -1,
indices : vec![1, 2, 3, 4]
};
let actual = GeLineq::merge_disj(&ge_lineq1, &ge_lineq2);
assert_eq!(actual.as_ref().expect("Not possible to merge lineqs").coeffs, expected.coeffs);
assert_eq!(actual.as_ref().expect("Not possible to merge lineqs").bounds, expected.bounds);
assert_eq!(actual.as_ref().expect("Not possible to merge lineqs").bias, expected.bias);
assert_eq!(actual.as_ref().expect("Not possible to merge lineqs").indices, expected.indices);sourcepub fn merge_conj(ge_lineq1: &GeLineq, ge_lineq2: &GeLineq) -> Option<GeLineq>
pub fn merge_conj(ge_lineq1: &GeLineq, ge_lineq2: &GeLineq) -> Option<GeLineq>
Takes two GeLineqs and merge those into one GeLineq under the condition that both of the GeLineqs must be valid.
If it’s not possible to merge the inequalities, i.e. it’s impossible to preserve the logic, none is returned.
sourcepub fn substitution(
main_gelineq: &GeLineq,
variable_index: u32,
sub_gelineq: &GeLineq
) -> Option<GeLineq>
pub fn substitution(
main_gelineq: &GeLineq,
variable_index: u32,
sub_gelineq: &GeLineq
) -> Option<GeLineq>
Takes a GeLineq, referred to as main GeLineq which has a variable (given by variable index) which in turn is a GeLineq, referred to as the sub GeLineq. The function substitutes the variable with the sub GeLineq into the main GeLineq if possible.
Example
Given the lineq x + Y - 2 >= 0 where Y: a + b - 1 >= 0 a substitution would give 2x + a + b - 3 >= 0. Lets see it in code:
use puanrs::GeLineq;
let main_gelineq:GeLineq = GeLineq {
coeffs : vec![1, 1],
bounds : vec![(0, 1), (0, 1)],
bias : -2,
indices : vec![1, 2]
};
let sub_gelineq: GeLineq = GeLineq {
coeffs : vec![1, 1],
bounds : vec![(0, 1), (0, 1)],
bias : -1,
indices : vec![3, 4]
};
let result = GeLineq::substitution(&main_gelineq, 2, &sub_gelineq);
assert_eq!(vec![2, 1, 1], result.as_ref().expect("No result generated").coeffs);
assert_eq!(vec![(0,1), (0,1), (0,1)], result.as_ref().expect("No result generated").bounds);
assert_eq!(-3, result.as_ref().expect("No result generated").bias);
assert_eq!(vec![1, 3, 4], result.as_ref().expect("No result generated").indices);sourcepub fn min_max_coefficients(gelineq: &GeLineq) -> Option<GeLineq>
pub fn min_max_coefficients(gelineq: &GeLineq) -> Option<GeLineq>
Takes a GeLineq and minimizes/maximizes the coefficients while preserving the logic
Example
Consider the GeLineq 2x + y + z >= 1 where x, y, z takes values between 0 and 1. This inequlity can be written as x + y + z >= 1 while preserving the logic.
use puanrs::GeLineq;
let gelineq: GeLineq = GeLineq {
coeffs : vec![2, 1, 1],
bounds : vec![(0,1), (0,1), (0,1)],
bias : -1,
indices : vec![0, 1, 2]
};
let result = GeLineq::min_max_coefficients(&gelineq);
assert_eq!(vec![1, 1, 1], result.as_ref().expect("").coeffs);