Skip to content

Commit

Permalink
Override __eq__ method also for Quadratic and others (#224)
Browse files Browse the repository at this point in the history
Additional fix of #222. Since `__eq__` cannot be inherited, we have to
manually override it.
  • Loading branch information
termoshtt authored Dec 17, 2024
1 parent 1b8ecb6 commit a70a9d0
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions python/ommx/ommx/v1/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1233,22 +1233,6 @@ def __neg__(self) -> Linear:
return -1 * self

def __eq__(self, other) -> Constraint: # type: ignore[reportIncompatibleMethodOverride]
"""
Create a constraint that this linear function is equal to the right-hand side.
Examples
========
>>> x = DecisionVariable.integer(1)
>>> y = DecisionVariable.integer(2)
>>> x + y == 1
Constraint(...)
To compare two objects, use :py:meth:`almost_equal` method.
>>> assert (x + y).almost_equal(Linear(terms={1: 1, 2: 1}))
"""
return Constraint(
function=self - other, equality=Equality.EQUALITY_EQUAL_TO_ZERO
)
Expand Down Expand Up @@ -1448,6 +1432,11 @@ def __rmul__(self, other):
def __neg__(self) -> Linear:
return -1 * self

def __eq__(self, other) -> Constraint: # type: ignore[reportIncompatibleMethodOverride]
return Constraint(
function=self - other, equality=Equality.EQUALITY_EQUAL_TO_ZERO
)


@dataclass
class Polynomial(AsConstraint):
Expand Down Expand Up @@ -1633,6 +1622,11 @@ def __rmul__(self, other):
def __neg__(self) -> Linear:
return -1 * self

def __eq__(self, other) -> Constraint: # type: ignore[reportIncompatibleMethodOverride]
return Constraint(
function=self - other, equality=Equality.EQUALITY_EQUAL_TO_ZERO
)


def as_function(
f: int
Expand Down Expand Up @@ -1867,6 +1861,11 @@ def __rmul__(self, other):
def __neg__(self) -> Function:
return -1 * self

def __eq__(self, other) -> Constraint: # type: ignore[reportIncompatibleMethodOverride]
return Constraint(
function=self - other, equality=Equality.EQUALITY_EQUAL_TO_ZERO
)


@dataclass
class Constraint:
Expand Down

0 comments on commit a70a9d0

Please sign in to comment.