Skip to content

Commit

Permalink
Cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
mmghannam committed Jul 20, 2024
1 parent 33c0801 commit ea247af
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 48 deletions.
2 changes: 1 addition & 1 deletion examples/simple_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ fn main() {
assert!((lp.obj_val() - 10.0).abs() < 1e-6);

assert!(lp.solving_time() >= 0.0);
}
}
3 changes: 1 addition & 2 deletions src/basis_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ pub enum ColBasisStatus {
Unknown = 5,
}


/// Row basis status
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RowBasisStatus {
Expand Down Expand Up @@ -56,4 +55,4 @@ impl From<i32> for RowBasisStatus {
_ => panic!("Invalid value for BasisStatus"),
}
}
}
}
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ pub mod ffi {
pub use soplex_sys::*;
}

mod basis_status;
mod model;
mod soplex_ptr;
mod param;
mod basis_status;
mod soplex_ptr;
pub use basis_status::*;

pub use param::*;

pub use model::*;
pub use model::*;
100 changes: 68 additions & 32 deletions src/model.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{BoolParam, ColBasisStatus, ffi, IntParam, ObjSense, RealParam, RowBasisStatus};
use crate::param::{ALGORITHM_PARAM_ID, OBJSENSE_PARAM_ID, REPR_PARAM_ID};
use crate::soplex_ptr::SoplexPtr;
use crate::status::Status;
use crate::{ffi, BoolParam, ColBasisStatus, IntParam, ObjSense, RealParam, RowBasisStatus};

