-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: expose the optimisation API in the python wrapper (#148)
The Python API does not enforce a particular optimisation routine. At the moment, we support: - LSU - LUS Both are, therefore, present in Python as well.
- Loading branch information
1 parent
947896d
commit 49bf281
Showing
8 changed files
with
207 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use pyo3::prelude::*; | ||
|
||
use crate::result::Solution; | ||
|
||
#[pyclass] | ||
pub enum OptimisationResult { | ||
/// The problem was solved to optimality, and the solution is an optimal one. | ||
Optimal(Solution), | ||
/// At least one solution was identified, and the solution is the best one. | ||
Satisfiable(Solution), | ||
/// The problem was unsatisfiable. | ||
Unsatisfiable(), | ||
/// None of the other variants were concluded. | ||
Unknown(), | ||
} | ||
|
||
#[pyclass(eq, eq_int)] | ||
#[derive(Clone, Copy, PartialEq, Eq)] | ||
pub enum Optimiser { | ||
LinearSatUnsat, | ||
LinearUnsatSat, | ||
} | ||
|
||
#[pyclass(eq, eq_int)] | ||
#[derive(Clone, Copy, PartialEq, Eq)] | ||
pub enum Direction { | ||
Minimise, | ||
Maximise, | ||
} | ||
|
||
pub fn register(m: &Bound<'_, PyModule>) -> PyResult<()> { | ||
m.add_class::<Optimiser>()?; | ||
m.add_class::<Direction>()?; | ||
m.add_class::<OptimisationResult>()?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from pumpkin_py import Model | ||
from pumpkin_py.optimisation import Direction, OptimisationResult | ||
|
||
|
||
def test_linear_sat_unsat_minimisation(): | ||
model = Model() | ||
|
||
objective = model.new_integer_variable(1, 5, name="objective") | ||
|
||
result = model.optimise(objective, direction=Direction.Minimise) | ||
|
||
assert isinstance(result, OptimisationResult.Optimal) | ||
|
||
solution = result._0 | ||
assert solution.int_value(objective) == 1 | ||
|
||
|
||
def test_linear_sat_unsat_maximisation(): | ||
model = Model() | ||
|
||
objective = model.new_integer_variable(1, 5, name="objective") | ||
|
||
result = model.optimise(objective, direction=Direction.Maximise) | ||
|
||
assert isinstance(result, OptimisationResult.Optimal) | ||
|
||
solution = result._0 | ||
assert solution.int_value(objective) == 5 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters