Skip to content

Commit e5fec98

Browse files
committed
Add HIGHS to tests and ignore those which fail due to unavailable features
1 parent fd27995 commit e5fec98

File tree

5 files changed

+32
-15
lines changed

5 files changed

+32
-15
lines changed

mip/__init__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
from mip.ndarray import LinExprTensor
88
from mip.entities import Column, Constr, LinExpr, Var, ConflictGraph
99
from mip.model import *
10-
from mip._version import __version__
10+
11+
try:
12+
from ._version import __version__
13+
except ImportError:
14+
__version__ = "unknown"
1115

1216
name = "mip"

mip/gurobi.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
MAX_NAME_SIZE = 512 # for variables and constraints
5252

5353
lib_path = None
54+
has_gurobi = False
5455

5556
if "GUROBI_HOME" in environ:
5657
if platform.lower().startswith("win"):
@@ -93,9 +94,9 @@
9394

9495

9596
if lib_path is None:
96-
found = False
97+
has_gurobi = False
9798
else:
98-
found = True
99+
has_gurobi = True
99100
grblib = ffi.dlopen(lib_path)
100101

101102
ffi.cdef(
@@ -339,7 +340,7 @@ class SolverGurobi(Solver):
339340
def __init__(self, model: Model, name: str, sense: str, modelp: CData = ffi.NULL):
340341
"""modelp should be informed if a model should not be created,
341342
but only allow access to an existing one"""
342-
if not found:
343+
if not has_gurobi:
343344
raise FileNotFoundError(
344345
"""Gurobi not found. Plase check if the
345346
Gurobi dynamic loadable library is reachable or define

mip/model.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
from typing import List, Tuple, Optional, Union, Dict, Any
55
import numbers
66
import mip
7-
from ._version import __version__
7+
8+
try:
9+
from ._version import __version__
10+
except ImportError:
11+
__version__ = "unknown"
812

913
logger = logging.getLogger(__name__)
1014

@@ -94,8 +98,7 @@ def __init__(
9498
else:
9599
import mip.gurobi
96100

97-
if mip.gurobi.found:
98-
101+
if mip.gurobi.has_gurobi:
99102
self.solver = mip.gurobi.SolverGurobi(self, name, sense)
100103
self.solver_name = mip.GUROBI
101104
else:
@@ -406,7 +409,7 @@ def clear(self: "Model"):
406409
# checking which solvers are available
407410
import mip.gurobi
408411

409-
if mip.gurobi.found:
412+
if mip.gurobi.has_gurobi:
410413
self.solver = mip.gurobi.SolverGurobi(self, self.name, sense)
411414
self.solver_name = mip.GUROBI
412415
else:

test/examples_test.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@
88
import importlib.machinery
99
import pytest
1010

11+
import mip.gurobi
12+
import mip.highs
13+
from mip import CBC, GUROBI, HIGHS
1114

1215
EXAMPLES = glob(join("..", "examples", "*.py")) + glob(join(".", "examples", "*.py"))
1316

14-
SOLVERS = ["cbc"]
15-
if "GUROBI_HOME" in environ:
16-
SOLVERS += ["gurobi"]
17+
SOLVERS = [CBC]
18+
if mip.gurobi.has_gurobi:
19+
SOLVERS += [GUROBI]
20+
if mip.highs.has_highs:
21+
SOLVERS += [HIGHS]
1722

1823

1924
@pytest.mark.parametrize("solver, example", product(SOLVERS, EXAMPLES))
@@ -22,4 +27,7 @@ def test_examples(solver, example):
2227
environ["SOLVER_NAME"] = solver
2328
loader = importlib.machinery.SourceFileLoader("example", example)
2429
mod = types.ModuleType(loader.name)
25-
loader.exec_module(mod)
30+
try:
31+
loader.exec_module(mod)
32+
except NotImplementedError as e:
33+
print("Skipping test for example '{}': {}".format(example, e))

test/test_gurobi.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import pytest
22

33
import mip
4+
import mip.gurobi
45

56

67
def test_gurobi_pip_installation():
78
# Even though we have no valid license yet, we could check that the binaries are found.
89
# If no valid license is found, an InterfacingError is thrown
910

10-
with pytest.raises(mip.InterfacingError):
11-
mip.Model(solver_name="GRB")
12-
11+
if mip.gurobi.has_gurobi:
12+
with pytest.raises(mip.InterfacingError):
13+
mip.Model(solver_name=mip.GUROBI)

0 commit comments

Comments
 (0)