Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,13 @@ impl SolvedModel {
self.highs.mut_ptr()
}

/// Get the objective value for the solution.
///
/// If an error occurs (e.g. the model is infeasible) then the returned value may be zero.
pub fn objective_value(&self) -> f64 {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about changing the return type to Option<f64>? I feel like this would be a lot more intuitive than having to check manually whether the value is meaningful or not. It also feels a bit more rusty?

Just throwing this suggestion into the discussion from the sideline, feel free to ignore me if you have a strong opinion about it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree in general (zero is a pretty crap default value), though I am wondering if there are cases where the user might want the objective value when the HighsModelStatus isn't Optimal (i.e. it hasn't fully converged). I'm not sure what HiGHS would do in this case.

What do you think @lovasoa? Are you happy to return None if the status is anything other than optimal?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alternative would be to panic if the status is not optimal, then the onus is on the user to ensure this doesn't happen.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally the behavior should match what highs does ? Return None in situations where Highs does not set the value, return Some when highs does have a value

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You definitively want the objective if the search stopped before proving the optimality. The case where this value would be meaningless are unfeasible, unbounded, and no feasible solution found.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lovasoa I'm assuming you'd still like to return None in the case that no error has occurred?

Looking at HighsModelStatus, I think the following statuses would indicate that there is a meaningful objective value:

  • Optimal
  • ObjectiveTarget
  • ReachedTimeLimit
  • ReachedIterationLimit

(We could also include Unknown if we want to be conservative.)]

So if we want to return an Option<f64> from this method, we could return Some(objective_value) if the status matches any of the above, else None.

HOWEVER: I'm not totally confident that an objective value would not be returned for certain other statuses, such as SolveError. It would be good to have this confirmed.

Alternatively, we can just update the doc comment to state that zero may be returned in case of error and put the responsibility back onto the user to check the status before using this value.

Let me know what you prefer.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fairly certain that the objective value - as with everything in Highs::info_ - always has a value, since it's cleared when HiGHS:: run() is called. I'll confirm tonight, when I'm at a computer

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the response @jajhall! I've noticed it seems to have a value of 0.0 when the model is infeasible, for example, but I'm not sure about other cases. I assume zero is the default value.

I think the suggestion here was that we could return None instead for cases where there isn't a meaningful objective value, as that's maybe a bit more "Rust-y" (it would help prevent accidental use of the value in cases where the model is infeasible etc.). But we could just return the value given by HiGHS and mention that it might be zero in case of error in the documentation, which I think would also be fine. That would have the advantage of being simpler and not requiring changes if new statuses are added to HiGHS in future.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay with both solutions. If we have a guarantee that Highs_getObjectiveValue does not access uninitialized memory even in cases when no value is available, then we can just return an f64, and be clear about its value in the documentation in all cases.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds simplest. I'll update the docs and add a test as you suggest.

unsafe { highs_sys::Highs_getObjectiveValue(self.as_ptr()) }
}

/// The status of the solution. Should be Optimal if everything went well.
pub fn status(&self) -> HighsModelStatus {
let model_status = unsafe { Highs_getModelStatus(self.highs.unsafe_mut_ptr()) };
Expand Down Expand Up @@ -819,4 +826,26 @@ mod test {
assert_eq!(solved.mip_gap(), f64::INFINITY);
assert_eq!(solved.get_solution().columns(), &[50.0]);
}

#[test]
fn test_objective_value() {
use crate::status::HighsModelStatus::Optimal;
use crate::{Model, RowProblem, Sense};
let mut p = RowProblem::default();
p.add_column(1., 0..50);
let mut m = Model::new(p);
m.make_quiet();
m.set_sense(Sense::Maximise);
let solved = m.solve();
assert_eq!(solved.status(), Optimal);
assert_eq!(solved.objective_value(), 50.0);
}

#[test]
fn test_objective_value_empty_model() {
use crate::{Model, RowProblem};
let m = Model::new(RowProblem::default());
let solved = m.solve();
assert_eq!(solved.objective_value(), 0.0);
}
}
Loading