Understand variable types and value representation in good_lp
mainIn good_lp, all variable values and coefficients are internally represented as f64. This affects how you define constraints and how you retrieve results:
- Defining Constraints: You can use either
f64ori32to express constraints on variable ranges.i32values are losslessly converted tof64. Note that types likeusizecannot be used directly if they cannot be converted tof64losslessly. - Integer Variables: You can restrict variables to integer values using the
(integer)qualifier within thevariables!macro. - Solution Values: Regardless of whether a variable is defined as continuous or integer, the
solution.value(variable)method always returns anf64. You must account for this when performing arithmetic on solution values to avoid compilation errors.
// Correct use of f64 and i32 to specify feasible ranges for Variables
variables! {
problem:
a <= 10.0;
2 <= b (integer) <= 4; // Variables can be restricted using qualifiers like (integer)
};
let model = problem
.maximise(b)
.using(default_solver)
.with(constraint!(a + 2 <= b))
.with(constraint!(1 + a >= 4.0 - b));
// Accessing solution values (always returns f64)
println!("a={} b={}", solution.value(a), solution.value(b));
println!("a + b = {}", solution.eval(a + b));
// WARNING: This will cause a compilation error because solution.value(a) is f64
// println!("a + 1 = {}", solution.value(a) + 1);