From b47c9a88a8fdb586c6e5dc1a985c309093bb6fad Mon Sep 17 00:00:00 2001 From: Laurent Perron Date: Tue, 13 Apr 2021 16:56:27 +0200 Subject: [PATCH 1/4] throw an error when calling ** on a CP-SAT linear expr --- ortools/sat/python/cp_model.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ortools/sat/python/cp_model.py b/ortools/sat/python/cp_model.py index c5be75a8d3..5e215b7b23 100644 --- a/ortools/sat/python/cp_model.py +++ b/ortools/sat/python/cp_model.py @@ -253,6 +253,11 @@ class LinearExpr(object): 'calling %% on a linear expression is not supported, ' 'please use CpModel.AddModuloEquality') + def __pow__(self, _): + raise NotImplementedError( + 'calling ** on a linear expression is not supported, ' + 'please use CpModel.AddMultiplicationEquality') + def __neg__(self): return _ProductCst(self, -1) From b60419ff334a45d51d2f0ac05a0c7297d39fcad4 Mon Sep 17 00:00:00 2001 From: Laurent Perron Date: Tue, 13 Apr 2021 19:19:53 +0200 Subject: [PATCH 2/4] print a warning when querying x == 0, or min(x, y) in cp_sat python --- examples/tests/cp_model_test.py | 26 ++++++++++++++++++++++++-- ortools/sat/python/cp_model.py | 6 ++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/examples/tests/cp_model_test.py b/examples/tests/cp_model_test.py index b455ac4ea7..ee2686f5e7 100644 --- a/examples/tests/cp_model_test.py +++ b/examples/tests/cp_model_test.py @@ -671,11 +671,33 @@ class CpModelTest(unittest.TestCase): model = cp_model.CpModel() # Creates the variables. - v0 = model.NewBoolVar("buggyVarIndexToVarProto") - v1 = model.NewBoolVar("v1") + v0 = model.NewBoolVar('buggyVarIndexToVarProto') + v1 = model.NewBoolVar('v1') self.assertEqual(model.VarIndexToVarProto(0).name, v0.Name()) + def testWrongBoolEvaluation(self): + print('testWrongBoolEvaluation') + + model = cp_model.CpModel() + + # Creates the variables. + v0 = model.NewIntVar(0, 10, 'v0') + v1 = model.NewIntVar(0, 10, 'v1') + v2 = model.NewIntVar(0, 10, 'v2') + + + if v0 == 2: + print('== passed') + + if v0 >= 3: + print('>= passed') + + model.Add(v2 == min(v0, v1)) + print('min passed') + + if v0: + print('bool passed') if __name__ == '__main__': unittest.main(verbosity=2) diff --git a/ortools/sat/python/cp_model.py b/ortools/sat/python/cp_model.py index 5e215b7b23..d9e170bc7c 100644 --- a/ortools/sat/python/cp_model.py +++ b/ortools/sat/python/cp_model.py @@ -574,6 +574,12 @@ class BoundedLinearExpression(object): def Bounds(self): return self.__bounds + def __bool__(self): + print('Warning: you are evaluating the BoundedLinearExpression \'', + str(self), + '\' as a Boolean value. This will always return True.') + return True + class Constraint(object): """Base class for constraints. From 14832f453339ec30f7cf623d10c0e4da202e6fbd Mon Sep 17 00:00:00 2001 From: Laurent Perron Date: Wed, 14 Apr 2021 08:54:12 +0200 Subject: [PATCH 3/4] add more checks for not supported python APIs --- ortools/sat/python/cp_model.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/ortools/sat/python/cp_model.py b/ortools/sat/python/cp_model.py index d9e170bc7c..b80affe068 100644 --- a/ortools/sat/python/cp_model.py +++ b/ortools/sat/python/cp_model.py @@ -258,6 +258,29 @@ class LinearExpr(object): 'calling ** on a linear expression is not supported, ' 'please use CpModel.AddMultiplicationEquality') + def __lshift__(self, _): + raise NotImplementedError( + 'calling left shift on a linear expression is not supported') + + def __rshift__(self, _): + raise NotImplementedError( + 'calling right shift on a linear expression is not supported') + + def __and__(self, _): + raise NotImplementedError( + 'calling and on a linear expression is not supported, ' + 'please use CpModel.AddBoolAnd') + + def __or__(self, _): + raise NotImplementedError( + 'calling or on a linear expression is not supported, ' + 'please use CpModel.AddBoolOr') + + def __xor__(self, _): + raise NotImplementedError( + 'calling xor on a linear expression is not supported, ' + 'please use CpModel.AddBoolXor') + def __neg__(self): return _ProductCst(self, -1) From 5d84e4e72fb877454d744f42fb67c06023ecb60b Mon Sep 17 00:00:00 2001 From: Laurent Perron Date: Wed, 14 Apr 2021 09:00:33 +0200 Subject: [PATCH 4/4] fix #2493 --- ortools/algorithms/knapsack_solver.cc | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ortools/algorithms/knapsack_solver.cc b/ortools/algorithms/knapsack_solver.cc index b75e7a5763..6bf13daccc 100644 --- a/ortools/algorithms/knapsack_solver.cc +++ b/ortools/algorithms/knapsack_solver.cc @@ -1180,7 +1180,13 @@ KnapsackSolver::~KnapsackSolver() {} void KnapsackSolver::Init(const std::vector& profits, const std::vector>& weights, - const std::vector& capacities) { + const std::vector& capacities) { + for (const std::vector& w : weights) { + CHECK_EQ(profits.size(), w.size()) + << "Profits and inner weights must have the same size (#items)"; + } + CHECK_EQ(capacities.size(), weights.size()) + << "Capacities and weights must have the same size (#bins)"; time_limit_ = absl::make_unique(time_limit_seconds_); is_solution_optimal_ = false; additional_profit_ = 0LL;