Skip to content

Commit 27305ff

Browse files
authored
Add objective_value method to SolvedModel (#29)
* Add `objective_value` method to `SolvedModel` Add a method to `SolvedModel` to retrieve the objective value for the solution. If the model failed to solve, this value may not be meaningful. Closes #25. * Clarify that returned value is zero in case of error * Add tests
1 parent eb4a672 commit 27305ff

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,13 @@ impl SolvedModel {
611611
self.highs.mut_ptr()
612612
}
613613

614+
/// Get the objective value for the solution.
615+
///
616+
/// If an error occurs (e.g. the model is infeasible) then the returned value may be zero.
617+
pub fn objective_value(&self) -> f64 {
618+
unsafe { highs_sys::Highs_getObjectiveValue(self.as_ptr()) }
619+
}
620+
614621
/// The status of the solution. Should be Optimal if everything went well.
615622
pub fn status(&self) -> HighsModelStatus {
616623
let model_status = unsafe { Highs_getModelStatus(self.highs.unsafe_mut_ptr()) };
@@ -819,4 +826,26 @@ mod test {
819826
assert_eq!(solved.mip_gap(), f64::INFINITY);
820827
assert_eq!(solved.get_solution().columns(), &[50.0]);
821828
}
829+
830+
#[test]
831+
fn test_objective_value() {
832+
use crate::status::HighsModelStatus::Optimal;
833+
use crate::{Model, RowProblem, Sense};
834+
let mut p = RowProblem::default();
835+
p.add_column(1., 0..50);
836+
let mut m = Model::new(p);
837+
m.make_quiet();
838+
m.set_sense(Sense::Maximise);
839+
let solved = m.solve();
840+
assert_eq!(solved.status(), Optimal);
841+
assert_eq!(solved.objective_value(), 50.0);
842+
}
843+
844+
#[test]
845+
fn test_objective_value_empty_model() {
846+
use crate::{Model, RowProblem};
847+
let m = Model::new(RowProblem::default());
848+
let solved = m.solve();
849+
assert_eq!(solved.objective_value(), 0.0);
850+
}
822851
}

0 commit comments

Comments
 (0)