Added a helper function to remove a variable from the model
This commit is contained in:
+41
-19
@@ -1,6 +1,7 @@
|
|||||||
use crate::{ffi, proto};
|
use crate::{ffi, proto};
|
||||||
use proto::constraint_proto::Constraint as CstEnum;
|
use proto::constraint_proto::Constraint as CstEnum;
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
|
use crate::proto::LinearArgumentProto;
|
||||||
|
|
||||||
/// A builder for CP SAT.
|
/// A builder for CP SAT.
|
||||||
///
|
///
|
||||||
@@ -98,6 +99,27 @@ impl CpModelBuilder {
|
|||||||
self.new_int_var_with_name(domain, "")
|
self.new_int_var_with_name(domain, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Removes the [IntVar] whose name matches the given name from the model.
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// # use cp_sat::builder::{CpModelBuilder, IntVar};
|
||||||
|
/// let mut model = CpModelBuilder::default();
|
||||||
|
/// let x = model.new_int_var_with_name([(0, 1)], "some cool name");
|
||||||
|
/// assert!(!model.proto().variables.is_empty());
|
||||||
|
/// let name = model.remove_int_var("some cool name");
|
||||||
|
/// assert!(model.proto().variables.is_empty());
|
||||||
|
pub fn remove_int_var(&mut self, name: &str) {
|
||||||
|
let position = self
|
||||||
|
.proto
|
||||||
|
.variables
|
||||||
|
.iter()
|
||||||
|
.position(|variable| variable.name == name)
|
||||||
|
.unwrap();
|
||||||
|
self.proto.variables.remove(position);
|
||||||
|
}
|
||||||
|
|
||||||
/// Creates a new integer variable with a name, and returns the
|
/// Creates a new integer variable with a name, and returns the
|
||||||
/// [IntVar] indentifier.
|
/// [IntVar] indentifier.
|
||||||
///
|
///
|
||||||
@@ -359,22 +381,22 @@ impl CpModelBuilder {
|
|||||||
vars: expr.vars.into_vec(),
|
vars: expr.vars.into_vec(),
|
||||||
coeffs: expr.coeffs.into_vec(),
|
coeffs: expr.coeffs.into_vec(),
|
||||||
domain: domain
|
domain: domain
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.flat_map(|(begin, end)| {
|
.flat_map(|(begin, end)| {
|
||||||
[
|
[
|
||||||
if begin == i64::MIN {
|
if begin == i64::MIN {
|
||||||
i64::MIN
|
i64::MIN
|
||||||
} else {
|
} else {
|
||||||
begin.saturating_sub(constant)
|
begin.saturating_sub(constant)
|
||||||
},
|
},
|
||||||
if end == i64::MAX {
|
if end == i64::MAX {
|
||||||
i64::MAX
|
i64::MAX
|
||||||
} else {
|
} else {
|
||||||
end.saturating_sub(constant)
|
end.saturating_sub(constant)
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -612,9 +634,9 @@ impl CpModelBuilder {
|
|||||||
pub fn add_hint(&mut self, var: impl Into<IntVar>, value: i64) {
|
pub fn add_hint(&mut self, var: impl Into<IntVar>, value: i64) {
|
||||||
let var = var.into();
|
let var = var.into();
|
||||||
let hints = self
|
let hints = self
|
||||||
.proto
|
.proto
|
||||||
.solution_hint
|
.solution_hint
|
||||||
.get_or_insert_with(Default::default);
|
.get_or_insert_with(Default::default);
|
||||||
if var.0 < 0 {
|
if var.0 < 0 {
|
||||||
hints.vars.push(var.not().0);
|
hints.vars.push(var.not().0);
|
||||||
hints.values.push(1 - value);
|
hints.values.push(1 - value);
|
||||||
|
|||||||
Reference in New Issue
Block a user