Skip to content

Commit

Permalink
refactor: remove OptimisationProcedure::new
Browse files Browse the repository at this point in the history
The new function should be associated with the struct instead of the
trait.
  • Loading branch information
maartenflippo committed Feb 10, 2025
1 parent 947896d commit 630977e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 17 deletions.
1 change: 0 additions & 1 deletion pumpkin-solver/src/bin/pumpkin-solver/flatzinc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use pumpkin_solver::constraints::cumulative;
use pumpkin_solver::optimisation::linear_sat_unsat::LinearSatUnsat;
use pumpkin_solver::optimisation::linear_unsat_sat::LinearUnsatSat;
use pumpkin_solver::optimisation::OptimisationDirection;
use pumpkin_solver::optimisation::OptimisationProcedure;
use pumpkin_solver::optimisation::OptimisationStrategy;
use pumpkin_solver::options::CumulativeOptions;
use pumpkin_solver::results::solution_iterator::IteratedSolution;
Expand Down
26 changes: 17 additions & 9 deletions pumpkin-solver/src/optimisation/linear_sat_unsat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,29 @@ use crate::variables::IntegerVariable;
use crate::ConstraintOperationError;
use crate::Solver;

/// Implements the linear SAT-UNSAT (LSU) optimisation procedure.
#[derive(Debug, Clone, Copy)]
pub struct LinearSatUnsat<Var: IntegerVariable, Callback> {
pub struct LinearSatUnsat<Var, Callback> {
direction: OptimisationDirection,
objective: Var,
solution_callback: Callback,
}

impl<Var, Callback> LinearSatUnsat<Var, Callback> {
/// Create a new instance of [`LinearSatUnsat`].
pub fn new(
direction: OptimisationDirection,
objective: Var,
solution_callback: Callback,
) -> Self {
Self {
direction,
objective,
solution_callback,
}
}
}

impl<Var: IntegerVariable, Callback> LinearSatUnsat<Var, Callback> {
/// Given the current objective value `best_objective_value`, it adds a constraint specifying
/// that the objective value should be at most `best_objective_value - 1`. Note that it is
Expand Down Expand Up @@ -64,14 +80,6 @@ where
Var: IntegerVariable,
Callback: Fn(&Solver, SolutionReference),
{
fn new(direction: OptimisationDirection, objective: Var, solution_callback: Callback) -> Self {
Self {
direction,
objective,
solution_callback,
}
}

fn optimise(
&mut self,
brancher: &mut impl Brancher,
Expand Down
18 changes: 13 additions & 5 deletions pumpkin-solver/src/optimisation/linear_unsat_sat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,32 @@ use crate::termination::TerminationCondition;
use crate::variables::IntegerVariable;
use crate::Solver;

/// Implements the linear UNSAT-SAT (LUS) optimisation procedure.
#[derive(Debug, Clone, Copy)]
pub struct LinearUnsatSat<Var: IntegerVariable, Callback> {
pub struct LinearUnsatSat<Var, Callback> {
direction: OptimisationDirection,
objective: Var,
solution_callback: Callback,
}

impl<Var: IntegerVariable, Callback: Fn(&Solver, SolutionReference)>
OptimisationProcedure<Var, Callback> for LinearUnsatSat<Var, Callback>
{
fn new(direction: OptimisationDirection, objective: Var, solution_callback: Callback) -> Self {
impl<Var, Callback> LinearUnsatSat<Var, Callback> {
/// Create a new instance of [`LinearUnsatSat`].
pub fn new(
direction: OptimisationDirection,
objective: Var,
solution_callback: Callback,
) -> Self {
Self {
direction,
objective,
solution_callback,
}
}
}

impl<Var: IntegerVariable, Callback: Fn(&Solver, SolutionReference)>
OptimisationProcedure<Var, Callback> for LinearUnsatSat<Var, Callback>
{
fn optimise(
&mut self,
brancher: &mut impl Brancher,
Expand Down
2 changes: 0 additions & 2 deletions pumpkin-solver/src/optimisation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ pub mod linear_sat_unsat;
pub mod linear_unsat_sat;

pub trait OptimisationProcedure<Var: IntegerVariable, Callback: Fn(&Solver, SolutionReference)> {
fn new(direction: OptimisationDirection, objective: Var, solution_callback: Callback) -> Self;

fn optimise(
&mut self,
brancher: &mut impl Brancher,
Expand Down

0 comments on commit 630977e

Please sign in to comment.