improve python exprs
This commit is contained in:
@@ -550,25 +550,6 @@ PYBIND11_MODULE(model_builder_helper, m) {
|
||||
py::class_<AffineExpr, std::shared_ptr<AffineExpr>, LinearExpr>(m,
|
||||
"AffineExpr")
|
||||
.def(py::init<std::shared_ptr<LinearExpr>, 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);
|
||||
|
||||
@@ -63,12 +63,12 @@ class LinearExpr : public std::enable_shared_from_this<LinearExpr> {
|
||||
static std::shared_ptr<LinearExpr> Constant(double value);
|
||||
|
||||
std::shared_ptr<LinearExpr> Add(std::shared_ptr<LinearExpr> expr);
|
||||
std::shared_ptr<LinearExpr> AddFloat(double cst);
|
||||
virtual std::shared_ptr<LinearExpr> AddFloat(double cst);
|
||||
std::shared_ptr<LinearExpr> Sub(std::shared_ptr<LinearExpr> expr);
|
||||
std::shared_ptr<LinearExpr> SubFloat(double cst);
|
||||
std::shared_ptr<LinearExpr> RSubFloat(double cst);
|
||||
std::shared_ptr<LinearExpr> MulFloat(double cst);
|
||||
std::shared_ptr<LinearExpr> Neg();
|
||||
virtual std::shared_ptr<LinearExpr> SubFloat(double cst);
|
||||
virtual std::shared_ptr<LinearExpr> RSubFloat(double cst);
|
||||
virtual std::shared_ptr<LinearExpr> MulFloat(double cst);
|
||||
virtual std::shared_ptr<LinearExpr> Neg();
|
||||
|
||||
std::shared_ptr<BoundedLinearExpression> Eq(std::shared_ptr<LinearExpr> rhs);
|
||||
std::shared_ptr<BoundedLinearExpression> EqCst(double rhs);
|
||||
@@ -243,11 +243,11 @@ class AffineExpr : public LinearExpr {
|
||||
double coefficient() const { return coeff_; }
|
||||
double offset() const { return offset_; }
|
||||
|
||||
std::shared_ptr<LinearExpr> AddFloat(double cst);
|
||||
std::shared_ptr<LinearExpr> SubFloat(double cst);
|
||||
std::shared_ptr<LinearExpr> RSubFloat(double cst);
|
||||
std::shared_ptr<LinearExpr> MulFloat(double cst);
|
||||
std::shared_ptr<LinearExpr> Neg();
|
||||
std::shared_ptr<LinearExpr> AddFloat(double cst) override;
|
||||
std::shared_ptr<LinearExpr> SubFloat(double cst) override;
|
||||
std::shared_ptr<LinearExpr> RSubFloat(double cst) override;
|
||||
std::shared_ptr<LinearExpr> MulFloat(double cst) override;
|
||||
std::shared_ptr<LinearExpr> Neg() override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<LinearExpr> expr_;
|
||||
|
||||
@@ -1079,40 +1079,6 @@ PYBIND11_MODULE(cp_model_helper, m) {
|
||||
py::class_<IntAffine, std::shared_ptr<IntAffine>, LinearExpr>(
|
||||
m, "IntAffine", DOC(operations_research, sat, python, IntAffine))
|
||||
.def(py::init<std::shared_ptr<LinearExpr>, 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,
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -103,27 +103,27 @@ class LinearExpr : public std::enable_shared_from_this<LinearExpr> {
|
||||
/// Returns (this) + (expr).
|
||||
std::shared_ptr<LinearExpr> Add(std::shared_ptr<LinearExpr> other);
|
||||
/// Returns (this) + (cst).
|
||||
std::shared_ptr<LinearExpr> AddInt(int64_t cst);
|
||||
virtual std::shared_ptr<LinearExpr> AddInt(int64_t cst);
|
||||
/// Returns (this) + (cst).
|
||||
std::shared_ptr<LinearExpr> AddFloat(double cst);
|
||||
/// Returns (this) - (expr).
|
||||
std::shared_ptr<LinearExpr> Sub(std::shared_ptr<LinearExpr> other);
|
||||
/// Returns (this) - (cst).
|
||||
std::shared_ptr<LinearExpr> SubInt(int64_t cst);
|
||||
virtual std::shared_ptr<LinearExpr> SubInt(int64_t cst);
|
||||
/// Returns (this) - (cst).
|
||||
std::shared_ptr<LinearExpr> SubFloat(double cst);
|
||||
/// Returns (expr) - (this).
|
||||
std::shared_ptr<LinearExpr> RSub(std::shared_ptr<LinearExpr> other);
|
||||
/// Returns (cst) - (this).
|
||||
std::shared_ptr<LinearExpr> RSubInt(int64_t cst);
|
||||
virtual std::shared_ptr<LinearExpr> RSubInt(int64_t cst);
|
||||
/// Returns (cst) - (this).
|
||||
std::shared_ptr<LinearExpr> RSubFloat(double cst);
|
||||
/// Returns (this) * (cst).
|
||||
std::shared_ptr<LinearExpr> MulInt(int64_t cst);
|
||||
virtual std::shared_ptr<LinearExpr> MulInt(int64_t cst);
|
||||
/// Returns (this) * (cst).
|
||||
std::shared_ptr<LinearExpr> MulFloat(double cst);
|
||||
/// Returns -(this).
|
||||
std::shared_ptr<LinearExpr> Neg();
|
||||
virtual std::shared_ptr<LinearExpr> Neg();
|
||||
|
||||
/// Returns (this) == (rhs).
|
||||
std::shared_ptr<BoundedLinearExpression> Eq(std::shared_ptr<LinearExpr> rhs);
|
||||
@@ -381,11 +381,11 @@ class IntAffine : public LinearExpr {
|
||||
/// Returns the offset.
|
||||
int64_t offset() const { return offset_; }
|
||||
|
||||
std::shared_ptr<LinearExpr> AddInt(int64_t cst);
|
||||
std::shared_ptr<LinearExpr> SubInt(int64_t cst);
|
||||
std::shared_ptr<LinearExpr> RSubInt(int64_t cst);
|
||||
std::shared_ptr<LinearExpr> MulInt(int64_t cst);
|
||||
std::shared_ptr<LinearExpr> Neg();
|
||||
std::shared_ptr<LinearExpr> AddInt(int64_t cst) override;
|
||||
std::shared_ptr<LinearExpr> SubInt(int64_t cst) override;
|
||||
std::shared_ptr<LinearExpr> RSubInt(int64_t cst) override;
|
||||
std::shared_ptr<LinearExpr> MulInt(int64_t cst) override;
|
||||
std::shared_ptr<LinearExpr> Neg() override;
|
||||
|
||||
private:
|
||||
std::shared_ptr<LinearExpr> expr_;
|
||||
|
||||
Reference in New Issue
Block a user