Skip to content

Commit 6df118b

Browse files
committed
Added stop_search method - closes #46
1 parent 83ef537 commit 6df118b

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.10.2 (unreleased)
2+
3+
- Added `stop_search` method
4+
15
## 0.10.1 (2023-03-20)
26

37
- Added `domain` method to `SatIntVar`

ext/or-tools/constraint.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#include <google/protobuf/text_format.h>
22
#include <ortools/sat/cp_model.h>
3+
#include <ortools/util/time_limit.h>
34

45
#include "ext.h"
56

67
using operations_research::Domain;
8+
using operations_research::TimeLimit;
79
using operations_research::sat::BoolVar;
810
using operations_research::sat::Constraint;
911
using operations_research::sat::CpModelBuilder;
@@ -403,15 +405,19 @@ void init_constraint(Rice::Module& m) {
403405
[](Object self, CpModelBuilder& model, SatParameters& parameters, Object callback) {
404406
Model m;
405407

408+
std::atomic<bool> stopped(false);
409+
m.GetOrCreate<TimeLimit>()->RegisterExternalBooleanAsLimit(&stopped);
410+
406411
if (!callback.is_nil()) {
407412
// TODO figure out how to use callback with multiple cores
408413
parameters.set_num_search_workers(1);
409414

410415
m.Add(NewFeasibleSolutionObserver(
411-
[callback](const CpSolverResponse& r) {
416+
[callback, &stopped](const CpSolverResponse& r) {
412417
// TODO find a better way to do this
413418
callback.call("response=", r);
414419
callback.call("on_solution_callback");
420+
stopped = callback.attr_get("@stopped");
415421
})
416422
);
417423
}

lib/or_tools/cp_solver_solution_callback.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,9 @@ def value(expr)
1616
def objective_value
1717
@response.objective_value
1818
end
19+
20+
def stop_search
21+
@stopped = true
22+
end
1923
end
2024
end

0 commit comments

Comments
 (0)