Skip to content

Commit f493531

Browse files
jk2997Joonho Kim
andauthored
impl WithTimeLimit for LpSolveProblem, CPLEXProblem, and lp-solvers Model<T> (#116)
* impl WithTimeLimit for LpSolveProblem, CPLEXProblem, and lp-solvers Model<T> * cargo fmt --------- Co-authored-by: Joonho Kim <[email protected]>
1 parent f10e1e8 commit f493531

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ lpsolve = { version = "1.0.1", optional = true }
4343
highs = { version = "2.0.0", optional = true }
4444
russcip = { version = "0.8.2", optional = true }
4545
lp-solvers = { version = "1.0.0", features = ["cplex"], optional = true }
46-
cplex-rs = { version = "0.1", optional = true }
46+
cplex-rs = { version = "0.1.9", optional = true }
4747
clarabel = { version = "0.11.0", optional = true, features = [] }
4848
fnv = "1.0.5"
4949

src/solvers/cplex.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
//! A solver that uses the [CPLEX](https://www.ibm.com/products/ilog-cplex-optimization-studio/cplex-optimizer) solver.
22
33
use std::collections::HashMap;
4+
use std::time::Duration;
45

56
use crate::{
67
constraint::ConstraintReference, variable::UnsolvedProblem, Constraint, ResolutionError,
7-
Solution, SolutionStatus, SolverModel, Variable, VariableDefinition,
8+
Solution, SolutionStatus, SolverModel, Variable, VariableDefinition, WithTimeLimit,
89
};
10+
use cplex_rs::parameters::TimeLimit;
911
use cplex_rs::{ConstraintType, Environment, Problem, ProblemType};
1012

1113
use super::ObjectiveDirection;
@@ -157,6 +159,16 @@ impl SolverModel for CPLEXProblem {
157159
}
158160
}
159161

162+
impl WithTimeLimit for CPLEXProblem {
163+
fn with_time_limit<T: Into<f64>>(mut self, seconds: T) -> Self {
164+
self.model
165+
.env_mut()
166+
.set_parameter(TimeLimit(Duration::from_secs_f64(seconds.into())))
167+
.expect("Failed to set CPLEX time limit");
168+
self
169+
}
170+
}
171+
160172
/// A wrapper to a solved SCIP problem
161173
pub struct CplexSolved {
162174
solution: cplex_rs::Solution,

src/solvers/lp_solvers.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub use lp_solvers::solvers::*;
1010
use lp_solvers::util::UniqueNameGenerator;
1111

1212
use crate::constraint::ConstraintReference;
13-
use crate::solvers::{MipGapError, ObjectiveDirection, SolutionStatus};
13+
use crate::solvers::{MipGapError, ObjectiveDirection, SolutionStatus, WithTimeLimit};
1414
use crate::variable::UnsolvedProblem;
1515
use crate::{
1616
Constraint, Expression, IntoAffineExpression, ResolutionError, Solution as GoodLpSolution,
@@ -135,6 +135,16 @@ impl<T: SolverTrait> SolverModel for Model<T> {
135135
}
136136
}
137137

138+
impl<T> crate::solvers::WithTimeLimit for Model<T>
139+
where
140+
T: lp_solvers::solvers::WithMaxSeconds<T>,
141+
{
142+
fn with_time_limit<U: Into<f64>>(mut self, seconds: U) -> Self {
143+
self.solver = self.solver.with_max_seconds(seconds.into() as u32);
144+
self
145+
}
146+
}
147+
138148
fn linear_coefficients_str(
139149
expr: &Expression,
140150
variables: &[lp_solvers::problem::Variable],

src/solvers/lpsolve.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! lp_solve is a free ([LGPL](https://lpsolve.sourceforge.net/5.5/LGPL.htm)) linear (integer) programming solver written in C and based on the revised simplex method.
22
//! good_lp uses the [lpsolve crate](https://docs.rs/lpsolve/) to call lpsolve. You will need a C compiler, but you won't have to install any additional library.
3-
use crate::solvers::{ObjectiveDirection, ResolutionError, Solution, SolutionStatus, SolverModel};
3+
use crate::solvers::{
4+
ObjectiveDirection, ResolutionError, Solution, SolutionStatus, SolverModel, WithTimeLimit,
5+
};
46
use crate::variable::UnsolvedProblem;
57
use crate::{
68
affine_expression_trait::IntoAffineExpression, constraint::ConstraintReference, ModelWithSOS1,
@@ -128,6 +130,13 @@ impl SolverModel for LpSolveProblem {
128130
}
129131
}
130132

133+
impl WithTimeLimit for LpSolveProblem {
134+
fn with_time_limit<T: Into<f64>>(mut self, seconds: T) -> Self {
135+
self.0.set_timeout(seconds.into().ceil() as i64);
136+
self
137+
}
138+
}
139+
131140
impl ModelWithSOS1 for LpSolveProblem {
132141
fn add_sos1<I: IntoAffineExpression>(&mut self, variables: I) {
133142
let iter = variables.linear_coefficients().into_iter();

0 commit comments

Comments
 (0)