pumpkin_solver/api/outputs/solution_callback_arguments.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
use crate::results::Solution;
use crate::Solver;
/// The input which is passed to the solution callback (which can be set using
/// [`Solver::with_solution_callback`]).
///
/// Provides direct access to the solution via [`SolutionCallbackArguments::solution`] and allows
/// logging the statistics of the [`Solver`] using [`SolutionCallbackArguments::log_statistics`].
#[derive(Debug)]
pub struct SolutionCallbackArguments<'a, 'b> {
/// The solver which found the solution
solver: &'a Solver,
/// The solution which has been found
pub solution: &'b Solution,
/// The (optional) objective value provided to the [`Solver`].
objective_value: Option<i64>,
}
impl<'a, 'b> SolutionCallbackArguments<'a, 'b> {
pub(crate) fn new(
solver: &'a Solver,
solution: &'b Solution,
objective_value: Option<i64>,
) -> Self {
Self {
solver,
solution,
objective_value,
}
}
/// Log the statistics of the [`Solver`].
///
/// If the solution was found using [`Solver::minimise`] or [`Solver::maximise`] then the
/// objective value of the current solution is included in the statistics.
pub fn log_statistics(&self) {
if let Some(objective_value) = self.objective_value {
self.solver.log_statistics_with_objective(objective_value)
} else {
self.solver.log_statistics()
}
}
}