use crate::variables::domain::float_interval::DEFAULT_FLOAT_PRECISION_DIGITS;
#[derive(Debug, Clone)]
pub struct SolverConfig {
pub float_precision_digits: i32,
pub timeout_ms: Option<u64>,
pub max_memory_mb: Option<u64>,
pub unbounded_inference_factor: u32,
pub prefer_lp_solver: bool,
}
impl Default for SolverConfig {
fn default() -> Self {
Self {
float_precision_digits: DEFAULT_FLOAT_PRECISION_DIGITS,
timeout_ms: Some(60000), max_memory_mb: Some(2048), unbounded_inference_factor: 1000, prefer_lp_solver: true, }
}
}
impl SolverConfig {
pub fn new() -> Self {
Self::default()
}
pub fn with_float_precision(mut self, precision_digits: i32) -> Self {
self.float_precision_digits = precision_digits;
self
}
pub fn with_timeout_ms(mut self, milliseconds: u64) -> Self {
self.timeout_ms = Some(milliseconds);
self
}
pub fn without_timeout(mut self) -> Self {
self.timeout_ms = None;
self
}
pub fn with_max_memory_mb(mut self, mb: u64) -> Self {
self.max_memory_mb = Some(mb);
self
}
pub fn without_memory_limit(mut self) -> Self {
self.max_memory_mb = None;
self
}
pub fn with_unbounded_inference_factor(mut self, factor: u32) -> Self {
self.unbounded_inference_factor = factor;
self
}
pub fn with_lp_solver(mut self) -> Self {
self.prefer_lp_solver = true;
self
}
pub fn without_lp_solver(mut self) -> Self {
self.prefer_lp_solver = false;
self
}
pub fn unlimited() -> Self {
Self {
float_precision_digits: DEFAULT_FLOAT_PRECISION_DIGITS,
timeout_ms: None,
max_memory_mb: None,
unbounded_inference_factor: 1000, prefer_lp_solver: false, }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = SolverConfig::default();
assert_eq!(config.float_precision_digits, DEFAULT_FLOAT_PRECISION_DIGITS);
assert_eq!(config.timeout_ms, Some(60000)); assert_eq!(config.max_memory_mb, Some(2048)); }
#[test]
fn test_unlimited_config() {
let config = SolverConfig::unlimited();
assert_eq!(config.float_precision_digits, DEFAULT_FLOAT_PRECISION_DIGITS);
assert_eq!(config.timeout_ms, None);
assert_eq!(config.max_memory_mb, None);
}
#[test]
fn test_builder_pattern() {
let config = SolverConfig::new()
.with_float_precision(4)
.with_timeout_ms(60000) .with_max_memory_mb(512);
assert_eq!(config.float_precision_digits, 4);
assert_eq!(config.timeout_ms, Some(60000));
assert_eq!(config.max_memory_mb, Some(512));
}
#[test]
fn test_without_methods() {
let config = SolverConfig::new()
.with_timeout_ms(30000) .with_max_memory_mb(256)
.without_timeout()
.without_memory_limit();
assert_eq!(config.timeout_ms, None);
assert_eq!(config.max_memory_mb, None);
}
#[test]
fn test_lp_solver_flag() {
let config = SolverConfig::new();
assert_eq!(config.prefer_lp_solver, true);
let config = SolverConfig::new().without_lp_solver();
assert_eq!(config.prefer_lp_solver, false);
let config = SolverConfig::new()
.without_lp_solver()
.with_lp_solver();
assert_eq!(config.prefer_lp_solver, true);
}
}