-
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 to python
- Loading branch information
1 parent
630977e
commit 6ff4a13
Showing
4 changed files
with
160 additions
and
20 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 | ||
|
||
|