Skip to content

Commit

Permalink
Cleaned up some unnecessary methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
jezsadler committed Sep 28, 2024
1 parent d42cae2 commit 164b179
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 53 deletions.
26 changes: 0 additions & 26 deletions src/omlt/base/constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,10 @@ class OmltConstraint(ABC):
def ctype(self):
return pyo.Constraint

def is_component_type(self):
return True

def is_expression_type(self, enum):
# The Pyomo ExpressionType.RELATIONAL is enum 1.
return enum.value == 1

def valid_model_component(self):
"""Return True if this can be used as a model component."""
return True

@abstractmethod
def __call__(self, *args: Any, **kwds: Any) -> Any:
pass


class OmltConstraintScalar(OmltConstraint):
Expand All @@ -44,12 +34,6 @@ def __init__(self, lang: str = DEFAULT_MODELING_LANGUAGE, **kwargs: Any):
if rhs is not None:
self.rhs = rhs
if not lhs and not sense and not rhs:
expr_tuple = kwargs.pop("expr_tuple", None)
if expr_tuple and expr_tuple[1] in {"==", ">=", "<=", ">", "<", "in"}:
self.lhs = expr_tuple[0]
self.sense = expr_tuple[1]
self.rhs = expr_tuple[2]
if not lhs and not sense and not rhs and not expr_tuple:
expr = kwargs.pop("expr", None)
if isinstance(expr, EqualityExpression):
self.lhs = expr.arg(0)
Expand All @@ -64,8 +48,6 @@ def __init__(self, lang: str = DEFAULT_MODELING_LANGUAGE, **kwargs: Any):
self.format = lang
self._parent = None

def __call__(self, *args: Any, **kwds: Any) -> Any:
"""Return the value of the body of the constraint."""

@property
def args(self):
Expand All @@ -84,9 +66,6 @@ def __init__(
self.name = None
self.format = lang

@abstractmethod
def __call__(self, *args: Any, **kwds: Any) -> Any:
pass

def keys(self, sort=False):
yield from self._index_set
Expand All @@ -101,11 +80,6 @@ def _constructed(self):
def _active(self):
"""Return True if the constraint is active."""

@_active.setter
@abstractmethod
def _active(self, val):
"""Set the constraint status to active or inactive."""

@property
@abstractmethod
def _data(self):
Expand Down
24 changes: 0 additions & 24 deletions src/omlt/base/pyomo.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,6 @@ def __init__(self, *args: Any, **kwargs: Any):
self.constraint._parent = self._parent
self.constraint.construct()

def __call__(self, *args: Any, **kwds: Any) -> Any:
return self.constraint.__call__(*args, **kwds)

@property
def __class__(self):
return type(self.constraint.expr)
Expand Down Expand Up @@ -235,7 +232,6 @@ def __init__(self, *args: Any, **kwargs: Any):
super().__init__(*args, **kwargs)
kwargs.pop("model", None)
kwargs.pop("lang", None)
kwargs.pop("expr_tuple", None)
self.constraint = pyo.Constraint(*args, **kwargs)
self._index_set = self.constraint._index_set

Expand Down Expand Up @@ -267,9 +263,6 @@ def __getitem__(self, index):
)
raise KeyError(msg)

def __call__(self, *args: Any, **kwds: Any) -> Any:
return self.constraint.__call__(*args, **kwds)

def __len__(self):
return len(self.constraint)

Expand All @@ -281,10 +274,6 @@ def _constructed(self):
def _active(self):
return self.constraint._active

@_active.setter
def _active(self, val):
self.constraint._active = val

@property
def _data(self):
return self.constraint._data
Expand Down Expand Up @@ -314,7 +303,6 @@ def __init__(self, expr=None, **kwargs):

self._parent = None
self.name = None
self.__class__ = type(self._expression)
self._args_ = self._expression._args_
self._format = "pyomo"
self.expr_factory = OmltExprFactory()
Expand Down Expand Up @@ -350,25 +338,13 @@ def _parse_expression_tuple(self, expr):
msg = ("Expression middle term was {%s}.", expr[1])
raise ValueError(msg)

def __class__(self):
return type(self._expression)

def is_potentially_variable(self):
return self._expression.is_potentially_variable()

def as_numeric(self):
return self._expression._apply_operation(self._expression.args)

def construct(self, data=None):
return self._expression.construct(data)

@property
def _constructed(self):
return self._expression.expr._constructed

@property
def const(self):
return self._expression.const

@property
def args(self):
Expand Down
3 changes: 0 additions & 3 deletions tests/base/test_constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,16 @@ def test_build_constraint():

assert c_le_expressions.sense == "<="
assert id(c_le_expressions.rhs) == id(e2._expression)
assert c_le_expressions() == VAR1_VALUE - VAR2_VALUE

c_le_var = e1 <= v2

assert c_le_var.sense == "<="
assert id(c_le_var.rhs) == id(v2._pyovar)
assert c_le_var() == VAR1_VALUE - VAR2_VALUE + CONST_VALUE

c_le_const = e1 <= CONST_VALUE

assert c_le_const.sense == "<="
assert c_le_const.rhs == CONST_VALUE
assert c_le_const() == VAR1_VALUE + CONST_VALUE


def test_constraint_invalid_lang():
Expand Down

0 comments on commit 164b179

Please sign in to comment.