Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ fn main() {
.unwrap_or_else(|| "/opt/ortools".into());
cc::Build::new()
.cpp(true)
.flag("-std=c++17")
.flags(["-std=c++17", "-DOR_PROTO_DLL="])
.file("src/cp_sat_wrapper.cpp")
.include(&[&ortools_prefix, "/include"].concat())
.include([&ortools_prefix, "/include"].concat())
.compile("cp_sat_wrapper.a");

println!("cargo:rustc-link-lib=dylib=ortools");
Expand Down
71 changes: 30 additions & 41 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,15 +327,26 @@ impl CpModelBuilder {
/// assert!(x_val != z_val);
/// assert!(y_val != z_val);
/// ```
pub fn add_all_different(&mut self, vars: impl IntoIterator<Item = IntVar>) -> Constraint {
pub fn add_all_different(
&mut self,
exprs: impl IntoIterator<Item = impl Into<LinearExpr>>,
) -> Constraint {
self.add_cst(CstEnum::AllDiff(proto::AllDifferentConstraintProto {
vars: vars.into_iter().map(|v| v.0).collect(),
exprs: exprs
.into_iter()
.map(Into::into)
.map(|expr| proto::LinearExpressionProto {
vars: expr.vars.into_vec(),
coeffs: expr.coeffs.into_vec(),
offset: expr.constant,
})
.collect(),
}))
}

/// Adds a linear constraint.
///
/// # Exemple
/// # Example
///
/// ```
/// # use cp_sat::builder::CpModelBuilder;
Expand Down Expand Up @@ -380,7 +391,7 @@ impl CpModelBuilder {

/// Adds an equality constraint between 2 linear expressions.
///
/// # Exemple
/// # Example
///
/// ```
/// # use cp_sat::builder::{CpModelBuilder, LinearExpr};
Expand All @@ -405,7 +416,7 @@ impl CpModelBuilder {

/// Adds a greater or equal constraint between 2 linear expressions.
///
/// # Exemple
/// # Example
///
/// ```
/// # use cp_sat::builder::{CpModelBuilder, LinearExpr};
Expand All @@ -430,7 +441,7 @@ impl CpModelBuilder {

/// Adds a lesser or equal constraint between 2 linear expressions.
///
/// # Exemple
/// # Example
///
/// ```
/// # use cp_sat::builder::{CpModelBuilder, LinearExpr};
Expand All @@ -455,7 +466,7 @@ impl CpModelBuilder {

/// Adds a stricly greater constraint between 2 linear expressions.
///
/// # Exemple
/// # Example
///
/// ```
/// # use cp_sat::builder::{CpModelBuilder, LinearExpr};
Expand All @@ -480,7 +491,7 @@ impl CpModelBuilder {

/// Adds a strictly lesser constraint between 2 linear expressions.
///
/// # Exemple
/// # Example
///
/// ```
/// # use cp_sat::builder::{CpModelBuilder, LinearExpr};
Expand All @@ -505,7 +516,7 @@ impl CpModelBuilder {

/// Adds a not equal constraint between 2 linear expressions.
///
/// # Exemple
/// # Example
///
/// ```
/// # use cp_sat::builder::{CpModelBuilder, LinearExpr};
Expand All @@ -526,40 +537,10 @@ impl CpModelBuilder {
self.add_linear_constraint(lhs.into() - rhs.into(), [(i64::MIN, -1), (1, i64::MAX)])
}

/// Adds a constraint that force the `target` to be equal to the
/// minimum of the given `exprs`.
///
/// # Exemple
///
/// ```
/// # use cp_sat::builder::{CpModelBuilder, LinearExpr};
/// # use cp_sat::proto::CpSolverStatus;
/// let mut model = CpModelBuilder::default();
/// let x = model.new_int_var([(0, 10)]);
/// let y = model.new_int_var([(5, 15)]);
/// let m = model.new_int_var([(-100, 100)]);
/// model.add_min_eq(m, [x, y]);
/// model.maximize(m);
/// let response = model.solve();
/// assert_eq!(response.status(), CpSolverStatus::Optimal);
/// assert_eq!(10., response.objective_value);
/// assert_eq!(10, m.solution_value(&response));
/// ```
pub fn add_min_eq(
&mut self,
target: impl Into<LinearExpr>,
exprs: impl IntoIterator<Item = impl Into<LinearExpr>>,
) -> Constraint {
self.add_cst(CstEnum::LinMin(proto::LinearArgumentProto {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why removing this constraint ?

Copy link
Contributor Author

@darshanpatel-arculus darshanpatel-arculus Aug 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lin_min constraint was removed from the protobuf definitions (by google) which results into removal CstEnum::LinMin variant.

google/or-tools@ef59381

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The commit you linked keep the implementation of addMinEquality in the C++ interface. Doing so as well here will allow to be backward compatible.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't add it initially, as I thought this was just a pure bindings crate. Nevertheless, I have added it back now. :)
Can you please verify the implementation ?

target: Some(target.into().into()),
exprs: exprs.into_iter().map(|e| e.into().into()).collect(),
}))
}

/// Adds a constraint that force the `target` to be equal to the
/// maximum of the given `exprs`.
///
/// # Exemple
/// # Example
///
/// ```
/// # use cp_sat::builder::{CpModelBuilder, LinearExpr};
Expand Down Expand Up @@ -669,6 +650,10 @@ impl CpModelBuilder {
offset: expr.constant as f64,
scaling_factor: 1.,
domain: vec![],
scaling_was_exact: false,
integer_before_offset: 0,
integer_after_offset: 0,
integer_scaling_factor: 0,
});
}

Expand Down Expand Up @@ -698,6 +683,10 @@ impl CpModelBuilder {
offset: -expr.constant as f64,
scaling_factor: -1.,
domain: vec![],
scaling_was_exact: false,
integer_before_offset: 0,
integer_after_offset: 0,
integer_scaling_factor: 0,
});
}

Expand Down Expand Up @@ -865,7 +854,7 @@ pub struct Constraint(usize);
/// It describes an expression in the form `ax+by+c`. Several [From]
/// and [std::ops] traits are implemented for easy modeling.
///
/// # Exemple
/// # Example
///
/// Most of the builder methods that takes something linear take in
/// practice a `impl Into<LinearExpr>`. In the example, we will use
Expand Down
Loading