Skip to content

Commit

Permalink
Added stop_search method - closes #46
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Apr 23, 2023
1 parent 83ef537 commit 6df118b
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.10.2 (unreleased)

- Added `stop_search` method

## 0.10.1 (2023-03-20)

- Added `domain` method to `SatIntVar`
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 @@ -403,15 +405,19 @@ 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) {
[callback, &stopped](const CpSolverResponse& r) {
// 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 6df118b

Please sign in to comment.