Skip to content

Commit

Permalink
Setting JuMP tests to only run when Julia is available
Browse files Browse the repository at this point in the history
  • Loading branch information
jezsadler committed Dec 6, 2024
1 parent ca30190 commit 65dca21
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 17 deletions.
7 changes: 5 additions & 2 deletions src/omlt/base/pyomo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from typing import Any

import pyomo.environ as pyo
from numpy import float32
from pyomo.core.base.var import _GeneralVarData

from omlt.base.constraint import OmltConstraintIndexed, OmltConstraintScalar
Expand Down Expand Up @@ -185,6 +186,7 @@ def _parent(self, value):
for idx in self.keys():
self[idx]._parent = value


# Constraints


Expand Down Expand Up @@ -332,7 +334,9 @@ def _parse_expression_tuple_term(self, term):
return term._expression
if isinstance(term, OmltScalarPyomo):
return term._pyovar
if isinstance(term, (pyo.Expression, pyo.Var, _GeneralVarData, int, float)):
if isinstance(
term, (pyo.Expression, pyo.Var, _GeneralVarData, int, float, float32)
):
return term
msg = ("Term of expression %s is an unsupported type. %s", term, type(term))
raise TypeError(msg)
Expand All @@ -356,7 +360,6 @@ def _parse_expression_tuple(self, expr):
msg = ("Expression middle term was {%s}.", expr[1])
raise ValueError(msg)


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

Expand Down
1 change: 1 addition & 0 deletions tests/base/test_block.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pyomo.environ as pyo
import pytest

from omlt import OmltBlock

INPUTS_LENGTH = 3
Expand Down
1 change: 1 addition & 0 deletions tests/base/test_constraint.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pyomo.environ as pyo
import pytest