/// A linear programming model.
pub struct Model {
Expand All @@ -17,7 +17,9 @@ pub struct ColId(usize);
impl Model {
/// Creates a new linear programming model.
pub fn new() -> Self {
Self { inner: SoplexPtr::new() }
Self {
inner: SoplexPtr::new(),
}
}

/// Adds a column to the model.
Expand All @@ -32,16 +34,26 @@ impl Model {
/// # Returns
///
/// The `ColId` of the added column.
pub fn add_col<const N: usize>(&mut self, mut colentries: [f64; N], objval: f64, lb: f64, ub: f64) -> ColId {
pub fn add_col<const N: usize>(
&mut self,
mut colentries: [f64; N],
objval: f64,
lb: f64,
ub: f64,
) -> ColId {
let nnonzeros = colentries.iter().filter(|&&x| x != 0.0).count();
let colsize = colentries.len();

unsafe {
ffi::SoPlex_addColReal(*self.inner,
colentries.as_mut_ptr(),
colsize as i32,
nnonzeros as i32,
objval, lb, ub);
ffi::SoPlex_addColReal(
*self.inner,
colentries.as_mut_ptr(),
colsize as i32,
nnonzeros as i32,
objval,
lb,
ub,
);
}

ColId(self.num_cols() - 1)
Expand All @@ -58,16 +70,24 @@ impl Model {
/// # Returns
///
/// The `RowId` of the added row.
pub fn add_row<const N: usize>(&mut self, mut rowentries: [f64; N], lhs: f64, rhs: f64) -> RowId {
pub fn add_row<const N: usize>(
&mut self,
mut rowentries: [f64; N],
lhs: f64,
rhs: f64,
) -> RowId {
let nnonzeros = rowentries.iter().filter(|&&x| x != 0.0).count();
let rowsize = rowentries.len();

unsafe {
ffi::SoPlex_addRowReal(*self.inner,
rowentries.as_mut_ptr(),
rowsize as i32,
nnonzeros as i32,
lhs, rhs);
ffi::SoPlex_addRowReal(
*self.inner,
rowentries.as_mut_ptr(),
rowsize as i32,
nnonzeros as i32,
lhs,
rhs,
);
}

RowId(self.num_rows() - 1)
Expand All @@ -94,7 +114,6 @@ impl Model {
unsafe { ffi::SoPlex_removeColReal(*self.inner, col_id.0 as i32) };
}


/// Remove a row from the model.
pub fn remove_row(&mut self, row_id: RowId) {
unsafe { ffi::SoPlex_removeRowReal(*self.inner, row_id.0 as i32) };
Expand Down Expand Up @@ -155,7 +174,6 @@ impl Model {
}
}


/// Change the bounds of a column.
///
/// # Arguments
Expand Down Expand Up @@ -220,14 +238,17 @@ impl Model {
}
}


/// Sets the factor update type.
///
/// # Arguments
/// * `factor_update_type` - The factor update type.
pub fn set_factor_update_type(&mut self, factor_update_type: crate::FactorUpdateType) {
unsafe {
ffi::SoPlex_setIntParam(*self.inner, crate::FACTOR_UPDATE_TYPE_PARAM_ID, factor_update_type.into());
ffi::SoPlex_setIntParam(
*self.inner,
crate::FACTOR_UPDATE_TYPE_PARAM_ID,
factor_update_type.into(),
);
}
}

Expand All @@ -237,7 +258,11 @@ impl Model {
/// * `simplifier_type` - The simplifier type.
pub fn set_simplifier_type(&mut self, simplifier_type: crate::Simplifier) {
unsafe {
ffi::SoPlex_setIntParam(*self.inner, crate::SIMPLIFIER_PARAM_ID, simplifier_type.into());
ffi::SoPlex_setIntParam(
*self.inner,
crate::SIMPLIFIER_PARAM_ID,
simplifier_type.into(),
);
}
}

Expand Down Expand Up @@ -267,7 +292,11 @@ impl Model {
/// * `ratio_tester_type` - The ratio tester type.
pub fn set_ratio_tester_type(&mut self, ratio_tester_type: crate::RatioTester) {
unsafe {
ffi::SoPlex_setIntParam(*self.inner, crate::RATIO_TESTER_PARAM_ID, ratio_tester_type.into());
ffi::SoPlex_setIntParam(
*self.inner,
crate::RATIO_TESTER_PARAM_ID,
ratio_tester_type.into(),
);
}
}

Expand All @@ -281,7 +310,6 @@ impl Model {
}
}


/// Sets the read mode.
///
/// # Arguments
Expand Down Expand Up @@ -328,7 +356,11 @@ impl Model {
/// * `hyper_pricing` - The hyper pricing parameter.
pub fn set_hyper_pricing(&mut self, hyper_pricing: crate::HyperPricing) {
unsafe {
ffi::SoPlex_setIntParam(*self.inner, crate::HYPER_PRICING_PARAM_ID, hyper_pricing.into());
ffi::SoPlex_setIntParam(
*self.inner,
crate::HYPER_PRICING_PARAM_ID,
hyper_pricing.into(),
);
}
}

Expand All @@ -338,7 +370,11 @@ impl Model {
/// * `solution_polishing` - The solution polishing type.
pub fn set_solution_polishing(&mut self, solution_polishing: crate::SolutionPolishing) {
unsafe {
ffi::SoPlex_setIntParam(*self.inner, crate::SOLUTION_POLISHING_PARAM_ID, solution_polishing.into());
ffi::SoPlex_setIntParam(
*self.inner,
crate::SOLUTION_POLISHING_PARAM_ID,
solution_polishing.into(),
);
}
}

Expand All @@ -348,7 +384,11 @@ impl Model {
/// * `decomp_verbosity` - The decomposition verbosity.
pub fn set_decomp_verbosity(&mut self, decomp_verbosity: crate::Verbosity) {
unsafe {
ffi::SoPlex_setIntParam(*self.inner, crate::DECOMP_VERBOSITY_PARAM_ID, decomp_verbosity.into());
ffi::SoPlex_setIntParam(
*self.inner,
crate::DECOMP_VERBOSITY_PARAM_ID,
decomp_verbosity.into(),
);
}
}

Expand Down Expand Up @@ -399,7 +439,6 @@ impl SolvedModel {
unsafe { ffi::SoPlex_objValueReal(*self.inner) }
}


/// Returns the primal solution of the model.
pub fn primal_solution(&self) -> Vec<f64> {
let mut primal = vec![0.0; self.num_cols()];
Expand Down Expand Up @@ -462,15 +501,16 @@ impl SolvedModel {

impl From<SolvedModel> for Model {
fn from(solved_model: SolvedModel) -> Self {
Self { inner: solved_model.inner }
Self {
inner: solved_model.inner,
}
}
}


#[cfg(test)]
mod tests {
use crate::Algorithm;
use super::*;
use crate::Algorithm;

#[test]
fn simple_problem() {
Expand Down Expand Up @@ -522,7 +562,6 @@ mod tests {
assert!((lp.obj_val() - -27.66666666).abs() < 1e-6);
}


#[test]
fn num_iterations() {
let mut lp = Model::new();
Expand Down Expand Up @@ -558,7 +597,6 @@ mod tests {
assert_eq!(lp.status(), Status::AbortTime);
}


#[test]
fn set_bool_param() {
// TODO: think of a better test,
Expand Down Expand Up @@ -598,7 +636,6 @@ mod tests {
assert_eq!(result, Status::Infeasible);
}


#[test]
fn basis_status() {
let mut lp = Model::new();
Expand All @@ -612,7 +649,6 @@ mod tests {
assert_eq!(row_basis_status, RowBasisStatus::AtUpper);
}


#[test]
fn set_obj_sense() {
let mut lp = Model::new();
Expand Down
6 changes: 0 additions & 6 deletions src/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ pub enum BoolParam {
SimplifierDominatedCols = 24,
}


pub(crate) const OBJSENSE_PARAM_ID: i32 = 0;
pub(crate) const REPR_PARAM_ID: i32 = 1;
pub(crate) const ALGORITHM_PARAM_ID: i32 = 2;
Expand Down Expand Up @@ -347,8 +346,6 @@ pub enum RealParam {
SimplifierModifyRowFac = 25,
}



macro_rules! impl_from_int_param {
($($param:ident),*) => {
$(
Expand Down Expand Up @@ -383,6 +380,3 @@ impl_from_int_param!(
SolutionPolishing,
ReadMode
);



5 changes: 2 additions & 3 deletions src/soplex_ptr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::ffi;
use std::ffi::c_void;
use std::ops::Deref;
use crate::ffi;

pub(crate) struct SoplexPtr {
ptr: *mut c_void,
Expand All @@ -14,7 +14,6 @@ impl SoplexPtr {
}
}


impl Default for SoplexPtr {
fn default() -> Self {
SoplexPtr::new()
Expand All @@ -35,4 +34,4 @@ impl Drop for SoplexPtr {
ffi::SoPlex_free(self.ptr);
}
}
}
}
1 change: 0 additions & 1 deletion src/status.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::convert::From;


/// Status of the solver
#[derive(Debug, PartialEq, Clone)]
pub enum Status {
Expand Down

0 comments on commit ea247af

Please sign in to comment.