diff --git a/ortools/constraint_solver/python/constraint_solver.i b/ortools/constraint_solver/python/constraint_solver.i index 69510d64f5..36790357f3 100644 --- a/ortools/constraint_solver/python/constraint_solver.i +++ b/ortools/constraint_solver/python/constraint_solver.i @@ -104,8 +104,10 @@ struct FailureProtect { // ============= Type conversions ============== // See ./constraint_solver_helpers.i +PY_CONVERT_HELPER_PTR(Constraint); PY_CONVERT_HELPER_PTR(Decision); PY_CONVERT_HELPER_PTR(DecisionBuilder); +PY_CONVERT_HELPER_PTR(Demon); PY_CONVERT_HELPER_PTR(SearchMonitor); PY_CONVERT_HELPER_PTR(IntervalVar); PY_CONVERT_HELPER_PTR(SequenceVar); @@ -114,11 +116,17 @@ PY_CONVERT_HELPER_PTR(LocalSearchFilter); PY_CONVERT_HELPER_PTR(LocalSearchFilterManager); PY_CONVERT_HELPER_INTEXPR_AND_INTVAR(); +%{ + +%} + // Actual conversions. This also includes the conversion to std::vector. PY_CONVERT(IntVar); PY_CONVERT(IntExpr); +PY_CONVERT(Constraint); PY_CONVERT(Decision); PY_CONVERT(DecisionBuilder); +PY_CONVERT(Demon); PY_CONVERT(SearchMonitor); PY_CONVERT(IntervalVar); PY_CONVERT(SequenceVar); @@ -603,6 +611,25 @@ PY_STRINGIFY_DEBUGSTRING(Decision); } } +%extend operations_research::IntVarLocalSearchFilter { + int64_t IndexFromVar(IntVar* const var) const { + int64_t index = -1; + $self->FindIndex(var, &index); + return index; + } +} + +// Extend IntVar to provide natural iteration over its domains. +%extend operations_research::IntVar { + %pythoncode { + def DomainIterator(self): + return iter(self.DomainIteratorAux(False)) + + def HoleIterator(self): + return iter(self.HoleIteratorAux(False)) + } // %pythoncode +} + // Extend IntVarIterator to make it iterable in python. %extend operations_research::IntVarIterator { %pythoncode { @@ -623,25 +650,6 @@ PY_STRINGIFY_DEBUGSTRING(Decision); } // %pythoncode } -// Extend IntVar to provide natural iteration over its domains. -%extend operations_research::IntVar { - %pythoncode { - def DomainIterator(self): - return iter(self.DomainIteratorAux(False)) - - def HoleIterator(self): - return iter(self.HoleIteratorAux(False)) - } // %pythoncode -} - -%extend operations_research::IntVarLocalSearchFilter { - int64_t IndexFromVar(IntVar* const var) const { - int64_t index = -1; - $self->FindIndex(var, &index); - return index; - } -} - // ############ BEGIN DUPLICATED CODE BLOCK ############ // IMPORTANT: keep this code block in sync with the .i // files in ../java and ../csharp. diff --git a/ortools/constraint_solver/python/pywrapcp_test.py b/ortools/constraint_solver/python/pywrapcp_test.py index 4c4dede5fb..7f6d8c4a3e 100755 --- a/ortools/constraint_solver/python/pywrapcp_test.py +++ b/ortools/constraint_solver/python/pywrapcp_test.py @@ -576,10 +576,10 @@ def __init__(self, solver, x): print("Constraint built") def Post(self): - print("in Post()") + print("in Post()", file=sys.stderr) self._demon = CustomDemon(self._x) self._x.WhenBound(self._demon) - print("out of Post()") + print("out of Post()", file=sys.stderr) def InitialPropagate(self): print("in InitialPropagate()") @@ -612,10 +612,10 @@ def Post(self): def InitialPropagate(self): if self._x.Bound(): if self._x.Value() < 5: - print("Reject %d" % self._x.Value()) + print("Reject %d" % self._x.Value(), file=sys.stderr) self.solver().Fail() else: - print("Accept %d" % self._x.Value()) + print("Accept %d" % self._x.Value(), file=sys.stderr) class WatchDomain(pywrapcp.PyDemon): @@ -910,7 +910,6 @@ def solve(self, local_search_type): int_vars, solver.CHOOSE_FIRST_UNBOUND, solver.ASSIGN_MAX_VALUE ) ls = None - if local_search_type == 0: # LNS print("Large Neighborhood Search") one_var_lns = self.OneVarLNS(int_vars) @@ -939,6 +938,7 @@ def solve(self, local_search_type): print("Objective value = %d" % collector.ObjectiveValue(0)) def test_large_neighborhood_search(self): + pass self.solve(0) def test_local_search(self):