Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ all_default_solvers = [
minilp = [
"microlp",
] # minilp is not maintained anymore, we use the microlp fork instead
enable_quadratic = ["clarabel"] # Enable quadratic programming support (requires clarabel solver)

[dependencies]
coin_cbc = { version = "0.1", optional = true, default-features = false }
Expand Down
15 changes: 13 additions & 2 deletions src/affine_expression_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
//! You can implement this trait if you want to implement your own
//! variant of the [Expression](crate::Expression) type, optimized for your use case.
use crate::expression::LinearExpression;
#[cfg(feature = "enable_quadratic")]
use crate::expression::QuadraticExpression;
use crate::{Expression, Solution, Variable};

/// An element that can be expressed as a linear combination of variables plus a constant
///
/// This trait can only be used with expressions that contain no quadratic terms.
/// Attempting to use it with an Expression containing quadratic terms will panic.
pub trait IntoAffineExpression {
/// The iterator returned by [`linear_coefficients`](IntoAffineExpression::linear_coefficients).
type Iter: IntoIterator<Item = (Variable, f64)>;
Expand All @@ -26,9 +31,13 @@ pub trait IntoAffineExpression {
Self: Sized,
{
let constant = self.constant();
let coefficients = self.linear_coefficients().into_iter().collect();
let linear_coefficients = self.linear_coefficients().into_iter().collect();
Expression {
linear: LinearExpression { coefficients },
#[cfg(feature = "enable_quadratic")]
quadratic: QuadraticExpression::new(),
linear: LinearExpression {
coefficients: linear_coefficients,
},
constant,
}
}
Expand Down Expand Up @@ -92,6 +101,8 @@ macro_rules! impl_affine_for_num {

fn into_expression(self) -> Expression {
Expression {
#[cfg(feature = "enable_quadratic")]
quadratic: QuadraticExpression::new(),
linear: LinearExpression { coefficients: std::default::Default::default() },
constant: f64::from(self),
}
Expand Down
Loading
Loading