From a7c121c2ba125e2594604a310eea260d7e7f4d39 Mon Sep 17 00:00:00 2001 From: Kieranoski Date: Wed, 15 Nov 2023 11:39:45 +0000 Subject: [PATCH 1/4] Add attempt at creating a problem in wrapper.cpp, update main.rs accordingly --- solvers/chuffed/build.rs | 2 + solvers/chuffed/src/main.rs | 54 ++----------------------- solvers/chuffed/wrapper.cpp | 78 +++++++++++++++++++++++++++++++++++++ solvers/chuffed/wrapper.h | 5 +++ solvers/chuffed/xyz.fzn | 7 ++++ 5 files changed, 96 insertions(+), 50 deletions(-) create mode 100644 solvers/chuffed/xyz.fzn diff --git a/solvers/chuffed/build.rs b/solvers/chuffed/build.rs index a20e120d8..b03c1e9f3 100644 --- a/solvers/chuffed/build.rs +++ b/solvers/chuffed/build.rs @@ -78,6 +78,8 @@ fn bind() { .allowlist_function("p_setcallback") .allowlist_function("p_print") .allowlist_function("branch_IntVar") + .allowlist_function("new_problem") + .allowlist_function("solve") .clang_arg("-Ivendor/build") // generated from configure.py .clang_arg("-Ivendor") .clang_arg(r"--std=gnu++11") diff --git a/solvers/chuffed/src/main.rs b/solvers/chuffed/src/main.rs index a22f3de3b..39177dd2f 100644 --- a/solvers/chuffed/src/main.rs +++ b/solvers/chuffed/src/main.rs @@ -1,60 +1,14 @@ use chuffed_rs::bindings::{ - get_idx, new_dummy_problem, p_addVars, p_print, p_setcallback, vec, ConLevel_CL_DEF, IntVar, - VarBranch_VAR_INORDER, VarBranch_VAR_MIN_MIN, + new_problem, solve }; -use chuffed_rs::wrappers::{ - all_different_wrapper, branch_wrapper, create_vars, output_vars_wrapper, var_sym_break_wrapper, -}; - -unsafe fn post_constraints(_n: i32) -> *mut vec<*mut IntVar> { - // Create constant - let n: i32 = _n; - // Create some variables - let x: *mut vec<*mut IntVar> = create_vars(n, 0, n, false); - - // Post some constraints - all_different_wrapper(x, ConLevel_CL_DEF); - - // Post some branchings - branch_wrapper(x as _, VarBranch_VAR_INORDER, VarBranch_VAR_MIN_MIN); - - // Declare output variables (optional) - output_vars_wrapper(x); - - // Declare symmetries (optional) - var_sym_break_wrapper(x); - // Return the variable - x -} - -// Custom printing function for this problem -#[no_mangle] -pub unsafe extern "C" fn callback(x: *mut vec<*mut IntVar>) { - print!("First output is: {}", get_idx(x, 0)); -} // Entry point for running this problem fn main() { - let args: Vec = std::env::args().collect(); - - if args.len() != 2 { - println!("Invalid number of arguments"); - return; - } - - let n: i32 = args[1].parse().expect("Invalid input"); unsafe { - let x = post_constraints(n); - // make new dummy problem - let p = new_dummy_problem(); - // Call problem.addvars() - p_addVars(p, x); - // Call problem.setcallback() - p_setcallback(p, Some(callback)); - // Commented out currently as trying to print causes the assertion of - // isFixed() in IntVar::getVal() to fail. - // p_print(p); + let p = new_problem(); + solve(p); + } } diff --git a/solvers/chuffed/wrapper.cpp b/solvers/chuffed/wrapper.cpp index 84a89b42a..0b7a3a0ed 100644 --- a/solvers/chuffed/wrapper.cpp +++ b/solvers/chuffed/wrapper.cpp @@ -1,4 +1,5 @@ #include "./wrapper.h" +#include "chuffed/flatzinc/flatzinc.h" DummyProblem *new_dummy_problem() { return new DummyProblem(); } void p_addVars(DummyProblem *p, vec *_searchVars) { @@ -23,3 +24,80 @@ void branch_IntVar(vec *x, VarBranch var_branch, ValBranch val_branch) { branch(*x, var_branch, val_branch); } + +// Construct problem with given number of variables +FlatZinc::FlatZincSpace *new_flat_zinc_space(int intVars, int boolVars, + int setVars) { + return new FlatZinc::FlatZincSpace(intVars, boolVars, setVars); +} + +// add new int var +void addIntVar(FlatZinc::FlatZincSpace flat_zinc_space, + FlatZinc::IntVarSpec *vs, const std::string &name) { + flat_zinc_space.newIntVar(vs, name); +} + +class XYZProblem : public Problem { + +public: + // Constants + int n; // number of variables + + // Variables + vec x; + vec y; + vec z; + + IntVar *sum; + + XYZProblem(int _n) : n(_n) { + // Create vars + createVars(x, n, 0, n - 1); + createVars(y, n, 0, n - 1); + createVars(z, n, 0, n - 1); + + // Post constraints + // find x, y, z : int(1..3) + // such that x + y = z + + for (int i = 0; i < n; i++) { + int_plus(x[i], y[i], sum); + int_linear(z, IRT_EQ, sum); + } + + // Branching + branch(x, VAR_INORDER, VAL_MIN); + branch(y, VAR_INORDER, VAL_MIN); + branch(z, VAR_INORDER, VAL_MIN); + + // Declare output variables + output_vars(x); + output_vars(y); + output_vars(z); + } + + // Function to print out solution + void print(std::ostream &out) override { + out << "x = "; + for (int i = 0; i < n; i++) { + out << x[i]->getVal() << " "; + } + out << std::endl; + out << "y = "; + for (int i = 0; i < n; i++) { + out << y[i]->getVal() << " "; + } + out << std::endl; + out << "z = "; + for (int i = 0; i < n; i++) { + out << z[i]->getVal() << " "; + } + out << std::endl; + } +}; + +// Create new problem +Problem *new_problem() { return new XYZProblem(3); } + +// Solve problem +void solve(Problem *p) { engine.solve((XYZProblem *)p); } diff --git a/solvers/chuffed/wrapper.h b/solvers/chuffed/wrapper.h index d8a10c4fd..24c9d8d0d 100644 --- a/solvers/chuffed/wrapper.h +++ b/solvers/chuffed/wrapper.h @@ -1,6 +1,8 @@ #include "chuffed/branching/branching.h" #include "chuffed/core/engine.h" #include "chuffed/core/propagator.h" +#include "chuffed/flatzinc/flatzinc.h" +#include "chuffed/primitives/primitives.h" #include "chuffed/vars/modelling.h" class DummyProblem { @@ -25,3 +27,6 @@ void destroy_vec_intvar(vec *v); void branch_IntVar(vec *x, VarBranch var_branch, ValBranch val_branch); + +Problem *new_problem(); +void solve(Problem *p); diff --git a/solvers/chuffed/xyz.fzn b/solvers/chuffed/xyz.fzn new file mode 100644 index 000000000..934105800 --- /dev/null +++ b/solvers/chuffed/xyz.fzn @@ -0,0 +1,7 @@ +predicate all_different_int(array [int] of var int: xs); +var 1..3: x:: output_var ; +var 1..3: y:: output_var ; +var 1..3: z:: output_var ; +constraint int_lin_eq([1,1,-1],[x,y,z],0); +solve :: int_search([x, y, z], input_order, indomain_min, complete) + satisfy; From f7523c1e599a239935fb0a7f65dac01a90d2db24 Mon Sep 17 00:00:00 2001 From: Kieranoski Date: Wed, 22 Nov 2023 15:39:00 +0000 Subject: [PATCH 2/4] Add attempt at fixing wrapper.cpp with exact min and max and using only add --- solvers/chuffed/wrapper.cpp | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/solvers/chuffed/wrapper.cpp b/solvers/chuffed/wrapper.cpp index 0b7a3a0ed..a85bfa632 100644 --- a/solvers/chuffed/wrapper.cpp +++ b/solvers/chuffed/wrapper.cpp @@ -48,21 +48,18 @@ class XYZProblem : public Problem { vec y; vec z; - IntVar *sum; - XYZProblem(int _n) : n(_n) { // Create vars - createVars(x, n, 0, n - 1); - createVars(y, n, 0, n - 1); - createVars(z, n, 0, n - 1); + createVars(x, n, 1, 3); + createVars(y, n, 1, 3); + createVars(z, n, 1, 3); // Post constraints // find x, y, z : int(1..3) // such that x + y = z for (int i = 0; i < n; i++) { - int_plus(x[i], y[i], sum); - int_linear(z, IRT_EQ, sum); + int_plus(x[i], y[i], z[i]); } // Branching @@ -97,7 +94,7 @@ class XYZProblem : public Problem { }; // Create new problem -Problem *new_problem() { return new XYZProblem(3); } +void *new_problem() { return new XYZProblem(3); } // Solve problem -void solve(Problem *p) { engine.solve((XYZProblem *)p); } +void solve(void *p) { engine.solve((XYZProblem *)p); } From 1b72828f9fe2d1f07eb5a154d92100519d9d5716 Mon Sep 17 00:00:00 2001 From: Kieranoski702 Date: Tue, 28 Nov 2023 13:31:38 +0000 Subject: [PATCH 3/4] Update wrapper with chris fix to stop seg fault. Add current cpp xyz problem to rust tests --- solvers/chuffed/build.rs | 3 +++ solvers/chuffed/src/lib.rs | 8 +++++++- solvers/chuffed/src/main.rs | 15 +++++++++++---- solvers/chuffed/tests/chuffed_cpp_run.rs | 18 ++++++++++++++++++ solvers/chuffed/wrapper.cpp | 10 ++++++---- solvers/chuffed/wrapper.h | 4 ++-- 6 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 solvers/chuffed/tests/chuffed_cpp_run.rs diff --git a/solvers/chuffed/build.rs b/solvers/chuffed/build.rs index d0006c0a6..de3cc64bd 100644 --- a/solvers/chuffed/build.rs +++ b/solvers/chuffed/build.rs @@ -84,6 +84,9 @@ fn bind() { .allowlist_function("p_setcallback") .allowlist_function("p_print") .allowlist_function("branch_IntVar") + .allowlist_function("new_xyz_problem") + .allowlist_function("solve_xyz") + .allowlist_function("int_plus") .clang_arg(format!("-I{}/build", out_dir)) .clang_arg("-Ivendor") .clang_arg(r"--std=gnu++11") diff --git a/solvers/chuffed/src/lib.rs b/solvers/chuffed/src/lib.rs index 45b17a4a3..8bb20923f 100644 --- a/solvers/chuffed/src/lib.rs +++ b/solvers/chuffed/src/lib.rs @@ -6,7 +6,7 @@ pub mod bindings { pub mod wrappers { use crate::bindings::{ all_different, branch_IntVar, createVar, createVars, make_vec_intvar, output_vars1, - var_sym_break, vec, ConLevel, IntVar, ValBranch, VarBranch, + var_sym_break, vec, ConLevel, IntVar, ValBranch, VarBranch, int_plus }; use core::ptr; @@ -61,4 +61,10 @@ pub mod wrappers { var_sym_break(x); } } + + pub unsafe fn int_plus_wrapper(x: *mut IntVar, y: *mut IntVar, z: *mut IntVar) { + unsafe { + int_plus(x, y, z); + } + } } diff --git a/solvers/chuffed/src/main.rs b/solvers/chuffed/src/main.rs index 39177dd2f..91a995742 100644 --- a/solvers/chuffed/src/main.rs +++ b/solvers/chuffed/src/main.rs @@ -1,14 +1,21 @@ use chuffed_rs::bindings::{ - new_problem, solve + new_xyz_problem, solve_xyz }; - // Entry point for running this problem fn main() { + let args: Vec = std::env::args().collect(); + + if args.len() != 2 { + println!("Invalid number of arguments"); + return; + } + + let n: i32 = args[1].parse().expect("Invalid input"); unsafe { - let p = new_problem(); - solve(p); + let p = new_xyz_problem(n); + solve_xyz(p); } } diff --git a/solvers/chuffed/tests/chuffed_cpp_run.rs b/solvers/chuffed/tests/chuffed_cpp_run.rs new file mode 100644 index 000000000..0f5c2541b --- /dev/null +++ b/solvers/chuffed/tests/chuffed_cpp_run.rs @@ -0,0 +1,18 @@ +use chuffed_rs::bindings::{ + new_xyz_problem, solve_xyz +}; + + +#[test] +fn run_cpp_problem() { + let n: i32 = 1; + + unsafe { + + let p = new_xyz_problem(n); + solve_xyz(p); + + // Pass test if no crash occurs + assert!(true); + } +} diff --git a/solvers/chuffed/wrapper.cpp b/solvers/chuffed/wrapper.cpp index a85bfa632..3e6529209 100644 --- a/solvers/chuffed/wrapper.cpp +++ b/solvers/chuffed/wrapper.cpp @@ -58,6 +58,8 @@ class XYZProblem : public Problem { // find x, y, z : int(1..3) // such that x + y = z + all_different(x); + for (int i = 0; i < n; i++) { int_plus(x[i], y[i], z[i]); } @@ -93,8 +95,8 @@ class XYZProblem : public Problem { } }; -// Create new problem -void *new_problem() { return new XYZProblem(3); } +// Create new xyz problem +void *new_xyz_problem(int n) { return new XYZProblem(n); } -// Solve problem -void solve(void *p) { engine.solve((XYZProblem *)p); } +// Solve xyz problem +void solve_xyz(void *p) { engine.solve((XYZProblem *)p); } diff --git a/solvers/chuffed/wrapper.h b/solvers/chuffed/wrapper.h index 24c9d8d0d..30c7108f3 100644 --- a/solvers/chuffed/wrapper.h +++ b/solvers/chuffed/wrapper.h @@ -28,5 +28,5 @@ void destroy_vec_intvar(vec *v); void branch_IntVar(vec *x, VarBranch var_branch, ValBranch val_branch); -Problem *new_problem(); -void solve(Problem *p); +void *new_xyz_problem(int n); +void solve_xyz(void *p); From 4df1b960f2f56b6795212a4c06e1b2a0e6d7d909 Mon Sep 17 00:00:00 2001 From: Kieranoski Date: Tue, 28 Nov 2023 13:41:08 +0000 Subject: [PATCH 4/4] Update formatting --- solvers/chuffed/src/lib.rs | 4 ++-- solvers/chuffed/src/main.rs | 5 +---- solvers/chuffed/tests/chuffed_cpp_run.rs | 12 ++++-------- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/solvers/chuffed/src/lib.rs b/solvers/chuffed/src/lib.rs index 8bb20923f..ec46d7f3c 100644 --- a/solvers/chuffed/src/lib.rs +++ b/solvers/chuffed/src/lib.rs @@ -5,8 +5,8 @@ pub mod bindings { pub mod wrappers { use crate::bindings::{ - all_different, branch_IntVar, createVar, createVars, make_vec_intvar, output_vars1, - var_sym_break, vec, ConLevel, IntVar, ValBranch, VarBranch, int_plus + all_different, branch_IntVar, createVar, createVars, int_plus, make_vec_intvar, + output_vars1, var_sym_break, vec, ConLevel, IntVar, ValBranch, VarBranch, }; use core::ptr; diff --git a/solvers/chuffed/src/main.rs b/solvers/chuffed/src/main.rs index 91a995742..d00676f22 100644 --- a/solvers/chuffed/src/main.rs +++ b/solvers/chuffed/src/main.rs @@ -1,6 +1,4 @@ -use chuffed_rs::bindings::{ - new_xyz_problem, solve_xyz -}; +use chuffed_rs::bindings::{new_xyz_problem, solve_xyz}; // Entry point for running this problem fn main() { @@ -16,6 +14,5 @@ fn main() { unsafe { let p = new_xyz_problem(n); solve_xyz(p); - } } diff --git a/solvers/chuffed/tests/chuffed_cpp_run.rs b/solvers/chuffed/tests/chuffed_cpp_run.rs index 0f5c2541b..3798a4a5f 100644 --- a/solvers/chuffed/tests/chuffed_cpp_run.rs +++ b/solvers/chuffed/tests/chuffed_cpp_run.rs @@ -1,18 +1,14 @@ -use chuffed_rs::bindings::{ - new_xyz_problem, solve_xyz -}; - +use chuffed_rs::bindings::{new_xyz_problem, solve_xyz}; #[test] fn run_cpp_problem() { - let n: i32 = 1; + let n: i32 = 1; unsafe { - let p = new_xyz_problem(n); solve_xyz(p); - + // Pass test if no crash occurs - assert!(true); + assert!(true); } }