Skip to content

Commit

Permalink
Fixing and ignoring some errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jezsadler committed Dec 9, 2024
1 parent d7b735d commit 8723cd3
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/omlt/base/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from omlt.base.language import DEFAULT_MODELING_LANGUAGE
from omlt.base.language import DEFAULT_MODELING_LANGUAGE # noqa: I001

from omlt.base.constraint import (
OmltConstraint,
Expand Down
2 changes: 1 addition & 1 deletion src/omlt/base/constraint.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def __init__(
self.format = lang


def keys(self, sort=False):
def keys(self, *, sort=False): # noqa: ARG002
yield from self._index_set


Expand Down
5 changes: 2 additions & 3 deletions src/omlt/base/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def log(self):

@abstractmethod
def tanh(self):
"""Return an expression representing the hyperbolic tangent of this expression."""
"""Return an expression representing the hyperbolic tangent of expression."""

@abstractmethod
def __add__(self, other):
Expand Down Expand Up @@ -85,8 +85,7 @@ def __ge__(self, other):
class OmltExprFactory:
def __init__(self):
self.exprs = {
subclass.format: subclass
for subclass in OmltExpr.__subclasses__()
subclass.format: subclass for subclass in OmltExpr.__subclasses__()
}

def register(self, lang, varclass):
Expand Down
29 changes: 13 additions & 16 deletions src/omlt/base/julia.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
Jl.seval("import JuMP")
jump = Jl.JuMP

PAIR = 2
EXPR_TWO = 2
EXPR_THREE = 3

# Elements


class JuMPVarInfo:
def __init__(
def __init__( # noqa: PLR0913
self,
lower_bound=None,
upper_bound=None,
Expand Down Expand Up @@ -175,11 +178,11 @@ def tanh(self):
class OmltScalarJuMP(OmltScalar):
format = "jump"

def __init__(self, *, binary=False, **kwargs):
def __init__(self, *, binary=False, **kwargs: Any):
super().__init__()

self._bounds = kwargs.pop("bounds", None)
if isinstance(self._bounds, tuple) and len(self._bounds) == 2:
if isinstance(self._bounds, tuple) and len(self._bounds) == PAIR:
_lb = self._bounds[0]
_ub = self._bounds[1]
elif self._bounds is None:
Expand Down Expand Up @@ -295,17 +298,15 @@ class OmltIndexedJuMP(OmltIndexed):

def __init__(self, *indexes: Any, binary: bool = False, **kwargs: Any):
index_set = indexes[0]
i_dict = {}
for i, val in enumerate(index_set):
i_dict[i] = val
i_dict = dict(enumerate(index_set))
self._index_set = tuple(i_dict[i] for i in range(len(index_set)))

self._bounds = kwargs.pop("bounds", None)

if isinstance(self._bounds, dict) and len(self._bounds) == len(self._index_set):
_lb = {k: v[0] for k, v in self._bounds.items()}
_ub = {k: v[1] for k, v in self._bounds.items()}
elif isinstance(self._bounds, tuple) and len(self._bounds) == 2:
elif isinstance(self._bounds, tuple) and len(self._bounds) == PAIR:
_lb = {i: self._bounds[0] for i in self._index_set}
_ub = {i: self._bounds[1] for i in self._index_set}
elif self._bounds is None:
Expand Down Expand Up @@ -391,7 +392,7 @@ def __iter__(self):
"""Return an iterator of the component data keys."""
return self._vars.__iter__()

def construct(self, data=None):
def construct(self, *, data=None): # noqa: ARG002
for idx in self._index_set:
if isinstance(idx, int):
name = str(self.name) + "[" + str(idx) + "]"
Expand Down Expand Up @@ -449,7 +450,7 @@ def __init__(self, *indexes: Any, **kwargs: Any):
self.format = "jump"
self._jumpcons = {idx: None for idx in self._index_set[0]}

def keys(self, sort=False):
def keys(self, *, sort=False): # noqa: ARG002
yield from self._index_set

def __setitem__(self, label, item):
Expand Down Expand Up @@ -477,7 +478,7 @@ def __init__(self, expr):
"d is exp, log, or tanh. %s was provided",
expr,
)
if len(expr) == 3:
if len(expr) == EXPR_THREE:
if expr[1] == "+":
self._jumpexpr = self.add(expr[0], expr[2])
elif expr[1] == "-":
Expand All @@ -488,7 +489,7 @@ def __init__(self, expr):
self._jumpexpr = self.divide(expr[0], expr[2])
else:
raise ValueError(msg)
elif len(expr) == 2:
elif len(expr) == EXPR_TWO:
if expr[0] == "exp":
self._jumpexpr = self.exponent(expr[1])
elif expr[0] == "log":
Expand Down Expand Up @@ -533,7 +534,7 @@ def add(self, a, b):
msg = ("Unrecognized types for addition, %s, %s", type(a), type(b))
raise TypeError(msg)

def subtract(self, a, b):
def subtract(self, a, b): # noqa: PLR0911
if isinstance(a, (int, float)) and isinstance(b, (JumpVar, OmltScalarJuMP)):
return jump.AffExpr(a, jump.OrderedDict([(b.varref, -1)]))
if isinstance(a, JumpVar) and isinstance(b, (int, float)):
Expand Down Expand Up @@ -760,10 +761,6 @@ def __setattr__(self, name, value):
elif isinstance(value, OmltConstraintIndexedJuMP):
value.model = self
value.name = name
# for idx, constr in value._jumpcons:
# self._conrefs[(name, idx)] = jump.add_constraint(
# self._jumpmodel, constr._jumpcon, (name, idx)
# )
elif isinstance(value, OmltBlockCore):
value.name = name
value._parent = self
Expand Down
10 changes: 5 additions & 5 deletions src/omlt/gbt/gbt_formulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ class GBTBigMFormulation(_PyomoFormulation):
constraints to enforce splitting rules according to:
References:
----------
-----------
* Misic, V. "Optimization of tree ensembles."
Operations Research 68.5 (2020): 1605-1624.
* Mistry, M., et al. "Mixed-integer convex nonlinear optimization with
gradient-boosted trees embedded."
INFORMS Journal on Computing (2020).
INFORMS Journal on Computing (2020).
Parameters:
tree_ensemble_structure (GradientBoostedTreeModel):
Expand Down Expand Up @@ -63,7 +63,7 @@ def _build_formulation(self):
)


def add_formulation_to_block(block, model_definition, input_vars, output_vars): # noqa: C901, PLR0915
def add_formulation_to_block(block, model_definition, input_vars, output_vars): # noqa: C901, PLR0912, PLR0915
r"""Adds the gradient-boosted trees formulation to the given Pyomo block.
.. math::
Expand All @@ -89,12 +89,12 @@ def add_formulation_to_block(block, model_definition, input_vars, output_vars):
References:
----------
-----------
* Misic, V. "Optimization of tree ensembles."
Operations Research 68.5 (2020): 1605-1624.
* Mistry, M., et al. "Mixed-integer convex nonlinear optimization with
gradient-boosted trees embedded."
INFORMS Journal on Computing (2020).
INFORMS Journal on Computing (2020).
Parameters:
block (Block):
Expand Down
2 changes: 1 addition & 1 deletion src/omlt/neuralnet/layers/full_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from omlt.neuralnet.layer import ConvLayer2D, PoolingLayer2D


def full_space_dense_layer(net_block, net, layer_block, layer):
def full_space_dense_layer(net_block, net, layer_block, layer): # noqa: PLR0912
r"""Add full-space formulation of the dense layer to the block.
.. math::
Expand Down
2 changes: 1 addition & 1 deletion src/omlt/neuralnet/layers/partition_based.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def default_partition_split_func(w, n):
return np.array_split(sorted_indexes, n)


def partition_based_dense_relu_layer(net_block, net, layer_block, layer, split_func):
def partition_based_dense_relu_layer(net_block, net, layer_block, layer, split_func): # noqa: C901,PLR0912,PLR0915
r"""Partition-based ReLU activation formulation.
Generates the constraints for the ReLU activation function:
Expand Down
11 changes: 3 additions & 8 deletions src/omlt/neuralnet/nn_formulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def output_indexes(self):
return network_outputs[0].output_indexes


def _build_neural_network_formulation( # noqa: C901
def _build_neural_network_formulation( # noqa: C901,PLR0912,PLR0915
block, network_structure, layer_constraints, activation_constraints
):
"""Adds the neural network formulation to the given Pyomo block.
Expand Down Expand Up @@ -362,7 +362,7 @@ def __init__(self, network_structure, activation_functions=None):
def _supported_default_activation_functions(self):
return dict(_DEFAULT_ACTIVATION_FUNCTIONS)

def _build_formulation(self):
def _build_formulation(self): # noqa: C901,PLR0912
_setup_scaled_inputs_outputs(
self.block, self.__scaling_object, self.__scaled_input_bounds
)
Expand Down Expand Up @@ -413,11 +413,6 @@ def _build_formulation(self):
input_layer_block.z[output_index] == block.scaled_inputs[idx_0]
)

# @input_layer_block.Expression(input_layer.output_indexes)
# def z(b, *output_index):
# pb = b.parent_block()
# return pb.scaled_inputs[output_index]
# loop over the layers and build the expressions
for layer in layers:
if isinstance(layer, InputLayer):
# skip the InputLayer
Expand Down Expand Up @@ -530,7 +525,7 @@ def __init__(self, network_structure, split_func=None):

self.__split_func = split_func

def _build_formulation(self): # noqa: C901
def _build_formulation(self): # noqa: C901,PLR0912,PLR0915
_setup_scaled_inputs_outputs(
self.block, self.__scaling_object, self.__scaled_input_bounds
)
Expand Down
21 changes: 13 additions & 8 deletions tests/base/test_jump.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,22 @@

from omlt import OffsetScaling

TWO = 2
FOUR = 4
FIVE = 5
SIX = 6
ELEVEN = 11

@pytest.mark.skipif(not julia_available, reason="Need JuMP for this test")
def test_variable_jump():
v = OmltScalarJuMP(bounds=(0, 5), initialize=(3,))

assert v.name is None
assert v.lb == 0
assert v.ub == 5
assert v.ub == FIVE

v.value = 4
assert v.value == 4
assert v.value == FOUR


@pytest.mark.skipif(not julia_available, reason="Need JuMP for this test")
Expand All @@ -58,31 +63,31 @@ def test_expression_linear_jump():

var_plus_three = jump_block.v1 + 3
assert isinstance(var_plus_three, OmltExprJuMP)
assert var_plus_three() == 5
assert var_plus_three() == FIVE

three_minus_var = 3 - jump_block.v1
assert isinstance(three_minus_var, OmltExprJuMP)
assert three_minus_var() == 1

three_times_var = 3 * jump_block.v1
assert isinstance(three_times_var, OmltExprJuMP)
assert three_times_var() == 6
assert three_times_var() == SIX

var_times_three = jump_block.v1 * 3
assert isinstance(var_times_three, OmltExprJuMP)
assert var_times_three() == 6
assert var_times_three() == SIX

expr_sum = var_plus_three + var_times_three
assert isinstance(expr_sum, OmltExprJuMP)
assert expr_sum() == 11
assert expr_sum() == ELEVEN

var_minus_expr = element2 - three_minus_var
assert isinstance(var_minus_expr, OmltExprJuMP)
assert var_minus_expr() == 2
assert var_minus_expr() == TWO

expr_minus_expr = var_plus_three - three_minus_var
assert isinstance(expr_minus_expr, OmltExprJuMP)
assert expr_minus_expr() == 4
assert expr_minus_expr() == FOUR

expr_div_int = var_plus_three / 5
assert isinstance(expr_div_int, OmltExprJuMP)
Expand Down

0 comments on commit 8723cd3

Please sign in to comment.