diff --git a/ortools/linear_solver/python/model_builder_helper.cc b/ortools/linear_solver/python/model_builder_helper.cc index 48f9df1dd0..9a84fbc736 100644 --- a/ortools/linear_solver/python/model_builder_helper.cc +++ b/ortools/linear_solver/python/model_builder_helper.cc @@ -550,25 +550,6 @@ PYBIND11_MODULE(model_builder_helper, m) { py::class_, LinearExpr>(m, "AffineExpr") .def(py::init, double, double>()) - .def("__add__", &AffineExpr::Add, py::arg("other").none(false), - "Returns `self` + `other`.") - .def("__add__", &AffineExpr::AddFloat, py::arg("cst"), - "Returns `self` + `cst`.") - .def("__radd__", &AffineExpr::Add, py::arg("other").none(false), - "Returns `self` + `other`.") - .def("__radd__", &AffineExpr::AddFloat, py::arg("cst"), - "Returns `self` + `cst`.") - .def("__sub__", &AffineExpr::Sub, py::arg("other").none(false), - "Returns `self` - `other`.") - .def("__sub__", &AffineExpr::SubFloat, py::arg("cst"), - "Returns `self` - `cst`.") - .def("__rsub__", &AffineExpr::RSubFloat, py::arg("cst"), - "Returns `cst` - `self`.") - .def("__mul__", &AffineExpr::MulFloat, py::arg("cst"), - "Returns `self` * `cst`.") - .def("__rmul__", &AffineExpr::MulFloat, py::arg("cst"), - "Returns `self` * `cst`.") - .def("__neg__", &AffineExpr::Neg, "Returns -`self`.") .def_property_readonly("expression", &AffineExpr ::expression) .def_property_readonly("coefficient", &AffineExpr::coefficient) .def_property_readonly("offset", &AffineExpr::offset); diff --git a/ortools/linear_solver/wrappers/model_builder_helper.h b/ortools/linear_solver/wrappers/model_builder_helper.h index 7c6e4f026f..cfd3de5e0e 100644 --- a/ortools/linear_solver/wrappers/model_builder_helper.h +++ b/ortools/linear_solver/wrappers/model_builder_helper.h @@ -63,12 +63,12 @@ class LinearExpr : public std::enable_shared_from_this { static std::shared_ptr Constant(double value); std::shared_ptr Add(std::shared_ptr expr); - std::shared_ptr AddFloat(double cst); + virtual std::shared_ptr AddFloat(double cst); std::shared_ptr Sub(std::shared_ptr expr); - std::shared_ptr SubFloat(double cst); - std::shared_ptr RSubFloat(double cst); - std::shared_ptr MulFloat(double cst); - std::shared_ptr Neg(); + virtual std::shared_ptr SubFloat(double cst); + virtual std::shared_ptr RSubFloat(double cst); + virtual std::shared_ptr MulFloat(double cst); + virtual std::shared_ptr Neg(); std::shared_ptr Eq(std::shared_ptr rhs); std::shared_ptr EqCst(double rhs); @@ -243,11 +243,11 @@ class AffineExpr : public LinearExpr { double coefficient() const { return coeff_; } double offset() const { return offset_; } - std::shared_ptr AddFloat(double cst); - std::shared_ptr SubFloat(double cst); - std::shared_ptr RSubFloat(double cst); - std::shared_ptr MulFloat(double cst); - std::shared_ptr Neg(); + std::shared_ptr AddFloat(double cst) override; + std::shared_ptr SubFloat(double cst) override; + std::shared_ptr RSubFloat(double cst) override; + std::shared_ptr MulFloat(double cst) override; + std::shared_ptr Neg() override; private: std::shared_ptr expr_; diff --git a/ortools/sat/python/cp_model_helper.cc b/ortools/sat/python/cp_model_helper.cc index 371a87b7a2..87ef120b8f 100644 --- a/ortools/sat/python/cp_model_helper.cc +++ b/ortools/sat/python/cp_model_helper.cc @@ -1079,40 +1079,6 @@ PYBIND11_MODULE(cp_model_helper, m) { py::class_, LinearExpr>( m, "IntAffine", DOC(operations_research, sat, python, IntAffine)) .def(py::init, int64_t, int64_t>()) - .def("__add__", &LinearExpr::Add, py::arg("other").none(false), - DOC(operations_research, sat, python, LinearExpr, Add)) - .def("__add__", &IntAffine::AddInt, py::arg("cst"), - DOC(operations_research, sat, python, LinearExpr, AddInt)) - .def("__add__", &LinearExpr::AddFloat, py::arg("cst"), - DOC(operations_research, sat, python, LinearExpr, AddFloat)) - .def("__radd__", &LinearExpr::Add, py::arg("other").none(false), - DOC(operations_research, sat, python, LinearExpr, Add)) - .def("__radd__", &IntAffine::AddInt, py::arg("cst"), - DOC(operations_research, sat, python, LinearExpr, AddInt)) - .def("__radd__", &LinearExpr::AddFloat, py::arg("cst"), - DOC(operations_research, sat, python, LinearExpr, AddFloat)) - .def("__sub__", &LinearExpr::Sub, py::arg("other").none(false), - DOC(operations_research, sat, python, LinearExpr, Sub)) - .def("__sub__", &IntAffine::SubInt, py::arg("cst"), - DOC(operations_research, sat, python, LinearExpr, SubInt)) - .def("__sub__", &LinearExpr::SubFloat, py::arg("cst"), - DOC(operations_research, sat, python, LinearExpr, SubFloat)) - .def("__rsub__", &LinearExpr::RSub, py::arg("other").none(false), - DOC(operations_research, sat, python, LinearExpr, RSub)) - .def("__rsub__", &IntAffine::RSubInt, py::arg("cst"), - DOC(operations_research, sat, python, LinearExpr, RSubInt)) - .def("__rsub__", &LinearExpr::SubFloat, py::arg("cst"), - DOC(operations_research, sat, python, LinearExpr, RSubFloat)) - .def("__mul__", &IntAffine::MulInt, py::arg("cst"), - DOC(operations_research, sat, python, LinearExpr, MulInt)) - .def("__mul__", &LinearExpr::MulFloat, py::arg("cst"), - DOC(operations_research, sat, python, LinearExpr, MulFloat)) - .def("__rmul__", &IntAffine::MulInt, py::arg("cst"), - DOC(operations_research, sat, python, LinearExpr, MulInt)) - .def("__rmul__", &LinearExpr::MulFloat, py::arg("cst"), - DOC(operations_research, sat, python, LinearExpr, MulFloat)) - .def("__neg__", &IntAffine::Neg, - DOC(operations_research, sat, python, LinearExpr, Neg)) .def_property_readonly("expression", &IntAffine::expression, "Returns the linear expression.") .def_property_readonly("coefficient", &IntAffine::coefficient, diff --git a/ortools/sat/python/cp_model_helper_test.py b/ortools/sat/python/cp_model_helper_test.py index b0dd988597..d5901787a7 100644 --- a/ortools/sat/python/cp_model_helper_test.py +++ b/ortools/sat/python/cp_model_helper_test.py @@ -303,7 +303,7 @@ class CpModelHelperTest(absltest.TestCase): self.assertEqual(str(e5), "(x - 1)") e6 = x - 2 * y self.assertTrue(e6.is_integer()) - self.assertEqual(str(e6), "(x + (-(2 * y)))") + self.assertEqual(str(e6), "(x + (-2 * y))") z = TestIntVar(2, "z", True) e7 = -z self.assertTrue(e7.is_integer()) @@ -323,7 +323,7 @@ class CpModelHelperTest(absltest.TestCase): self.assertEqual(str(e11), "(x + 2 * y + 3 * z - 5)") e12 = x - y - 2 * z - self.assertEqual(str(e12), "(x + (-y) + (-(2 * z)))") + self.assertEqual(str(e12), "(x + (-y) + (-2 * z))") def test_float_lin_expr(self): x = TestIntVar(0, "x") diff --git a/ortools/sat/python/cp_model_test.py b/ortools/sat/python/cp_model_test.py index ce36281958..aa06c59b2e 100644 --- a/ortools/sat/python/cp_model_test.py +++ b/ortools/sat/python/cp_model_test.py @@ -252,7 +252,7 @@ class CpModelTest(absltest.TestCase): y = model.NewIntVar(0, 2, "y") z = model.NewIntVar(0, 3, "z") expr = x - y - 2 * z - self.assertEqual(str(expr), "(x + (-y) + (-(2 * z)))") + self.assertEqual(str(expr), "(x + (-y) + (-2 * z))") def test_equality_overload(self) -> None: model = cp_model.CpModel() diff --git a/ortools/sat/python/linear_expr.h b/ortools/sat/python/linear_expr.h index 631f17f05f..ae92d1c676 100644 --- a/ortools/sat/python/linear_expr.h +++ b/ortools/sat/python/linear_expr.h @@ -103,27 +103,27 @@ class LinearExpr : public std::enable_shared_from_this { /// Returns (this) + (expr). std::shared_ptr Add(std::shared_ptr other); /// Returns (this) + (cst). - std::shared_ptr AddInt(int64_t cst); + virtual std::shared_ptr AddInt(int64_t cst); /// Returns (this) + (cst). std::shared_ptr AddFloat(double cst); /// Returns (this) - (expr). std::shared_ptr Sub(std::shared_ptr other); /// Returns (this) - (cst). - std::shared_ptr SubInt(int64_t cst); + virtual std::shared_ptr SubInt(int64_t cst); /// Returns (this) - (cst). std::shared_ptr SubFloat(double cst); /// Returns (expr) - (this). std::shared_ptr RSub(std::shared_ptr other); /// Returns (cst) - (this). - std::shared_ptr RSubInt(int64_t cst); + virtual std::shared_ptr RSubInt(int64_t cst); /// Returns (cst) - (this). std::shared_ptr RSubFloat(double cst); /// Returns (this) * (cst). - std::shared_ptr MulInt(int64_t cst); + virtual std::shared_ptr MulInt(int64_t cst); /// Returns (this) * (cst). std::shared_ptr MulFloat(double cst); /// Returns -(this). - std::shared_ptr Neg(); + virtual std::shared_ptr Neg(); /// Returns (this) == (rhs). std::shared_ptr Eq(std::shared_ptr rhs); @@ -381,11 +381,11 @@ class IntAffine : public LinearExpr { /// Returns the offset. int64_t offset() const { return offset_; } - std::shared_ptr AddInt(int64_t cst); - std::shared_ptr SubInt(int64_t cst); - std::shared_ptr RSubInt(int64_t cst); - std::shared_ptr MulInt(int64_t cst); - std::shared_ptr Neg(); + std::shared_ptr AddInt(int64_t cst) override; + std::shared_ptr SubInt(int64_t cst) override; + std::shared_ptr RSubInt(int64_t cst) override; + std::shared_ptr MulInt(int64_t cst) override; + std::shared_ptr Neg() override; private: std::shared_ptr expr_;