Skip to content

Commit

Permalink
Added stop_search method
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Oct 6, 2024
1 parent 3b12af9 commit 92f2e21
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 0.13.2 (unreleased)

- Added `stop_search` method

## 0.13.1 (2024-10-05)

- Added binary installation for Debian 12
Expand Down
8 changes: 7 additions & 1 deletion ext/or-tools/constraint.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include <google/protobuf/text_format.h>
#include <ortools/sat/cp_model.h>
#include <ortools/util/time_limit.h>

#include "ext.h"

using operations_research::Domain;
using operations_research::TimeLimit;
using operations_research::sat::BoolVar;
using operations_research::sat::Constraint;
using operations_research::sat::CpModelBuilder;
Expand Down Expand Up @@ -424,17 +426,21 @@ void init_constraint(Rice::Module& m) {
[](Object self, CpModelBuilder& model, SatParameters& parameters, Object callback) {
Model m;

std::atomic<bool> stopped(false);
m.GetOrCreate<TimeLimit>()->RegisterExternalBooleanAsLimit(&stopped);

if (!callback.is_nil()) {
// TODO figure out how to use callback with multiple cores
parameters.set_num_search_workers(1);

m.Add(NewFeasibleSolutionObserver(
[&callback](const CpSolverResponse& r) {
[&](const CpSolverResponse& r) {
// ensure Ruby thread
if (ruby_native_thread_p()) {
// TODO find a better way to do this
callback.call("response=", r);
callback.call("on_solution_callback");
stopped = callback.attr_get("@stopped");
}
})
);
Expand Down
4 changes: 4 additions & 0 deletions lib/or_tools/cp_solver_solution_callback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@ def value(expr)
def objective_value
@response.objective_value
end

def stop_search
@stopped = true
end
end
end

0 comments on commit 92f2e21

Please sign in to comment.