diff --git a/src/algorithms/knapsack_solver.swig b/src/algorithms/knapsack_solver.swig index 929e72a1f8..c3816cc1cd 100644 --- a/src/algorithms/knapsack_solver.swig +++ b/src/algorithms/knapsack_solver.swig @@ -18,18 +18,4 @@ #include "algorithms/knapsack_solver.h" %} -#if defined(SWIGJAVA) -%rename (bestSolutionContains) operations_research::KnapsackSolver::BestSolutionContains; -%rename (getName) operations_research::KnapsackSolver::GetName; -%rename (init) operations_research::KnapsackSolver::Init; -%rename (solve) operations_research::KnapsackSolver::Solve; -%rename (useReduction) operations_research::KnapsackSolver::use_reduction; -%rename (setUseReduction) operations_research::KnapsackSolver::set_use_reduction; -#endif // SWIGJAVA - -#if defined(SWIGCSHARP) -%rename (UseReduction) operations_research::KnapsackSolver::use_reduction; -%rename (SetUseReduction) operations_research::KnapsackSolver::set_use_reduction; -#endif // SWIGCSHARP - %include "algorithms/knapsack_solver.h" diff --git a/src/constraint_solver/constraint_solver.swig b/src/constraint_solver/constraint_solver.swig index 5eb7394445..10c8da359c 100644 --- a/src/constraint_solver/constraint_solver.swig +++ b/src/constraint_solver/constraint_solver.swig @@ -16,7 +16,6 @@ %include util/data.swig %include util/util.swig -#ifdef SWIGPYTHON %module(directors="1") operations_research %feature("director") operations_research::SearchMonitor; @@ -957,7 +956,6 @@ static void SetPythonFlags(bool trace_propagation, } } } // namespace operations_research -#endif // SWIGPYTHON // Protect from failure. Since the code is very similar in Python, Java and C# // we use the same block. @@ -973,19 +971,8 @@ static void SetPythonFlags(bool trace_propagation, delete intercept; } else { solver->clear_fail_intercept(); - #if defined(SWIGCSHARP) - SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException, "fail"); - return $null; - #elif defined(SWIGJAVA) - jclass fail_class = jenv->FindClass( - "com/google/ortools/constraintsolver/" - "Solver$FailException"); - jenv->ThrowNew(fail_class, "fail"); - return $null; - #elif defined(SWIGPYTHON) PyErr_SetString(PyExc_IndexError, "fail"); SWIG_fail; - #endif } } %enddef @@ -1015,1070 +1002,6 @@ PROTECT_FROM_FAILURE(Solver::Fail(), arg1); #undef PROTECT_FROM_FAILURE } // namespace operations_research -#if defined(SWIGJAVA) -%module(directors="1") operations_research_constraint_solver; -%feature("director") DecisionBuilder; -%feature("director") Decision; -%feature("director") SearchMonitor; -%feature("director") Action; -%feature("director") LocalSearchOperator; -%feature("director") PathOperator; -%feature("director") BaseLns; -%feature("director") IntVarLocalSearchOperator; -%feature("director") SequenceVarLocalSearchOperator; -%feature("director") IntVarLocalSearchFilter; -%include "std_vector.i" - -%template(IntVector) std::vector; - -%{ -#include - -#include "constraint_solver/constraint_solver.h" -#include "constraint_solver/constraint_solveri.h" - -namespace operations_research { -class LocalSearchPhaseParameters { - public: - LocalSearchPhaseParameters() {} - ~LocalSearchPhaseParameters() {} -}; -} // namespace operations_research - - -struct FailureProtect { - jmp_buf exception_buffer; - void JumpBack() { - longjmp(exception_buffer, 1); - } -}; -%} - -%ignore operations_research::Solver::MakeIntVarArray; -%ignore operations_research::Solver::MakeBoolVarArray; -%ignore operations_research::Solver::MakeFixedDurationIntervalVarArray; -%ignore operations_research::IntVarLocalSearchFilter::FindIndex; - -%rename (nextWrap) operations_research::DecisionBuilder::Next; -%rename (toString) *::DebugString; -%rename (tryDecisions) operations_research::Solver::Try; - -%rename("%(lowercamelcase)s", %$isfunction) ""; - -namespace operations_research { -%define CONVERT_VECTOR(type) -%typemap(jni) const std::vector& "jobjectArray" -%typemap(jtype) const std::vector& "type[]" -%typemap(jstype) const std::vector& "type[]" -%typemap(javain) const std::vector& "$javainput" -%typemap(in) const std::vector& (std::vector result) { - jclass object_class = - jenv->FindClass("com/google/ortools/" - "constraintsolver/type"); - if (nullptr == object_class) - return $null; - jmethodID method_id = - jenv->GetStaticMethodID(object_class, - "getCPtr", - "(Lcom/google/ortools/" - "constraintsolver/type;)J"); - assert(method_id != nullptr); - for (int i = 0; i < jenv->GetArrayLength($input); i++) { - jobject elem = jenv->GetObjectArrayElement($input, i); - jlong ptr_value = jenv->CallStaticLongMethod(object_class, method_id, elem); - result.push_back(reinterpret_cast(ptr_value)); - } - $1 = &result; -} -%enddef -CONVERT_VECTOR(IntVar); -CONVERT_VECTOR(SearchMonitor); -CONVERT_VECTOR(DecisionBuilder); -CONVERT_VECTOR(IntervalVar); -CONVERT_VECTOR(SequenceVar); -CONVERT_VECTOR(LocalSearchOperator); -CONVERT_VECTOR(LocalSearchFilter); - -%typemap(javacode) Solver %{ - /** - * This exceptions signal that a failure has been raised in the C++ world. - * - */ - public static class FailException extends Exception { - public FailException() { - super(); - } - - public FailException(String message) { - super(message); - } - } - - public IntVar[] makeIntVarArray(int count, long min, long max) { - IntVar[] array = new IntVar[count]; - for (int i = 0; i < count; ++i) { - array[i] = makeIntVar(min, max); - } - return array; - } - - public IntVar[] makeIntVarArray(int count, long min, long max, String name) { - IntVar[] array = new IntVar[count]; - for (int i = 0; i < count; ++i) { - String var_name = name + i; - array[i] = makeIntVar(min, max, var_name); - } - return array; - } - - public IntVar[] makeBoolVarArray(int count) { - IntVar[] array = new IntVar[count]; - for (int i = 0; i < count; ++i) { - array[i] = makeBoolVar(); - } - return array; - } - - public IntVar[] makeBoolVarArray(int count, String name) { - IntVar[] array = new IntVar[count]; - for (int i = 0; i < count; ++i) { - String var_name = name + i; - array[i] = makeBoolVar(var_name); - } - return array; - } - - public IntervalVar[] makeFixedDurationIntervalVarArray(int count, - long start_min, - long start_max, - long duration, - boolean optional) { - IntervalVar[] array = new IntervalVar[count]; - for (int i = 0; i < count; ++i) { - array[i] = makeFixedDurationIntervalVar(start_min, - start_max, - duration, - optional, - ""); - } - return array; - } - - public IntervalVar[] makeFixedDurationIntervalVarArray(int count, - long start_min, - long start_max, - long duration, - boolean optional, - String name) { - IntervalVar[] array = new IntervalVar[count]; - for (int i = 0; i < count; ++i) { - array[i] = makeFixedDurationIntervalVar(start_min, - start_max, - duration, - optional, - name + i); - } - return array; - } - -%} - -%extend IntVarLocalSearchFilter { - int Index(IntVar* const var) { - int64 index = -1; - self->FindIndex(var, &index); - return index; - } -} -} // namespace operations_research - -namespace operations_research { -class LocalSearchPhaseParameters { - public: - LocalSearchPhaseParameters(); - ~LocalSearchPhaseParameters(); -}; -} // namespace operations_research - -#endif // SWIGJAVA - -#if defined(SWIGCSHARP) -%module(directors="1", allprotected="1") operations_research_constraint_solver; -%feature("director") Action; -%feature("director") BaseLNS; -%feature("director") Decision; -%feature("director") DecisionBuilder; -%feature("director") IntVarLocalSearchFilter; -%feature("director") IntVarLocalSearchOperator; -%feature("director") SequenceVarLocalSearchOperator; -%feature("director") LocalSearchOperator; -%feature("director") OptimizeVar; -%feature("director") SearchLimit; -%feature("director") SearchMonitor; -%feature("director") SymmetryBreaker; - -%include "std_vector.i" - -%template(IntVector) std::vector; - -%{ -#include - -#include "constraint_solver/constraint_solver.h" -#include "constraint_solver/constraint_solveri.h" - -namespace operations_research { -class LocalSearchPhaseParameters { - public: - LocalSearchPhaseParameters() {} - ~LocalSearchPhaseParameters() {} -}; -} // namespace operations_research - -struct FailureProtect { - jmp_buf exception_buffer; - void JumpBack() { - longjmp(exception_buffer, 1); - } -}; -%} - -%ignore operations_research::Solver::MakeIntVarArray; -%ignore operations_research::Solver::MakeBoolVarArray; -%ignore operations_research::Solver::MakeFixedDurationIntervalVarArray; -%ignore operations_research::IntVarLocalSearchFilter::FindIndex; -%ignore operations_research::SequenceVarLocalSearchOperatorTemplate::Value; - -// Generic rename rule. -%rename("%(camelcase)s", %$isfunction) ""; - -// Rename rule on directors; -%rename (NextWrap) operations_research::DecisionBuilder::Next; - -// Rename rule on SearchLimit -%rename (IsCrossed) operations_research::SearchLimit::crossed; - -// Rename rules on Solver. -%rename (Add) operations_research::Solver::AddConstraint; - -// Rename rule on DisjunctiveConstraint. -%rename (SequenceVar) operations_research::DisjunctiveConstraint::MakeSequenceVar; - -// Generic rename rules. -%rename (ToString) *::DebugString; - -// Keep the .solver() API. -%rename (solver) *::solver; - -// Rename NewSearch and EndSearch to add pinning. -%rename (NewSearchAux) operations_research::Solver::NewSearch; -%rename (EndSearchAux) operations_research::Solver::EndSearch; - -%typemap(csinterfaces_derived) operations_research::Constraint "IConstraintWithStatus"; -%typemap(cscode) operations_research::Constraint %{ - public static implicit operator IntVar(Constraint eq) - { - return eq.Var(); - } - - public static implicit operator IntExpr(Constraint eq) - { - return eq.Var(); - } - public static IntExpr operator+(Constraint a, Constraint b) { - return a.solver().MakeSum(a.Var(), b.Var()); - } - public static IntExpr operator+(Constraint a, long v) { - return a.solver().MakeSum(a.Var(), v); - } - public static IntExpr operator+(long v, Constraint a) { - return a.solver().MakeSum(a.Var(), v); - } - public static IntExpr operator-(Constraint a, Constraint b) { - return a.solver().MakeDifference(a.Var(), b.Var()); - } - public static IntExpr operator-(Constraint a, long v) { - return a.solver().MakeSum(a.Var(), -v); - } - public static IntExpr operator-(long v, Constraint a) { - return a.solver().MakeDifference(v, a.Var()); - } - public static IntExpr operator*(Constraint a, Constraint b) { - return a.solver().MakeProd(a.Var(), b.Var()); - } - public static IntExpr operator*(Constraint a, long v) { - return a.solver().MakeProd(a.Var(), v); - } - public static IntExpr operator*(long v, Constraint a) { - return a.solver().MakeProd(a.Var(), v); - } - public static IntExpr operator/(Constraint a, long v) { - return a.solver().MakeDiv(a.Var(), v); - } - public static IntExpr operator-(Constraint a) { - return a.solver().MakeOpposite(a.Var()); - } - public IntExpr Abs() { - return this.solver().MakeAbs(this.Var()); - } - public IntExpr Square() { - return this.solver().MakeSquare(this.Var()); - } - public static WrappedConstraint operator ==(Constraint a, long v) { - return new WrappedConstraint(a.solver().MakeEquality(a.Var(), v)); - } - public static WrappedConstraint operator ==(long v, Constraint a) { - return new WrappedConstraint(a.solver().MakeEquality(a.Var(), v)); - } - public static WrappedConstraint operator !=(Constraint a, long v) { - return new WrappedConstraint(a.solver().MakeNonEquality(a.Var(), v)); - } - public static WrappedConstraint operator !=(long v, Constraint a) { - return new WrappedConstraint(a.solver().MakeNonEquality(a.Var(), v)); - } - public static WrappedConstraint operator >=(Constraint a, long v) { - return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), v)); - } - public static WrappedConstraint operator >=(long v, Constraint a) { - return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), v)); - } - public static WrappedConstraint operator >(Constraint a, long v) { - return new WrappedConstraint(a.solver().MakeGreater(a.Var(), v)); - } - public static WrappedConstraint operator >(long v, Constraint a) { - return new WrappedConstraint(a.solver().MakeLess(a.Var(), v)); - } - public static WrappedConstraint operator <=(Constraint a, long v) { - return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), v)); - } - public static WrappedConstraint operator <=(long v, Constraint a) { - return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), v)); - } - public static WrappedConstraint operator <(Constraint a, long v) { - return new WrappedConstraint(a.solver().MakeLess(a.Var(), v)); - } - public static WrappedConstraint operator <(long v, Constraint a) { - return new WrappedConstraint(a.solver().MakeGreater(a.Var(), v)); - } - public static WrappedConstraint operator >=(Constraint a, Constraint b) { - return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), b.Var())); - } - public static WrappedConstraint operator >(Constraint a, Constraint b) { - return new WrappedConstraint(a.solver().MakeGreater(a.Var(), b.Var())); - } - public static WrappedConstraint operator <=(Constraint a, Constraint b) { - return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), b.Var())); - } - public static WrappedConstraint operator <(Constraint a, Constraint b) { - return new WrappedConstraint(a.solver().MakeLess(a.Var(), b.Var())); - } - public static ConstraintEquality operator ==(Constraint a, Constraint b) { - return new ConstraintEquality(a, b, true); - } - public static ConstraintEquality operator !=(Constraint a, Constraint b) { - return new ConstraintEquality(a, b, false); - } -%} - - -// Add arithmetic operators to integer expressions. -%typemap(cscode) operations_research::IntExpr %{ - public static IntExpr operator+(IntExpr a, IntExpr b) { - return a.solver().MakeSum(a, b); - } - public static IntExpr operator+(IntExpr a, long v) { - return a.solver().MakeSum(a, v); - } - public static IntExpr operator+(long v, IntExpr a) { - return a.solver().MakeSum(a, v); - } - public static IntExpr operator-(IntExpr a, IntExpr b) { - return a.solver().MakeDifference(a, b); - } - public static IntExpr operator-(IntExpr a, long v) { - return a.solver().MakeSum(a, -v); - } - public static IntExpr operator-(long v, IntExpr a) { - return a.solver().MakeDifference(v, a); - } - public static IntExpr operator*(IntExpr a, IntExpr b) { - return a.solver().MakeProd(a, b); - } - public static IntExpr operator*(IntExpr a, long v) { - return a.solver().MakeProd(a, v); - } - public static IntExpr operator*(long v, IntExpr a) { - return a.solver().MakeProd(a, v); - } - public static IntExpr operator/(IntExpr a, long v) { - return a.solver().MakeDiv(a, v); - } - public static IntExpr operator-(IntExpr a) { - return a.solver().MakeOpposite(a); - } - public IntExpr Abs() { - return this.solver().MakeAbs(this); - } - public IntExpr Square() { - return this.solver().MakeSquare(this); - } - public static IntExprEquality operator ==(IntExpr a, IntExpr b) { - return new IntExprEquality(a, b, true); - } - public static IntExprEquality operator !=(IntExpr a, IntExpr b) { - return new IntExprEquality(a, b, false); - } - public static WrappedConstraint operator ==(IntExpr a, long v) { - return new WrappedConstraint(a.solver().MakeEquality(a, v)); - } - public static WrappedConstraint operator !=(IntExpr a, long v) { - return new WrappedConstraint(a.solver().MakeNonEquality(a.Var(), v)); - } - public static WrappedConstraint operator >=(IntExpr a, long v) { - return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a, v)); - } - public static WrappedConstraint operator >(IntExpr a, long v) { - return new WrappedConstraint(a.solver().MakeGreater(a, v)); - } - public static WrappedConstraint operator <=(IntExpr a, long v) { - return new WrappedConstraint(a.solver().MakeLessOrEqual(a, v)); - } - public static WrappedConstraint operator <(IntExpr a, long v) { - return new WrappedConstraint(a.solver().MakeLess(a, v)); - } - public static WrappedConstraint operator >=(IntExpr a, IntExpr b) { - return new WrappedConstraint(a.solver().MakeGreaterOrEqual(a.Var(), b.Var())); - } - public static WrappedConstraint operator >(IntExpr a, IntExpr b) { - return new WrappedConstraint(a.solver().MakeGreater(a.Var(), b.Var())); - } - public static WrappedConstraint operator <=(IntExpr a, IntExpr b) { - return new WrappedConstraint(a.solver().MakeLessOrEqual(a.Var(), b.Var())); - } - public static WrappedConstraint operator <(IntExpr a, IntExpr b) { - return new WrappedConstraint(a.solver().MakeLess(a.Var(), b.Var())); - } -%} - -CS_TYPEMAP_STDVECTOR_OBJECT(operations_research::IntVar, IntVar) -CS_TYPEMAP_STDVECTOR_OBJECT(operations_research::SearchMonitor, SearchMonitor) -CS_TYPEMAP_STDVECTOR_OBJECT(operations_research::DecisionBuilder, DecisionBuilder) -CS_TYPEMAP_STDVECTOR_OBJECT(operations_research::IntervalVar, IntervalVar) -CS_TYPEMAP_STDVECTOR_OBJECT(operations_research::SequenceVar, SequenceVar) -CS_TYPEMAP_STDVECTOR_OBJECT(operations_research::LocalSearchOperator, LocalSearchOperator) -CS_TYPEMAP_STDVECTOR_OBJECT(operations_research::LocalSearchFilter, LocalSearchFilter) -CS_TYPEMAP_STDVECTOR_OBJECT(operations_research::SymmetryBreaker, SymmetryBreaker) - -namespace operations_research { -%extend IntervalVar { - Constraint* EndsAfterEnd(IntervalVar* other) { - return self->solver()->MakeIntervalVarRelation(self, operations_research::Solver::ENDS_AFTER_END, other); - } - Constraint* EndsAfterStart(IntervalVar* other) { - return self->solver()->MakeIntervalVarRelation(self, operations_research::Solver::ENDS_AFTER_START, other); - } - Constraint* EndsAtEnd(IntervalVar* other) { - return self->solver()->MakeIntervalVarRelation(self, operations_research::Solver::ENDS_AT_END, other); - } - Constraint* EndsAtStart(IntervalVar* other) { - return self->solver()->MakeIntervalVarRelation(self, operations_research::Solver::ENDS_AT_START, other); - } - Constraint* StartsAfterEnd(IntervalVar* other) { - return self->solver()->MakeIntervalVarRelation(self, operations_research::Solver::STARTS_AFTER_END, other); - } - Constraint* StartsAfterStart(IntervalVar* other) { - return self->solver()->MakeIntervalVarRelation(self, - operations_research::Solver::STARTS_AFTER_START, - other); - } - Constraint* StartsAtEnd(IntervalVar* other) { - return self->solver()->MakeIntervalVarRelation(self, operations_research::Solver::STARTS_AT_END, other); - } - Constraint* StartsAtStart(IntervalVar* other) { - return self->solver()->MakeIntervalVarRelation(self, - operations_research::Solver::STARTS_AT_START, - other); - } - Constraint* EndsAfter(int64 date) { - return self->solver()->MakeIntervalVarRelation(self, - operations_research::Solver::ENDS_AFTER, - date); - } - Constraint* EndsAt(int64 date) { - return self->solver()->MakeIntervalVarRelation(self, - operations_research::Solver::ENDS_AT, - date); - } - Constraint* EndsBefore(int64 date) { - return self->solver()->MakeIntervalVarRelation(self, - operations_research::Solver::ENDS_BEFORE, - date); - } - Constraint* StartsAfter(int64 date) { - return self->solver()->MakeIntervalVarRelation(self, - operations_research::Solver::STARTS_AFTER, - date); - } - Constraint* StartsAt(int64 date) { - return self->solver()->MakeIntervalVarRelation(self, - operations_research::Solver::STARTS_AT, - date); - } - Constraint* StartsBefore(int64 date) { - return self->solver()->MakeIntervalVarRelation(self, - operations_research::Solver::STARTS_BEFORE, - date); - } - Constraint* CrossesDate(int64 date) { - return self->solver()->MakeIntervalVarRelation(self, - operations_research::Solver::CROSS_DATE, - date); - } - Constraint* AvoidsDate(int64 date) { - return self->solver()->MakeIntervalVarRelation(self, - operations_research::Solver::AVOID_DATE, - date); - } - IntervalVar* RelaxedMax() { - return self->solver()->MakeIntervalRelaxedMax(self); - } - IntervalVar* RelaxedMin() { - return self->solver()->MakeIntervalRelaxedMin(self); - } -} - -%extend IntExpr { - Constraint* MapTo(const std::vector& vars) { - return self->solver()->MakeMapDomain(self->Var(), vars); - } - IntExpr* IndexOf(const std::vector& vars) { - return self->solver()->MakeElement(vars, self->Var()); - } - IntExpr* IndexOf(const std::vector& vars) { - return self->solver()->MakeElement(vars, self->Var()); - } - IntVar* IsEqual(int64 value) { - return self->solver()->MakeIsEqualCstVar(self->Var(), value); - } - IntVar* IsDifferent(int64 value) { - return self->solver()->MakeIsDifferentCstVar(self->Var(), value); - } - IntVar* IsGreater(int64 value) { - return self->solver()->MakeIsGreaterCstVar(self->Var(), value); - } - IntVar* IsGreaterOrEqual(int64 value) { - return self->solver()->MakeIsGreaterOrEqualCstVar(self->Var(), value); - } - IntVar* IsLess(int64 value) { - return self->solver()->MakeIsLessCstVar(self->Var(), value); - } - IntVar* IsLessOrEqual(int64 value) { - return self->solver()->MakeIsLessOrEqualCstVar(self->Var(), value); - } - IntVar* IsMember(const std::vector& values) { - return self->solver()->MakeIsMemberVar(self->Var(), values); - } - IntVar* IsMember(const std::vector& values) { - return self->solver()->MakeIsMemberVar(self->Var(), values); - } - Constraint* Member(const std::vector& values) { - return self->solver()->MakeMemberCt(self->Var(), values); - } - Constraint* Member(const std::vector& values) { - return self->solver()->MakeMemberCt(self->Var(), values); - } - IntVar* IsEqual(IntExpr* const other) { - return self->solver()->MakeIsEqualVar(self->Var(), other->Var()); - } - IntVar* IsDifferent(IntExpr* const other) { - return self->solver()->MakeIsDifferentVar(self->Var(), other->Var()); - } - IntVar* IsGreater(IntExpr* const other) { - return self->solver()->MakeIsGreaterVar(self->Var(), other->Var()); - } - IntVar* IsGreaterOrEqual(IntExpr* const other) { - return self->solver()->MakeIsGreaterOrEqualVar(self->Var(), other->Var()); - } - IntVar* IsLess(IntExpr* const other) { - return self->solver()->MakeIsLessVar(self->Var(), other->Var()); - } - IntVar* IsLessOrEqual(IntExpr* const other) { - return self->solver()->MakeIsLessOrEqualVar(self->Var(), other->Var()); - } - OptimizeVar* Minimize(long step) { - return self->solver()->MakeMinimize(self->Var(), step); - } - OptimizeVar* Maximize(long step) { - return self->solver()->MakeMaximize(self->Var(), step); - } -} - -%typemap(cscode) Solver %{ - public IntVar[] MakeIntVarArray(int count, long min, long max) { - IntVar[] array = new IntVar[count]; - for (int i = 0; i < count; ++i) { - array[i] = MakeIntVar(min, max); - } - return array; - } - - public IntVar[] MakeIntVarArray(int count, long min, long max, string name) { - IntVar[] array = new IntVar[count]; - for (int i = 0; i < count; ++i) { - string var_name = name + i; - array[i] = MakeIntVar(min, max, var_name); - } - return array; - } - - public IntVar[] MakeIntVarArray(int count, long[] values) { - IntVar[] array = new IntVar[count]; - for (int i = 0; i < count; ++i) { - array[i] = MakeIntVar(values); - } - return array; - } - - public IntVar[] MakeIntVarArray(int count, long[] values, string name) { - IntVar[] array = new IntVar[count]; - for (int i = 0; i < count; ++i) { - string var_name = name + i; - array[i] = MakeIntVar(values, var_name); - } - return array; - } - - public IntVar[] MakeIntVarArray(int count, int[] values) { - IntVar[] array = new IntVar[count]; - for (int i = 0; i < count; ++i) { - array[i] = MakeIntVar(values); - } - return array; - } - - public IntVar[] MakeIntVarArray(int count, int[] values, string name) { - IntVar[] array = new IntVar[count]; - for (int i = 0; i < count; ++i) { - string var_name = name + i; - array[i] = MakeIntVar(values, var_name); - } - return array; - } - - public IntVar[] MakeBoolVarArray(int count) { - IntVar[] array = new IntVar[count]; - for (int i = 0; i < count; ++i) { - array[i] = MakeBoolVar(); - } - return array; - } - - public IntVar[] MakeBoolVarArray(int count, string name) { - IntVar[] array = new IntVar[count]; - for (int i = 0; i < count; ++i) { - string var_name = name + i; - array[i] = MakeBoolVar(var_name); - } - return array; - } - - public IntVar[,] MakeIntVarMatrix(int rows, int cols, long min, long max) { - IntVar[,] array = new IntVar[rows, cols]; - for (int i = 0; i < rows; ++i) { - for (int j = 0; j < cols; ++j) { - array[i,j] = MakeIntVar(min, max); - } - } - return array; - } - - public IntVar[,] MakeIntVarMatrix(int rows, int cols, - long min, long max, string name) { - IntVar[,] array = new IntVar[rows, cols]; - for (int i = 0; i < rows; ++i) { - for (int j = 0; j < cols; ++j) { - string var_name = name + "["+ i + ", " + j +"]"; - array[i,j] = MakeIntVar(min, max, var_name); - } - } - return array; - } - - public IntVar[,] MakeIntVarMatrix(int rows, int cols, long[] values) { - IntVar[,] array = new IntVar[rows, cols]; - for (int i = 0; i < rows; ++i) { - for (int j = 0; j < cols; ++j) { - array[i,j] = MakeIntVar(values); - } - } - return array; - } - - public IntVar[,] MakeIntVarMatrix(int rows, int cols, - long[] values, string name) { - IntVar[,] array = new IntVar[rows, cols]; - for (int i = 0; i < rows; ++i) { - for (int j = 0; j < cols; ++j) { - string var_name = name + "["+ i + ", " + j +"]"; - array[i,j] = MakeIntVar(values, var_name); - } - } - return array; - } - - public IntVar[,] MakeIntVarMatrix(int rows, int cols, int[] values) { - IntVar[,] array = new IntVar[rows, cols]; - for (int i = 0; i < rows; ++i) { - for (int j = 0; j < cols; ++j) { - array[i,j] = MakeIntVar(values); - } - } - return array; - } - - public IntVar[,] MakeIntVarMatrix(int rows, int cols, - int[] values, string name) { - IntVar[,] array = new IntVar[rows, cols]; - for (int i = 0; i < rows; ++i) { - for (int j = 0; j < cols; ++j) { - string var_name = name + "["+ i + ", " + j +"]"; - array[i,j] = MakeIntVar(values, var_name); - } - } - return array; - } - - public IntVar[,] MakeBoolVarMatrix(int rows, int cols) { - IntVar[,] array = new IntVar[rows, cols]; - for (int i = 0; i < rows; ++i) { - for (int j = 0; j < cols; ++j) { - array[i,j] = MakeBoolVar(); - } - } - return array; - } - - public IntVar[,] MakeBoolVarMatrix(int rows, int cols, string name) { - IntVar[,] array = new IntVar[rows, cols]; - for (int i = 0; i < rows; ++i) { - for (int j = 0; j < cols; ++j) { - string var_name = name + "["+ i + ", " + j +"]"; - array[i,j] = MakeBoolVar(var_name); - } - } - return array; - } - - public IntervalVar[] MakeFixedDurationIntervalVarArray(int count, - long start_min, - long start_max, - long duration, - bool optional) { - IntervalVar[] array = new IntervalVar[count]; - for (int i = 0; i < count; ++i) { - array[i] = MakeFixedDurationIntervalVar(start_min, - start_max, - duration, - optional, - ""); - } - return array; - } - - public IntervalVar[] MakeFixedDurationIntervalVarArray(int count, - long start_min, - long start_max, - long duration, - bool optional, - string name) { - IntervalVar[] array = new IntervalVar[count]; - for (int i = 0; i < count; ++i) { - array[i] = MakeFixedDurationIntervalVar(start_min, - start_max, - duration, - optional, - name + i); - } - return array; - } - - public IntervalVar[] MakeFixedDurationIntervalVarArray(int count, - long[] start_min, - long[] start_max, - long[] duration, - bool optional, - string name) { - IntervalVar[] array = new IntervalVar[count]; - for (int i = 0; i < count; ++i) { - array[i] = MakeFixedDurationIntervalVar(start_min[i], - start_max[i], - duration[i], - optional, - name + i); - } - return array; - } - - public IntervalVar[] MakeFixedDurationIntervalVarArray(int count, - int[] start_min, - int[] start_max, - int[] duration, - bool optional, - string name) { - IntervalVar[] array = new IntervalVar[count]; - for (int i = 0; i < count; ++i) { - array[i] = MakeFixedDurationIntervalVar(start_min[i], - start_max[i], - duration[i], - optional, - name + i); - } - return array; - } - public IntervalVar[] MakeFixedDurationIntervalVarArray(IntVar[] starts, - int[] durations, - string name) { - int count = starts.Length; - IntervalVar[] array = new IntervalVar[count]; - for (int i = 0; i < count; ++i) { - array[i] = MakeFixedDurationIntervalVar(starts[i], - durations[i], - name + i); - } - return array; - } - public IntervalVar[] MakeFixedDurationIntervalVarArray(IntVar[] starts, - long[] durations, - string name) { - int count = starts.Length; - IntervalVar[] array = new IntervalVar[count]; - for (int i = 0; i < count; ++i) { - array[i] = MakeFixedDurationIntervalVar(starts[i], - durations[i], - name + i); - } - return array; - } - public void NewSearch(DecisionBuilder db) { - pinned_decision_builder_ = db; - pinned_search_monitors_.Clear(); - NewSearchAux(db); - } - - public void NewSearch(DecisionBuilder db, SearchMonitor sm1) { - pinned_decision_builder_ = db; - pinned_search_monitors_.Clear(); - pinned_search_monitors_.Add(sm1); - NewSearchAux(db, sm1); - } - - - public void NewSearch(DecisionBuilder db, - SearchMonitor sm1, - SearchMonitor sm2) { - pinned_decision_builder_ = db; - pinned_search_monitors_.Clear(); - pinned_search_monitors_.Add(sm1); - pinned_search_monitors_.Add(sm2); - NewSearchAux(db, sm1, sm2); - } - - public void NewSearch(DecisionBuilder db, - SearchMonitor sm1, - SearchMonitor sm2, - SearchMonitor sm3) { - pinned_decision_builder_ = db; - pinned_search_monitors_.Clear(); - pinned_search_monitors_.Add(sm1); - pinned_search_monitors_.Add(sm2); - pinned_search_monitors_.Add(sm3); - NewSearchAux(db, sm1, sm2, sm3); - } - - public void NewSearch(DecisionBuilder db, - SearchMonitor sm1, - SearchMonitor sm2, - SearchMonitor sm3, - SearchMonitor sm4) { - pinned_decision_builder_ = db; - pinned_search_monitors_.Clear(); - pinned_search_monitors_.Add(sm1); - pinned_search_monitors_.Add(sm2); - pinned_search_monitors_.Add(sm3); - pinned_search_monitors_.Add(sm4); - NewSearchAux(db, sm1, sm2, sm3, sm4); - } - - public void NewSearch(DecisionBuilder db, SearchMonitor[] monitors) { - pinned_decision_builder_ = db; - pinned_search_monitors_.Clear(); - pinned_search_monitors_.AddRange(monitors); - NewSearchAux(db, monitors); - } - - public void EndSearch() { - pinned_decision_builder_ = null; - pinned_search_monitors_.Clear(); - EndSearchAux(); - } - - private System.Collections.Generic.List pinned_search_monitors_ - = new System.Collections.Generic.List(); - private DecisionBuilder pinned_decision_builder_; -%} - -%extend IntVarLocalSearchFilter { - int Index(IntVar* const var) { - int64 index = -1; - self->FindIndex(var, &index); - return index; - } -} - -} // namespace operations_research - -CS_VECTOR_RETURN1(operations_research, SequenceVarLocalSearchOperator, Sequence, - int, int64) -CS_VECTOR_RETURN1(operations_research, SequenceVarLocalSearchOperator, - OldSequence, int, int64) - -%typemap(cscode) operations_research::SequenceVarLocalSearchOperator %{ - public int[] Sequence(long a) { - int size = SequenceSize(a); - int[] result = new int[size]; - for (int position = 0; position < size; ++position) { - result[position] = SequenceValueAt(a, position); - } - return result; - } - public int[] OldSequence(long a) { - int size = OldSequenceSize(a); - int[] result = new int[size]; - for (int position = 0; position < size; ++position) { - result[position] = OldSequenceValueAt(a, position); - } - return result; - } -%} - -CS_VECTOR_RETURN2(operations_research, SolutionCollector, ForwardSequence, - int, int, SequenceVar*) -CS_VECTOR_RETURN2(operations_research, SolutionCollector, BackwardSequence, - int, int, SequenceVar*) -CS_VECTOR_RETURN2(operations_research, SolutionCollector, Unperformed, - int, int, SequenceVar*) - -%typemap(cscode) operations_research::SolutionCollector %{ - public int[] ForwardSequence(int a, SequenceVar b) { - int size = ForwardSequenceSize(a, b); - int[] result = new int[size]; - for (int position = 0; position < size; ++position) { - result[position] = ForwardSequenceValueAt(a, b, position); - } - return result; - } - public int[] BackwardSequence(int a, SequenceVar b) { - int size = BackwardSequenceSize(a, b); - int[] result = new int[size]; - for (int position = 0; position < size; ++position) { - result[position] = BackwardSequenceValueAt(a, b, position); - } - return result; - } - public int[] Unperformed(int a, SequenceVar b) { - int size = UnperformedSize(a, b); - int[] result = new int[size]; - for (int position = 0; position < size; ++position) { - result[position] = UnperformedValueAt(a, b, position); - } - return result; - } -%} - -CS_VECTOR_RETURN1(operations_research, Assignment, ForwardSequence, - int, const SequenceVar* const) -CS_VECTOR_RETURN1(operations_research, Assignment, BackwardSequence, - int, const SequenceVar* const) -CS_VECTOR_RETURN1(operations_research, Assignment, Unperformed, - int, const SequenceVar* const) - -%typemap(cscode) operations_research::Assignment %{ - public int[] ForwardSequence(SequenceVar a) { - int size = ForwardSequenceSize(a); - int[] result = new int[size]; - for (int position = 0; position < size; ++position) { - result[position] = ForwardSequenceValueAt(a, position); - } - return result; - } - public int[] BackwardSequence(SequenceVar a) { - int size = BackwardSequenceSize(a); - int[] result = new int[size]; - for (int position = 0; position < size; ++position) { - result[position] = BackwardSequenceValueAt(a, position); - } - return result; - } - public int[] Unperformed(SequenceVar a) { - int size = UnperformedSize(a); - int[] result = new int[size]; - for (int position = 0; position < size; ++position) { - result[position] = UnperformedValueAt(a, position); - } - return result; - } -%} - -CS_VECTOR_RETURN0(operations_research, SequenceVarElement, ForwardSequence, - int) -CS_VECTOR_RETURN0(operations_research, SequenceVarElement, BackwardSequence, - int) -CS_VECTOR_RETURN0(operations_research, SequenceVarElement, Unperformed, - int) - -%typemap(cscode) operations_research::SequenceVarElement %{ - public int[] ForwardSequence() { - int size = ForwardSequenceSize(); - int[] result = new int[size]; - for (int position = 0; position < size; ++position) { - result[position] = ForwardSequenceValueAt(position); - } - return result; - } - public int[] BackwardSequence() { - int size = BackwardSequenceSize(); - int[] result = new int[size]; - for (int position = 0; position < size; ++position) { - result[position] = BackwardSequenceValueAt(position); - } - return result; - } - public int[] Unperformed() { - int size = UnperformedSize(); - int[] result = new int[size]; - for (int position = 0; position < size; ++position) { - result[position] = UnperformedValueAt(position); - } - return result; - } -%} - - -%extend operations_research::IntVarLocalSearchOperator { - int64 Value(int64 index) const { return self->Value(index); } - int64 OldValue(int64 index) const { return self->OldValue(index); } -} - -namespace operations_research { -class LocalSearchPhaseParameters { - public: - LocalSearchPhaseParameters(); - ~LocalSearchPhaseParameters(); -}; -} // namespace operations_research - -#endif // SWIGCSHARP - // Wrap cp includes %include constraint_solver/constraint_solver.h %include constraint_solver/constraint_solveri.h @@ -2091,7 +1014,6 @@ typedef Assignment::AssignmentContainer AssignmentContainer; %template(AssignmentIntContainer) AssignmentContainer; } -#if defined(SWIGPYTHON) %pythoncode { class PyDecisionBuilder(object): def NextWrapper(self, solver): @@ -2134,5 +1056,3 @@ class PyDemon(Demon): return "PyDemon" } -#endif // SWIGPYTHON - diff --git a/src/graph/graph.swig b/src/graph/graph.swig index 7007b55cd8..f4ffd121b3 100644 --- a/src/graph/graph.swig +++ b/src/graph/graph.swig @@ -22,7 +22,6 @@ #include "graph/min_cost_flow.h" %} -#if defined(SWIGPYTHON) // Instructs swig to ignore the std::vector* parameters as inputs since // they are actually outputs. Note that the name "result" is important and // only such parameters will be ignored. @@ -32,102 +31,6 @@ // Apply std::vector to python list conversion. %apply std::vector* OUTPUT {std::vector* result} -#endif // SWIGPYTHON - -#if defined(SWIGJAVA) -// Rename rules on EbertGraph. -%rename(reserve) operations_research::EbertGraphBase::Reserve; -%rename(numNodes) operations_research::StarGraphBase::num_nodes; -%rename(numArcs) operations_research::StarGraphBase::num_arcs; -%rename(maxNumNodes) operations_research::StarGraphBase::max_num_nodes; -%rename(maxNumArcs) operations_research::StarGraphBase::max_num_arcs; -%rename(addArc) operations_research::EbertGraphBase::AddArc; - -// Rename rules on MaxFlow. -%rename (getSourceNodeIndex) operations_research::GenericMaxFlow::GetSourceNodeIndex; -%rename (getSinkNodeIndex) operations_research::GenericMaxFlow::GetSinkNodeIndex; -%rename (setArcCapacity) operations_research::GenericMaxFlow::SetArcCapacity; -%rename (setArcFlow) operations_research::GenericMaxFlow::SetArcFlow; -%rename (solve) operations_research::GenericMaxFlow::Solve; -%rename (getOptimalFlow) operations_research::GenericMaxFlow::GetOptimalFlow; -%rename (flow) operations_research::GenericMaxFlow::Flow; -%rename (capacity) operations_research::GenericMaxFlow::Capacity; - -// Rename rules on SimpleMaxFlow. -%rename(addArcWithCapacity) operations_research::SimpleMaxFlow::AddArcWithCapacity; -%rename(getNumNodes) operations_research::SimpleMaxFlow::NumNodes; -%rename(getNumArcs) operations_research::SimpleMaxFlow::NumArcs; -%rename(getTail) operations_research::SimpleMaxFlow::Tail; -%rename(getHead) operations_research::SimpleMaxFlow::Head; -%rename(getCapacity) operations_research::SimpleMaxFlow::Capacity; -%rename(solve) operations_research::SimpleMaxFlow::Solve; -%rename(getOptimalFlow) operations_research::SimpleMaxFlow::OptimalFlow; -%rename(getFlow) operations_research::SimpleMaxFlow::Flow; -%rename(getSourceSideMinCut) operations_research::SimpleMaxFlow::GetSourceSideMinCut; -%rename(getSinkSideMinCut) operations_research::SimpleMaxFlow::GetSinkSideMinCut; - -// Rename rules on MinCostFlow. -%rename(setNodeSupply) operations_research::GenericMinCostFlow::SetNodeSupply; -%rename(setArcUnitCost) operations_research::GenericMinCostFlow::SetArcUnitCost; -%rename(setArcCapacity) operations_research::GenericMinCostFlow::SetArcCapacity; -%rename(setArcFlow) operations_research::GenericMinCostFlow::SetArcFlow; -%rename(solve) operations_research::GenericMinCostFlow::Solve; -%rename(getOptimalCost) operations_research::GenericMinCostFlow::GetOptimalCost; -%rename(flow) operations_research::GenericMinCostFlow::Flow; -%rename(capacity) operations_research::GenericMinCostFlow::Capacity; -%rename(cost) operations_research::GenericMinCostFlow::Cost; -%rename(supply) operations_research::GenericMinCostFlow::Supply; - -// Rename rules on SimpleMinCostFlow. -%rename(addArcWithCapacityAndUnitCost) operations_research::SimpleMinCostFlow::AddArcWithCapacityAndUnitCost; -%rename(setNodeSupply) operations_research::SimpleMinCostFlow::SetNodeSupply; -%rename(solve) operations_research::SimpleMinCostFlow::Solve; -%rename(getOptimalCost) operations_research::SimpleMinCostFlow::OptimalCost; -%rename(getNumNodes) operations_research::SimpleMinCostFlow::NumNodes; -%rename(getNumArcs) operations_research::SimpleMinCostFlow::NumArcs; -%rename(getTail) operations_research::SimpleMinCostFlow::Tail; -%rename(getHead) operations_research::SimpleMinCostFlow::Head; -%rename(getFlow) operations_research::SimpleMinCostFlow::Flow; -%rename(getCapacity) operations_research::SimpleMinCostFlow::Capacity; -%rename(getUnitCost) operations_research::SimpleMinCostFlow::UnitCost; -%rename(getSupply) operations_research::SimpleMinCostFlow::Supply; - -// Rename rules on SimpleLinearSumAssignment. -%rename(addArcWithCost) operations_research::SimpleLinearSumAssignment::AddArcWithCost; -%rename(getNumNodes) operations_research::SimpleLinearSumAssignment::NumNodes; -%rename(getNumArcs) operations_research::SimpleLinearSumAssignment::NumArcs; -%rename(getLeftNode) operations_research::SimpleLinearSumAssignment::LeftNode; -%rename(getRightNode) operations_research::SimpleLinearSumAssignment::RightNode; -%rename(getCost) operations_research::SimpleLinearSumAssignment::Cost; -%rename(solve) operations_research::SimpleLinearSumAssignment::Solve; -%rename(getOptimalCost) operations_research::SimpleLinearSumAssignment::OptimalCost; -%rename(getRightMate) operations_research::SimpleLinearSumAssignment::RightMate; -%rename(getAssignmentCost) operations_research::SimpleLinearSumAssignment::AssignmentCost; - -#endif // SWIGJAVA - -#if defined(SWIGCSHARP) -// Rename rules on EbertGraph. -%rename(EndArcIndex) operations_research::StarGraphBase::end_arc_index; -%rename(EndNodeIndex) operations_research::StarGraphBase::end_node_index; -%rename(MaxEndArcIndex) operations_research::StarGraphBase::max_end_arc_index; -%rename(MaxEndNodeIndex) operations_research::StarGraphBase::max_end_node_index; -%rename(MaxNumArcs) operations_research::StarGraphBase::max_num_arcs; -%rename(MaxNumArcs) operations_research::StarGraphBase::max_num_arcs; -%rename(MaxNumNodes) operations_research::StarGraphBase::max_num_nodes; -%rename(MaxNumNodes) operations_research::StarGraphBase::max_num_nodes; -%rename(NumArcs) operations_research::StarGraphBase::num_arcs; -%rename(NumNodes) operations_research::StarGraphBase::num_nodes; - -// Rename rules on MaxFlow. -%rename (Graph) operations_research::MaxFlow::graph; -%rename (GetStatus) operations_research::MaxFlow::status; - -// Rename rules on MinCostFlow. -%rename (Graph) operations_research::MinCostFlow::graph; -%rename (GetStatus) operations_research::MinCostFlow::status; -#endif // SWIGCSHARP - // Use the SimpleLinearSumAssignment in SWIG (java and python). %rename(ComplexLinearSumAssignment) operations_research::LinearSumAssignment; diff --git a/src/linear_solver/linear_solver.swig b/src/linear_solver/linear_solver.swig index c31fa2f6a6..047e476e8e 100644 --- a/src/linear_solver/linear_solver.swig +++ b/src/linear_solver/linear_solver.swig @@ -23,7 +23,6 @@ #include "linear_solver/linear_solver_ext.h" %} -#ifdef SWIGPYTHON // Define the renaming of methods. %ignore MakeBoolVarArray; %ignore MakeIntVarArray; @@ -481,661 +480,6 @@ PY_PROTO_TYPEMAP(ortools.linear_solver.linear_solver2_pb2, PY_PROTO_TYPEMAP(ortools.linear_solver.linear_solver2_pb2, operations_research::new_proto::MPModelRequest, MPModelRequest); -#endif // SWIGPYTHON - -#ifdef SWIGJAVA - -%module(directors="1") operations_research_solver; - -%typemap(javaimports) SWIGTYPE %{ -import java.lang.reflect.*; -%} - -namespace operations_research { -// Rename rules on MPVariable. -%rename (basisStatus) MPVariable::basis_status; -%rename (reducedCost) MPVariable::reduced_cost; -%rename (setBounds) MPVariable::SetBounds; -%rename (setInteger) MPVariable::SetInteger; -%rename (setLb) MPVariable::SetLB; -%rename (setUb) MPVariable::SetUB; -%rename (solutionValue) MPVariable::solution_value; - -// Rename rules on MPConstraint. -%rename (basisStatus) MPConstraint::basis_status; -%rename (dualValue) MPConstraint::dual_value; -%rename (getCoefficient) MPConstraint::GetCoefficient; -%rename (setBounds) MPConstraint::SetBounds; -%rename (setCoefficient) MPConstraint::SetCoefficient; -%rename (setLb) MPConstraint::SetLB; -%rename (setUb) MPConstraint::SetUB; -%rename (setIsLazy) MPConstraint::set_is_lazy; -%rename (isLazy) MPConstraint::is_lazy; - -// Rename rules on MPObjective. -%rename (addOffset) MPObjective::AddOffset; -%rename (bestBound) MPObjective::BestBound; -%rename (clear) MPObjective::Clear; -%rename (getCoefficient) MPObjective::GetCoefficient; -%rename (setCoefficient) MPObjective::SetCoefficient; -%rename (setMaximization) MPObjective::SetMaximization; -%rename (setMinimization) MPObjective::SetMinimization; -%rename (setOffset) MPObjective::SetOffset; -%rename (setOptimizationDirection) MPObjective::SetOptimizationDirection; -%rename (value) MPObjective::Value; - -// Rename rules on MPSolverParameters. -%rename (getDoubleParam) MPSolverParameters::GetDoubleParam; -%rename (getIntegerParam) MPSolverParameters::GetIntegerParam; -%rename (reset) MPSolverParameters::Reset; -%rename (resetDoubleParam) MPSolverParameters::ResetDoubleParam; -%rename (resetIntegerParam) MPSolverParameters::ResetIntegerParam; -%rename (setDoubleParam) MPSolverParameters::SetDoubleParam; -%rename (setIntegerParam) MPSolverParameters::SetIntegerParam; - -// Rename rules on MPSolver. -%rename (checkAllNamesValidity) MPSolver::CheckAllNamesValidity; -%rename (checkNameValidity) MPSolver::CheckNameValidity; -%rename (clear) MPConstraint::Clear; -%rename (clear) MPObjective::Clear; -%rename (clear) MPSolver::Clear; -%rename (computeExactConditionNumber) MPSolver::ComputeExactConditionNumber; -%rename (loadModelFromProto) MPSolver::LoadModelFromProto; -%rename (lookupVariableOrNull) MPSolver::LookupVariableOrNull; -%rename (lookupConstraintOrNull) MPSolver::LookupConstraintOrNull; -%rename (makeBoolVar) MPSolver::MakeBoolVar; -%rename (makeIntVar) MPSolver::MakeIntVar; -%rename (makeNumVar) MPSolver::MakeNumVar; -%rename (makeConstraint) MPSolver::MakeRowConstraint; -%rename (makeVar) MPSolver::MakeVar; -%rename (minimization) MPSolver::Minimization; -%rename (numConstraints) MPSolver::NumConstraints; -%rename (numVariables) MPSolver::NumVariables; -%rename (objective) MPSolver::MutableObjective; -%rename (reset) MPSolver::Reset; -%rename (setMaximization) MPObjective::SetMaximization(); -%rename (setMinimization) MPObjective::SetMinimization(); -%rename (setCoefficient) MPObjective::SetCoefficient; -%rename (setOffset) MPObjective::SetOffset; -%rename (setOptimizationDirection) MPObjective::SetOptimizationDirection; -%rename (setTimeLimit) MPSolver::set_time_limit; -%rename (setWriteModelFilename) MPSolver::set_write_model_filename; -%rename (solve) MPSolver::Solve; -%rename (solverVersion) MPSolver::SolverVersion; -%rename (suppressOutput) MPSolver::SuppressOutput; -%rename (timeLimit) MPSolver::time_limit; -%rename (wallTime) MPSolver::wall_time; -%rename (writeModelFilename) MPSolver::write_model_filename; -// Ignore non-mutable version of the objective accessor. -%ignore MPSolver::Objective; -// Ignore Make*VarArray: see replacement java code below. -%ignore MPSolver::MakeVarArray; -%ignore MPSolver::MakeNumVarArray; -%ignore MPSolver::MakeIntVarArray; -%ignore MPSolver::MakeBoolVarArray; -// The following 6 methods that use protocol buffers as output -// arguments are replaced by methods that return a protocol buffer, -// see code below. -%ignore MPSolver::ExportModelToNewProto; -%ignore MPSolver::FillSolutionResponseProto; -%ignore MPSolver::SolveWithProto; -%ignore MPSolver::ExportModel; -%ignore MPSolver::FillSolutionResponse; -%ignore MPSolver::SolveWithProtocolBuffers; -// Ignore export to string methods. -%ignore operations_research::MPSolver::ExportModelAsLpFormat; -%ignore operations_research::MPSolver::ExportModelAsMpsFormat; -// Access to the underlying solver is available only in C++. -%ignore MPSolver::underlying_solver; - - -// Add java code on MPSolver. -%typemap(javacode) MPSolver %{ - public MPVariable[] makeVarArray(int count, - double lb, - double ub, - boolean integer) { - MPVariable[] array = new MPVariable[count]; - for (int i = 0; i < count; ++i) { - array[i] = makeVar(lb, ub, integer, ""); - } - return array; - } - - public MPVariable[] makeVarArray(int count, - double lb, - double ub, - boolean integer, - String var_name) { - MPVariable[] array = new MPVariable[count]; - for (int i = 0; i < count; ++i) { - array[i] = makeVar(lb, ub, integer, var_name + i); - } - return array; - } - - public MPVariable[] makeNumVarArray(int count, double lb, double ub) { - return makeVarArray(count, lb, ub, false); - } - - public MPVariable[] makeNumVarArray(int count, - double lb, - double ub, - String var_name) { - return makeVarArray(count, lb, ub, false, var_name); - } - - public MPVariable[] makeIntVarArray(int count, double lb, double ub) { - return makeVarArray(count, lb, ub, true); - } - - public MPVariable[] makeIntVarArray(int count, - double lb, - double ub, - String var_name) { - return makeVarArray(count, lb, ub, true, var_name); - } - - public MPVariable[] makeBoolVarArray(int count) { - return makeVarArray(count, 0.0, 1.0, true); - } - - public MPVariable[] makeBoolVarArray(int count, String var_name) { - return makeVarArray(count, 0.0, 1.0, true, var_name); - } - - public static final int getSolverEnum(String solver_name) - throws java.lang.ClassNotFoundException, - java.lang.NoSuchFieldException, - java.lang.IllegalAccessException { -Class c = Class.forName("com.google.ortools.linearsolver.MPSolver"); - Field field = c.getField(solver_name); - return field.getInt(null); - } -%} - -%extend MPSolver { - std::string exportModelAsLpFormat(bool obfuscated) { - std::string output; - if (!self->ExportModelAsLpFormat(obfuscated, &output)) return ""; - return output; - } - - std::string exportModelAsMpsFormat(bool fixed_format, bool obfuscated) { - std::string output; - if (!self->ExportModelAsMpsFormat(fixed_format, obfuscated, &output)) { - return ""; - } - return output; - } -} -} // namespace operations_research - -#endif // SWIGJAVA - -#if defined(SWIGCSHARP) - -%module(directors="1") operations_research_linear_solver; - -%typemap(csimports) SWIGTYPE %{ - using System; - using System.Collections.Generic; - using System.Runtime.InteropServices; -%} - -namespace operations_research { -// Use system infinity. -%ignore MPSolver::infinity; -// Automatic renaming to camel case. -%rename("%(camelcase)s", %$isfunction) ""; -// Rename MakeRowConstraint into MakeConstraint -%rename (MakeConstraint) MPSolver::MakeRowConstraint; -// Rename classes, remove MP prefix. -%rename (Solver) MPSolver; -%rename (Variable) MPVariable; -%rename (Constraint) MPConstraint; -%rename (Objective) MPObjective; -%rename (SolverParameters) MPSolverParameters; - -// Ignore code on MPSolver, see replacement java code below. -%ignore MPSolver::MakeVarArray; -%ignore MPSolver::MakeNumVarArray; -%ignore MPSolver::MakeIntVarArray; -%ignore MPSolver::MakeBoolVarArray; -// The following 3 methods that use protocol buffers as output -// arguments are replaced by methods that return a protocol buffer, -// see code below. -%ignore MPSolver::ExportModel; -%ignore MPSolver::ExportModelToNewProto; -%ignore MPSolver::FillSolutionResponse; -%ignore MPSolver::SolveWithProtocolBuffers; -// Ignore Objective(), use MutableObjective() instead. -%ignore MPSolver::Objective; -%rename (Objective) MPSolver::MutableObjective; -// Ignore export to string methods. -%ignore operations_research::MPSolver::ExportModelAsLpFormat; -%ignore operations_research::MPSolver::ExportModelAsMpsFormat; - -%typemap(cscode) MPVariable %{ - public static LinearExpr operator+(Variable a, double v) - { - return new VarWrapper(a) + v; - } - - public static LinearExpr operator+(double v, Variable a) - { - return a + v; - } - - public static LinearExpr operator+(Variable a, LinearExpr b) - { - return new VarWrapper(a) + b; - } - - public static LinearExpr operator+(Variable a, Variable b) - { - return new VarWrapper(a) + new VarWrapper(b); - } - - public static LinearExpr operator+(LinearExpr a, Variable b) - { - return a + new VarWrapper(b); - } - - public static LinearExpr operator-(Variable a, double v) - { - return new VarWrapper(a) - v; - } - - public static LinearExpr operator-(double v, Variable a) - { - return v - new VarWrapper(a); - } - - public static LinearExpr operator-(Variable a, LinearExpr b) - { - return new VarWrapper(a) - b; - } - - public static LinearExpr operator-(LinearExpr a, Variable b) - { - return a - new VarWrapper(b); - } - - public static LinearExpr operator-(Variable a, Variable b) - { - return new VarWrapper(a) - new VarWrapper(b); - } - - public static LinearExpr operator-(Variable a) - { - return - new VarWrapper(a); - } - - public static LinearExpr operator*(Variable a, double v) - { - return new VarWrapper(a) * v; - } - - public static LinearExpr operator/(Variable a, double v) - { - return new VarWrapper(a) / v; - } - - public static LinearExpr operator*(double v, Variable a) - { - return v * new VarWrapper(a); - } - - public static RangeConstraint operator==(Variable a, double v) - { - return new VarWrapper(a) == v; - } - - public static RangeConstraint operator==(double v, Variable a) - { - return v == new VarWrapper(a); - } - - public static RangeConstraint operator!=(Variable a, double v) - { - return new VarWrapper(a) != v; - } - - public static RangeConstraint operator!=(double v, Variable a) - { - return new VarWrapper(a) != v; - } - - public static Equality operator==(Variable a, LinearExpr b) - { - return new VarWrapper(a) == b; - } - - public static Equality operator==(LinearExpr a, Variable b) - { - return a == new VarWrapper(b); - } - - public static VarEquality operator==(Variable a, Variable b) - { - return new VarEquality(a, b, true); - } - - public static Equality operator!=(Variable a, LinearExpr b) - { - return new VarWrapper(a) != b; - } - - public static Equality operator!=(LinearExpr a, Variable b) - { - return a != new VarWrapper(b); - } - - public static VarEquality operator!=(Variable a, Variable b) - { - return new VarEquality(a, b, false); - } - - public static RangeConstraint operator<=(Variable a, double v) - { - return new VarWrapper(a) <= v; - } - - public static RangeConstraint operator>=(Variable a, double v) - { - return new VarWrapper(a) >= v; - } - - public static RangeConstraint operator<=(double v, Variable a) - { - return new VarWrapper(a) >= v; - } - - public static RangeConstraint operator>=(double v, Variable a) - { - return new VarWrapper(a) <= v; - } - - public static RangeConstraint operator<=(Variable a, LinearExpr b) - { - return new VarWrapper(a) <= b; - } - - public static RangeConstraint operator>=(Variable a, LinearExpr b) - { - return new VarWrapper(a) >= b; - } - - public static RangeConstraint operator<=(Variable a, Variable b) - { - return new VarWrapper(a) <= new VarWrapper(b); - } - - public static RangeConstraint operator>=(Variable a, Variable b) - { - return new VarWrapper(a) >= new VarWrapper(b); - } - - public static RangeConstraint operator<=(LinearExpr a, Variable b) - { - return a <= new VarWrapper(b); - } - - public static RangeConstraint operator>=(LinearExpr a, Variable b) - { - return a >= new VarWrapper(b); - } -%} - -%extend MPSolver { - std::string ExportModelAsLpFormat(bool obfuscated) { - std::string output; - if (!self->ExportModelAsLpFormat(obfuscated, &output)) return ""; - return output; - } - - std::string ExportModelAsMpsFormat(bool fixed_format, bool obfuscated) { - std::string output; - if (!self->ExportModelAsMpsFormat(fixed_format, obfuscated, &output)) { - return ""; - } - return output; - } -} - -// Add csharp code on Solver. -%typemap(cscode) MPSolver %{ - public Variable[] MakeVarArray(int count, - double lb, - double ub, - bool integer) { - Variable[] array = new Variable[count]; - for (int i = 0; i < count; ++i) { - array[i] = MakeVar(lb, ub, integer, ""); - } - return array; - } - - public Variable[] MakeVarArray(int count, - double lb, - double ub, - bool integer, - string var_name) { - Variable[] array = new Variable[count]; - for (int i = 0; i < count; ++i) { - array[i] = MakeVar(lb, ub, integer, var_name + i); - } - return array; - } - - public Variable[,] MakeVarMatrix(int rows, - int cols, - double lb, - double ub, - bool integer) { - Variable[,] matrix = new Variable[rows, cols]; - for (int i = 0; i < rows; ++i) { - for (int j = 0; j < cols; ++j) { - matrix[i,j] = MakeVar(lb, ub, integer, ""); - } - } - return matrix; - } - - public Variable[,] MakeVarMatrix(int rows, - int cols, - double lb, - double ub, - bool integer, - string name) { - Variable[,] matrix = new Variable[rows, cols]; - for (int i = 0; i < rows; ++i) { - for (int j = 0; j < cols; ++j) { - string var_name = name + "[" + i + ", " + j +"]"; - matrix[i,j] = MakeVar(lb, ub, integer, var_name); - } - } - return matrix; - } - - public Variable[] MakeNumVarArray(int count, double lb, double ub) { - return MakeVarArray(count, lb, ub, false); - } - - public Variable[] MakeNumVarArray(int count, - double lb, - double ub, - string var_name) { - return MakeVarArray(count, lb, ub, false, var_name); - } - - public Variable[,] MakeNumVarMatrix(int rows, - int cols, - double lb, - double ub) { - Variable[,] matrix = new Variable[rows, cols]; - for (int i = 0; i < rows; ++i) { - for (int j = 0; j < cols; ++j) { - matrix[i,j] = MakeNumVar(lb, ub, ""); - } - } - return matrix; - } - - public Variable[,] MakeNumVarMatrix(int rows, - int cols, - double lb, - double ub, - string name) { - Variable[,] matrix = new Variable[rows, cols]; - for (int i = 0; i < rows; ++i) { - for (int j = 0; j < cols; ++j) { - string var_name = name + "[" + i + ", " + j +"]"; - matrix[i,j] = MakeNumVar(lb, ub, var_name); - } - } - return matrix; - } - - public Variable[] MakeIntVarArray(int count, double lb, double ub) { - return MakeVarArray(count, lb, ub, true); - } - - public Variable[] MakeIntVarArray(int count, - double lb, - double ub, - string var_name) { - return MakeVarArray(count, lb, ub, true, var_name); - } - - public Variable[,] MakeIntVarMatrix(int rows, - int cols, - double lb, - double ub) { - Variable[,] matrix = new Variable[rows, cols]; - for (int i = 0; i < rows; ++i) { - for (int j = 0; j < cols; ++j) { - matrix[i,j] = MakeIntVar(lb, ub, ""); - } - } - return matrix; - } - - public Variable[,] MakeIntVarMatrix(int rows, - int cols, - double lb, - double ub, - string name) { - Variable[,] matrix = new Variable[rows, cols]; - for (int i = 0; i < rows; ++i) { - for (int j = 0; j < cols; ++j) { - string var_name = name + "[" + i + ", " + j +"]"; - matrix[i,j] = MakeIntVar(lb, ub, var_name); - } - } - return matrix; - } - - public Variable[] MakeBoolVarArray(int count) { - return MakeVarArray(count, 0.0, 1.0, true); - } - - public Variable[] MakeBoolVarArray(int count, string var_name) { - return MakeVarArray(count, 0.0, 1.0, true, var_name); - } - - public Variable[,] MakeBoolVarMatrix(int rows, int cols) { - Variable[,] matrix = new Variable[rows, cols]; - for (int i = 0; i < rows; ++i) { - for (int j = 0; j < cols; ++j) { - matrix[i,j] = MakeBoolVar(""); - } - } - return matrix; - } - - public Variable[,] MakeBoolVarMatrix(int rows, int cols, string name) { - Variable[,] matrix = new Variable[rows, cols]; - for (int i = 0; i < rows; ++i) { - for (int j = 0; j < cols; ++j) { - string var_name = name + "[" + i + ", " + j +"]"; - matrix[i,j] = MakeBoolVar(var_name); - } - } - return matrix; - } - - public static int GetSolverEnum(String solverType) { - System.Reflection.FieldInfo fieldInfo = - typeof(Solver).GetField(solverType); - if (fieldInfo != null) { - return (int)fieldInfo.GetValue(null); - } else { - throw new System.ApplicationException("Solver not supported"); - } - } - - public static Solver CreateSolver(String name, String type) { - System.Reflection.FieldInfo fieldInfo = - typeof(Solver).GetField(type); - if (fieldInfo != null) { - return new Solver(name, (int)fieldInfo.GetValue(null)); - } else { - return null; - } - } - - public Constraint Add(LinearConstraint constraint) { - return constraint.Extract(this); - } - - public void Minimize(LinearExpr expr) - { - Objective().Clear(); - Objective().SetMinimization(); - Dictionary coefficients = - new Dictionary(); - double constant = expr.Visit(coefficients); - foreach (KeyValuePair pair in coefficients) - { - Objective().SetCoefficient(pair.Key, pair.Value); - } - Objective().SetOffset(constant); - } - - public void Maximize(LinearExpr expr) - { - Objective().Clear(); - Objective().SetMaximization(); - Dictionary coefficients = - new Dictionary(); - double constant = expr.Visit(coefficients); - foreach (KeyValuePair pair in coefficients) - { - Objective().SetCoefficient(pair.Key, pair.Value); - } - Objective().SetOffset(constant); - } - - public void Minimize(Variable var) - { - Objective().Clear(); - Objective().SetMinimization(); - Objective().SetCoefficient(var, 1.0); - } - - public void Maximize(Variable var) - { - Objective().Clear(); - Objective().SetMaximization(); - Objective().SetCoefficient(var, 1.0); - } -%} - -} // namespace operations_research - -#endif // SWIGCSHARP // Wrap linear_solver includes %include "linear_solver/linear_solver.h" diff --git a/src/util/callback.swig b/src/util/callback.swig index 87a8a6a0ba..a4ff6ccfa4 100644 --- a/src/util/callback.swig +++ b/src/util/callback.swig @@ -19,7 +19,6 @@ using std::string; %} -#if defined(SWIGPYTHON) namespace operations_research { // ----- Callback Wrapping ----- %{ @@ -148,268 +147,3 @@ static bool PyCallbackBool(PyObject* pyfunc) { $1 = NewPermanentCallback(&PyCallbackBool, $input); } } // namespace operations_research -#endif // SWIGPYTHON - -#if defined(SWIGJAVA) - -%{ -#include "base/jniutil.h" -#include "base/scoped_ptr.h" -%} - -%module(directors="1") main -%feature("director") LongResultCallback1; -%feature("director") LongResultCallback2; -%feature("director") LongResultCallback3; -%include -%{ -#include -#include "base/callback.h" -#include "base/integral_types.h" - -// When a director is created for a class with SWIG, the C++ part of the -// director keeps a JNI global reference to the Java part. This global reference -// only gets deleted in the destructor of the C++ part, but by default, this -// only happens when the Java part is processed by the GC (however, this never -// happens, because there is the JNI global reference...). -// -// To break the cycle, it is necessary to delete the C++ part manually. For the -// callback classes, this is done by deriving them from the respective C++ -// ResultCallback classes. When the java callback class is asked for a C++ -// callback class, it hands over its C++ part. It is expected, that whoever -// receives the C++ callback class, owns it and destroys it after they no longer -// need it. But by destroying it, they also break the reference cycle and the -// Java part may be processed by the GC. -// -// However, this behavior also means that the callback class can only be used -// in one context and that if its C++ callback class is not received by someone -// who destroys it in the end, it will stay in memory forever. -class LongResultCallback1 : private ResultCallback1 { - public: - LongResultCallback1() : used_as_permanent_handler_(false) {} - virtual int64 run(int64) = 0; - ResultCallback1* GetPermanentCallback() { - CHECK(!used_as_permanent_handler_); - used_as_permanent_handler_ = true; - return this; - } - virtual ~LongResultCallback1() {} - private: - virtual bool IsRepeatable() const { return true; } - - virtual int64 Run(int64 i) { - return run(i); - } - bool used_as_permanent_handler_; -}; - -class LongResultCallback2 : private ResultCallback2 { - public: - LongResultCallback2() : used_as_permanent_handler_(false) {} - virtual ~LongResultCallback2() {} - virtual int64 run(int64, int64) = 0; - ResultCallback2* GetPermanentCallback() { - CHECK(!used_as_permanent_handler_); - used_as_permanent_handler_ = true; - return this; - } - private: - virtual bool IsRepeatable() const { return true; } - - virtual int64 Run(int64 i, int64 j) { - return run(i, j); - } - bool used_as_permanent_handler_; -}; - -class LongResultCallback3 : private ResultCallback3 { - public: - LongResultCallback3() : used_as_permanent_handler_(false) {} - virtual ~LongResultCallback3() {} - virtual int64 run(int64, int64, int64) = 0; - ResultCallback3* GetPermanentCallback() { - CHECK(!used_as_permanent_handler_); - used_as_permanent_handler_ = true; - return this; - } - private: - virtual bool IsRepeatable() const { return true; } - - virtual int64 Run(int64 i, int64 j, int64 k) { - return run(i, j, k); - } - bool used_as_permanent_handler_; -}; - -%} - -class LongResultCallback1 : private ResultCallback1 { - public: - virtual int64 run(int64) = 0; - ResultCallback1* GetPermanentCallback(); - virtual ~LongResultCallback1(); - private: - virtual bool IsRepeatable() const; - virtual int64 Run(int64 i); - bool used_as_permanent_handler_; -}; -class LongResultCallback2 : private ResultCallback2 { - public: - virtual int64 run(int64, int64) = 0; - ResultCallback2* GetPermanentCallback(); - virtual ~LongResultCallback2(); - private: - virtual bool IsRepeatable() const; - virtual int64 Run(int64 i, int64 j); - bool used_as_permanent_handler_; -}; -class LongResultCallback3 : private ResultCallback3 { - public: - virtual int64 run(int64, int64, int64) = 0; - ResultCallback3* GetPermanentCallback(); - virtual ~LongResultCallback3(); - private: - virtual bool IsRepeatable() const; - virtual int64 Run(int64 i, int64 j, int64 k); - bool used_as_permanent_handler_; -}; - -// Typemaps for callbacks in java. -%typemap(jstype) ResultCallback1* "LongResultCallback1"; -%typemap(javain) ResultCallback1* "$descriptor(ResultCallback1*).getCPtr($javainput.GetPermanentCallback())"; -%typemap(jstype) ResultCallback2* "LongResultCallback2"; -%typemap(javain) ResultCallback2* "$descriptor(ResultCallback2*).getCPtr($javainput.GetPermanentCallback())"; - -%typemap(jstype) ResultCallback3* -"LongResultCallback3"; -%typemap(javain) ResultCallback3* -"$descriptor(ResultCallback3*).getCPtr($javainput.GetPermanentCallback())"; -#endif // SWIGJAVA - -#if defined(SWIGCSHARP) -%include base/base.swig - -%{ -#include -#include "base/callback.h" -#include "base/integral_types.h" -%} - -%module(directors="1") main -%feature("director") LongResultCallback1; -%feature("director") LongResultCallback2; -%feature("director") LongResultCallback3; -%{ -#include -#include "base/callback.h" -#include "base/integral_types.h" - -class LongResultCallback1 : public ResultCallback1 { - public: - LongResultCallback1() : used_as_permanent_handler_(false) {} - virtual ~LongResultCallback1() {} - virtual int64 Run(int64 i) = 0; - ResultCallback1* GetPermanentCallback() { - CHECK(!used_as_permanent_handler_); - used_as_permanent_handler_ = true; - return this; - } - private: - virtual bool IsRepeatable() const { return true; } - - bool used_as_permanent_handler_; -}; - -class LongResultCallback2 : public ResultCallback2 { - public: - LongResultCallback2() : used_as_permanent_handler_(false) {} - virtual ~LongResultCallback2() {} - virtual int64 Run(int64, int64) = 0; - ResultCallback2* GetPermanentCallback() { - CHECK(!used_as_permanent_handler_); - used_as_permanent_handler_ = true; - return this; - } - private: - virtual bool IsRepeatable() const { return true; } - - bool used_as_permanent_handler_; -}; - -class LongResultCallback3 : public ResultCallback3 { - public: - LongResultCallback3() : used_as_permanent_handler_(false) {} - virtual ~LongResultCallback3() {} - virtual int64 Run(int64, int64, int64) = 0; - ResultCallback3* GetPermanentCallback() { - CHECK(!used_as_permanent_handler_); - used_as_permanent_handler_ = true; - return this; - } - private: - virtual bool IsRepeatable() const { return true; } - - bool used_as_permanent_handler_; -}; - -%} - -%typemap(cscode) LongResultCallback1 %{ - public SWIGTYPE_p_ResultCallback1T_long_long_long_long_t DisownAndGetPermanentCallback() { - swigCMemOwn = false; - return GetPermanentCallback(); - } -%} - -%typemap(cscode) LongResultCallback2 %{ - public SWIGTYPE_p_ResultCallback2T_long_long_long_long_long_long_t DisownAndGetPermanentCallback() { - swigCMemOwn = false; - return GetPermanentCallback(); - } -%} - -%typemap(cscode) LongResultCallback3 %{ - public SWIGTYPE_p_ResultCallback3T_long_long_long_long_long_long_long_long_t DisownAndGetPermanentCallback() { - swigCMemOwn = false; - return GetPermanentCallback(); - } -%} - -class LongResultCallback1 : private ResultCallback1 { - public: - virtual int64 Run(int64 i) = 0; - ResultCallback1* GetPermanentCallback(); - virtual ~LongResultCallback1(); - private: - virtual bool IsRepeatable() const; - bool used_as_permanent_handler_; -}; -class LongResultCallback2 : private ResultCallback2 { - public: - virtual int64 Run(int64, int64) = 0; - ResultCallback2* GetPermanentCallback(); - virtual ~LongResultCallback2(); - private: - virtual bool IsRepeatable() const; - bool used_as_permanent_handler_; -}; -class LongResultCallback3 : private ResultCallback3 { - public: - virtual int64 Run(int64, int64, int64) = 0; - ResultCallback3* GetPermanentCallback(); - virtual ~LongResultCallback3(); - private: - virtual bool IsRepeatable() const; - bool used_as_permanent_handler_; -}; - -// Typemaps for callbacks in csharp. -%typemap(cstype) ResultCallback1* "LongResultCallback1"; -%typemap(csin) ResultCallback1* "$descriptor(ResultCallback1*).getCPtr($csinput.DisownAndGetPermanentCallback())"; -%typemap(cstype) ResultCallback2* "LongResultCallback2"; -%typemap(csin) ResultCallback2* "$descriptor(ResultCallback2*).getCPtr($csinput.DisownAndGetPermanentCallback())"; -%typemap(cstype) ResultCallback3* -"LongResultCallback3"; -%typemap(csin) ResultCallback3* -"$descriptor(ResultCallback3*).getCPtr($csinput.DisownAndGetPermanentCallback())"; -#endif // SWIGCSHARP diff --git a/src/util/data.swig b/src/util/data.swig index e4478c99bf..77dba85053 100644 --- a/src/util/data.swig +++ b/src/util/data.swig @@ -18,7 +18,6 @@ using std::string; %} -#if defined(SWIGPYTHON) %{ inline int PyIntOrLong_Check(PyObject* obj) { return PyInt_Check(obj) || PyLong_Check(obj); @@ -246,433 +245,3 @@ PY_LIST_OUTPUT_TYPEMAP(double, PyFloat_Check, PyFloat_FromDouble); %enddef // PY_PROTO_TYPEMAP } // namespace operations_research -#endif // SWIGPYTHON - -#if defined(SWIGJAVA) - -%{ -#include "base/jniutil.h" -#include "base/scoped_ptr.h" -%} - -%include -%{ -#include -#include "base/integral_types.h" -%} - -// Typemaps to represent const std::vector& arguments as arrays of JavaType. -%define VECTOR_AS_JAVA_ARRAY(CType, JavaType, JavaTypeName) -%typemap(jni) const std::vector& "j" #JavaType "Array" -%typemap(jtype) const std::vector& #JavaType "[]" -%typemap(jstype) const std::vector& #JavaType "[]" -%typemap(javain) const std::vector& "$javainput" -%typemap(in) const std::vector& %{ - if($input) { - $1 = new std::vector; - const int size = jenv->GetArrayLength($input); - $1->reserve(size); - j ## JavaType *values = jenv->Get ## JavaTypeName ## ArrayElements((j ## JavaType ## Array)$input, NULL); - for (int i = 0; i < size; ++i) { - JavaType value = values[i]; - $1->emplace_back(value); - } - jenv->Release ## JavaTypeName ## ArrayElements((j ## JavaType ## Array)$input, values, JNI_ABORT); - } - else { - SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null table"); - return $null; - } -%} -%typemap(freearg) const std::vector& { - delete $1; -} -%typemap(out) const std::vector& %{ - $result = jenv->New ## JavaTypeName ## Array($1->size()); - jenv->Set ## JavaTypeName ## ArrayRegion( - $result, 0, $1->size(), reinterpret_cast(&(*$1)[0])); -%} -%typemap(javaout) const std::vector & { - return $jnicall; -} -%enddef -VECTOR_AS_JAVA_ARRAY(int, int, Int); -VECTOR_AS_JAVA_ARRAY(int64, long, Long); - -// Convert long[][] to std::vector > -%typemap(jni) const std::vector >& "jobjectArray" -%typemap(jtype) const std::vector >& "long[][]" -%typemap(jstype) const std::vector >& "long[][]" -%typemap(javain) const std::vector >& "$javainput" -%typemap(in) const std::vector >& (std::vector > result) { - const int size = jenv->GetArrayLength($input); - result.clear(); - result.resize(size); - for (int index1 = 0; index1 < size; ++index1) { - jlongArray inner_array = - (jlongArray)jenv->GetObjectArrayElement($input, index1); - const int tuple_size = jenv->GetArrayLength(inner_array); - result[index1].reserve(tuple_size); - jlong* const values = - jenv->GetLongArrayElements((jlongArray)inner_array, NULL); - for (int index2 = 0; index2 < tuple_size; ++index2) { - const int64 value = values[index2]; - result[index1].emplace_back(value); - } - jenv->ReleaseLongArrayElements((jlongArray)inner_array, values, JNI_ABORT); - jenv->DeleteLocalRef(inner_array); - } - $1 = &result; -} - -// SWIG macros to be used in generating Java wrappers for C++ protocol -// message parameters. Each protocol message is serialized into -// byte[] before passing into (or returning from) C++ code. - -// If the C++ function expects an input protocol message: -// foo(const MyProto* message,...) -// Use PROTO_INPUT macro: -// PROTO_INPUT(MyProto, com.google.proto.protos.test.MyProto, message) -// -// if the C++ function returns a protocol message: -// MyProto* foo(); -// Use PROTO2_RETURN macro: -// PROTO2_RETURN(MyProto, com.google.proto.protos.test.MyProto, giveOwnership) -// -> the 'giveOwnership' parameter should be true iff the C++ function -// returns a new proto which should be deleted by the client. - -// Passing each protocol message from Java to C++ by value. Each ProtocolMessage -// is serialized into byte[] when it is passed from Java to C++, the C++ code -// deserializes into C++ native protocol message. -// -// @param CppProtoType the fully qualified C++ protocol message type -// @param JavaProtoType the corresponding fully qualified Java protocol message -// type -// @param param_name the parameter name -%define PROTO_INPUT(CppProtoType, JavaProtoType, param_name) -%typemap(jni) PROTO_TYPE* INPUT, PROTO_TYPE& INPUT "jbyteArray" -%typemap(jtype) PROTO_TYPE* INPUT, PROTO_TYPE& INPUT "byte[]" -%typemap(jstype) PROTO_TYPE* INPUT, PROTO_TYPE& INPUT "JavaProtoType" -%typemap(javain) PROTO_TYPE* INPUT, PROTO_TYPE& INPUT "$javainput.toByteArray()" -%typemap(in) PROTO_TYPE* INPUT (CppProtoType temp), - PROTO_TYPE& INPUT (CppProtoType temp) { - int proto_size = 0; - std::unique_ptr proto_buffer( - JNIUtil::MakeCharArray(jenv, $input, &proto_size)); - bool parsed_ok = temp.ParseFromArray(proto_buffer.get(), proto_size); - if (!parsed_ok) { - SWIG_JavaThrowException(jenv, - SWIG_JavaRuntimeException, - "Unable to parse CppProtoType protocol message."); - } - $1 = &temp; -} - -%apply PROTO_TYPE& INPUT { const CppProtoType& param_name } -%apply PROTO_TYPE& INPUT { CppProtoType& param_name } -%apply PROTO_TYPE* INPUT { const CppProtoType* param_name } -%apply PROTO_TYPE* INPUT { CppProtoType* param_name } - -%enddef // end PROTO_INPUT - -%define PROTO2_RETURN(CppProtoType, JavaProtoType, giveOwnership) -%typemap(jni) CppProtoType* "jbyteArray" -%typemap(jtype) CppProtoType* "byte[]" -%typemap(jstype) CppProtoType* "JavaProtoType" -%typemap(javaout) CppProtoType* { - byte[] buf = $jnicall; - if (buf == null || buf.length == 0) { - return null; - } - try { - return JavaProtoType.parseFrom(buf); - } catch (com.google.protobuf.InvalidProtocolBufferException e) { - throw new RuntimeException( - "Unable to parse JavaProtoType protocol message."); - } -} -%typemap(out) CppProtoType* { - std::unique_ptr buf(new char[$1->ByteSize()]); - $1->SerializeWithCachedSizesToArray(reinterpret_cast(buf.get())); - $result = JNIUtil::MakeJByteArray(jenv, buf.get(), $1->ByteSize()); - if (giveOwnership) { - // To prevent a memory leak. - delete $1; - $1 = NULL; - } -} -%enddef // end PROTO2_RETURN - -#endif // SWIGJAVA - -#if defined(SWIGCSHARP) -%include base/base.swig - -//////////////////////////////////////////////// -// -// CS_TYPEMAP_STDVECTOR -// -// Map c# arrays to c++ vectors for POD types. -// -//////////////////////////////////////////////// - -// This typemap will transform a .net array into a pair length, c array. -// This pair is then used to rebuild a vector from it. -%define CS_TYPEMAP_STDVECTOR(TYPE, CTYPE, CSHARPTYPE) - -%typemap(ctype) const std::vector& %{ int length$argnum, CTYPE* %} -%typemap(imtype) const std::vector& %{ int length$argnum, CSHARPTYPE[] %} -%typemap(cstype) const std::vector& %{ CSHARPTYPE[] %} -%typemap(csin) const std::vector& "$csinput.Length, $csinput" -%typemap(freearg) const std::vector& { delete $1; } -%typemap(in) const std::vector& %{ - $1 = new std::vector; - $1->reserve(length$argnum); - for(int i = 0; i < length$argnum; ++i) - $1->emplace_back($input[i]); - %} -%enddef // CS_TYPEMAP_STDVECTOR - -//////////////////////////////////////////////// -// -// CS_TYPEMAP_STDVECTOR_IN1 -// -// Map c# bi-dimensional arrays to c++ vectors of vectors for POD types. -// -//////////////////////////////////////////////// - -// This typemap will transform a multidimensional array into a triplet -// size_z, size_y, flattened array. This is then recomposed in the -// final part to rebuild the std::vector> part. -%define CS_TYPEMAP_STDVECTOR_IN1(TYPE, CTYPE, CSHARPTYPE) -%typemap(ctype) const std::vector >& %{ int len$argnum_1, int len$argnum_2, CTYPE* %} -%typemap(imtype) const std::vector >& %{ int len$argnum_1, int len$argnum_2, CSHARPTYPE[] %} -%typemap(cstype) const std::vector >& %{ CSHARPTYPE[,] %} -%typemap(csin) const std::vector >& "$csinput.GetLength(0), $csinput.GetLength(1), Google.OrTools.Util.NestedArrayHelper.GetFlatArray($csinput)" -%typemap(in) const std::vector >& (std::vector > result) %{ - - const int size_x = len$argnum_1; - const int size_y = len$argnum_2; - - result.clear(); - result.resize(size_x); - - TYPE* inner_array = reinterpret_cast($input); - - for (int index1 = 0; index1 < size_x; ++index1) { - result[index1].reserve(size_y); - for (int index2 = 0; index2 < size_y; ++index2) { - const TYPE value = inner_array[index1 * size_y + index2]; - result[index1].emplace_back(value); - } - } - - $1 = &result; -%} -%enddef // CS_TYPEMAP_STDVECTOR_IN1 - -//////////////////////////////////////////////// -// -// CS_TYPEMAP_PTRARRAY -// -//////////////////////////////////////////////// - -// This typemap will perform the same transformation for an array of object. -// The result is an vector of the C objects. -%define CS_TYPEMAP_PTRARRAY(CTYPE, TYPE) - -%typemap(cscode) CTYPE %{ - public static IntPtr[] getCPtr(TYPE[] arr) { - IntPtr[] pointers = new IntPtr[arr.Length]; - for (int i = 0; i < arr.Length; i++) { - pointers[i] = (IntPtr)TYPE.getCPtr(arr[i]); - } - return pointers; - } -%} - -%typemap(ctype) CTYPE** "CTYPE**" - -%typemap(imtype, - inattributes="[In, Out, MarshalAs(UnmanagedType.LPArray)]", - outattributes="[return: MarshalAs(UnmanagedType.LPArray)]") -CTYPE** "IntPtr[]" - -%typemap(cstype) CTYPE** "TYPE[]" -%typemap(csin) CTYPE** "TYPE.getCPtr($csinput)" -%typemap(in) CTYPE** "$1 = $input;" -%typemap(freearg) CTYPE** "" -%typemap(argout) CTYPE** "" - -%enddef // CS_TYPEMAP_PTRARRAY - -//////////////////////////////////////////////// -// -// CS_TYPEMAP_STDVECTOR_OBJECT -// -// Map c# arrays to c++ vectors for swiged C++ objects. -// -//////////////////////////////////////////////// - -%define CS_TYPEMAP_STDVECTOR_OBJECT(CTYPE, TYPE) - -%typemap(cscode) CTYPE %{ - public static IntPtr[] getCPtr(TYPE[] arr) { - IntPtr[] pointers = new IntPtr[arr.Length]; - for (int i = 0; i < arr.Length; i++) - pointers[i] = (IntPtr)TYPE.getCPtr(arr[i]); - return pointers; - } -%} - -%typemap(ctype) const std::vector& "int length$argnum, CTYPE**" -%typemap(imtype) const std::vector& "int length$argnum, IntPtr[]" -%typemap(cstype) const std::vector& "TYPE[]" -%typemap(csin) const std::vector& "$csinput.Length, TYPE.getCPtr($csinput)" -%typemap(in) const std::vector& (std::vector result) { - result.reserve(length$argnum); - for (int i = 0; i < length$argnum; i++) { - result.emplace_back($input[i]); - } - $1 = &result; -} -%enddef // CS_TYPEMAP_STDVECTOR_OBJECT - -%{ -#include -#include "base/integral_types.h" -%} - -%{ -#include -#include "base/integral_types.h" -%} - -CS_TYPEMAP_STDVECTOR(int64, int64, long) -CS_TYPEMAP_STDVECTOR(int, int, int) -CS_TYPEMAP_STDVECTOR_IN1(int64, int64, long) -CS_TYPEMAP_STDVECTOR_IN1(int, int, int) - -// SWIG macros to be used in generating C# wrappers for C++ protocol -// message parameters. Each protocol message is serialized into -// byte[] before passing into (or returning from) C++ code. - -// If the C++ function expects an input protocol message, transferring -// ownership to the caller (in C++): -// foo(const MyProto* message,...) -// Use PROTO_INPUT macro: -// PROTO_INPUT(MyProto, Google.Proto.Protos.Test.MyProto, message) -// -// if the C++ function returns a protocol message: -// MyProto* foo(); -// Use PROTO2_RETURN macro: -// PROTO2_RETURN(MyProto, Google.Proto.Protos.Test.MyProto, true) -// -// Replace true by false if the C++ function returns a pointer to a -// protocol message object whose ownership is not transferred to the -// (C++) caller. -// -// Passing each protocol message from C# to C++ by value. Each ProtocolMessage -// is serialized into byte[] when it is passed from C# to C++, the C++ code -// deserializes into C++ native protocol message. -// -// @param CppProtoType the fully qualified C++ protocol message type -// @param CSharpProtoType the corresponding fully qualified C# protocol message -// type -// @param param_name the parameter name -// @param deleteCppReturn indicates that the resulting object is a native -// (java, c#, python) object, and thus the C++ proto can be safely deleted -// after the conversion. -%define PROTO_INPUT(CppProtoType, CSharpProtoType, param_name) -%typemap(ctype) PROTO_TYPE* INPUT, PROTO_TYPE& INPUT "int proto_size, char*" -%typemap(imtype) PROTO_TYPE* INPUT, PROTO_TYPE& INPUT "int proto_size, byte[]" -%typemap(cstype) PROTO_TYPE* INPUT, PROTO_TYPE& INPUT "CSharpProtoType" -%typemap(csin) PROTO_TYPE* INPUT, PROTO_TYPE& INPUT "$csinput.GetByteArrayLength(), $csinput.ToByteArray()" -%typemap(in) PROTO_TYPE* INPUT (CppProtoType temp), PROTO_TYPE& INPUT (CppProtoType temp) { - int proto_size = 0; - std::unique_ptr proto_buffer($input); - bool parsed_ok = temp.ParseFromArray(proto_buffer.get(), proto_size); - if (!parsed_ok) { - SWIG_CSharpSetPendingException( - SWIG_CSharpSystemException, - "Unable to parse CppProtoType protocol message."); - } - $1 = &temp; -} - -%apply PROTO_TYPE& INPUT { const CppProtoType& param_name } -%apply PROTO_TYPE& INPUT { CppProtoType& param_name } -%apply PROTO_TYPE* INPUT { const CppProtoType* param_name } -%apply PROTO_TYPE* INPUT { CppProtoType* param_name } - -%enddef // end PROTO_INPUT - -%define PROTO2_RETURN(CppProtoType, CSharpProtoType, deleteCppReturn) -%typemap(ctype) CppProtoType* "char*" -%typemap(imtype) CppProtoType* "byte[]" -%typemap(cstype) CppProtoType* "CSharpProtoType" -%typemap(csout) CppProtoType* { - byte[] buf = $imcall; - if (buf == null || buf.Length == 0) { - return null; - } - try { - return CSharpProtoType.ParseFrom(buf); - } - catch (Google.Protobuf.InvalidProtocolBufferException e) { - throw new SystemException( - "Unable to parse CSharpProtoType protocol message."); - } -} -%typemap(out) CppProtoType* { - std::unique_ptr buf(new char[$1->ByteSize()]); - $1->SerializeWithCachedSizesToArray(reinterpret_cast(buf.get())); - $result = buf.get(); - if (deleteCppReturn) { - // To prevent a memory leak. - delete $1; - $1 = NULL; - } -} -%enddef // end PROTO2_RETURN_AND_DELETE - -%define CS_VECTOR_RETURN0(Namespace, Class, Method, RType) -%ignore Namespace::Class::Method; - -%extend Namespace::Class { - int Method ## Size() const { - return self->Method().size(); - } - RType Method ## ValueAt(int index) const { - return self->Method()[index]; - } -} -%enddef // CS_VECTOR_RETURN0 - -%define CS_VECTOR_RETURN1(Namespace, Class, Method, RType, ArgType) -%ignore Namespace::Class::Method; - -%extend Namespace::Class { - int Method ## Size(ArgType a) const { - return self->Method(a).size(); - } - RType Method ## ValueAt(ArgType a, int index) const { - return self->Method(a)[index]; - } -} -%enddef // CS_VECTOR_RETURN1 - -%define CS_VECTOR_RETURN2(Namespace, Class, Method, RType, - ArgType1, ArgType2) -%ignore Namespace::Class::Method; - -%extend Namespace::Class { - int Method ## Size(ArgType1 a, ArgType2 b) const { - return self->Method(a, b).size(); - } - RType Method ## ValueAt(ArgType1 a, ArgType2 b, int index) const { - return self->Method(a, b)[index]; - } -} -%enddef // CS_VECTOR_RETURN2 -#endif // SWIGCSHARP