Cleaned up some of the docs and added the new functions int_var_domain, set_int_var_domain, add_leq, and require now that all variables be given names

This commit is contained in:
hamilcarBarca17
2023-03-10 13:43:38 -07:00
parent 20bc0eeefc
commit 1fe1ba87a8
6 changed files with 234 additions and 220 deletions
+58 -58
View File
@@ -3,82 +3,82 @@ use cp_sat::proto::{CpSolverStatus, SatParameters};
#[test]
fn presentation_problem() {
let mut model = CpModelBuilder::default();
let domain = [(0, 2)];
let x = model.new_int_var_with_name(domain, "x");
let y = model.new_int_var_with_name(domain, "y");
let z = model.new_int_var_with_name(domain, "z");
let mut model = CpModelBuilder::default();
let domain = [(0, 2)];
let x = model.new_int_var(domain, "x");
let y = model.new_int_var(domain, "y");
let z = model.new_int_var(domain, "z");
model.add_ne(x, y);
model.add_ne(x, y);
let response = model.solve();
println!("{:?}", response);
let response = model.solve();
assert_eq!(response.status(), CpSolverStatus::Optimal);
let x_val = x.solution_value(&response);
let y_val = y.solution_value(&response);
let z_val = z.solution_value(&response);
println!("x = {}", x_val);
println!("y = {}", y_val);
println!("z = {}", z_val);
assert_eq!(response.status(), CpSolverStatus::Optimal);
let x_val = x.solution_value(&response);
let y_val = y.solution_value(&response);
let z_val = z.solution_value(&response);
println!("x = {}", x_val);
println!("y = {}", y_val);
println!("z = {}", z_val);
assert!(x_val != y_val);
assert!(0 <= x_val && x_val <= 2);
assert!(0 <= y_val && y_val <= 2);
assert!(0 <= z_val && z_val <= 2);
assert_ne!(x_val, y_val);
assert!((0..=2).contains(&x_val));
assert!((0..=2).contains(&y_val));
assert!((0..=2).contains(&z_val));
}
#[test]
fn solving_a_cp_problem() {
let mut model = CpModelBuilder::default();
let var_upper_bound = vec![50, 45, 37].into_iter().max().unwrap();
let x = model.new_int_var_with_name([(0, var_upper_bound)], "x");
let y = model.new_int_var_with_name([(0, var_upper_bound)], "y");
let z = model.new_int_var_with_name([(0, var_upper_bound)], "z");
let mut model = CpModelBuilder::default();
let var_upper_bound = vec![50, 45, 37].into_iter().max().unwrap();
let x = model.new_int_var([(0, var_upper_bound)], "x");
let y = model.new_int_var([(0, var_upper_bound)], "y");
let z = model.new_int_var([(0, var_upper_bound)], "z");
model.add_le([(2, x), (7, y), (3, z)], 50);
model.add_le([(3, x), (-5, y), (7, z)], 45);
model.add_le([(5, x), (2, y), (-6, z)], 37);
model.add_le([(2, x), (7, y), (3, z)], 50);
model.add_le([(3, x), (-5, y), (7, z)], 45);
model.add_le([(5, x), (2, y), (-6, z)], 37);
model.maximize([(2, x), (2, y), (3, z)]);
model.maximize([(2, x), (2, y), (3, z)]);
let response = model.solve();
assert_eq!(response.status(), CpSolverStatus::Optimal);
let x_val = x.solution_value(&response);
let y_val = y.solution_value(&response);
let z_val = z.solution_value(&response);
println!("objective: {}", response.objective_value);
println!("x = {}", x_val);
println!("y = {}", y_val);
println!("z = {}", z_val);
let response = model.solve();
assert_eq!(response.status(), CpSolverStatus::Optimal);
let x_val = x.solution_value(&response);
let y_val = y.solution_value(&response);
let z_val = z.solution_value(&response);
println!("objective: {}", response.objective_value);
println!("x = {}", x_val);
println!("y = {}", y_val);
println!("z = {}", z_val);
assert_eq!(35., response.objective_value);
assert!(2 * x_val + 7 * y_val + 3 * z_val <= 50);
assert!(3 * x_val - 5 * y_val + 7 * z_val <= 45);
assert!(5 * x_val + 2 * y_val - 6 * z_val <= 37);
assert_eq!(35., response.objective_value);
assert!(2 * x_val + 7 * y_val + 3 * z_val <= 50);
assert!(3 * x_val - 5 * y_val + 7 * z_val <= 45);
assert!(5 * x_val + 2 * y_val - 6 * z_val <= 37);
}
#[test]
fn solve_with_time_limit_sample_sat() {
let mut model = CpModelBuilder::default();
let domain = [(0, 2)];
let x = model.new_int_var_with_name(domain, "x");
let y = model.new_int_var_with_name(domain, "y");
let z = model.new_int_var_with_name(domain, "z");
let mut model = CpModelBuilder::default();
let domain = [(0, 2)];
let x = model.new_int_var(domain, "x");
let y = model.new_int_var(domain, "y");
let z = model.new_int_var(domain, "z");
model.add_ne(x, y);
model.add_ne(x, y);
let mut params = SatParameters::default();
params.max_time_in_seconds = Some(10.);
let params = SatParameters {
max_time_in_seconds: Some(10.),
..SatParameters::default()
};
let response = model.solve_with_parameters(&params);
println!("{:?}", response);
let response = model.solve_with_parameters(&params);
assert_eq!(response.status(), CpSolverStatus::Optimal);
let x_val = x.solution_value(&response);
let y_val = y.solution_value(&response);
let z_val = z.solution_value(&response);
println!("x = {}", x_val);
println!("y = {}", y_val);
println!("z = {}", z_val);
assert_eq!(response.status(), CpSolverStatus::Optimal);
let x_val = x.solution_value(&response);
let y_val = y.solution_value(&response);
let z_val = z.solution_value(&response);
println!("x = {}", x_val);
println!("y = {}", y_val);
println!("z = {}", z_val);
}