From fd2da99277e93395939013b6df926ad739981d8b Mon Sep 17 00:00:00 2001 From: hamilcarBarca17 Date: Wed, 15 Mar 2023 13:40:20 -0600 Subject: [PATCH] Reverted mistaken missed functionality --- src/cp_sat_wrapper.cpp | 14 ++++++++++++++ src/ffi.rs | 28 ++++++++++++++++++++++++++++ src/lib.rs | 2 +- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/cp_sat_wrapper.cpp b/src/cp_sat_wrapper.cpp index 13ec927..1dc82db 100644 --- a/src/cp_sat_wrapper.cpp +++ b/src/cp_sat_wrapper.cpp @@ -61,6 +61,20 @@ cp_sat_wrapper_cp_model_stats(unsigned char* model_buf, size_t model_size) { return strdup(stats.c_str()); } +extern "C" char* +cp_sat_wrapper_cp_solver_response_stats( + unsigned char* response_buf, + size_t response_size, + bool has_objective) +{ + sat::CpSolverResponse response; + const bool res = response.ParseFromArray(response_buf, response_size); + assert(res); + + const std::string stats = sat::CpSolverResponseStats(response, has_objective); + return strdup(stats.c_str()); +} + extern "C" char* cp_sat_wrapper_validate_cp_model(unsigned char* model_buf, size_t model_size) { sat::CpModelProto model; diff --git a/src/ffi.rs b/src/ffi.rs index d1a3e25..681d5f3 100644 --- a/src/ffi.rs +++ b/src/ffi.rs @@ -17,6 +17,11 @@ extern "C" { out_size: &mut usize, ) -> *mut u8; fn cp_sat_wrapper_cp_model_stats(model_buf: *const u8, model_size: usize) -> *mut c_char; + fn cp_sat_wrapper_cp_solver_response_stats( + response_buf: *const u8, + response_size: usize, + has_objective: bool, + ) -> *mut c_char; fn cp_sat_wrapper_validate_cp_model(model_buf: *const u8, model_size: usize) -> *mut c_char; fn cp_sat_wrapper_solution_is_feasible( model_buf: *const u8, @@ -81,6 +86,29 @@ pub fn cp_model_stats(model: &proto::CpModelProto) -> String { res } +/// Returns a string with some statistics on the solver response. +/// +/// If the second argument is false, we will just display NA for the +/// objective value instead of zero. It is not really needed but it +/// makes things a bit clearer to see that there is no objective. +pub fn cp_solver_response_stats(response: &proto::CpSolverResponse, has_objective: bool) -> String { + let mut response_buf = Vec::default(); + response.encode(&mut response_buf).unwrap(); + let char_ptr = unsafe { + cp_sat_wrapper_cp_solver_response_stats( + response_buf.as_ptr(), + response_buf.len(), + has_objective, + ) + }; + let res = unsafe { CStr::from_ptr(char_ptr) } + .to_str() + .unwrap() + .to_owned(); + unsafe { libc::free(char_ptr as _) }; + res +} + /// Verifies that the given model satisfies all the properties /// described in the proto comments. Returns an empty string if it is /// the case, otherwise fails at the first error and returns a diff --git a/src/lib.rs b/src/lib.rs index df2fce9..b04bfd6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -33,7 +33,7 @@ //! let response = model.solve(); //! println!( //! "{}", -//! cp_sat::ffi::cp_model_stats(&model.proto()) +//! cp_sat::ffi::cp_solver_response_stats(&response, false) //! ); //! //! if response.status() == CpSolverStatus::Optimal {