This commit is contained in:
Laurent Perron
2025-03-24 13:31:22 -07:00
parent 1180412228
commit 60b5457b47
5 changed files with 24 additions and 16 deletions

View File

@@ -16,16 +16,17 @@
#include <string>
#if !defined(__PORTABLE_PLATFORM__)
#include "ortools/util/parse_proto.h"
#endif // !defined(__PORTABLE_PLATFORM__)
#include "absl/strings/ascii.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "google/protobuf/message.h"
#include "google/protobuf/message_lite.h"
#include "google/protobuf/text_format.h"
#if !defined(__PORTABLE_PLATFORM__)
#include "ortools/util/parse_proto.h"
#endif // !defined(__PORTABLE_PLATFORM__)
namespace operations_research {
template <class P>
@@ -33,7 +34,10 @@ std::string ProtobufDebugString(const P& message) {
#if defined(__PORTABLE_PLATFORM__)
return std::string(message.GetTypeName());
#else // defined(__PORTABLE_PLATFORM__)
return message.DebugString();
std::string output;
google::protobuf::TextFormat::PrintToString(message, &output);
absl::StripTrailingAsciiWhitespace(&output);
return output;
#endif // !defined(__PORTABLE_PLATFORM__)
}
@@ -42,7 +46,12 @@ std::string ProtobufShortDebugString(const P& message) {
#if defined(__PORTABLE_PLATFORM__)
return std::string(message.GetTypeName());
#else // defined(__PORTABLE_PLATFORM__)
return message.ShortDebugString();
std::string output;
google::protobuf::TextFormat::Printer printer;
printer.SetSingleLineMode(true);
printer.PrintToString(message, &output);
absl::StripTrailingAsciiWhitespace(&output);
return output;
#endif // !defined(__PORTABLE_PLATFORM__)
}

View File

@@ -25,7 +25,6 @@ cc_library(
"//ortools/sat:cp_model_solver",
"//ortools/sat:model",
"//ortools/sat:sat_parameters_cc_proto",
"//ortools/util:time_limit",
"@abseil-cpp//absl/log:check",
],
)

View File

@@ -48,7 +48,7 @@ void* SolveCpNewEnv() { return new Model(); }
void SolveCpDestroyEnv(void* const cenv) { delete static_cast<Model*>(cenv); }
void SolveCpStopSolve(void* cenv) { StopSearch(static_cast<Model*>(cenv)); }
void SolveCpStopSearch(void* cenv) { StopSearch(static_cast<Model*>(cenv)); }
void SolveCpInterruptible(void* const cenv, const void* creq, int creq_len,
const void* cparams, int cparams_len, void** cres,

View File

@@ -27,7 +27,7 @@ void SolveCpModelWithParameters(const void* creq, int creq_len,
void* SolveCpNewEnv();
void SolveCpDestroyEnv(void* cenv);
void SolveCpStopSolve(void* cenv);
void SolveCpStopSearch(void* cenv);
// Allows for interruptible solves. Solves can be interrupted by calling
// `SolveCpStopSolve` with the `cenv` argument.
void SolveCpInterruptible(void* cenv, const void* creq, int creq_len,

View File

@@ -72,7 +72,7 @@ func SolveCpModelWithParameters(input *cmpb.CpModelProto, params *sppb.SatParame
// SolveCpModelInterruptibleWithParameters solves a CP Model with the given input proto
// and parameters and returns a CPSolverResponse. The solve can be interrupted by calling
// the `stopSolve`.
// the `stopSearch`.
func SolveCpModelInterruptibleWithParameters(input *cmpb.CpModelProto, params *sppb.SatParameters, interrupt <-chan struct{}) (*cmpb.CpSolverResponse, error) {
// Create the environment for interrupting solves.
env := newEnvWrapper()
@@ -100,7 +100,7 @@ func SolveCpModelInterruptibleWithParameters(input *cmpb.CpModelProto, params *s
go func() {
select {
case <-interrupt:
env.stopSolve()
env.stopSearch()
case <-solveDone:
}
}()
@@ -111,7 +111,7 @@ func SolveCpModelInterruptibleWithParameters(input *cmpb.CpModelProto, params *s
// so).
select {
case <-interrupt:
env.stopSolve()
env.stopSearch()
default:
}
@@ -141,7 +141,7 @@ type envWrapper struct {
// The returned object must be destroyed with delete() for the C++ object not to
// leak.
//
// This object is thread-safe: delete() and stopSolve() can be called
// This object is thread-safe: delete() and stopSearch() can be called
// concurrently.
func newEnvWrapper() *envWrapper {
return &envWrapper{
@@ -149,14 +149,14 @@ func newEnvWrapper() *envWrapper {
}
}
// stopSolve triggers the C++ SolveCpStopSolve method with the environment.
// stopSearch triggers the C++ SolveCpStopSearch method with the environment.
//
// If the environment has been deleted this has no effect.
func (intr *envWrapper) stopSolve() {
func (intr *envWrapper) stopSearch() {
intr.mutex.Lock()
defer intr.mutex.Unlock()
if uintptr(intr.ptr) != 0 {
C.SolveCpStopSolve(intr.ptr)
C.SolveCpStopSearch(intr.ptr)
}
}