Skip to content

Commit

Permalink
cp/python update two
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizux committed Oct 2, 2023
1 parent 6d12c24 commit 934557e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
46 changes: 27 additions & 19 deletions ortools/constraint_solver/python/constraint_solver.i
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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<Class>.
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);
Expand Down Expand Up @@ -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 {
Expand All @@ -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.
Expand Down
10 changes: 5 additions & 5 deletions ortools/constraint_solver/python/pywrapcp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()")
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 934557e

Please sign in to comment.