from omlt.base import (
OmltConstraintFactory,
OmltVarFactory,
Expand Down
1 change: 0 additions & 1 deletion tests/base/test_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import pytest

from omlt.base import OmltExpr, OmltExprFactory, OmltVarFactory
from omlt.dependencies import julia_available

VAR1_VALUE = 6
VAR2_VALUE = 3
Expand Down
3 changes: 2 additions & 1 deletion tests/base/test_formulation.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import pytest
from pyomo.environ import ConcreteModel, Objective, SolverFactory, value

from omlt import OmltBlock
from omlt.formulation import _setup_scaled_inputs_outputs
from omlt.scaling import OffsetScaling
from pyomo.environ import ConcreteModel, Objective, SolverFactory, value


def test_scaled_inputs_outputs():
Expand Down
36 changes: 24 additions & 12 deletions tests/base/test_jump.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
import re

import pytest

from omlt.base.julia import (
OmltConstraintScalarJuMP,
OmltScalarJuMP,
OmltBlockJuMP,
OmltConstraintScalarJuMP,
OmltExprJuMP,
OmltScalarJuMP,
)
from omlt.dependencies import julia_available, onnx, onnx_available
from omlt.neuralnet import (
ReluBigMFormulation,
ReluPartitionFormulation,
FullSpaceSmoothNNFormulation,
ReducedSpaceSmoothNNFormulation,
ReluBigMFormulation,
ReluPartitionFormulation,
)
from omlt.dependencies import onnx, onnx_available

if onnx_available:
from omlt.io.onnx import load_onnx_neural_network

from omlt import OffsetScaling
import pytest


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

assert v.name is None
assert v.lb == 0
Expand All @@ -31,6 +34,7 @@ def test_variable_jump():
assert v.value == 4


@pytest.mark.skipif(not julia_available, reason="Need JuMP for this test")
def test_expression_linear_jump():
# JumpVar + int
# OmltExprJuMP/AffExpr + OmltExprJuMP/AffExpr
Expand Down Expand Up @@ -87,7 +91,7 @@ def test_expression_linear_jump():
constraint = var_minus_expr == expr_div_int
assert isinstance(constraint, OmltConstraintScalarJuMP)


@pytest.mark.skipif(not julia_available, reason="Need JuMP for this test")
def test_expression_nonlinear_jump():
jump_block = OmltBlockJuMP()
jump_block.v1 = OmltScalarJuMP(initialize=2)
Expand All @@ -108,6 +112,7 @@ def test_expression_nonlinear_jump():
assert isinstance(hypt.tanh(), OmltExprJuMP)


@pytest.mark.skipif(not julia_available, reason="Need JuMP for this test")
def test_expression_bad_definition_jump():
expected_msg1 = re.escape(
"('Tried to create an OmltExprJuMP with an invalid expression. Expressions "
Expand All @@ -122,16 +127,17 @@ def test_expression_bad_definition_jump():
"d is exp, log, or tanh. %s was provided', ('invalid', 'pair'))"
)
with pytest.raises(ValueError, match=expected_msg2):
OmltExprJuMP(("invalid","pair"))
OmltExprJuMP(("invalid", "pair"))
expected_msg3 = re.escape(
"('Tried to create an OmltExprJuMP with an invalid expression. Expressions "
"must be tuples (a, b, c) where b is +, -, *, or /, or tuples (d, e) where "
"d is exp, log, or tanh. %s was provided', ('invalid', 'triple', 'expression'))"
)
with pytest.raises(ValueError, match=expected_msg3):
OmltExprJuMP(("invalid","triple","expression"))
OmltExprJuMP(("invalid", "triple", "expression"))


@pytest.mark.skipif(not julia_available, reason="Need JuMP for this test")
def test_expression_bad_arithmetic_jump():
v = OmltScalarJuMP()
expected_msg = (
Expand Down Expand Up @@ -163,19 +169,23 @@ def test_expression_bad_arithmetic_jump():
v / "invalid"


@pytest.mark.skipif(not julia_available, reason="Need JuMP for this test")
def test_two_node_relu_big_m_jump(two_node_network_relu):
m_neural_net_block = OmltBlockJuMP()
formulation = ReluBigMFormulation(two_node_network_relu)
m_neural_net_block.build_formulation(formulation)


@pytest.mark.skipif(not julia_available, reason="Need JuMP for this test")
def test_two_node_relu_partition_jump(two_node_network_relu):
m_neural_net_block = OmltBlockJuMP()
formulation = ReluPartitionFormulation(two_node_network_relu)
m_neural_net_block.build_formulation(formulation)


@pytest.mark.skipif(not onnx_available, reason="Need ONNX for this test")
@pytest.mark.skipif(
not onnx_available or not julia_available, reason="Need JuMP and ONNX for this test"
)
def test_full_space_sigmoid_jump(datadir):
m_neural_net_block = OmltBlockJuMP()
neural_net = onnx.load(datadir.file("keras_linear_131_sigmoid.onnx"))
Expand All @@ -196,7 +206,9 @@ def test_full_space_sigmoid_jump(datadir):
m_neural_net_block.build_formulation(formulation)


@pytest.mark.skipif(not onnx_available, reason="Need ONNX for this test")
@pytest.mark.skipif(
not onnx_available or not julia_available, reason="Need JuMP and ONNX for this test"
)
def test_reduced_space_linear_jump(datadir):
m_neural_net_block = OmltBlockJuMP()
neural_net = onnx.load(datadir.file("keras_linear_131.onnx"))
Expand Down
1 change: 1 addition & 0 deletions tests/base/test_scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import numpy as np
import pytest

from omlt import OffsetScaling
from omlt.scaling import convert_to_dict

Expand Down
2 changes: 1 addition & 1 deletion tests/base/test_var.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import pyomo.environ as pyo
import pytest

from omlt.base import OmltVarFactory
from omlt.dependencies import julia_available

VAR_VALUE = 3
FIX_VALUE = 2
Expand Down

0 comments on commit 65dca21

Please sign in to comment.